From 8361c38b1b6361569d1bd5d84bdf0ce399fd03bc Mon Sep 17 00:00:00 2001 From: XiaoMoMi <70987828+Xiao-MoMi@users.noreply.github.com> Date: Thu, 24 Oct 2024 19:45:19 +0800 Subject: [PATCH] 2.2.31 --- .../mechanic/fishing/CustomFishingHook.java | 28 ++++++++---- .../mechanic/game/AbstractGamingPlayer.java | 2 +- .../bukkit/fishing/BukkitFishingManager.java | 44 ++++++++----------- .../requirement/BukkitRequirementManager.java | 3 +- gradle.properties | 2 +- 5 files changed, 43 insertions(+), 36 deletions(-) diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/fishing/CustomFishingHook.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/fishing/CustomFishingHook.java index 9c8fe9b4..9bc3d132 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/mechanic/fishing/CustomFishingHook.java +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/fishing/CustomFishingHook.java @@ -74,6 +74,7 @@ public class CustomFishingHook { private Loot nextLoot; private GamingPlayer gamingPlayer; private BaitAnimationTask baitAnimationTask; + private boolean valid = true; private static TriFunction, Effect, List> mechanicProviders = defaultMechanicProviders(); @@ -237,10 +238,12 @@ public class CustomFishingHook { */ @ApiStatus.Internal public void stop() { - if (task != null) task.cancel(); - if (hook.isValid()) hook.remove(); - if (hookMechanic != null) hookMechanic.destroy(); - if (gamingPlayer != null) gamingPlayer.destroy(); + if (!this.valid) return; + this.valid = false; + if (this.task != null) this.task.cancel(); + if (this.hook.isValid()) this.hook.remove(); + if (this.hookMechanic != null) hookMechanic.destroy(); + if (this.gamingPlayer != null) gamingPlayer.destroy(); if (this.baitAnimationTask != null) { this.baitAnimationTask.cancel(); this.baitAnimationTask = null; @@ -251,7 +254,12 @@ public class CustomFishingHook { * Ends the life of the custom fishing hook. */ public void destroy() { - plugin.getFishingManager().destroyHook(context.holder().getUniqueId()); + // if the hook exists in cache + this.plugin.getFishingManager().destroyHook(this.context.holder().getUniqueId()); + // if not, then destroy the tasks. This should never happen + if (this.valid) { + stop(); + } } /** @@ -260,7 +268,7 @@ public class CustomFishingHook { * @return the context. */ public Context getContext() { - return context; + return this.context; } /** @@ -270,7 +278,7 @@ public class CustomFishingHook { */ @NotNull public FishHook getHookEntity() { - return hook; + return this.hook; } /** @@ -304,7 +312,7 @@ public class CustomFishingHook { public boolean isHookValid() { if (hook == null) return false; - return hook.isValid(); + return hook.isValid() && valid; } /** @@ -441,6 +449,8 @@ public class CustomFishingHook { */ public void handleFailedFishing() { + if (!valid) return; + // update the hook location context.arg(ContextKeys.OTHER_LOCATION, hook.getLocation()); context.arg(ContextKeys.OTHER_X, hook.getLocation().getBlockX()); @@ -456,6 +466,8 @@ public class CustomFishingHook { */ public void handleSuccessfulFishing() { + if (!valid) return; + // update the hook location Location hookLocation = hook.getLocation(); context.arg(ContextKeys.OTHER_LOCATION, hookLocation); diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/AbstractGamingPlayer.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/AbstractGamingPlayer.java index 47ec5fcc..abf0eecf 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/AbstractGamingPlayer.java +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/game/AbstractGamingPlayer.java @@ -66,8 +66,8 @@ public abstract class AbstractGamingPlayer implements GamingPlayer, Runnable { */ @Override public void destroy() { - if (task != null) task.cancel(); valid = false; + if (task != null) task.cancel(); } /** diff --git a/core/src/main/java/net/momirealms/customfishing/bukkit/fishing/BukkitFishingManager.java b/core/src/main/java/net/momirealms/customfishing/bukkit/fishing/BukkitFishingManager.java index d37e900c..9aeb39d2 100644 --- a/core/src/main/java/net/momirealms/customfishing/bukkit/fishing/BukkitFishingManager.java +++ b/core/src/main/java/net/momirealms/customfishing/bukkit/fishing/BukkitFishingManager.java @@ -77,17 +77,7 @@ public class BukkitFishingManager implements FishingManager, Listener { @Override public Optional getFishHook(UUID player) { - CustomFishingHook hook = castHooks.get(player); - if (hook != null) { - if (hook.isHookValid()) { - return Optional.of(hook); - } else { - hook.stop(); - this.castHooks.remove(player); - return Optional.empty(); - } - } - return Optional.empty(); + return Optional.ofNullable(castHooks.get(player)); } @Override @@ -266,16 +256,16 @@ public class BukkitFishingManager implements FishingManager, Listener { hook.get().onReelIn(); return; } - } - if (player.getGameMode() != GameMode.CREATIVE) { - ItemStack itemStack = player.getInventory().getItemInMainHand(); - if (itemStack.getType() != Material.FISHING_ROD) itemStack = player.getInventory().getItemInOffHand(); - if (plugin.getItemManager().hasCustomMaxDamage(itemStack)) { - event.getHook().pullHookedEntity(); - event.getHook().remove(); - event.setCancelled(true); - plugin.getItemManager().increaseDamage(player, itemStack, event.getCaught() instanceof Item ? 3 : 5, true); + if (player.getGameMode() != GameMode.CREATIVE) { + ItemStack itemStack = player.getInventory().getItemInMainHand(); + if (itemStack.getType() != Material.FISHING_ROD) itemStack = player.getInventory().getItemInOffHand(); + if (plugin.getItemManager().hasCustomMaxDamage(itemStack)) { + event.setCancelled(true); + event.getHook().pullHookedEntity(); + hook.get().destroy(); + plugin.getItemManager().increaseDamage(player, itemStack, event.getCaught() instanceof Item ? 3 : 5, true); + } } } } @@ -324,9 +314,13 @@ public class BukkitFishingManager implements FishingManager, Listener { if (EventUtils.fireAndCheckCancel(new RodCastEvent(event, gears))) { return; } - plugin.debug(context); + plugin.debug(context::toString); CustomFishingHook customHook = new CustomFishingHook(plugin, hook, gears, context); - this.castHooks.put(player.getUniqueId(), customHook); + CustomFishingHook previous = this.castHooks.put(player.getUniqueId(), customHook); + if (previous != null) { + plugin.debug("Previous hook is still in cache, which is not an expected behavior"); + previous.stop(); + } } private void onInGround(PlayerFishEvent event) { @@ -377,9 +371,9 @@ public class BukkitFishingManager implements FishingManager, Listener { @Override public void destroyHook(UUID uuid) { - this.getFishHook(uuid).ifPresent(hook -> { + CustomFishingHook hook = this.castHooks.remove(uuid); + if (hook != null) { hook.stop(); - this.castHooks.remove(uuid); - }); + } } } diff --git a/core/src/main/java/net/momirealms/customfishing/bukkit/requirement/BukkitRequirementManager.java b/core/src/main/java/net/momirealms/customfishing/bukkit/requirement/BukkitRequirementManager.java index cdb8fcea..1c3de337 100644 --- a/core/src/main/java/net/momirealms/customfishing/bukkit/requirement/BukkitRequirementManager.java +++ b/core/src/main/java/net/momirealms/customfishing/bukkit/requirement/BukkitRequirementManager.java @@ -273,12 +273,13 @@ public class BukkitRequirementManager implements RequirementManager { boolean mainOrOff = section.getString("hand","main").equalsIgnoreCase("main"); int amount = section.getInt("amount", 1); List items = ListUtils.toList(section.get("item")); + boolean any = items.contains("any") || items.contains("*"); return context -> { ItemStack itemStack = mainOrOff ? context.holder().getInventory().getItemInMainHand() : context.holder().getInventory().getItemInOffHand(); String id = plugin.getItemManager().getItemID(itemStack); - if (items.contains(id) && itemStack.getAmount() >= amount) return true; + if ((items.contains(id) || any) && itemStack.getAmount() >= amount) return true; if (runActions) ActionManager.trigger(context, actions); return false; }; diff --git a/gradle.properties b/gradle.properties index a70f1a6e..5a310982 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ # Project settings # Rule: [major update].[feature update].[bug fix] -project_version=2.2.30 +project_version=2.2.31 config_version=36 project_group=net.momirealms