Implemented Clearing dropped items feature
This commit is contained in:
@ -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);
|
||||
|
@ -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<String> exceptItemList;
|
||||
private final List<String> 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--;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user