From 2d40da58fe92f2dde90f7e7e34d95bbb422d0334 Mon Sep 17 00:00:00 2001 From: Nathan Date: Sat, 8 Jun 2024 02:42:48 +0200 Subject: [PATCH] fix: adding Async task for DiscordWebhook --- .../com/expectale/entitylimiter/Checker.java | 45 ++++++++++--------- .../entitylimiter/EntityLimiter.java | 2 +- .../command/EntityLimiterCommand.java | 4 +- .../EntityLimiterConfiguration.java | 2 +- .../listener/EntityListener.java | 18 ++++---- .../{utilis => utils}/DiscordWebhook.java | 2 +- 6 files changed, 39 insertions(+), 34 deletions(-) rename src/main/java/com/expectale/entitylimiter/{utilis => utils}/DiscordWebhook.java (99%) diff --git a/src/main/java/com/expectale/entitylimiter/Checker.java b/src/main/java/com/expectale/entitylimiter/Checker.java index 092a1e2..1c50104 100644 --- a/src/main/java/com/expectale/entitylimiter/Checker.java +++ b/src/main/java/com/expectale/entitylimiter/Checker.java @@ -1,7 +1,7 @@ package com.expectale.entitylimiter; import com.expectale.entitylimiter.configuration.EntityLimiterConfiguration; -import com.expectale.entitylimiter.utilis.DiscordWebhook; +import com.expectale.entitylimiter.utils.DiscordWebhook; import org.bukkit.*; import org.bukkit.entity.ArmorStand; import org.bukkit.entity.Entity; @@ -19,30 +19,30 @@ public class Checker { private static BukkitRunnable task = null; - private static final HashSet chunksCheched = new HashSet<>(); + private static final HashSet chunksChecked = new HashSet<>(); public static void start() { - final EntityLimiterConfiguration configuration = EntityLimiter.getINSTANCE().getConfiguration(); + final EntityLimiterConfiguration configuration = EntityLimiter.getInstance().getConfiguration(); task = new BukkitRunnable() { long lastChunkCheck = System.currentTimeMillis(); @Override public void run() { if (configuration.isTPSMeter() && Bukkit.getTPS()[0] < configuration.getTPSMeterTrigger()) { - for (EntityType type : EntityLimiter.getINSTANCE().getConfiguration().getEntityType()) { + for (EntityType type : EntityLimiter.getInstance().getConfiguration().getEntityType()) { removeEntities(type); } } if (configuration.isChunkTask() && System.currentTimeMillis() > lastChunkCheck + (long) configuration.getChunkTaskRefresh() * 60 * 1000) { - for (EntityType type : EntityLimiter.getINSTANCE().getConfiguration().getEntityType()) { + for (EntityType type : EntityLimiter.getInstance().getConfiguration().getEntityType()) { removeEntities(type); } lastChunkCheck = System.currentTimeMillis(); } - chunksCheched.clear(); + chunksChecked.clear(); } }; - task.runTaskTimer(EntityLimiter.getINSTANCE(), 20, 20); + task.runTaskTimer(EntityLimiter.getInstance(), 20, 20); } public static void stop() { @@ -52,12 +52,12 @@ public static void stop() { } } - public static void addChechedChunk(Chunk chunk) { - chunksCheched.add(chunk); + public static void addCheckedChunk(Chunk chunk) { + chunksChecked.add(chunk); } - public static boolean isChechedChunk(Chunk chunk) { - return !chunksCheched.contains(chunk); + public static boolean isCheckedChunk(Chunk chunk) { + return !chunksChecked.contains(chunk); } public static int countEntityInChunk(Chunk chunk, EntityType type) { @@ -72,7 +72,7 @@ public static int countEntityInChunk(Chunk chunk, EntityType type) { } public static void removeEntitiesInChunk(Chunk chunk, EntityType type) { - final EntityLimiterConfiguration configuration = EntityLimiter.getINSTANCE().getConfiguration(); + final EntityLimiterConfiguration configuration = EntityLimiter.getInstance().getConfiguration(); if (configuration.isDiscord() && !configuration.getDiscordWebhook().isEmpty()) { sendDiscordAlert(chunk, type); @@ -90,7 +90,7 @@ public static void removeEntitiesInChunk(Chunk chunk, EntityType type) { } public static void removeEntities(EntityType type) { - final EntityLimiterConfiguration configuration = EntityLimiter.getINSTANCE().getConfiguration(); + final EntityLimiterConfiguration configuration = EntityLimiter.getInstance().getConfiguration(); for (World world : Bukkit.getWorlds()) { if (!configuration.getDisableIfNameContains().contains(world.getName()) && !configuration.getDisabledWorlds().contains(world.getName())) { for (Chunk chunk : world.getLoadedChunks()) { @@ -142,7 +142,7 @@ public static void sendInGameCheck(Player player, List chunks, EntityType } public static void sendInGameAlert(Player player, Chunk chunk, EntityType type) { - final EntityLimiterConfiguration configuration = EntityLimiter.getINSTANCE().getConfiguration(); + final EntityLimiterConfiguration configuration = EntityLimiter.getInstance().getConfiguration(); StringBuilder players = new StringBuilder(); for (Entity entity : chunk.getWorld().getNearbyLivingEntities(new Location(chunk.getWorld(), chunk.getX() * 16, 150, chunk.getZ() * 16), 150)) { @@ -159,7 +159,7 @@ public static void sendInGameAlert(Player player, Chunk chunk, EntityType type) } public static void sendDiscordAlert(Chunk chunk, EntityType type) { - final EntityLimiterConfiguration configuration = EntityLimiter.getINSTANCE().getConfiguration(); + final EntityLimiterConfiguration configuration = EntityLimiter.getInstance().getConfiguration(); StringBuilder players = new StringBuilder(); for (Entity entity : chunk.getWorld().getNearbyLivingEntities(new Location(chunk.getWorld(), chunk.getX() * 16, 150, chunk.getZ() * 16), 150)) { @@ -183,11 +183,16 @@ public static void sendDiscordAlert(Chunk chunk, EntityType type) { .addField("Counter", countEntityInChunk(chunk, type) + "/" + configuration.getChunkLimit(), false) .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(); - } catch (IOException e) { - throw new RuntimeException(e); - } + new BukkitRunnable() { + @Override + public void run() { + try { + webhook.execute(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + }.runTaskAsynchronously(EntityLimiter.getInstance()); } } diff --git a/src/main/java/com/expectale/entitylimiter/EntityLimiter.java b/src/main/java/com/expectale/entitylimiter/EntityLimiter.java index ffd52db..8fe022d 100644 --- a/src/main/java/com/expectale/entitylimiter/EntityLimiter.java +++ b/src/main/java/com/expectale/entitylimiter/EntityLimiter.java @@ -12,7 +12,7 @@ public final class EntityLimiter extends JavaPlugin { private static EntityLimiter INSTANCE; - public static EntityLimiter getINSTANCE() { + public static EntityLimiter getInstance() { return INSTANCE; } diff --git a/src/main/java/com/expectale/entitylimiter/command/EntityLimiterCommand.java b/src/main/java/com/expectale/entitylimiter/command/EntityLimiterCommand.java index 237e8ee..c92b2f8 100644 --- a/src/main/java/com/expectale/entitylimiter/command/EntityLimiterCommand.java +++ b/src/main/java/com/expectale/entitylimiter/command/EntityLimiterCommand.java @@ -26,7 +26,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String msg, String[] if (sender instanceof Player) { if (args.length == 1 && args[0].equalsIgnoreCase("reload")) { - EntityLimiter.getINSTANCE().reloadConfig(); + EntityLimiter.getInstance().reloadConfig(); sender.sendMessage(ChatColor.YELLOW + "The configuration file has been successfully reloaded"); } else if (args.length >= 1 && args[0].equalsIgnoreCase("check")) { Chunk chunk = ((Player)sender).getChunk(); @@ -51,7 +51,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String msg, String[] } } List chunks = Checker.getChunksAround(chunk, radius); - for (EntityType type : EntityLimiter.getINSTANCE().getConfiguration().getEntityType()) { + for (EntityType type : EntityLimiter.getInstance().getConfiguration().getEntityType()) { Checker.sendInGameCheck((Player)sender, chunks, type); } } else { diff --git a/src/main/java/com/expectale/entitylimiter/configuration/EntityLimiterConfiguration.java b/src/main/java/com/expectale/entitylimiter/configuration/EntityLimiterConfiguration.java index 4554dfe..32c986f 100644 --- a/src/main/java/com/expectale/entitylimiter/configuration/EntityLimiterConfiguration.java +++ b/src/main/java/com/expectale/entitylimiter/configuration/EntityLimiterConfiguration.java @@ -68,7 +68,7 @@ public void reload(final Configuration configuration) { if (type != null) { entityType.add(type); } else { - EntityLimiter.getINSTANCE().getLogger().warning("Invalid entity type : " + entity); + EntityLimiter.getInstance().getLogger().warning("Invalid entity type : " + entity); } } } diff --git a/src/main/java/com/expectale/entitylimiter/listener/EntityListener.java b/src/main/java/com/expectale/entitylimiter/listener/EntityListener.java index efcff62..95d9851 100644 --- a/src/main/java/com/expectale/entitylimiter/listener/EntityListener.java +++ b/src/main/java/com/expectale/entitylimiter/listener/EntityListener.java @@ -13,9 +13,9 @@ public class EntityListener implements Listener { @EventHandler public void onVehicleEntityCollision(VehicleEntityCollisionEvent event) { - final EntityLimiterConfiguration configuration = EntityLimiter.getINSTANCE().getConfiguration(); - if (configuration.getEntityType().contains(event.getVehicle().getType()) && Checker.isChechedChunk(event.getVehicle().getChunk())) { - Checker.addChechedChunk(event.getVehicle().getChunk()); + final EntityLimiterConfiguration configuration = EntityLimiter.getInstance().getConfiguration(); + if (configuration.getEntityType().contains(event.getVehicle().getType()) && Checker.isCheckedChunk(event.getVehicle().getChunk())) { + Checker.addCheckedChunk(event.getVehicle().getChunk()); if (!configuration.getDisableIfNameContains().contains(event.getVehicle().getWorld().getName()) && !configuration.getDisabledWorlds().contains(event.getVehicle().getWorld().getName())) { if (configuration.getChunkLimit() <= Checker.countEntityInChunk(event.getVehicle().getChunk(), event.getVehicle().getType())) { Checker.removeEntitiesInChunk(event.getVehicle().getChunk(), event.getVehicle().getType()); @@ -26,9 +26,9 @@ public void onVehicleEntityCollision(VehicleEntityCollisionEvent event) { @EventHandler public void onVehicleCreate(VehicleCreateEvent event) { - final EntityLimiterConfiguration configuration = EntityLimiter.getINSTANCE().getConfiguration(); - if (configuration.getEntityType().contains(event.getVehicle().getType()) && Checker.isChechedChunk(event.getVehicle().getChunk())) { - Checker.addChechedChunk(event.getVehicle().getChunk()); + final EntityLimiterConfiguration configuration = EntityLimiter.getInstance().getConfiguration(); + if (configuration.getEntityType().contains(event.getVehicle().getType()) && Checker.isCheckedChunk(event.getVehicle().getChunk())) { + Checker.addCheckedChunk(event.getVehicle().getChunk()); if (!configuration.getDisableIfNameContains().contains(event.getVehicle().getWorld().getName()) && !configuration.getDisabledWorlds().contains(event.getVehicle().getWorld().getName())) { if (configuration.getChunkLimit() <= Checker.countEntityInChunk(event.getVehicle().getChunk(), event.getVehicle().getType())) { Checker.removeEntitiesInChunk(event.getVehicle().getChunk(), event.getVehicle().getType()); @@ -39,11 +39,11 @@ public void onVehicleCreate(VehicleCreateEvent event) { @EventHandler public void onEntitySpawnEvent(EntitySpawnEvent event) { - final EntityLimiterConfiguration configuration = EntityLimiter.getINSTANCE().getConfiguration(); + final EntityLimiterConfiguration configuration = EntityLimiter.getInstance().getConfiguration(); if (configuration.getEntityType().contains(event.getEntity().getType())) { event.getEntity().setGravity(false); - if (Checker.isChechedChunk(event.getEntity().getChunk())) { - Checker.addChechedChunk(event.getEntity().getChunk()); + if (Checker.isCheckedChunk(event.getEntity().getChunk())) { + Checker.addCheckedChunk(event.getEntity().getChunk()); if (!configuration.getDisableIfNameContains().contains(event.getEntity().getWorld().getName()) && !configuration.getDisabledWorlds().contains(event.getEntity().getWorld().getName())) { if (configuration.getChunkLimit() <= Checker.countEntityInChunk(event.getEntity().getChunk(), event.getEntity().getType())) { Checker.removeEntitiesInChunk(event.getEntity().getChunk(), event.getEntity().getType()); diff --git a/src/main/java/com/expectale/entitylimiter/utilis/DiscordWebhook.java b/src/main/java/com/expectale/entitylimiter/utils/DiscordWebhook.java similarity index 99% rename from src/main/java/com/expectale/entitylimiter/utilis/DiscordWebhook.java rename to src/main/java/com/expectale/entitylimiter/utils/DiscordWebhook.java index 7228491..25ce386 100644 --- a/src/main/java/com/expectale/entitylimiter/utilis/DiscordWebhook.java +++ b/src/main/java/com/expectale/entitylimiter/utils/DiscordWebhook.java @@ -1,4 +1,4 @@ -package com.expectale.entitylimiter.utilis; +package com.expectale.entitylimiter.utils; import javax.net.ssl.HttpsURLConnection; import java.awt.Color;