From 964f253f403713eb3299b653a6982dd2254176b8 Mon Sep 17 00:00:00 2001 From: xGinko Date: Sun, 3 Nov 2024 23:41:51 +0100 Subject: [PATCH] ditch removal listener --- .../modules/TrailsWhenThrown.java | 39 ++++++++----------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/src/main/java/me/xginko/snowballfight/modules/TrailsWhenThrown.java b/src/main/java/me/xginko/snowballfight/modules/TrailsWhenThrown.java index 1d26087..1027a7e 100644 --- a/src/main/java/me/xginko/snowballfight/modules/TrailsWhenThrown.java +++ b/src/main/java/me/xginko/snowballfight/modules/TrailsWhenThrown.java @@ -3,10 +3,6 @@ import com.cryptomorin.xseries.XEntityType; import com.cryptomorin.xseries.particles.XParticle; import com.destroystokyo.paper.ParticleBuilder; -import com.github.benmanes.caffeine.cache.Cache; -import com.github.benmanes.caffeine.cache.Caffeine; -import com.github.benmanes.caffeine.cache.RemovalCause; -import com.github.benmanes.caffeine.cache.RemovalListener; import me.xginko.snowballfight.SnowballFight; import me.xginko.snowballfight.WrappedSnowball; import org.bukkit.Location; @@ -17,27 +13,29 @@ import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.bukkit.event.entity.ProjectileLaunchEvent; -import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import space.arim.morepaperlib.scheduling.ScheduledTask; -import java.time.Duration; +import java.util.Map; import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; -public class TrailsWhenThrown extends SnowballModule implements RemovalListener, Listener { +public class TrailsWhenThrown extends SnowballModule implements Listener { - private final long maxTrailSeconds, initialDelayTicks, periodTicks; + private final long trailDurationMillis, initialDelayTicks, periodTicks; private final int particlesPerTick; private final boolean onlyPlayers; - private Cache particleTracker; + private Map particleTracker; protected TrailsWhenThrown() { super("settings.trails", true, "\nSpawn colored particle trails when a snowball is launched."); this.onlyPlayers = config.getBoolean(configPath + ".only-thrown-by-player", true, "If enabled will only work if the snowball was thrown by a player."); - this.maxTrailSeconds = Math.max(1, config.getInt(configPath + ".trail-duration-seconds", 20)); + this.trailDurationMillis = TimeUnit.SECONDS.toMillis( + Math.max(1, config.getInt(configPath + ".trail-duration-seconds", 20))); this.particlesPerTick = Math.max(1, config.getInt(configPath + ".particles-per-tick", 10, "How many particles to spawn per tick. Recommended to leave low.")); this.initialDelayTicks = Math.max(1, config.getInt(configPath + ".initial-delay-ticks", 3, @@ -50,7 +48,7 @@ protected TrailsWhenThrown() { @Override public void enable() { - particleTracker = Caffeine.newBuilder().expireAfterWrite(Duration.ofSeconds(maxTrailSeconds)).evictionListener(this).build(); + particleTracker = new ConcurrentHashMap<>(); plugin.getServer().getPluginManager().registerEvents(this, plugin); } @@ -58,25 +56,19 @@ public void enable() { public void disable() { HandlerList.unregisterAll(this); if (particleTracker != null) { - particleTracker.invalidateAll(); - particleTracker.cleanUp(); + particleTracker.forEach(((uuid, scheduledTask) -> scheduledTask.cancel())); + particleTracker.clear(); particleTracker = null; } } - @Override - public void onRemoval(@Nullable UUID uuid, @Nullable ScheduledTask scheduledTask, @NonNull RemovalCause cause) { - if (scheduledTask != null) // Shouldn't happen since we aren't using weakKeys but just in case - scheduledTask.cancel(); - } - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) private void onProjectileLaunch(ProjectileLaunchEvent event) { if (event.getEntityType() != XEntityType.SNOWBALL.get()) return; if (onlyPlayers && !(event.getEntity().getShooter() instanceof Player)) return; final Snowball snowball = (Snowball) event.getEntity(); - if (particleTracker.getIfPresent(snowball.getUniqueId()) != null) return; + if (particleTracker.containsKey(snowball.getUniqueId())) return; final WrappedSnowball wrappedSnowball = SnowballFight.snowballs().get(snowball); @@ -87,10 +79,11 @@ private void onProjectileLaunch(ProjectileLaunchEvent event) { .color(wrappedSnowball.getSecondaryColor()) .count(particlesPerTick); + final long expireTimeMillis = System.currentTimeMillis() + trailDurationMillis; + @Nullable ScheduledTask particleTask = SnowballFight.scheduling().entitySpecificScheduler(snowball).runAtFixedRate(() -> { - if (snowball.isDead()) { // Stop the task because it would keep running until server restart on bukkit scheduler. - ScheduledTask task = particleTracker.getIfPresent(snowball.getUniqueId()); - if (task != null) task.cancel(); + if (snowball.isDead() || System.currentTimeMillis() >= expireTimeMillis) { + particleTracker.remove(snowball.getUniqueId()).cancel(); return; }