From 16c343aae502fa1853f78149ac7fd86c78a16d7b Mon Sep 17 00:00:00 2001 From: xGinko Date: Sun, 18 Aug 2024 00:22:44 +0200 Subject: [PATCH] rework withers section --- .../aef/modules/packets/MapCursorLag.java | 2 +- .../withers/DisableWitherSkulls.java | 39 ----------- .../withers/RateLimitWitherSkulls.java | 2 +- .../withers/RemoveAllSkullsPeriodically.java | 55 ---------------- .../withers/RemoveSkullsOnChunkload.java | 10 ++- .../withers/RemoveSkullsOnChunkunload.java | 46 ------------- .../withers/WitherSkullDropsAtSpawn.java | 66 +++++++++++++++++++ ...gAtSpawn.java => WitherSummonAtSpawn.java} | 6 +- .../me/xginko/aef/utils/LocationUtil.java | 52 +++++++++++++++ .../withers/DisableWitherSkulls.java | 39 ----------- .../withers/RateLimitWitherSkulls.java | 2 +- .../withers/RemoveAllSkullsPeriodically.java | 42 ------------ .../withers/RemoveSkullsOnChunkload.java | 9 +++ .../withers/RemoveSkullsOnChunkunload.java | 44 ------------- .../withers/WitherSkullDropsAtSpawn.java | 66 +++++++++++++++++++ ...gAtSpawn.java => WitherSummonAtSpawn.java} | 6 +- .../me/xginko/aef/utils/LocationUtil.java | 52 +++++++++++++++ .../java/me/xginko/aef/utils/EntityUtil.java | 14 ++-- 18 files changed, 272 insertions(+), 280 deletions(-) delete mode 100755 AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/DisableWitherSkulls.java delete mode 100755 AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveAllSkullsPeriodically.java delete mode 100755 AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveSkullsOnChunkunload.java create mode 100755 AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSkullDropsAtSpawn.java rename AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/{WitherSpawningAtSpawn.java => WitherSummonAtSpawn.java} (94%) delete mode 100755 AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/DisableWitherSkulls.java delete mode 100755 AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveAllSkullsPeriodically.java delete mode 100755 AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveSkullsOnChunkunload.java create mode 100755 AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSkullDropsAtSpawn.java rename AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/{WitherSpawningAtSpawn.java => WitherSummonAtSpawn.java} (94%) diff --git a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/packets/MapCursorLag.java b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/packets/MapCursorLag.java index 609406f46..196575a3f 100644 --- a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/packets/MapCursorLag.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/packets/MapCursorLag.java @@ -27,7 +27,7 @@ public MapCursorLag() { @Override public void enable() { - if (EntityUtil.canDisableMapPositionCursor()) + if (EntityUtil.MAP_SET_TRACKING_POS_AVAILABLE) plugin.getServer().getPluginManager().registerEvents(this, plugin); PacketEvents.getAPI().getEventManager().registerListener(asAbstract); } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/DisableWitherSkulls.java b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/DisableWitherSkulls.java deleted file mode 100755 index 890ff9c3d..000000000 --- a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/DisableWitherSkulls.java +++ /dev/null @@ -1,39 +0,0 @@ -package me.xginko.aef.modules.preventions.withers; - -import com.cryptomorin.xseries.XEntityType; -import me.xginko.aef.modules.AEFModule; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.HandlerList; -import org.bukkit.event.Listener; -import org.bukkit.event.entity.ProjectileLaunchEvent; - -public class DisableWitherSkulls extends AEFModule implements Listener { - - public DisableWitherSkulls() { - super("preventions.withers.disable-withers-from-shooting-skulls"); - config.addComment(configPath, "Prevents wither skulls from being shot."); - } - - @Override - public void enable() { - plugin.getServer().getPluginManager().registerEvents(this, plugin); - } - - @Override - public boolean shouldEnable() { - return config.getBoolean(configPath, false); - } - - @Override - public void disable() { - HandlerList.unregisterAll(this); - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onProjectileLaunch(ProjectileLaunchEvent event) { - if (event.getEntityType() == XEntityType.WITHER_SKULL.get()) { - event.setCancelled(true); - } - } -} diff --git a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/RateLimitWitherSkulls.java b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/RateLimitWitherSkulls.java index 014448697..51fe25dc9 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/RateLimitWitherSkulls.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/RateLimitWitherSkulls.java @@ -18,7 +18,7 @@ public class RateLimitWitherSkulls extends AEFModule implements Listener { private final ExpiringSet targetingPlayers, targetingOther, notTargeting; public RateLimitWitherSkulls() { - super("preventions.withers.rate-limit-wither-skulls"); + super("preventions.withers.rate-limit-shooting-skulls"); config.addComment(configPath + ".enable", """ This can help combat lag caused by a ton of wither skulls\s spawning but weakens withers."""); diff --git a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveAllSkullsPeriodically.java b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveAllSkullsPeriodically.java deleted file mode 100755 index 8be1e0fdc..000000000 --- a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveAllSkullsPeriodically.java +++ /dev/null @@ -1,55 +0,0 @@ -package me.xginko.aef.modules.preventions.withers; - -import io.papermc.paper.threadedregions.scheduler.ScheduledTask; -import me.xginko.aef.modules.AEFModule; -import org.bukkit.Chunk; -import org.bukkit.World; -import org.bukkit.entity.Entity; -import org.bukkit.entity.EntityType; - -import java.util.function.Consumer; - -public class RemoveAllSkullsPeriodically extends AEFModule implements Consumer { - - private ScheduledTask scheduledTask; - private final long checkPeriod; - - public RemoveAllSkullsPeriodically() { - super("preventions.withers.remove-flying-wither-skulls.periodically-remove-all-flying-skulls"); - config.addComment(configPath + ".enable", "Enable if a lot of wither skulls at spawn are causing lag."); - checkPeriod = config.getInt(configPath + ".check-period-in-ticks", 80); - } - - @Override - public void enable() { - this.scheduledTask = plugin.getServer().getGlobalRegionScheduler() - .runAtFixedRate(plugin, this, checkPeriod, checkPeriod); - } - - @Override - public boolean shouldEnable() { - return config.getBoolean(configPath + ".enable", false); - } - - @Override - public void disable() { - if (scheduledTask != null) scheduledTask.cancel(); - } - - @Override - public void accept(ScheduledTask task) { - for (World world : plugin.getServer().getWorlds()) { - for (Chunk chunk : world.getLoadedChunks()) { - plugin.getServer().getRegionScheduler().execute(plugin, world, chunk.getX(), chunk.getZ(), () -> { - for (Entity entity : chunk.getEntities()) { - entity.getScheduler().execute(plugin, () -> { - if (entity.getType().equals(EntityType.WITHER_SKULL)) { - entity.remove(); - } - }, null, 1L); - } - }); - } - } - } -} diff --git a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveSkullsOnChunkload.java b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveSkullsOnChunkload.java index 63ccf5640..9c206304c 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveSkullsOnChunkload.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveSkullsOnChunkload.java @@ -7,12 +7,13 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; +import org.bukkit.event.entity.ProjectileLaunchEvent; import org.bukkit.event.world.ChunkLoadEvent; public class RemoveSkullsOnChunkload extends AEFModule implements Listener { public RemoveSkullsOnChunkload() { - super("preventions.withers.remove-flying-wither-skulls.on-chunk-load"); + super("preventions.withers.remove-skulls-on-chunk-load"); config.addComment(configPath, """ Removes wither skulls when the chunk gets loaded.\s Use if you have a ton of them at spawn and they are causing lag."""); @@ -33,6 +34,13 @@ public void disable() { HandlerList.unregisterAll(this); } + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + private void onProjectileLaunch(ProjectileLaunchEvent event) { + if (event.getEntityType() == XEntityType.WITHER_SKULL.get()) { + event.getEntity().setPersistent(false); // Don't save skull when chunk unloads + } + } + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) private void onChunkLoad(ChunkLoadEvent event) { if (event.isNewChunk()) return; diff --git a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveSkullsOnChunkunload.java b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveSkullsOnChunkunload.java deleted file mode 100755 index 65ba074c7..000000000 --- a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveSkullsOnChunkunload.java +++ /dev/null @@ -1,46 +0,0 @@ -package me.xginko.aef.modules.preventions.withers; - -import com.cryptomorin.xseries.XEntityType; -import me.xginko.aef.modules.AEFModule; -import org.bukkit.entity.Entity; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.HandlerList; -import org.bukkit.event.Listener; -import org.bukkit.event.world.ChunkUnloadEvent; - -public class RemoveSkullsOnChunkunload extends AEFModule implements Listener { - - public RemoveSkullsOnChunkunload() { - super("preventions.withers.remove-flying-wither-skulls.on-chunk-unload"); - config.addComment(configPath, """ - Removes wither skulls when the chunk gets unloaded.\s - Use if you have a ton of them at spawn and they are causing lag."""); - } - - @Override - public void enable() { - plugin.getServer().getPluginManager().registerEvents(this, plugin); - } - - @Override - public boolean shouldEnable() { - return config.getBoolean(configPath, false); - } - - @Override - public void disable() { - HandlerList.unregisterAll(this); - } - - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - private void onChunkUnload(ChunkUnloadEvent event) { - for (Entity entity : event.getChunk().getEntities()) { - entity.getScheduler().execute(plugin, () -> { - if (entity.getType() == XEntityType.WITHER_SKULL.get()) { - entity.remove(); - } - }, null, 1L); - } - } -} diff --git a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSkullDropsAtSpawn.java b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSkullDropsAtSpawn.java new file mode 100755 index 000000000..f6f0d6650 --- /dev/null +++ b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSkullDropsAtSpawn.java @@ -0,0 +1,66 @@ +package me.xginko.aef.modules.preventions.withers; + +import io.github.thatsmusic99.configurationmaster.api.ConfigSection; +import me.xginko.aef.modules.AEFModule; +import me.xginko.aef.utils.LocationUtil; +import org.bukkit.entity.EntityType; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.HandlerList; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityExplodeEvent; +import org.bukkit.util.NumberConversions; + +import java.util.HashMap; +import java.util.Map; + +public class WitherSkullDropsAtSpawn extends AEFModule implements Listener { + + private final Map worldsAndTheirRadiuses = new HashMap<>(); + + public WitherSkullDropsAtSpawn() { + super("preventions.withers.disable-item-drops-at-spawn"); + config.addComment(configPath + ".enable", + "Prevents wither skulls from dropping items when they hit a block\n" + + "within a certain radius from 00. Can help with lag."); + Map defaults = new HashMap<>(); + defaults.put("world", 5000); + defaults.put("world_nether", 5000); + defaults.put("world_the_end", 5000); + ConfigSection section = config.getConfigSection(configPath + ".worlds", defaults); + for (String world : section.getKeys(false)) { + try { + double radiusSquared = NumberConversions.square(Integer.parseInt(section.getString(world))); + this.worldsAndTheirRadiuses.put(world, radiusSquared); + } catch (NumberFormatException e) { + warn("Radius for world '" + world + "' is not a valid integer."); + } + } + } + + @Override + public void enable() { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @Override + public boolean shouldEnable() { + return config.getBoolean(configPath + ".enable", false); + } + + @Override + public void disable() { + HandlerList.unregisterAll(this); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + private void onEntityExplode(EntityExplodeEvent event) { + if (event.getEntityType() != EntityType.WITHER_SKULL) return; + if (!worldsAndTheirRadiuses.containsKey(event.getLocation().getWorld().getName())) return; + + if (LocationUtil.getSquaredDistance2DTo00(event.getLocation()) + <= worldsAndTheirRadiuses.get(event.getLocation().getWorld().getName())) { + event.setYield(0); + } + } +} diff --git a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSpawningAtSpawn.java b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSummonAtSpawn.java similarity index 94% rename from AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSpawningAtSpawn.java rename to AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSummonAtSpawn.java index a0811b950..b2f0b52e9 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSpawningAtSpawn.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSummonAtSpawn.java @@ -18,13 +18,13 @@ import java.util.HashMap; import java.util.Map; -public class WitherSpawningAtSpawn extends AEFModule implements Listener { +public class WitherSummonAtSpawn extends AEFModule implements Listener { private final Map worldsAndTheirRadiuses = new HashMap<>(); private final boolean playersShouldBeInformed; - public WitherSpawningAtSpawn() { - super("preventions.withers.disable-wither-spawning-at-spawn"); + public WitherSummonAtSpawn() { + super("preventions.withers.disable-summon-at-spawn"); config.addComment(configPath + ".enable", """ Disables spawning withers near a configurable radius around\s spawn. Helps if players are generating endless amounts of withers\s diff --git a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/utils/LocationUtil.java b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/utils/LocationUtil.java index d3b3a6baf..221f9939e 100644 --- a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/utils/LocationUtil.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/utils/LocationUtil.java @@ -4,6 +4,7 @@ import org.apache.commons.math3.util.FastMath; import org.bukkit.Location; import org.bukkit.World; +import org.bukkit.util.NumberConversions; public class LocationUtil { @@ -16,10 +17,37 @@ public static boolean isNetherCeiling(Location location) { && location.y() > AnarchyExploitFixes.config().nether_ceiling_max_y; } + public static double getSquaredDistance2DTo00(Location location) { + return NumberConversions.square(location.getX()) + NumberConversions.square(location.getZ()); + } + public static double getDistance2DTo00(Location location) { return FastMath.hypot(location.getX(), location.getZ()); } + public static double getRelSquaredDistance2D(Location from, Location to) { + double toX = to.getX(); + double toZ = to.getZ(); + double fromX = from.getX(); + double fromZ = from.getZ(); + + final World.Environment toEnv = to.getWorld().getEnvironment(); + final World.Environment fromEnv = from.getWorld().getEnvironment(); + if (toEnv != fromEnv) { + if (fromEnv == World.Environment.NETHER) { + fromX *= 8; + fromZ *= 8; + } + if (toEnv == World.Environment.NETHER) { + toX *= 8; + toZ *= 8; + } + } + + return NumberConversions.square(toX - fromX) + + NumberConversions.square(toZ - fromZ); + } + public static double getRelDistance2D(Location from, Location to) { double toX = to.x(); double toZ = to.z(); @@ -42,6 +70,30 @@ public static double getRelDistance2D(Location from, Location to) { return FastMath.hypot(toX - fromX, toZ - fromZ); } + public static double getRelSquaredDistance3D(Location from, Location to) { + double toX = to.getX(); + double toZ = to.getZ(); + double fromX = from.getX(); + double fromZ = from.getZ(); + + final World.Environment toEnv = to.getWorld().getEnvironment(); + final World.Environment fromEnv = from.getWorld().getEnvironment(); + if (toEnv != fromEnv) { + if (fromEnv == World.Environment.NETHER) { + fromX *= 8; + fromZ *= 8; + } + if (toEnv == World.Environment.NETHER) { + toX *= 8; + toZ *= 8; + } + } + + return NumberConversions.square(toX - fromX) + + NumberConversions.square(from.getY() - to.getY()) + + NumberConversions.square(toZ - fromZ); + } + public static double getRelDistance3D(Location from, Location to) { double toX = to.x(); double toZ = to.z(); diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/DisableWitherSkulls.java b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/DisableWitherSkulls.java deleted file mode 100755 index 890ff9c3d..000000000 --- a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/DisableWitherSkulls.java +++ /dev/null @@ -1,39 +0,0 @@ -package me.xginko.aef.modules.preventions.withers; - -import com.cryptomorin.xseries.XEntityType; -import me.xginko.aef.modules.AEFModule; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.HandlerList; -import org.bukkit.event.Listener; -import org.bukkit.event.entity.ProjectileLaunchEvent; - -public class DisableWitherSkulls extends AEFModule implements Listener { - - public DisableWitherSkulls() { - super("preventions.withers.disable-withers-from-shooting-skulls"); - config.addComment(configPath, "Prevents wither skulls from being shot."); - } - - @Override - public void enable() { - plugin.getServer().getPluginManager().registerEvents(this, plugin); - } - - @Override - public boolean shouldEnable() { - return config.getBoolean(configPath, false); - } - - @Override - public void disable() { - HandlerList.unregisterAll(this); - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onProjectileLaunch(ProjectileLaunchEvent event) { - if (event.getEntityType() == XEntityType.WITHER_SKULL.get()) { - event.setCancelled(true); - } - } -} diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/RateLimitWitherSkulls.java b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/RateLimitWitherSkulls.java index 81d75d4a1..fc87f3797 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/RateLimitWitherSkulls.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/RateLimitWitherSkulls.java @@ -18,7 +18,7 @@ public class RateLimitWitherSkulls extends AEFModule implements Listener { private final ExpiringSet targetingPlayers, targetingOther, notTargeting; public RateLimitWitherSkulls() { - super("preventions.withers.rate-limit-wither-skulls"); + super("preventions.withers.rate-limit-shooting-skulls"); config.addComment(configPath + ".enable", "This can help combat lag caused by a ton of wither skulls\n" + "spawning but weakens withers."); diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveAllSkullsPeriodically.java b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveAllSkullsPeriodically.java deleted file mode 100755 index 341a7fe5e..000000000 --- a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveAllSkullsPeriodically.java +++ /dev/null @@ -1,42 +0,0 @@ -package me.xginko.aef.modules.preventions.withers; - -import me.xginko.aef.modules.AEFModule; -import org.bukkit.World; -import org.bukkit.entity.WitherSkull; -import org.bukkit.scheduler.BukkitTask; - -public class RemoveAllSkullsPeriodically extends AEFModule implements Runnable { - - private final long checkPeriod; - private BukkitTask bukkitTask; - - public RemoveAllSkullsPeriodically() { - super("preventions.withers.remove-flying-wither-skulls.periodically-remove-all-flying-skulls"); - config.addComment(configPath + ".enable", "Enable if a lot of wither skulls at spawn are causing lag."); - this.checkPeriod = config.getInt(configPath + ".check-period-in-ticks", 80); - } - - @Override - public void enable() { - bukkitTask = plugin.getServer().getScheduler().runTaskTimer(plugin, this, checkPeriod, checkPeriod); - } - - @Override - public boolean shouldEnable() { - return config.getBoolean(configPath + ".enable", false); - } - - @Override - public void disable() { - if (bukkitTask != null) bukkitTask.cancel(); - } - - @Override - public void run() { - for (World world : plugin.getServer().getWorlds()) { - for (WitherSkull entity : world.getEntitiesByClass(WitherSkull.class)) { - entity.remove(); - } - } - } -} diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveSkullsOnChunkload.java b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveSkullsOnChunkload.java index f107a2f94..4a359eae9 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveSkullsOnChunkload.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveSkullsOnChunkload.java @@ -2,11 +2,13 @@ import com.cryptomorin.xseries.XEntityType; import me.xginko.aef.modules.AEFModule; +import me.xginko.aef.utils.EntityUtil; import org.bukkit.entity.Entity; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; +import org.bukkit.event.entity.ProjectileLaunchEvent; import org.bukkit.event.world.ChunkLoadEvent; public class RemoveSkullsOnChunkload extends AEFModule implements Listener { @@ -33,6 +35,13 @@ public void disable() { HandlerList.unregisterAll(this); } + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + private void onProjectileLaunch(ProjectileLaunchEvent event) { + if (event.getEntityType() == XEntityType.WITHER_SKULL.get()) { + EntityUtil.setPersistent(event.getEntity(), false); // Don't save skull when chunk unloads + } + } + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) private void onChunkLoad(ChunkLoadEvent event) { if (event.isNewChunk()) return; diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveSkullsOnChunkunload.java b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveSkullsOnChunkunload.java deleted file mode 100755 index 543ffee91..000000000 --- a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/RemoveSkullsOnChunkunload.java +++ /dev/null @@ -1,44 +0,0 @@ -package me.xginko.aef.modules.preventions.withers; - -import com.cryptomorin.xseries.XEntityType; -import me.xginko.aef.modules.AEFModule; -import org.bukkit.entity.Entity; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.HandlerList; -import org.bukkit.event.Listener; -import org.bukkit.event.world.ChunkUnloadEvent; - -public class RemoveSkullsOnChunkunload extends AEFModule implements Listener { - - public RemoveSkullsOnChunkunload() { - super("preventions.withers.remove-flying-wither-skulls.on-chunk-unload"); - config.addComment(configPath, - "Removes wither skulls when the chunk gets unloaded.\n" + - "Use if you have a ton of them at spawn and they are causing lag."); - } - - @Override - public void enable() { - plugin.getServer().getPluginManager().registerEvents(this, plugin); - } - - @Override - public boolean shouldEnable() { - return config.getBoolean(configPath, true); - } - - @Override - public void disable() { - HandlerList.unregisterAll(this); - } - - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - private void onChunkUnload(ChunkUnloadEvent event) { - for (Entity entity : event.getChunk().getEntities()) { - if (entity.getType() == XEntityType.WITHER_SKULL.get()) { - entity.remove(); - } - } - } -} diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSkullDropsAtSpawn.java b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSkullDropsAtSpawn.java new file mode 100755 index 000000000..f6f0d6650 --- /dev/null +++ b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSkullDropsAtSpawn.java @@ -0,0 +1,66 @@ +package me.xginko.aef.modules.preventions.withers; + +import io.github.thatsmusic99.configurationmaster.api.ConfigSection; +import me.xginko.aef.modules.AEFModule; +import me.xginko.aef.utils.LocationUtil; +import org.bukkit.entity.EntityType; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.HandlerList; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityExplodeEvent; +import org.bukkit.util.NumberConversions; + +import java.util.HashMap; +import java.util.Map; + +public class WitherSkullDropsAtSpawn extends AEFModule implements Listener { + + private final Map worldsAndTheirRadiuses = new HashMap<>(); + + public WitherSkullDropsAtSpawn() { + super("preventions.withers.disable-item-drops-at-spawn"); + config.addComment(configPath + ".enable", + "Prevents wither skulls from dropping items when they hit a block\n" + + "within a certain radius from 00. Can help with lag."); + Map defaults = new HashMap<>(); + defaults.put("world", 5000); + defaults.put("world_nether", 5000); + defaults.put("world_the_end", 5000); + ConfigSection section = config.getConfigSection(configPath + ".worlds", defaults); + for (String world : section.getKeys(false)) { + try { + double radiusSquared = NumberConversions.square(Integer.parseInt(section.getString(world))); + this.worldsAndTheirRadiuses.put(world, radiusSquared); + } catch (NumberFormatException e) { + warn("Radius for world '" + world + "' is not a valid integer."); + } + } + } + + @Override + public void enable() { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @Override + public boolean shouldEnable() { + return config.getBoolean(configPath + ".enable", false); + } + + @Override + public void disable() { + HandlerList.unregisterAll(this); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + private void onEntityExplode(EntityExplodeEvent event) { + if (event.getEntityType() != EntityType.WITHER_SKULL) return; + if (!worldsAndTheirRadiuses.containsKey(event.getLocation().getWorld().getName())) return; + + if (LocationUtil.getSquaredDistance2DTo00(event.getLocation()) + <= worldsAndTheirRadiuses.get(event.getLocation().getWorld().getName())) { + event.setYield(0); + } + } +} diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSpawningAtSpawn.java b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSummonAtSpawn.java similarity index 94% rename from AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSpawningAtSpawn.java rename to AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSummonAtSpawn.java index 53dd3c3e9..46648df8e 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSpawningAtSpawn.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSummonAtSpawn.java @@ -17,13 +17,13 @@ import java.util.HashMap; import java.util.Map; -public class WitherSpawningAtSpawn extends AEFModule implements Listener { +public class WitherSummonAtSpawn extends AEFModule implements Listener { private final Map worldsAndTheirRadiuses = new HashMap<>(); private final boolean playersShouldBeInformed; - public WitherSpawningAtSpawn() { - super("preventions.withers.disable-wither-spawning-at-spawn"); + public WitherSummonAtSpawn() { + super("preventions.withers.disable-summon-at-spawn"); config.addComment(configPath + ".enable", "Disables spawning withers near a configurable radius around\n" + "spawn. Helps if players are generating endless amounts of withers\n" + diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/utils/LocationUtil.java b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/utils/LocationUtil.java index 2182aaab2..b8d541274 100644 --- a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/utils/LocationUtil.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/utils/LocationUtil.java @@ -4,6 +4,7 @@ import org.apache.commons.math3.util.FastMath; import org.bukkit.Location; import org.bukkit.World; +import org.bukkit.util.NumberConversions; public class LocationUtil { @@ -16,10 +17,37 @@ public static boolean isNetherCeiling(Location location) { && location.getY() > AnarchyExploitFixes.config().nether_ceiling_max_y; } + public static double getSquaredDistance2DTo00(Location location) { + return NumberConversions.square(location.getX()) + NumberConversions.square(location.getZ()); + } + public static double getDistance2DTo00(Location location) { return FastMath.hypot(location.getX(), location.getZ()); } + public static double getRelSquaredDistance2D(Location from, Location to) { + double toX = to.getX(); + double toZ = to.getZ(); + double fromX = from.getX(); + double fromZ = from.getZ(); + + final World.Environment toEnv = to.getWorld().getEnvironment(); + final World.Environment fromEnv = from.getWorld().getEnvironment(); + if (toEnv != fromEnv) { + if (fromEnv == World.Environment.NETHER) { + fromX *= 8; + fromZ *= 8; + } + if (toEnv == World.Environment.NETHER) { + toX *= 8; + toZ *= 8; + } + } + + return NumberConversions.square(toX - fromX) + + NumberConversions.square(toZ - fromZ); + } + public static double getRelDistance2D(Location from, Location to) { double toX = to.getX(); double toZ = to.getZ(); @@ -42,6 +70,30 @@ public static double getRelDistance2D(Location from, Location to) { return FastMath.hypot(toX - fromX, toZ - fromZ); } + public static double getRelSquaredDistance3D(Location from, Location to) { + double toX = to.getX(); + double toZ = to.getZ(); + double fromX = from.getX(); + double fromZ = from.getZ(); + + final World.Environment toEnv = to.getWorld().getEnvironment(); + final World.Environment fromEnv = from.getWorld().getEnvironment(); + if (toEnv != fromEnv) { + if (fromEnv == World.Environment.NETHER) { + fromX *= 8; + fromZ *= 8; + } + if (toEnv == World.Environment.NETHER) { + toX *= 8; + toZ *= 8; + } + } + + return NumberConversions.square(toX - fromX) + + NumberConversions.square(from.getY() - to.getY()) + + NumberConversions.square(toZ - fromZ); + } + public static double getRelDistance3D(Location from, Location to) { double toX = to.getX(); double toZ = to.getZ(); diff --git a/shared/src/main/java/me/xginko/aef/utils/EntityUtil.java b/shared/src/main/java/me/xginko/aef/utils/EntityUtil.java index 687448f5a..ed703aa0b 100644 --- a/shared/src/main/java/me/xginko/aef/utils/EntityUtil.java +++ b/shared/src/main/java/me/xginko/aef/utils/EntityUtil.java @@ -24,8 +24,7 @@ public class EntityUtil { - private static final boolean MAP_SET_TRACKING_POS_AVAILABLE; - private static final boolean IS_SWIMMING_AVAILABLE; + public static final boolean MAP_SET_TRACKING_POS_AVAILABLE, IS_SWIMMING_AVAILABLE, IS_SET_PERSISTENT_AVAILABLE; static { MAP_SET_TRACKING_POS_AVAILABLE @@ -33,19 +32,24 @@ public class EntityUtil { && Crafty.hasMethod(MapView.class, "setTrackingPosition", boolean.class); IS_SWIMMING_AVAILABLE = Crafty.hasMethod(LivingEntity.class, "isSwimming", boolean.class); + IS_SET_PERSISTENT_AVAILABLE + = Crafty.hasMethod(Entity.class, "setPersistent", boolean.class); } - public static boolean canDisableMapPositionCursor() { - return MAP_SET_TRACKING_POS_AVAILABLE; + public static void setPersistent(Entity entity, boolean persistent) { + if (IS_SET_PERSISTENT_AVAILABLE) { + entity.setPersistent(persistent); + } } /** * Disables that a position cursor will be shown when the map is near its center. - * Check {@link EntityUtil#canDisableMapPositionCursor()} before calling this method. * * @param itemFrame the {@link ItemFrame} entity to disable the tracking status of */ public static void disableMapPositionCursor(@NotNull ItemFrame itemFrame) { + if (!MAP_SET_TRACKING_POS_AVAILABLE) return; + ItemStack itemInsideFrame = itemFrame.getItem(); if (itemInsideFrame == null) return; // Shouldn't be null but just in case if (itemInsideFrame.getType() != XMaterial.MAP.parseMaterial()