From 882e58e38e5647ee56b8959b902d77c3c0e7de95 Mon Sep 17 00:00:00 2001 From: LabodiDavid Date: Sun, 6 Apr 2025 14:47:43 +0200 Subject: [PATCH] Implemented Clearing dropped items feature --- src/main/java/hu/ditservices/STPlugin.java | 5 ++ .../handlers/ClearDropItemsTask.java | 76 +++++++++++++++++++ src/main/resources/config.yml | 18 ++++- src/main/resources/en.yml | 5 +- src/main/resources/hu.yml | 5 +- 5 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 src/main/java/hu/ditservices/handlers/ClearDropItemsTask.java diff --git a/src/main/java/hu/ditservices/STPlugin.java b/src/main/java/hu/ditservices/STPlugin.java index d5d6f88..4d7413b 100644 --- a/src/main/java/hu/ditservices/STPlugin.java +++ b/src/main/java/hu/ditservices/STPlugin.java @@ -78,6 +78,11 @@ public final class STPlugin extends JavaPlugin implements CommandExecutor, Liste private void scheduleTasks() { Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new TPS(), 0, 1); + if (this.config.getBoolean("ClearDropItems.enabled")) { + new ClearDropItemsTask(this).runTaskTimer(this, 20L, 20L); + this.log.info(ChatColor.stripColor(this.getPrefix()) + "Clear dropped items enabled!"); + } + if (this.config.getBoolean("Saving.enabled") && this.config.getInt("Saving.interval") > 0) { long intervalTicks = Math.convert(Math.Convert.SECONDS, Math.Convert.TICKS, instance.config.getInt("Saving.interval")); Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new SaveHandler(), 0L, intervalTicks); diff --git a/src/main/java/hu/ditservices/handlers/ClearDropItemsTask.java b/src/main/java/hu/ditservices/handlers/ClearDropItemsTask.java new file mode 100644 index 0000000..3217fd2 --- /dev/null +++ b/src/main/java/hu/ditservices/handlers/ClearDropItemsTask.java @@ -0,0 +1,76 @@ +package hu.ditservices.handlers; + +import hu.ditservices.STPlugin; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.World; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Item; +import org.bukkit.scheduler.BukkitRunnable; + +import java.util.List; + +public class ClearDropItemsTask extends BukkitRunnable { + private final STPlugin plugin; + private int countdown; + private final int interval; // In seconds + private final List exceptItemList; + private final List exceptWorldList; + private final boolean shouldBroadcast; + + public ClearDropItemsTask(STPlugin plugin) { + this.plugin = plugin; + this.interval = plugin.getConfig().getInt("ClearDropItems.interval", 300); + this.countdown = this.interval; + // Get the exception list from the config (should be a list of item type names, e.g., ["DIAMOND", "GOLD_INGOT"]) + this.exceptItemList = plugin.getConfig().getStringList("ClearDropItems.except"); + this.exceptWorldList = plugin.getConfig().getStringList("ClearDropItems.skipWorlds"); + this.shouldBroadcast = plugin.getConfig().getBoolean("ClearDropItems.broadcastMsg", true); + } + + @Override + public void run() { + if (this.shouldBroadcast) { + if (countdown == 60) { + Bukkit.broadcastMessage(plugin.getPrefix() + ChatColor.RED + plugin.getTranslatedText("cleardropitems.oneMin")); + } + + if (countdown == 10) { + Bukkit.broadcastMessage(plugin.getPrefix() + ChatColor.RED + plugin.getTranslatedText("cleardropitems.tenSec")); + } + } + + + if (countdown <= 0) { + int removed = 0; + + for (World world : Bukkit.getWorlds()) { + if (this.exceptWorldList.contains(world.getName())) { + continue; + } + for (Entity entity : world.getEntities()) { + + if (entity instanceof Item) { + Item item = (Item) entity; + String itemType = item.getItemStack().getType().toString(); + // Check if the item is in the exception list + boolean isException = this.exceptItemList.stream() + .anyMatch(ex -> ex.equalsIgnoreCase(itemType)); + if (!isException) { + item.remove(); + removed++; + } + } + } + } + if (this.shouldBroadcast) { + Bukkit.broadcastMessage(plugin.getPrefix() + ChatColor.GREEN + plugin.getTranslatedText("cleardropitems.cleared").replace("%REMOVECOUNT%",String.valueOf(removed))); + } + + // Reset the countdown to the interval for the next cycle + countdown = interval; + } else { + countdown--; + } + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index d554dc6..b792fa7 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -91,12 +91,28 @@ ServerPassword: preventMove: true preventBuild: true +# Dropped items clearing options +ClearDropItems: + enabled: false + interval: 300 # Interval in seconds between each clear cycle + broadcastMsg: true # Broadcasts alerts before clearing 1 minutes and 10 seconds before and when cleared. Set this to 'false' to make cleaning silently. + # Dropped items that should always be kept (never cleared) + except: + - DIAMOND + - GOLD_INGOT + # - COAL + # - IRON_INGOT + # - BREAD + # Worlds to skip (dropped items in these worlds won't be cleared) + skipWorlds: + - world_nether + - world_the_end # Cooldown options (only applicable for this plugin!) Cooldown: enabled: true # The time must be passed between using this plugin commands. - seconds: 2 #The command cooldown in seconds + seconds: 1 #The command cooldown in seconds Msg: '{PREFIX} Please wait {SECONDS} seconds!' # The message when the user spamming a command # # Plugin Manager - Enable/Disable plugins without server restart diff --git a/src/main/resources/en.yml b/src/main/resources/en.yml index e9ae03a..9fd667c 100644 --- a/src/main/resources/en.yml +++ b/src/main/resources/en.yml @@ -36,4 +36,7 @@ pmanager.already.enabled: " already enabled!" pmanager.success.enabled: " successfully enabled!" pmanager.already.disabled: " already disabled!" pmanager.success.disabled: " successfully disabled!" -list.help: "To list all SimplifyTools commands use the '/help SIMPLIFYTOOLS' command!" \ No newline at end of file +list.help: "To list all SimplifyTools commands use the '/help SIMPLIFYTOOLS' command!" +cleardropitems.oneMin: "Attention: Dropped items will be cleared in 1 minute!" +cleardropitems.tenSec: "Attention: Dropped items will be cleared in 10 seconds!" +cleardropitems.cleared: "Dropped items have been cleared (%REMOVECOUNT% items removed)." \ No newline at end of file diff --git a/src/main/resources/hu.yml b/src/main/resources/hu.yml index 63b0431..47d05dd 100644 --- a/src/main/resources/hu.yml +++ b/src/main/resources/hu.yml @@ -36,4 +36,7 @@ pmanager.already.enabled: " már eddig is engedélyezve volt!" pmanager.success.enabled: " sikeresen engedélyezve!" pmanager.already.disabled: " már kivolt kapcsolva eddig is!" pmanager.success.disabled: " sikeresen kikapcsolva!" -list.help: "Használd a '/help SIMPLIFYTOOLS' parancsot a plugin parancsainak listázásához!" \ No newline at end of file +list.help: "Használd a '/help SIMPLIFYTOOLS' parancsot a plugin parancsainak listázásához!" +cleardropitems.oneMin: "Figyelem! Az eldobott tárgyak 1 perc múlva törlésre kerülnek!" +cleardropitems.tenSec: "Figyelem! Az eldobott tárgyak 10 másodpercen belül törlésre kerülnek!" +cleardropitems.cleared: "Az eldobott tárgyak törlésre kerültek. (%REMOVECOUNT% tárgy lett eltávolítva)." \ No newline at end of file