diff options
| author | Krakenied <Krakenied1@gmail.com> | 2023-05-27 17:09:56 +0200 |
|---|---|---|
| committer | Krakenied <Krakenied1@gmail.com> | 2023-05-27 17:09:56 +0200 |
| commit | 0a78d93f605e4632d67cb3319c229e9c3f9e8fd0 (patch) | |
| tree | 39de9b891a2e7d246d77967699972fa69613a7e7 | |
| parent | 0086abeaeb41d94c684cedae916ea0b97b2b7a5a (diff) | |
Add support for custom PlayerBlockTracker forks
4 files changed, 30 insertions, 9 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 e593fcb2..fb4de482 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/BukkitQuestsPlugin.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/BukkitQuestsPlugin.java @@ -53,16 +53,19 @@ import com.leonardobishop.quests.common.tasktype.TaskTypeManager; import com.leonardobishop.quests.common.updater.Updater; import org.bstats.bukkit.MetricsLite; import org.bukkit.Bukkit; +import org.bukkit.block.Block; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitTask; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.*; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -281,8 +284,14 @@ public class BukkitQuestsPlugin extends JavaPlugin implements Quests { if (Bukkit.getPluginManager().isPluginEnabled("Essentials")) { this.essentialsHook = new EssentialsHook(); } - if (Bukkit.getPluginManager().isPluginEnabled("PlayerBlockTracker")) { - this.playerBlockTrackerHook = new PlayerBlockTrackerHook(); + try { + String className = questsConfig.getString("options.playerblocktracker-class-name", "com.gestankbratwurst.playerblocktracker.PlayerBlockTracker"); + + //noinspection unchecked + Class<? extends Plugin> pluginClazz = (Class<? extends Plugin>) Class.forName(className); + Method isTrackedMethod = pluginClazz.getMethod("isTracked", Block.class); + this.playerBlockTrackerHook = new PlayerBlockTrackerHook(pluginClazz, isTrackedMethod); + } catch (ClassCastException | ClassNotFoundException | NoSuchMethodException ignored) { } taskTypeManager.registerTaskType(new MiningTaskType(this)); diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/playerblocktracker/PlayerBlockTrackerHook.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/playerblocktracker/PlayerBlockTrackerHook.java index 72705312..0221ba57 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/playerblocktracker/PlayerBlockTrackerHook.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/playerblocktracker/PlayerBlockTrackerHook.java @@ -1,27 +1,39 @@ package com.leonardobishop.quests.bukkit.hook.playerblocktracker; -import com.gestankbratwurst.playerblocktracker.PlayerBlockTracker; -import org.bukkit.Bukkit; import org.bukkit.block.Block; import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.plugin.Plugin; import org.bukkit.plugin.RegisteredListener; +import java.lang.reflect.Method; + public class PlayerBlockTrackerHook implements AbstractPlayerBlockTrackerHook { + private final Class<? extends Plugin> pluginClazz; + private final Method isTrackedMethod; + + public PlayerBlockTrackerHook(Class<? extends Plugin> pluginClazz, Method isTrackedMethod) { + this.pluginClazz = pluginClazz; + this.isTrackedMethod = isTrackedMethod; + } + @Override public boolean checkBlock(Block block) { - return PlayerBlockTracker.isTracked(block); + try { + return (boolean) this.isTrackedMethod.invoke(null, block); + } catch (Throwable e) { // suppress all errors and exceptions + return false; + } } @Override public void fixPlayerBlockTracker() { - PlayerBlockTracker playerBlockTracker = (PlayerBlockTracker) Bukkit.getPluginManager().getPlugin("PlayerBlockTracker"); HandlerList handlerList = BlockBreakEvent.getHandlerList(); RegisteredListener[] listeners = handlerList.getRegisteredListeners(); for (RegisteredListener listener : listeners) { - if (listener.getPlugin() == playerBlockTracker && listener.getPriority() == EventPriority.MONITOR) { + if (listener.getPlugin().getClass() == this.pluginClazz && listener.getPriority() == EventPriority.MONITOR) { handlerList.unregister(listener); handlerList.register(listener); } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/CommandTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/CommandTaskType.java index 78f36ef5..6af9fcd0 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/CommandTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/CommandTaskType.java @@ -12,8 +12,6 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.player.PlayerCommandPreprocessEvent; -import java.util.List; - public final class CommandTaskType extends BukkitTaskType { private final BukkitQuestsPlugin plugin; diff --git a/bukkit/src/main/resources/resources/bukkit/config.yml b/bukkit/src/main/resources/resources/bukkit/config.yml index adef1a1f..ab6e0c18 100644 --- a/bukkit/src/main/resources/resources/bukkit/config.yml +++ b/bukkit/src/main/resources/resources/bukkit/config.yml @@ -208,6 +208,8 @@ options: record-log-history: true # Replace placeholders from PlaceholderAPI in rewards, rewardstrings and start strings quests-use-placeholderapi: false + # PlayerBlockTracker class to be used with the hook + playerblocktracker-class-name: "com.gestankbratwurst.playerblocktracker.PlayerBlockTracker" # Verify quests exist when a player's data is loaded - inconsistencies may arise when # players progress on specific quests and those quests are later removed. The problem is that their progress # is still kept in the quest progress file, which may lead to issues such as players reaching a quest started |
