From 468c5e478030e5b5fd285296397942ae324a63ef Mon Sep 17 00:00:00 2001 From: JasperLorelai Date: Thu, 12 Nov 2026 02:14:40 +0100 Subject: [PATCH 1/2] refactor: Cleanup --- .../spells/targeted/StunSpell.java | 82 ++++++++++--------- 1 file changed, 43 insertions(+), 39 deletions(-) diff --git a/core/src/main/java/com/nisovin/magicspells/spells/targeted/StunSpell.java b/core/src/main/java/com/nisovin/magicspells/spells/targeted/StunSpell.java index 12c661989..5be1f5652 100644 --- a/core/src/main/java/com/nisovin/magicspells/spells/targeted/StunSpell.java +++ b/core/src/main/java/com/nisovin/magicspells/spells/targeted/StunSpell.java @@ -1,12 +1,9 @@ package com.nisovin.magicspells.spells.targeted; -import java.util.Set; -import java.util.Map; -import java.util.UUID; -import java.util.HashSet; -import java.util.HashMap; +import java.util.*; import org.bukkit.Location; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.Listener; import org.bukkit.event.EventHandler; @@ -22,9 +19,11 @@ import com.nisovin.magicspells.util.config.ConfigData; import com.nisovin.magicspells.spells.TargetedEntitySpell; +import com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent; + public class StunSpell extends TargetedSpell implements TargetedEntitySpell { - private final Map stunnedLivingEntities; + private final Map stunned = new HashMap<>(); private final int interval; private final ConfigData duration; @@ -34,7 +33,7 @@ public class StunSpell extends TargetedSpell implements TargetedEntitySpell { private final ConfigData useTargetLocation; private final ConfigData powerAffectsDuration; - private final StunListener listener; + private final StunListener listener = new StunListener(); public StunSpell(MagicConfig config, String spellName) { super(config, spellName); @@ -46,9 +45,6 @@ public StunSpell(MagicConfig config, String spellName) { stunMonitor = getConfigDataBoolean("stun-monitor", true); useTargetLocation = getConfigDataBoolean("use-target-location", true); powerAffectsDuration = getConfigDataBoolean("power-affects-duration", true); - - listener = new StunListener(); - stunnedLivingEntities = new HashMap<>(); } @Override @@ -77,38 +73,46 @@ public CastResult castAtEntity(SpellData data) { long duration = this.duration.get(data) * TimeUtil.MILLISECONDS_PER_SECOND / 20; if (powerAffectsDuration.get(data)) duration = Math.round(duration * data.power()); - boolean stunBody = this.stunBody.get(data); - boolean stunMonitor = this.stunMonitor.get(data); - boolean useTargetLocation = this.useTargetLocation.get(data); - Location targetLocation = useTargetLocation ? data.target().getLocation() : null; - - StunnedInfo info = new StunnedInfo(data.caster(), data.target(), System.currentTimeMillis() + duration, stunBody, stunMonitor, useTargetLocation, targetLocation); - stunnedLivingEntities.put(data.target().getUniqueId(), info); + StunnedInfo info = new StunnedInfo( + data.caster(), + data.target(), + System.currentTimeMillis() + duration, + stunBody.get(data), + stunMonitor.get(data), + useTargetLocation.get(data) ? data.target().getLocation() : null + ); + stunned.put(data.target().getUniqueId(), info); playSpellEffects(data); - playSpellEffectsBuff(data.target(), e -> stunnedLivingEntities.containsKey(e.getUniqueId()), data); + playSpellEffectsBuff(data.target(), this::isStunned, data); return new CastResult(PostCastAction.HANDLE_NORMALLY, data); } - public boolean isStunned(LivingEntity entity) { - return stunnedLivingEntities.containsKey(entity.getUniqueId()); + public boolean isStunned(Entity entity) { + return stunned.containsKey(entity.getUniqueId()); } - public void removeStun(LivingEntity entity) { - stunnedLivingEntities.remove(entity.getUniqueId()); + public void removeStun(Entity entity) { + stunned.remove(entity.getUniqueId()); } - private record StunnedInfo(LivingEntity caster, LivingEntity target, long until, boolean stunBody, boolean stunMonitor, boolean useTargetLocation, Location targetLocation) { - } + private record StunnedInfo( + LivingEntity caster, + LivingEntity target, + long until, + boolean stunBody, + boolean stunMonitor, + Location targetLocation + ) {} private class StunListener implements Listener { - @EventHandler + @EventHandler(ignoreCancelled = true) private void onMove(PlayerMoveEvent event) { Player player = event.getPlayer(); - if (!isStunned(player)) return; - StunnedInfo info = stunnedLivingEntities.get(player.getUniqueId()); + + StunnedInfo info = stunned.get(player.getUniqueId()); if (info == null) return; if (System.currentTimeMillis() >= info.until) { @@ -119,7 +123,8 @@ private void onMove(PlayerMoveEvent event) { boolean stunBody = info.stunBody && event.hasExplicitlyChangedPosition(); boolean stunMonitor = info.stunMonitor && event.hasChangedOrientation(); if (!stunBody && !stunMonitor) return; - player.teleport(info.useTargetLocation ? info.targetLocation : event.getFrom()); + + player.teleport(Objects.requireNonNullElse(info.targetLocation, event.getFrom())); } @EventHandler @@ -130,16 +135,17 @@ private void onInteract(PlayerInteractEvent event) { @EventHandler private void onQuit(PlayerQuitEvent event) { - Player player = event.getPlayer(); - if (!isStunned(player)) return; - removeStun(player); + removeStun(event.getPlayer()); } - @EventHandler + @EventHandler(ignoreCancelled = true) private void onDeath(PlayerDeathEvent event) { - Player player = event.getEntity(); - if (!isStunned(player)) return; - removeStun(player); + removeStun(event.getEntity()); + } + + @EventHandler + private void onRemove(EntityRemoveFromWorldEvent event) { + removeStun(event.getEntity()); } } @@ -150,13 +156,11 @@ private class StunMonitor implements Runnable { @Override public void run() { - for (UUID id : stunnedLivingEntities.keySet()) { - StunnedInfo info = stunnedLivingEntities.get(id); + for (StunnedInfo info : stunned.values()) { LivingEntity entity = info.target; - long until = info.until; if (entity instanceof Player) continue; - if (entity.isValid() && until > System.currentTimeMillis()) { + if (entity.isValid() && info.until > System.currentTimeMillis()) { entity.teleport(info.targetLocation); continue; } From 607ea2d703c94c453be672c3da4fe76aaac3a0b6 Mon Sep 17 00:00:00 2001 From: JasperLorelai Date: Thu, 12 Nov 2026 19:01:41 +0100 Subject: [PATCH 2/2] fix: Expiration and movement handling - Expiration is now duration-based instead of being checked on interval for non-players, and only on movement for players. - Non-player movement is now cancelled instead of relying on interval-teleportation. Removed "interval" config option. --- .../spells/targeted/StunSpell.java | 64 ++++++++----------- 1 file changed, 27 insertions(+), 37 deletions(-) diff --git a/core/src/main/java/com/nisovin/magicspells/spells/targeted/StunSpell.java b/core/src/main/java/com/nisovin/magicspells/spells/targeted/StunSpell.java index 5be1f5652..651f2c68e 100644 --- a/core/src/main/java/com/nisovin/magicspells/spells/targeted/StunSpell.java +++ b/core/src/main/java/com/nisovin/magicspells/spells/targeted/StunSpell.java @@ -1,10 +1,11 @@ package com.nisovin.magicspells.spells.targeted; import java.util.*; +import java.util.concurrent.TimeUnit; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Entity; -import org.bukkit.entity.Player; import org.bukkit.event.Listener; import org.bukkit.event.EventHandler; import org.bukkit.entity.LivingEntity; @@ -13,6 +14,8 @@ import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.player.PlayerInteractEvent; +import io.papermc.paper.event.entity.EntityMoveEvent; + import com.nisovin.magicspells.util.*; import com.nisovin.magicspells.MagicSpells; import com.nisovin.magicspells.spells.TargetedSpell; @@ -25,7 +28,6 @@ public class StunSpell extends TargetedSpell implements TargetedEntitySpell { private final Map stunned = new HashMap<>(); - private final int interval; private final ConfigData duration; private final ConfigData stunBody; @@ -38,7 +40,6 @@ public class StunSpell extends TargetedSpell implements TargetedEntitySpell { public StunSpell(MagicConfig config, String spellName) { super(config, spellName); - interval = getConfigInt("interval", 5); duration = getConfigDataInt("duration", 200); stunBody = getConfigDataBoolean("stun-body", true); @@ -57,7 +58,6 @@ public void initialize() { } registerEvents(listener); - MagicSpells.scheduleRepeatingTask(new StunMonitor(), interval, interval); } @Override @@ -83,6 +83,16 @@ public CastResult castAtEntity(SpellData data) { ); stunned.put(data.target().getUniqueId(), info); + Bukkit.getAsyncScheduler().runDelayed( + MagicSpells.getInstance(), + _ -> { + if (info.until > System.currentTimeMillis()) return; + MagicSpells.scheduleDelayedTask(() -> removeStun(data.target()), 0); + }, + duration, + TimeUnit.MILLISECONDS + ); + playSpellEffects(data); playSpellEffectsBuff(data.target(), this::isStunned, data); @@ -108,23 +118,30 @@ private record StunnedInfo( private class StunListener implements Listener { + @EventHandler(ignoreCancelled = true) + private void onMove(EntityMoveEvent event) { + onMove(event.getEntity(), event.hasExplicitlyChangedPosition(), event.hasChangedOrientation(), event.getFrom()); + } + @EventHandler(ignoreCancelled = true) private void onMove(PlayerMoveEvent event) { - Player player = event.getPlayer(); + onMove(event.getPlayer(), event.hasExplicitlyChangedPosition(), event.hasChangedOrientation(), event.getFrom()); + } - StunnedInfo info = stunned.get(player.getUniqueId()); + private void onMove(LivingEntity entity, boolean hasExplicitlyChangedPosition, boolean hasChangedOrientation, Location from) { + StunnedInfo info = stunned.get(entity.getUniqueId()); if (info == null) return; if (System.currentTimeMillis() >= info.until) { - removeStun(player); + removeStun(entity); return; } - boolean stunBody = info.stunBody && event.hasExplicitlyChangedPosition(); - boolean stunMonitor = info.stunMonitor && event.hasChangedOrientation(); + boolean stunBody = info.stunBody && hasExplicitlyChangedPosition; + boolean stunMonitor = info.stunMonitor && hasChangedOrientation; if (!stunBody && !stunMonitor) return; - player.teleport(Objects.requireNonNullElse(info.targetLocation, event.getFrom())); + entity.teleport(Objects.requireNonNullElse(info.targetLocation, from)); } @EventHandler @@ -150,31 +167,4 @@ private void onRemove(EntityRemoveFromWorldEvent event) { } - private class StunMonitor implements Runnable { - - private final Set toRemove = new HashSet<>(); - - @Override - public void run() { - for (StunnedInfo info : stunned.values()) { - LivingEntity entity = info.target; - if (entity instanceof Player) continue; - - if (entity.isValid() && info.until > System.currentTimeMillis()) { - entity.teleport(info.targetLocation); - continue; - } - - toRemove.add(entity); - } - - for (LivingEntity entity : toRemove) { - removeStun(entity); - } - - toRemove.clear(); - } - - } - }