Skip to content

Commit

Permalink
fix: adding Async task for DiscordWebhook
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan committed Jun 8, 2024
1 parent 129f681 commit 2d40da5
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 34 deletions.
45 changes: 25 additions & 20 deletions src/main/java/com/expectale/entitylimiter/Checker.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,30 +19,30 @@
public class Checker {

private static BukkitRunnable task = null;
private static final HashSet<Chunk> chunksCheched = new HashSet<>();
private static final HashSet<Chunk> 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() {
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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()) {
Expand Down Expand Up @@ -142,7 +142,7 @@ public static void sendInGameCheck(Player player, List<Chunk> 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)) {
Expand All @@ -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)) {
Expand All @@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public final class EntityLimiter extends JavaPlugin {

private static EntityLimiter INSTANCE;

public static EntityLimiter getINSTANCE() {
public static EntityLimiter getInstance() {
return INSTANCE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -51,7 +51,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String msg, String[]
}
}
List<Chunk> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.expectale.entitylimiter.utilis;
package com.expectale.entitylimiter.utils;

import javax.net.ssl.HttpsURLConnection;
import java.awt.Color;
Expand Down

0 comments on commit 2d40da5

Please # to comment.