From 2d779d19bf8d0753bcca96aeb308d50c2db2e670 Mon Sep 17 00:00:00 2001 From: Jules Date: Mon, 2 Oct 2023 00:01:49 +0200 Subject: [PATCH] Fixed an issue w/ casting mode on logout --- .../api/event/PlayerDataLoadEvent.java | 2 +- .../event/PlayerEnterCastingModeEvent.java | 12 +++++-- .../api/event/PlayerExitCastingModeEvent.java | 11 ++++-- .../mmocore/api/player/PlayerData.java | 34 ++++++++----------- .../skill/cast/SkillCastingInstance.java | 2 +- .../skill/cast/listener/KeyCombos.java | 6 ++-- .../mmocore/skill/cast/listener/SkillBar.java | 13 +++---- .../skill/cast/listener/SkillScroller.java | 9 +++-- 8 files changed, 44 insertions(+), 45 deletions(-) diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/event/PlayerDataLoadEvent.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/event/PlayerDataLoadEvent.java index 96ca55a2..cbabebeb 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/event/PlayerDataLoadEvent.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/event/PlayerDataLoadEvent.java @@ -4,7 +4,7 @@ import net.Indyuce.mmocore.api.player.PlayerData; import org.bukkit.event.HandlerList; /** - * @deprecated Use {@link AsyncPlayerDataLoadEvent} instead + * @deprecated Use {@link io.lumine.mythic.lib.api.event.SynchronizedDataLoadEvent} instead */ @Deprecated public class PlayerDataLoadEvent extends PlayerDataEvent { diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/event/PlayerEnterCastingModeEvent.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/event/PlayerEnterCastingModeEvent.java index 857fdc5e..718ddee0 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/event/PlayerEnterCastingModeEvent.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/event/PlayerEnterCastingModeEvent.java @@ -7,13 +7,19 @@ import org.bukkit.event.HandlerList; import org.jetbrains.annotations.NotNull; public class PlayerEnterCastingModeEvent extends PlayerDataEvent implements Cancellable { - private static final HandlerList handlerList = new HandlerList(); private boolean cancelled = false; + private static final HandlerList HANDLERS = new HandlerList(); + + @Deprecated public PlayerEnterCastingModeEvent(@NotNull Player who) { super(PlayerData.get(who)); } + public PlayerEnterCastingModeEvent(@NotNull PlayerData playerData) { + super(playerData); + } + @Override public boolean isCancelled() { return cancelled; @@ -30,7 +36,7 @@ public class PlayerEnterCastingModeEvent extends PlayerDataEvent implements Canc return getHandlerList(); } - public static HandlerList getHandlerList(){ - return handlerList; + public static HandlerList getHandlerList() { + return HANDLERS; } } diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/event/PlayerExitCastingModeEvent.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/event/PlayerExitCastingModeEvent.java index 0dbfb910..d8d03647 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/event/PlayerExitCastingModeEvent.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/event/PlayerExitCastingModeEvent.java @@ -8,13 +8,18 @@ import org.bukkit.event.HandlerList; import org.jetbrains.annotations.NotNull; public class PlayerExitCastingModeEvent extends PlayerDataEvent implements Cancellable { - private static final HandlerList handlerList = new HandlerList(); private boolean cancelled = false; + private static final HandlerList HANDLERS = new HandlerList(); + + @Deprecated public PlayerExitCastingModeEvent(@NotNull Player who) { super(PlayerData.get(who)); } + public PlayerExitCastingModeEvent(@NotNull PlayerData who) { + super(who); + } @Override public boolean isCancelled() { @@ -29,10 +34,10 @@ public class PlayerExitCastingModeEvent extends PlayerDataEvent implements Cance @NotNull @Override public HandlerList getHandlers() { - return handlerList; + return HANDLERS; } public static HandlerList getHandlerList() { - return handlerList; + return HANDLERS; } } diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java index de488cde..e112550a 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java @@ -357,7 +357,7 @@ public class PlayerData extends SynchronizedDataHolder implements OfflinePlayerD /** * @return If the item is unlocked by the player - * This is used for skills that can be locked & unlocked. + * This is used for skills that can be locked & unlocked. */ public boolean hasUnlocked(Unlockable unlockable) { return unlockable.isUnlockedByDefault() || unlockedItems.contains(unlockable.getUnlockNamespacedKey()); @@ -429,7 +429,7 @@ public class PlayerData extends SynchronizedDataHolder implements OfflinePlayerD boundSkills.forEach((slot, info) -> info.close()); // Stop skill casting - if (isCasting()) leaveSkillCasting(); + if (isCasting()) leaveSkillCasting(true); } public List getFriends() { @@ -1012,23 +1012,16 @@ public class PlayerData extends SynchronizedDataHolder implements OfflinePlayerD @Deprecated public boolean setSkillCasting(@NotNull SkillCastingInstance skillCasting) { - Validate.isTrue(!isCasting(), "Player already in casting mode"); - PlayerEnterCastingModeEvent event = new PlayerEnterCastingModeEvent(getPlayer()); - Bukkit.getPluginManager().callEvent(event); - if (event.isCancelled()) return false; - - skillCasting.close(); - setSkillCasting(); - return true; + return setSkillCasting(); } /** - * @return true if the PlayerEnterCastingModeEvent successfully put the player into casting mode, otherwise if the event is cancelled, returns false. - * @apiNote Changed to a boolean to reflect the cancellation state of the event being fired + * @return If the PlayerEnterCastingModeEvent successfully put the player + * into casting mode, otherwise if the event is cancelled, returns false. */ public boolean setSkillCasting() { Validate.isTrue(!isCasting(), "Player already in casting mode"); - PlayerEnterCastingModeEvent event = new PlayerEnterCastingModeEvent(getPlayer()); + PlayerEnterCastingModeEvent event = new PlayerEnterCastingModeEvent(this); Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) return false; @@ -1042,22 +1035,23 @@ public class PlayerData extends SynchronizedDataHolder implements OfflinePlayerD } /** - * API Method to leave casting mode and fire the PlayerExitCastingModeEvent - * - * @return true if the skill casting mode was left, or false if the event was cancelled, keeping the player in casting mode. + * @return If player successfully left skill casting i.e the Bukkit + * event has not been cancelled */ public boolean leaveSkillCasting() { return leaveSkillCasting(false); } /** - * @param skipEvent Skip Firing the PlayerExitCastingModeEvent - * @return true if the PlayerExitCastingModeEvent is not cancelled, or if the event is skipped. + * @param skipEvent Skip firing the exit event + * @return If player successfully left skill casting i.e the Bukkit + * event has not been cancelled */ public boolean leaveSkillCasting(boolean skipEvent) { Validate.isTrue(isCasting(), "Player not in casting mode"); + if (!skipEvent) { - PlayerExitCastingModeEvent event = new PlayerExitCastingModeEvent(getPlayer()); + PlayerExitCastingModeEvent event = new PlayerExitCastingModeEvent(this); Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) return false; } @@ -1227,7 +1221,7 @@ public class PlayerData extends SynchronizedDataHolder implements OfflinePlayerD * checks if they could potentially upgrade to one of these * * @return If the player can change its current class to - * a subclass + * a subclass */ @Deprecated public boolean canChooseSubclass() { diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/skill/cast/SkillCastingInstance.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/skill/cast/SkillCastingInstance.java index 7dac62a8..efe640ce 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/skill/cast/SkillCastingInstance.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/skill/cast/SkillCastingInstance.java @@ -40,7 +40,7 @@ public abstract class SkillCastingInstance extends BukkitRunnable implements Lis @Override public void run() { if (!caster.isOnline() || caster.getPlayer().isDead()) { - caster.leaveSkillCasting(); + caster.leaveSkillCasting(true); return; } diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/skill/cast/listener/KeyCombos.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/skill/cast/listener/KeyCombos.java index 1cc38e6c..27890325 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/skill/cast/listener/KeyCombos.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/skill/cast/listener/KeyCombos.java @@ -118,7 +118,7 @@ public class KeyCombos implements SkillCastingListener { // Hash current combo and check if (casting.combos.getCombos().containsKey(casting.current)) { final int spellSlot = casting.combos.getCombos().get(casting.current); - playerData.leaveSkillCasting(); + playerData.leaveSkillCasting(true); // Cast spell if (playerData.hasSkillBound(spellSlot)) { @@ -130,7 +130,7 @@ public class KeyCombos implements SkillCastingListener { // Check if current combo is too large if (casting.current.countKeys() >= casting.combos.getLongest()) { - playerData.leaveSkillCasting(); + playerData.leaveSkillCasting(true); if (failComboSound != null) failComboSound.playTo(player); } } @@ -175,7 +175,7 @@ public class KeyCombos implements SkillCastingListener { @Override public void onTick() { - if (getCaster().getBoundSkills().isEmpty()) getCaster().leaveSkillCasting(); + if (getCaster().getBoundSkills().isEmpty()) getCaster().leaveSkillCasting(true); else if (actionBarOptions != null) if (actionBarOptions.isSubtitle) getCaster().getPlayer().sendTitle(" ", actionBarOptions.format(this), 0, 20, 0); else getCaster().displayActionBar(actionBarOptions.format(this)); diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/skill/cast/listener/SkillBar.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/skill/cast/listener/SkillBar.java index 3601009e..08bb6608 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/skill/cast/listener/SkillBar.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/skill/cast/listener/SkillBar.java @@ -112,15 +112,10 @@ public class SkillBar implements SkillCastingListener { final Player player = event.getPlayer(); if (disableSneak && player.isSneaking()) return; - MMOCore.plugin.soundManager.getSound(SoundEvent.SPELL_CAST_END).playTo(player); - - new BukkitRunnable() { - @Override - public void run() { - MMOCore.plugin.configManager.getSimpleMessage("casting.no-longer").send(getCaster().getPlayer()); - } - }.runTask(MMOCore.plugin); - getCaster().leaveSkillCasting(); + if (getCaster().leaveSkillCasting()) { + MMOCore.plugin.soundManager.getSound(SoundEvent.SPELL_CAST_END).playTo(player); + MMOCore.plugin.configManager.getSimpleMessage("casting.no-longer").send(getCaster().getPlayer()); + } } private String getFormat(PlayerData data) { diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/skill/cast/listener/SkillScroller.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/skill/cast/listener/SkillScroller.java index 6d5a66c7..34f59ee5 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/skill/cast/listener/SkillScroller.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/skill/cast/listener/SkillScroller.java @@ -69,7 +69,8 @@ public class SkillScroller implements SkillCastingListener { // Cancel event if necessary if (event.getPressed().shouldCancelEvent()) event.setCancelled(true); - playerData.leaveSkillCasting(); + if (!playerData.leaveSkillCasting()) return; + if (leaveSound != null) leaveSound.playTo(player); return; } @@ -81,9 +82,7 @@ public class SkillScroller implements SkillCastingListener { if (event.getPressed().shouldCancelEvent()) event.setCancelled(true); // Enter casting mode - if (!playerData.setSkillCasting()) { - return; - } + if (!playerData.setSkillCasting()) return; if (enterSound != null) enterSound.playTo(player); } @@ -105,7 +104,7 @@ public class SkillScroller implements SkillCastingListener { if (!playerData.isCasting()) return; if (playerData.getBoundSkills().isEmpty()) { - playerData.leaveSkillCasting(); + playerData.leaveSkillCasting(true); return; }