diff --git a/README.md b/README.md index 28d67b3..f1094ed 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,9 @@ This project was inspired by the work on [ArmorStand-Limiter](https://github.com # Support -[![support image](https://www.heroxwar.com/discordLogo.png)](https://discord.com/invite/3QGe3ts) +[![support image](https://www.heroxwar.com/discordLogo.png)](https://discord.gg/5qQbJq45QY) -**[https://discord.com/invite/3QGe3ts](https://discord.com/invite/3QGe3ts)** +**[https://discord.com/invite/3QGe3ts](https://discord.gg/5qQbJq45QY)** # Download diff --git a/src/main/java/com/expectale/entitylimiter/Checker.java b/src/main/java/com/expectale/entitylimiter/Checker.java index b9e0cf2..092a1e2 100644 --- a/src/main/java/com/expectale/entitylimiter/Checker.java +++ b/src/main/java/com/expectale/entitylimiter/Checker.java @@ -11,7 +11,9 @@ import java.awt.Color; import java.io.IOException; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; public class Checker { @@ -100,21 +102,43 @@ public static void removeEntities(EntityType type) { } } - public static void sendInGameCheck(Player player, Chunk chunk, EntityType type) { - final EntityLimiterConfiguration configuration = EntityLimiter.getINSTANCE().getConfiguration(); + public static List getChunksAround(Chunk chunk, int radius) { + int chunkX = chunk.getX(); + int chunkZ = chunk.getZ(); + int minX = chunkX - radius; + int minZ = chunkZ - radius; + int maxX = chunkX + radius; + int maxZ = chunkZ + radius; + + List chunks = new ArrayList<>(); + for (int x = minX; x <= maxX; x++) { + for (int z = minZ; z <= maxZ; z++) { + chunks.add(chunk.getWorld().getChunkAt(x, z)); + } + } + + return chunks; + } + + public static void sendInGameCheck(Player player, List chunks, EntityType type) { StringBuilder players = new StringBuilder(); - for (Entity entity : chunk.getWorld().getNearbyLivingEntities(new Location(chunk.getWorld(), chunk.getX() * 16, 150, chunk.getZ() * 16), 150)) { + Chunk centerChunk = chunks.get(chunks.size() / 2); + for (Entity entity : centerChunk.getWorld().getNearbyLivingEntities(new Location(centerChunk.getWorld(), centerChunk.getX() * 16, 150, centerChunk.getZ() * 16), 150)) { if (entity instanceof Player) { players.append(" ").append(entity.getName()); } } - player.sendMessage(ChatColor.YELLOW + "Check Entity Limiter :"); - player.sendMessage(ChatColor.GRAY + "- World " + chunk.getWorld().getName()); - player.sendMessage(ChatColor.GRAY + "- X " + chunk.getX() * 16); - player.sendMessage(ChatColor.GRAY + "- Z " + chunk.getZ() * 16); - player.sendMessage(ChatColor.GRAY + "- Nearby players (150 blocks) " + ((players.length() == 0) ? "NONE" : players.toString())); - player.sendMessage(ChatColor.GRAY + "- Counter " + countEntityInChunk(chunk, type) + "/" + configuration.getChunkLimit()); + int counter = 0; + for (Chunk chunk : chunks) { + counter += countEntityInChunk(chunk, type); + } + player.sendMessage(ChatColor.YELLOW + "Check Entity Limiter (" + type.toString() + ") :"); + player.sendMessage(ChatColor.GRAY + "- World " + centerChunk.getWorld().getName()); + player.sendMessage(ChatColor.GRAY + "- X " + centerChunk.getX() * 16); + player.sendMessage(ChatColor.GRAY + "- Z " + centerChunk.getZ() * 16); + player.sendMessage(ChatColor.GRAY + "- Nearby players (150 blocks) " + ((players.length() == 0) ? (ChatColor.RED + "NONE") : players.toString())); + player.sendMessage(ChatColor.GRAY + "- Counter " + counter); } public static void sendInGameAlert(Player player, Chunk chunk, EntityType type) { @@ -145,8 +169,8 @@ public static void sendDiscordAlert(Chunk chunk, EntityType type) { } DiscordWebhook webhook = new DiscordWebhook(configuration.getDiscordWebhook()); - webhook.setAvatarUrl("https://upload.wikimedia.org/wikipedia/commons/thumb/3/3b/OOjs_UI_icon_alert-warning.svg/1024px-OOjs_UI_icon_alert-warning.svg.png"); - webhook.setUsername("Alert !"); + webhook.setAvatarUrl("https://labocraft.fr/storage/img/logo64x64.png"); + webhook.setUsername("Entity-Limiter"); webhook.setTts(false); webhook.addEmbed(new DiscordWebhook.EmbedObject() .setTitle("A significant number of entites ("+ type.toString() +") have been removed") @@ -157,7 +181,7 @@ public static void sendDiscordAlert(Chunk chunk, EntityType type) { .addField("Z", chunk.getZ() * 16 + "", false) .addField("Nearby players (150 blocks)", (players.length() == 0) ? "NONE" : players.toString(), false) .addField("Counter", countEntityInChunk(chunk, type) + "/" + configuration.getChunkLimit(), false) - .setAuthor("Entity-Limiter", "https://labocraft.fr", "https://labocraft.fr/storage/img/logo64x64.png") + .setAuthor("Entity-Limiter Alert", "https://labocraft.fr", "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3b/OOjs_UI_icon_alert-warning.svg/1024px-OOjs_UI_icon_alert-warning.svg.png") ); try { webhook.execute(); diff --git a/src/main/java/com/expectale/entitylimiter/EntityLimiter.java b/src/main/java/com/expectale/entitylimiter/EntityLimiter.java index 7bb8b45..ffd52db 100644 --- a/src/main/java/com/expectale/entitylimiter/EntityLimiter.java +++ b/src/main/java/com/expectale/entitylimiter/EntityLimiter.java @@ -33,7 +33,7 @@ public void reloadConfig() { public void onEnable() { INSTANCE = this; reloadConfig(); - getCommand("ml").setExecutor(new EntityLimiterCommand()); + getCommand("entity-limiter").setExecutor(new EntityLimiterCommand()); getServer().getPluginManager().registerEvents(new EntityListener(), this); getLogger().info("EntityLimiter is enabled"); } diff --git a/src/main/java/com/expectale/entitylimiter/command/EntityLimiterCommand.java b/src/main/java/com/expectale/entitylimiter/command/EntityLimiterCommand.java index 7cf0c5c..237e8ee 100644 --- a/src/main/java/com/expectale/entitylimiter/command/EntityLimiterCommand.java +++ b/src/main/java/com/expectale/entitylimiter/command/EntityLimiterCommand.java @@ -2,14 +2,25 @@ import com.expectale.entitylimiter.Checker; import com.expectale.entitylimiter.EntityLimiter; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.Chunk; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; +import java.util.List; + public class EntityLimiterCommand implements CommandExecutor { + + private void sendHelpMessage(CommandSender sender) { + sender.sendMessage(ChatColor.YELLOW + "Usage Entity Limiter : "); + sender.sendMessage(ChatColor.GRAY + "- /ml reload "); + sender.sendMessage(ChatColor.GRAY + "- /ml check [] []"); + } + @Override public boolean onCommand(CommandSender sender, Command cmd, String msg, String[] args) { @@ -17,15 +28,34 @@ public boolean onCommand(CommandSender sender, Command cmd, String msg, String[] if (args.length == 1 && args[0].equalsIgnoreCase("reload")) { EntityLimiter.getINSTANCE().reloadConfig(); sender.sendMessage(ChatColor.YELLOW + "The configuration file has been successfully reloaded"); - } else if (args.length == 1 && args[0].equalsIgnoreCase("check")) { - sender.sendMessage(ChatColor.YELLOW + "SOON"); + } else if (args.length >= 1 && args[0].equalsIgnoreCase("check")) { + Chunk chunk = ((Player)sender).getChunk(); + int radius = 1; + if (args.length >= 2) { + Player player = Bukkit.getPlayer(args[1]); + if (player != null) { + chunk = player.getChunk(); + } else { + sender.sendMessage(ChatColor.RED + "Can't find player " + args[1]); + sendHelpMessage(sender); + return true; + } + } + if (args.length >= 3) { + try { + radius = Integer.parseInt(args[2]); + } catch (Exception exception) { + sender.sendMessage(ChatColor.RED + "Invalid radius " + args[2]); + sendHelpMessage(sender); + return true; + } + } + List chunks = Checker.getChunksAround(chunk, radius); for (EntityType type : EntityLimiter.getINSTANCE().getConfiguration().getEntityType()) { - Checker.sendInGameCheck((Player)sender, ((Player)sender).getChunk(), type); + Checker.sendInGameCheck((Player)sender, chunks, type); } } else { - sender.sendMessage(ChatColor.YELLOW + "Usage Entity Limiter : "); - sender.sendMessage(ChatColor.GRAY + "- /ml reload "); - sender.sendMessage(ChatColor.GRAY + "- /ml check"); + sendHelpMessage(sender); } } return true;