diff options
| author | LMBishop <13875753+LMBishop@users.noreply.github.com> | 2022-08-02 01:48:20 +0100 |
|---|---|---|
| committer | LMBishop <13875753+LMBishop@users.noreply.github.com> | 2022-08-02 01:48:20 +0100 |
| commit | 92aa72cac46ee100dcf19aeed7795a6d9287222e (patch) | |
| tree | b8cff3b01b6a0d3ca75caeed25b553446200e43f /bukkit/src/main/java | |
| parent | 43c204cb331eb43d7ed4b7cef4221f2ac78ea963 (diff) | |
Run CoreProtect check async (closes #432)
Diffstat (limited to 'bukkit/src/main/java')
4 files changed, 50 insertions, 21 deletions
diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/BukkitQuestsPlugin.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/BukkitQuestsPlugin.java index ad967cdc..dbb0fe00 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/BukkitQuestsPlugin.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/BukkitQuestsPlugin.java @@ -283,7 +283,7 @@ public class BukkitQuestsPlugin extends JavaPlugin implements Quests { this.placeholderAPIProcessor = (player, s) -> placeholderAPIHook.replacePlaceholders(player, s); } if (Bukkit.getPluginManager().isPluginEnabled("CoreProtect")) { - this.coreProtectHook = new CoreProtectHook(); + this.coreProtectHook = new CoreProtectHook(this); } if (Bukkit.getPluginManager().isPluginEnabled("Essentials")) { this.essentialsHook = new EssentialsHook(); diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/coreprotect/AbstractCoreProtectHook.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/coreprotect/AbstractCoreProtectHook.java index 75f96ecd..4c6d674b 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/coreprotect/AbstractCoreProtectHook.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/coreprotect/AbstractCoreProtectHook.java @@ -2,15 +2,17 @@ package com.leonardobishop.quests.bukkit.hook.coreprotect; import org.bukkit.block.Block; +import java.util.concurrent.CompletableFuture; + public interface AbstractCoreProtectHook { /** * Check whether or not the most recent edit to a block was the result of a player. * * @param block the block - * @param time the time to look back in seconds + * @param time the time to look back in seconds * @return true if from a player */ - boolean checkBlock(Block block, int time); + CompletableFuture<Boolean> checkBlock(Block block, int time); } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/coreprotect/CoreProtectHook.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/coreprotect/CoreProtectHook.java index 1dbfe3bd..e0c3c9ef 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/coreprotect/CoreProtectHook.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/coreprotect/CoreProtectHook.java @@ -1,29 +1,41 @@ package com.leonardobishop.quests.bukkit.hook.coreprotect; +import com.leonardobishop.quests.bukkit.BukkitQuestsPlugin; +import com.leonardobishop.quests.common.plugin.Quests; import net.coreprotect.CoreProtect; import net.coreprotect.CoreProtectAPI; import org.bukkit.Bukkit; import org.bukkit.block.Block; import java.util.List; +import java.util.concurrent.CompletableFuture; public class CoreProtectHook implements AbstractCoreProtectHook { + private final BukkitQuestsPlugin plugin; private final CoreProtectAPI api; - public CoreProtectHook() { + public CoreProtectHook(BukkitQuestsPlugin plugin) { + this.plugin = plugin; api = ((CoreProtect) Bukkit.getPluginManager().getPlugin("CoreProtect")).getAPI(); } @Override - public boolean checkBlock(Block block, int time) { - List<String[]> lookup = api.blockLookup(block, time); - if (lookup.isEmpty()) return false; + public CompletableFuture<Boolean> checkBlock(Block block, int time) { + CompletableFuture<Boolean> future = new CompletableFuture<>(); + plugin.getScheduler().doAsync(() -> { + List<String[]> lookup = api.blockLookup(block, time); + if (lookup.isEmpty()) { + plugin.getScheduler().doSync(() -> future.complete(false)); + } else { + String[] result = lookup.get(0); + CoreProtectAPI.ParseResult parseResult = api.parseResult(result); + boolean value = !parseResult.getPlayer().isEmpty() && parseResult.getActionId() == 1; - String[] result = lookup.get(0); - CoreProtectAPI.ParseResult parseResult = api.parseResult(result); - - return !parseResult.getPlayer().isEmpty() && parseResult.getActionId() == 1; + plugin.getScheduler().doSync(() -> future.complete(value)); + } + }); + return future; } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MiningTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MiningTaskType.java index 037db1c7..4ccb17bf 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MiningTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MiningTaskType.java @@ -57,19 +57,34 @@ public final class MiningTaskType extends BukkitTaskType { super.debug("check-coreprotect is enabled, but CoreProtect is not detected on the server", quest.getId(), task.getId(), player.getUniqueId()); } - if (plugin.getCoreProtectHook() != null && plugin.getCoreProtectHook().checkBlock(event.getBlock(), coreProtectTime)) { - super.debug("CoreProtect indicates blocks was recently placed, continuing...", quest.getId(), task.getId(), player.getUniqueId()); - continue; - } + Runnable increment = () -> { + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); - int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); - super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); + int blocksNeeded = (int) task.getConfigValue("amount"); - int blocksNeeded = (int) task.getConfigValue("amount"); + if (progress >= blocksNeeded) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); + } + }; - if (progress >= blocksNeeded) { - super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); - taskProgress.setCompleted(true); + if (plugin.getCoreProtectHook() != null) { + super.debug("Running CoreProtect lookup (may take a while)", quest.getId(), task.getId(), player.getUniqueId()); + plugin.getCoreProtectHook().checkBlock(event.getBlock(), coreProtectTime).thenAccept(result -> { + if (result) { + super.debug("CoreProtect lookup indicates this is a player placed block, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + } else { + super.debug("CoreProtect lookup OK", quest.getId(), task.getId(), player.getUniqueId()); + increment.run(); + } + }).exceptionally(ex -> { + super.debug("CoreProtect lookup failed: " + ex.getMessage(), quest.getId(), task.getId(), player.getUniqueId()); + ex.printStackTrace(); + return null; + }); + } else { + increment.run(); } } } |
