Skip to content

Commit

Permalink
shorten code
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Aug 17, 2024
1 parent 552a7c0 commit 76d58ab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Integer> worldsAndTheirRadiuses = new HashMap<>();
private final Map<String, Double> worldsAndTheirRadiuses = new HashMap<>();
private final boolean playersShouldBeInformed;

public WitherSummonAtSpawn() {
Expand All @@ -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.");
}
Expand All @@ -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()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Integer> worldsAndTheirRadiuses = new HashMap<>();
private final Map<String, Double> worldsAndTheirRadiuses = new HashMap<>();
private final boolean playersShouldBeInformed;

public WitherSummonAtSpawn() {
Expand All @@ -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.");
}
Expand All @@ -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()));
}
}
}
Expand Down

0 comments on commit 76d58ab

Please sign in to comment.