From 76d58abea0383e587ef5f3c57e74507f4c681a72 Mon Sep 17 00:00:00 2001 From: xGinko Date: Sun, 18 Aug 2024 00:52:01 +0200 Subject: [PATCH] shorten code --- .../withers/WitherSummonAtSpawn.java | 24 ++++++++----------- .../withers/WitherSummonAtSpawn.java | 23 +++++++----------- 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSummonAtSpawn.java b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSummonAtSpawn.java index b2f0b52e..4a513c31 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSummonAtSpawn.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSummonAtSpawn.java @@ -6,21 +6,20 @@ import me.xginko.aef.modules.AEFModule; import me.xginko.aef.utils.LocationUtil; import net.kyori.adventure.text.TextReplacementConfig; -import org.bukkit.Location; -import org.bukkit.entity.Entity; import org.bukkit.entity.Player; 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.CreatureSpawnEvent; +import org.bukkit.util.NumberConversions; import java.util.HashMap; import java.util.Map; public class WitherSummonAtSpawn extends AEFModule implements Listener { - private final Map worldsAndTheirRadiuses = new HashMap<>(); + private final Map worldsAndTheirRadiuses = new HashMap<>(); private final boolean playersShouldBeInformed; public WitherSummonAtSpawn() { @@ -37,8 +36,8 @@ public WitherSummonAtSpawn() { ConfigSection section = config.getConfigSection(configPath + ".worlds", defaults); for (String world : section.getKeys(false)) { try { - Integer radius = Integer.parseInt(section.getString(world)); - this.worldsAndTheirRadiuses.put(world, radius); + 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."); } @@ -65,20 +64,17 @@ private void onCreatureSpawn(CreatureSpawnEvent event) { if (event.getEntityType() != XEntityType.WITHER_SKULL.get()) return; if (event.getSpawnReason() != CreatureSpawnEvent.SpawnReason.BUILD_WITHER) return; - final Entity wither = event.getEntity(); - final String world = wither.getWorld().getName(); - if (!worldsAndTheirRadiuses.containsKey(world)) return; - - final Integer disabledRadius = worldsAndTheirRadiuses.get(world); - final Location witherLocation = wither.getLocation(); - if (LocationUtil.getDistance2DTo00(witherLocation) > disabledRadius) return; + String worldName = event.getLocation().getWorld().getName(); + if (!worldsAndTheirRadiuses.containsKey(worldName)) return; + if (LocationUtil.getSquaredDistance2DTo00(event.getLocation()) > worldsAndTheirRadiuses.get(worldName)) return; event.setCancelled(true); if (playersShouldBeInformed) { - for (Player nearbyPlayer : witherLocation.getNearbyPlayers(8)) { + for (Player nearbyPlayer : event.getLocation().getNearbyPlayers(8)) { nearbyPlayer.sendMessage(AnarchyExploitFixes.getLang(nearbyPlayer.locale()).preventions_witherSpawningDisabledInRadius - .replaceText(TextReplacementConfig.builder().matchLiteral("%radius%").replacement(disabledRadius.toString()).build())); + .replaceText(TextReplacementConfig.builder().matchLiteral("%radius%") + .replacement(worldsAndTheirRadiuses.get(worldName).toString()).build())); } } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSummonAtSpawn.java b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSummonAtSpawn.java index 46648df8..eb12ac3f 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSummonAtSpawn.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/preventions/withers/WitherSummonAtSpawn.java @@ -5,21 +5,20 @@ import me.xginko.aef.AnarchyExploitFixes; import me.xginko.aef.modules.AEFModule; import me.xginko.aef.utils.LocationUtil; -import org.bukkit.Location; -import org.bukkit.entity.Entity; import org.bukkit.entity.Player; 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.CreatureSpawnEvent; +import org.bukkit.util.NumberConversions; import java.util.HashMap; import java.util.Map; public class WitherSummonAtSpawn extends AEFModule implements Listener { - private final Map worldsAndTheirRadiuses = new HashMap<>(); + private final Map worldsAndTheirRadiuses = new HashMap<>(); private final boolean playersShouldBeInformed; public WitherSummonAtSpawn() { @@ -36,8 +35,8 @@ public WitherSummonAtSpawn() { ConfigSection section = config.getConfigSection(configPath + ".worlds", defaults); for (String world : section.getKeys(false)) { try { - Integer radius = Integer.parseInt(section.getString(world)); - this.worldsAndTheirRadiuses.put(world, radius); + 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."); } @@ -64,20 +63,16 @@ private void onCreatureSpawn(CreatureSpawnEvent event) { if (event.getEntityType() != XEntityType.WITHER_SKULL.get()) return; if (event.getSpawnReason() != CreatureSpawnEvent.SpawnReason.BUILD_WITHER) return; - final Entity entity = event.getEntity(); - final String world = entity.getWorld().getName(); - if (!worldsAndTheirRadiuses.containsKey(world)) return; - - final Integer disabledRadius = worldsAndTheirRadiuses.get(world); - final Location witherLocation = entity.getLocation(); - if (LocationUtil.getDistance2DTo00(witherLocation) > disabledRadius) return; + String worldName = event.getLocation().getWorld().getName(); + if (!worldsAndTheirRadiuses.containsKey(worldName)) return; + if (LocationUtil.getSquaredDistance2DTo00(event.getLocation()) > worldsAndTheirRadiuses.get(worldName)) return; event.setCancelled(true); if (playersShouldBeInformed) { - for (Player nearbyPlayer : witherLocation.getNearbyPlayers(8)) { + for (Player nearbyPlayer : event.getLocation().getNearbyPlayers(8)) { nearbyPlayer.sendMessage(AnarchyExploitFixes.getLang(nearbyPlayer.getLocale()).withers_SpawningDisabledInRadius - .replace("%radius%", disabledRadius.toString())); + .replace("%radius%", worldsAndTheirRadiuses.get(worldName).toString())); } } }