From 92aa72cac46ee100dcf19aeed7795a6d9287222e Mon Sep 17 00:00:00 2001 From: LMBishop <13875753+LMBishop@users.noreply.github.com> Date: Tue, 2 Aug 2022 01:48:20 +0100 Subject: Run CoreProtect check async (closes #432) --- .../quests/bukkit/BukkitQuestsPlugin.java | 2 +- .../hook/coreprotect/AbstractCoreProtectHook.java | 6 ++-- .../bukkit/hook/coreprotect/CoreProtectHook.java | 30 +++++++++++----- .../bukkit/tasktype/type/MiningTaskType.java | 41 +++++++++++++++------- 4 files changed, 54 insertions(+), 25 deletions(-) (limited to 'bukkit') 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 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 lookup = api.blockLookup(block, time); - if (lookup.isEmpty()) return false; - - String[] result = lookup.get(0); - CoreProtectAPI.ParseResult parseResult = api.parseResult(result); - - return !parseResult.getPlayer().isEmpty() && parseResult.getActionId() == 1; + public CompletableFuture checkBlock(Block block, int time) { + CompletableFuture future = new CompletableFuture<>(); + plugin.getScheduler().doAsync(() -> { + List 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; + + 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; - } - - int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); - super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); - - int blocksNeeded = (int) task.getConfigValue("amount"); - - if (progress >= blocksNeeded) { - super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); - taskProgress.setCompleted(true); + Runnable increment = () -> { + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); + + 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 (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(); } } } -- cgit v1.2.3-70-g09d2