From 87244bdb1169267cc6171dca2de57c8f3b9fcdbf Mon Sep 17 00:00:00 2001 From: Krakenied Date: Tue, 11 Jun 2024 18:48:05 +0200 Subject: PyroFishingPro support improvements Add dedicated PyroFishingPro item support Use dedicated PyroFishingPro player getter --- bukkit/libs/PyroFishingPro-4.8.28.jar | Bin 1797 -> 0 bytes bukkit/libs/PyroFishingPro-4.9.7.jar | Bin 0 -> 1934 bytes .../quests/bukkit/config/BukkitQuestsLoader.java | 5 ++ .../bukkit/item/PyroFishingProQuestItem.java | 61 +++++++++++++++++++++ .../dependent/PyroFishingProFishingTaskType.java | 60 ++++++++++++++++---- docs/configuration/defining-items.md | 19 +++++++ 6 files changed, 134 insertions(+), 11 deletions(-) delete mode 100644 bukkit/libs/PyroFishingPro-4.8.28.jar create mode 100644 bukkit/libs/PyroFishingPro-4.9.7.jar create mode 100644 bukkit/src/main/java/com/leonardobishop/quests/bukkit/item/PyroFishingProQuestItem.java diff --git a/bukkit/libs/PyroFishingPro-4.8.28.jar b/bukkit/libs/PyroFishingPro-4.8.28.jar deleted file mode 100644 index 6742612f..00000000 Binary files a/bukkit/libs/PyroFishingPro-4.8.28.jar and /dev/null differ diff --git a/bukkit/libs/PyroFishingPro-4.9.7.jar b/bukkit/libs/PyroFishingPro-4.9.7.jar new file mode 100644 index 00000000..58b1067c Binary files /dev/null and b/bukkit/libs/PyroFishingPro-4.9.7.jar differ diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/config/BukkitQuestsLoader.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/config/BukkitQuestsLoader.java index 15513ff6..086569fb 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/config/BukkitQuestsLoader.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/config/BukkitQuestsLoader.java @@ -7,6 +7,7 @@ import com.leonardobishop.quests.bukkit.item.ItemsAdderQuestItem; import com.leonardobishop.quests.bukkit.item.MMOItemsQuestItem; import com.leonardobishop.quests.bukkit.item.OraxenQuestItem; import com.leonardobishop.quests.bukkit.item.ParsedQuestItem; +import com.leonardobishop.quests.bukkit.item.PyroFishingProQuestItem; import com.leonardobishop.quests.bukkit.item.QuestItem; import com.leonardobishop.quests.bukkit.item.QuestItemRegistry; import com.leonardobishop.quests.bukkit.item.SlimefunQuestItem; @@ -504,6 +505,10 @@ public class BukkitQuestsLoader implements QuestsLoader { if (!Bukkit.getPluginManager().isPluginEnabled("Oraxen")) return FileVisitResult.CONTINUE; item = new OraxenQuestItem(id, config.getString("item.id")); break; + case "pyrofishingpro": + if (!Bukkit.getPluginManager().isPluginEnabled("PyroFishingPro")) return FileVisitResult.CONTINUE; + item = new PyroFishingProQuestItem(id, config.getInt("item.fish-number", -1), config.getString("item.tier")); + break; } questItemRegistry.registerItem(id, item); diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/item/PyroFishingProQuestItem.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/item/PyroFishingProQuestItem.java new file mode 100644 index 00000000..16fff5e8 --- /dev/null +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/item/PyroFishingProQuestItem.java @@ -0,0 +1,61 @@ +package com.leonardobishop.quests.bukkit.item; + +import com.leonardobishop.quests.bukkit.util.NamespacedKeyUtils; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataContainer; +import org.bukkit.persistence.PersistentDataType; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.Range; + +public final class PyroFishingProQuestItem extends QuestItem { + + private static final NamespacedKey FISH_NUMBER_KEY = NamespacedKeyUtils.fromString("pyrofishingpro:fishnumber"); + private static final NamespacedKey TIER_KEY = NamespacedKeyUtils.fromString("pyrofishingpro:tier"); + + // there is really no way to get the item properly + private static final ItemStack ITEM = new ItemStack(Material.COOKED_COD, 1); + + static { + final ItemMeta meta = PyroFishingProQuestItem.ITEM.getItemMeta(); + //noinspection deprecation + meta.setDisplayName("This item type cannot be gotten"); + PyroFishingProQuestItem.ITEM.setItemMeta(meta); + } + + private final int fishNumber; + private final String tier; + + public PyroFishingProQuestItem(final @NotNull String id, final @Range(from = -1, to = Integer.MAX_VALUE) int fishNumber, final @Nullable String tier) { + super("pyrofishingpro", id); + this.fishNumber = fishNumber; + this.tier = tier; + } + + @Override + public @NotNull ItemStack getItemStack() { + return PyroFishingProQuestItem.ITEM; + } + + @Override + public boolean compareItemStack(final @NotNull ItemStack other, final boolean exactMatch) { + final ItemMeta meta = other.getItemMeta(); + final PersistentDataContainer pdc = meta.getPersistentDataContainer(); + + final int fishNumber = pdc.getOrDefault(PyroFishingProQuestItem.FISH_NUMBER_KEY, PersistentDataType.INTEGER, -1); + if (fishNumber == -1) { + return false; + } + + final String tier = pdc.get(PyroFishingProQuestItem.TIER_KEY, PersistentDataType.STRING); + if (tier == null) { + return false; + } + + return (this.fishNumber == -1 || this.fishNumber == fishNumber) + && (this.tier == null || this.tier.equals(tier)); + } +} diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/PyroFishingProFishingTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/PyroFishingProFishingTaskType.java index bbd11c41..483b9635 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/PyroFishingProFishingTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/PyroFishingProFishingTaskType.java @@ -12,11 +12,16 @@ import me.arsmagica.API.PyroFishCatchEvent; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerFishEvent; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + public final class PyroFishingProFishingTaskType extends BukkitTaskType { private final BukkitQuestsPlugin plugin; + private Method getPlayerMethod = null; private Player lastPlayer = null; public PyroFishingProFishingTaskType(BukkitQuestsPlugin plugin) { @@ -26,24 +31,57 @@ public final class PyroFishingProFishingTaskType extends BukkitTaskType { super.addConfigValidator(TaskUtils.useRequiredConfigValidator(this, "amount")); super.addConfigValidator(TaskUtils.useIntegerConfigValidator(this, "amount")); super.addConfigValidator(TaskUtils.useIntegerConfigValidator(this, "fish-number")); + + for (Method method : PyroFishCatchEvent.class.getMethods()) { + if (method.getReturnType() != Player.class) { + continue; + } + + if (method.getParameterTypes().length != 0) { + continue; + } + + if (this.getPlayerMethod != null) { + // set it to null and break as there is more than 1 method returning a player + this.getPlayerMethod = null; + break; + } else { + this.getPlayerMethod = method; + } + } + + if (this.getPlayerMethod == null) { + this.plugin.getLogger().warning("No valid player getter found for PyroFishCatchEvent, using legacy workaround."); + this.plugin.getServer().getPluginManager().registerEvents(new PlayerFishListener(), this.plugin); + } } - @EventHandler(priority = EventPriority.LOW) - public void onPlayerFish(PlayerFishEvent event) { - PlayerFishEvent.State state = event.getState(); - if (state == PlayerFishEvent.State.CAUGHT_FISH) { - lastPlayer = event.getPlayer(); + private Player getLastPlayer(PyroFishCatchEvent event) { + if (this.getPlayerMethod != null) { + try { + return (Player) this.getPlayerMethod.invoke(event); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + } else { + return this.lastPlayer; } } - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onPyroFishCatch(PyroFishCatchEvent event) { - if (lastPlayer == null) { - return; + private final class PlayerFishListener implements Listener { + @EventHandler(priority = EventPriority.LOW) + public void onPlayerFish(PlayerFishEvent event) { + PlayerFishEvent.State state = event.getState(); + if (state == PlayerFishEvent.State.CAUGHT_FISH) { + PyroFishingProFishingTaskType.this.lastPlayer = event.getPlayer(); + } } + } - Player player = lastPlayer; - if (player.hasMetadata("NPC")) { + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onPyroFishCatch(PyroFishCatchEvent event) { + Player player = getLastPlayer(event); + if (player == null || player.hasMetadata("NPC")) { return; } diff --git a/docs/configuration/defining-items.md b/docs/configuration/defining-items.md index 1d9c8981..1e7aeaf3 100644 --- a/docs/configuration/defining-items.md +++ b/docs/configuration/defining-items.md @@ -230,6 +230,9 @@ The types of quest items are as follows: - `mmoitems` (items from MMOItems) - `slimefun` (items from Slimefun) - `executableitems` (items from ExecutableItems) +- `itemsadder` (items from ItemsAdder) +- `oraxen` (items from Oraxen) +- `pyrofishingpro` (items from PyroFishingPro) ### Importing items @@ -331,6 +334,22 @@ item: id: "oraxen_id" #oraxen id ``` +#### PyroFishingPro + +**PyroFishingPro quest items** are ItemStacks which belong to the +PyroFishingPro plugin. Can be used with many task types except `fishing`. +To ensure the orderly functioning, `pyrofishingpro_fishing` type should +be used instead of utilising defined item in regular `fishing` tasks. + + items/testitem.yml + +``` yaml +type: "pyrofishingpro" +item: + fish-number: 123 #pyrofishingpro fish number (optional) + tier: "Mythical" #pyrofishing fish tier (optional) +``` + ### Referencing a quest item In most cases where an ItemStack is accepted in Quests, you can simply -- cgit v1.2.3-70-g09d2