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..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,13 +1,11 @@ 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 java.util.concurrent.TimeUnit; +import org.bukkit.Bukkit; import org.bukkit.Location; -import org.bukkit.entity.Player; +import org.bukkit.entity.Entity; import org.bukkit.event.Listener; import org.bukkit.event.EventHandler; import org.bukkit.entity.LivingEntity; @@ -16,17 +14,20 @@ 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; 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; private final ConfigData stunBody; @@ -34,21 +35,17 @@ 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); - interval = getConfigInt("interval", 5); duration = getConfigDataInt("duration", 200); stunBody = getConfigDataBoolean("stun-body", true); stunMonitor = getConfigDataBoolean("stun-monitor", true); useTargetLocation = getConfigDataBoolean("use-target-location", true); powerAffectsDuration = getConfigDataBoolean("power-affects-duration", true); - - listener = new StunListener(); - stunnedLivingEntities = new HashMap<>(); } @Override @@ -61,7 +58,6 @@ public void initialize() { } registerEvents(listener); - MagicSpells.scheduleRepeatingTask(new StunMonitor(), interval, interval); } @Override @@ -77,49 +73,75 @@ 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); + + 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(), 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(EntityMoveEvent event) { + onMove(event.getEntity(), event.hasExplicitlyChangedPosition(), event.hasChangedOrientation(), event.getFrom()); + } + + @EventHandler(ignoreCancelled = true) private void onMove(PlayerMoveEvent event) { - Player player = event.getPlayer(); - if (!isStunned(player)) return; - StunnedInfo info = stunnedLivingEntities.get(player.getUniqueId()); + onMove(event.getPlayer(), event.hasExplicitlyChangedPosition(), event.hasChangedOrientation(), event.getFrom()); + } + + 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(info.useTargetLocation ? info.targetLocation : event.getFrom()); + + entity.teleport(Objects.requireNonNullElse(info.targetLocation, from)); } @EventHandler @@ -130,45 +152,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()); } - } - - private class StunMonitor implements Runnable { - - private final Set toRemove = new HashSet<>(); - - @Override - public void run() { - for (UUID id : stunnedLivingEntities.keySet()) { - StunnedInfo info = stunnedLivingEntities.get(id); - LivingEntity entity = info.target; - long until = info.until; - if (entity instanceof Player) continue; - - if (entity.isValid() && until > System.currentTimeMillis()) { - entity.teleport(info.targetLocation); - continue; - } - - toRemove.add(entity); - } - - for (LivingEntity entity : toRemove) { - removeStun(entity); - } - - toRemove.clear(); + @EventHandler + private void onRemove(EntityRemoveFromWorldEvent event) { + removeStun(event.getEntity()); } }