diff options
| author | Krakenied <Krakenied1@gmail.com> | 2023-03-19 04:19:29 +0100 |
|---|---|---|
| committer | Leonardo Bishop <13875753+LMBishop@users.noreply.github.com> | 2023-04-02 12:20:10 +0100 |
| commit | 4d61a74b7785f66607bcd808a0e8fdfba5f1cb9f (patch) | |
| tree | debf43c03d534dff2958afe0203bda261413c8f4 /bukkit/src/main | |
| parent | 8cb5b45c8b9b1d9e37d7e3fcef4fbbf13b8d2c3f (diff) | |
Add allow silk touch option
Partially closes https://github.com/LMBishop/Quests/issues/499
Diffstat (limited to 'bukkit/src/main')
| -rw-r--r-- | bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MiningTaskType.java | 113 | ||||
| -rw-r--r-- | bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java | 6 |
2 files changed, 76 insertions, 43 deletions
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 f1f4c363..4249600e 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 @@ -1,17 +1,22 @@ package com.leonardobishop.quests.bukkit.tasktype.type; import com.leonardobishop.quests.bukkit.BukkitQuestsPlugin; +import com.leonardobishop.quests.bukkit.hook.coreprotect.AbstractCoreProtectHook; import com.leonardobishop.quests.bukkit.tasktype.BukkitTaskType; import com.leonardobishop.quests.bukkit.util.TaskUtils; import com.leonardobishop.quests.common.player.QPlayer; import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.inventory.ItemStack; public final class MiningTaskType extends BukkitTaskType { @@ -28,64 +33,81 @@ public final class MiningTaskType extends BukkitTaskType { super.addConfigValidator(TaskUtils.useBooleanConfigValidator(this, "check-coreprotect")); super.addConfigValidator(TaskUtils.useIntegerConfigValidator(this, "check-coreprotect-time")); super.addConfigValidator(TaskUtils.useBooleanConfigValidator(this, "reverse-if-placed")); - super.addConfigValidator(TaskUtils.useBooleanConfigValidator(this, "use-similar-blocks")); + super.addConfigValidator(TaskUtils.useBooleanConfigValidator(this, "allow-silk-touch")); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onBlockBreak(BlockBreakEvent event) { - if (event.getPlayer().hasMetadata("NPC")) return; + Player player = event.getPlayer(); + if (player.hasMetadata("NPC")) { + return; + } - QPlayer qPlayer = plugin.getPlayerManager().getPlayer(event.getPlayer().getUniqueId()); + QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId()); if (qPlayer == null) { return; } - Player player = event.getPlayer(); + Block block = event.getBlock(); + Material material = block.getType(); + ItemStack item = plugin.getVersionSpecificHandler().getItemInMainHand(player); + boolean silkTouchPresent = item != null && item.getEnchantmentLevel(Enchantment.SILK_TOUCH) > 0; - for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(event.getPlayer(), qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { Quest quest = pendingTask.quest(); Task task = pendingTask.task(); TaskProgress taskProgress = pendingTask.taskProgress(); - super.debug("Player mined block " + event.getBlock().getType(), quest.getId(), task.getId(), event.getPlayer().getUniqueId()); + super.debug("Player mined block " + material.name(), quest.getId(), task.getId(), event.getPlayer().getUniqueId()); + + boolean allowSilkTouch = (boolean) task.getConfigValue("allow-silk-touch", true); + if (!allowSilkTouch && silkTouchPresent) { + continue; + } + + super.debug("allow-silk-touch is disabled, checking block", quest.getId(), task.getId(), player.getUniqueId()); - if (TaskUtils.matchBlock(this, pendingTask, event.getBlock(), player.getUniqueId())) { + if (TaskUtils.matchBlock(this, pendingTask, block, player.getUniqueId())) { boolean coreProtectEnabled = (boolean) task.getConfigValue("check-coreprotect", false); int coreProtectTime = (int) task.getConfigValue("check-coreprotect-time", 3600); - if (coreProtectEnabled && plugin.getCoreProtectHook() == null) { - super.debug("check-coreprotect is enabled, but CoreProtect is not detected on the server", quest.getId(), task.getId(), player.getUniqueId()); - } - 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) { + int amount = (int) task.getConfigValue("amount"); + if (progress >= amount) { super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); taskProgress.setCompleted(true); } }; - if (coreProtectEnabled && 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(); + if (coreProtectEnabled) { + AbstractCoreProtectHook coreProtectHook = plugin.getCoreProtectHook(); + if (coreProtectHook != null) { + super.debug("Running CoreProtect lookup (may take a while)", quest.getId(), task.getId(), player.getUniqueId()); + + // Run CoreProtect lookup + plugin.getCoreProtectHook().checkBlock(block, 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(throwable -> { + super.debug("CoreProtect lookup failed: " + throwable.getMessage(), quest.getId(), task.getId(), player.getUniqueId()); + throwable.printStackTrace(); + return null; + }); + + continue; + } + + super.debug("check-coreprotect is enabled, but CoreProtect is not detected on the server", quest.getId(), task.getId(), player.getUniqueId()); } + + increment.run(); } } } @@ -93,32 +115,37 @@ public final class MiningTaskType extends BukkitTaskType { // subtract if enabled @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onBlockPlace(BlockPlaceEvent event) { - if (event.getPlayer().hasMetadata("NPC")) return; + Player player = event.getPlayer(); + if (player.hasMetadata("NPC")) { + return; + } - QPlayer qPlayer = plugin.getPlayerManager().getPlayer(event.getPlayer().getUniqueId()); + QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId()); if (qPlayer == null) { return; } - Player player = event.getPlayer(); + Block block = event.getBlock(); + Material material = block.getType(); - for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(event.getPlayer(), qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this, TaskUtils.TaskConstraint.WORLD)) { Quest quest = pendingTask.quest(); Task task = pendingTask.task(); TaskProgress taskProgress = pendingTask.taskProgress(); - super.debug("Player placed block " + event.getBlock().getType(), quest.getId(), task.getId(), event.getPlayer().getUniqueId()); + super.debug("Player placed block " + material.name(), quest.getId(), task.getId(), player.getUniqueId()); + + boolean reverseIfPlaced = (boolean) task.getConfigValue("reverse-if-placed", false); + if (!reverseIfPlaced) { + continue; + } + super.debug("reverse-if-placed is enabled, checking block", quest.getId(), task.getId(), player.getUniqueId()); - if (task.getConfigValue("reverse-if-placed") != null && ((boolean) task.getConfigValue("reverse-if-placed"))) { - super.debug("reverse-if-placed is enabled, checking block", quest.getId(), task.getId(), event.getPlayer().getUniqueId()); - if (TaskUtils.matchBlock(this, pendingTask, event.getBlock(), player.getUniqueId())) { - int progress = TaskUtils.getIntegerTaskProgress(taskProgress); - taskProgress.setProgress(--progress); - super.debug("Decrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); - } + if (TaskUtils.matchBlock(this, pendingTask, block, player.getUniqueId())) { + int progress = TaskUtils.decrementIntegerTaskProgress(taskProgress); + super.debug("Decrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); } } } - } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java index c7471652..92b303a7 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java @@ -137,6 +137,12 @@ public class TaskUtils { return progress; } + public static int decrementIntegerTaskProgress(TaskProgress taskProgress) { + int progress = getIntegerTaskProgress(taskProgress); + taskProgress.setProgress(--progress); + return progress; + } + public static List<PendingTask> getApplicableTasks(Player player, QPlayer qPlayer, TaskType type, TaskConstraint... constraints) { List<PendingTask> tasks = new ArrayList<>(); List<TaskConstraint> taskConstraints = Arrays.asList(constraints); |
