From 262f5919c5c7e724ec52ca583d0692fbf6da1a8d Mon Sep 17 00:00:00 2001 From: czmdav Date: Mon, 20 Sep 2021 00:21:44 +0200 Subject: [PATCH] optimizations --- .../com/teszvesz/remover/ChunkLoadEv.java | 17 ++++- .../teszvesz/remover/EnderChestRemove.java | 69 ------------------- .../java/com/teszvesz/remover/InvRemove.java | 69 +++++++++++++------ .../java/com/teszvesz/remover/MainPlugin.java | 11 +-- .../com/teszvesz/remover/TaskUtility.java | 8 +-- src/main/resources/plugin.yml | 6 +- 6 files changed, 74 insertions(+), 106 deletions(-) delete mode 100644 src/main/java/com/teszvesz/remover/EnderChestRemove.java diff --git a/src/main/java/com/teszvesz/remover/ChunkLoadEv.java b/src/main/java/com/teszvesz/remover/ChunkLoadEv.java index 396da57..b065491 100644 --- a/src/main/java/com/teszvesz/remover/ChunkLoadEv.java +++ b/src/main/java/com/teszvesz/remover/ChunkLoadEv.java @@ -3,9 +3,12 @@ import lombok.val; import lombok.var; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.Chunk; import org.bukkit.Material; import org.bukkit.block.ShulkerBox; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; import org.bukkit.entity.ItemFrame; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -17,9 +20,19 @@ public class ChunkLoadEv implements Listener { + private boolean enabled = false; + @EventHandler public void onChunkLoad(ChunkLoadEvent e){ - processChunk(e.getChunk()); + if (enabled) { + processChunk(e.getChunk()); + } + } + + public boolean enable(CommandSender sender, Command cmd, String label, String[] args) { + enabled = !enabled; + sender.sendMessage("Chunk processing on ChunkLoadEvent is " + (enabled ? ChatColor.GREEN + "enabled" : ChatColor.RED + "disabled")); + return false; } public static void processChunk(Chunk c) { @@ -43,7 +56,7 @@ public static void processChunk(Chunk c) { for (val m : MainPlugin.itemsList) { if (i.contains(m)) { - Bukkit.getLogger().info("Removed: " + m.toString()); + Bukkit.getLogger().info("Removed: " + m); i.remove(m); } } diff --git a/src/main/java/com/teszvesz/remover/EnderChestRemove.java b/src/main/java/com/teszvesz/remover/EnderChestRemove.java deleted file mode 100644 index 5c0da9b..0000000 --- a/src/main/java/com/teszvesz/remover/EnderChestRemove.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.teszvesz.remover; - -import com.lishid.openinv.OpenInv; -import lombok.val; -import lombok.var; -import org.bukkit.Bukkit; -import org.bukkit.block.ShulkerBox; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.inventory.meta.BlockStateMeta; - -public class EnderChestRemove implements CommandExecutor { - - private final MainPlugin plugin; - - public EnderChestRemove(MainPlugin plugin) { - this.plugin = plugin; - } - - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - removeAll(sender); - return true; - } - - void removeAll(CommandSender sender) { - val openinv = (OpenInv) plugin.getServer().getPluginManager().getPlugin("OpenInv"); - for(val p : Bukkit.getOfflinePlayers()){ - val player = openinv.loadPlayer(p); - if(player != null) { - sender.sendMessage(">" + p.getName() + " playerdata loaded..."); - val inv = player.getEnderChest(); - - for(val m : MainPlugin.itemsList){ - inv.remove(m); - } - - for (var i = 0; i < inv.getSize(); i++) { - val item = inv.getItem(i); - if(item != null) { - if (item.getItemMeta() instanceof BlockStateMeta) { - val im = (BlockStateMeta) item.getItemMeta(); - if (im.getBlockState() instanceof ShulkerBox) { - - val shulker = (ShulkerBox) im.getBlockState(); - - for(val m : MainPlugin.itemsList){ - shulker.getInventory().remove(m); - } - - im.setBlockState(shulker); - shulker.update(); - item.setItemMeta(im); - inv.setItem(i, item); - - } - } - } - - } - - player.saveData(); - openinv.unload(p); - } - } - } - -} diff --git a/src/main/java/com/teszvesz/remover/InvRemove.java b/src/main/java/com/teszvesz/remover/InvRemove.java index 46a7d2e..2dc859a 100644 --- a/src/main/java/com/teszvesz/remover/InvRemove.java +++ b/src/main/java/com/teszvesz/remover/InvRemove.java @@ -11,11 +11,13 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; import org.bukkit.inventory.meta.BlockStateMeta; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicLong; public class InvRemove implements CommandExecutor { @@ -35,79 +37,92 @@ void removeAll(CommandSender sender) { val openinv = (OpenInv) plugin.getServer().getPluginManager().getPlugin("OpenInv"); val TIMEOUT = 30; // 30 sec - TaskUtility.runWithTimingsAsync("inventory removal", () -> { - for (val p : Bukkit.getOfflinePlayers()) { + TaskUtility.runWithTimingsAsync(plugin, "inventory removal", () -> { + var checkpoint = System.currentTimeMillis() + 15000; + AtomicLong processedPlayerCount = new AtomicLong(0); + val op = Bukkit.getOfflinePlayers(); + sender.sendMessage("processing of " + op.length + " players started..."); + for (val p : op) { try { TaskUtility.waitForBukkitTask(plugin, () -> { val player = openinv.loadPlayer(p); if (player != null) { - sender.sendMessage(">" + p.getName() + " playerdata loaded..."); + plugin.getLogger().info("loaded data for " + p.getName()); + plugin.getLogger().info("processing inventory..."); val inv = player.getInventory(); - for(val m : MainPlugin.itemsList){ inv.remove(m); } - //ShulkerDelete - for (var i = 0; i < inv.getSize(); i++) { val item = inv.getItem(i); if(item != null) { if (item.getItemMeta() instanceof BlockStateMeta) { val im = (BlockStateMeta) item.getItemMeta(); if (im.getBlockState() instanceof ShulkerBox) { - val shulker = (ShulkerBox) im.getBlockState(); - for(val m : MainPlugin.itemsList){ shulker.getInventory().remove(m); } - im.setBlockState(shulker); shulker.update(); item.setItemMeta(im); inv.setItem(i, item); - } } } + } + plugin.getLogger().info("processing enderchest..."); + val ender = player.getEnderChest(); + for(val m : MainPlugin.itemsList){ + ender.remove(m); + } + for (var i = 0; i < ender.getSize(); i++) { + val item = ender.getItem(i); + if(item != null) { + if (item.getItemMeta() instanceof BlockStateMeta) { + val im = (BlockStateMeta) item.getItemMeta(); + if (im.getBlockState() instanceof ShulkerBox) { + val shulker = (ShulkerBox) im.getBlockState(); + for(val m : MainPlugin.itemsList){ + shulker.getInventory().remove(m); + } + im.setBlockState(shulker); + shulker.update(); + item.setItemMeta(im); + ender.setItem(i, item); + } + } + } } - //playervaults + + plugin.getLogger().info("processing playervault..."); for(var i = 1; i <= 100; i++){ if(VaultManager.getInstance().vaultExists(player.getUniqueId().toString(),i)){ val pv = VaultManager.getInstance().getVault(player.getUniqueId().toString(), i); if(pv != null){ - for(val m : MainPlugin.itemsList){ pv.remove(m); } - - for (var j = 0; j < pv.getSize(); j++) { val item = pv.getItem(j); if(item != null) { if (item.getItemMeta() instanceof BlockStateMeta) { val im = (BlockStateMeta) item.getItemMeta(); if (im.getBlockState() instanceof ShulkerBox) { - val shulker = (ShulkerBox) im.getBlockState(); - for(val m : MainPlugin.itemsList){ shulker.getInventory().remove(m); } - im.setBlockState(shulker); shulker.update(); item.setItemMeta(im); pv.setItem(j, item); - } } } - } - VaultManager.getInstance().saveVault(pv, player.getUniqueId().toString(), i); } } @@ -115,16 +130,28 @@ void removeAll(CommandSender sender) { player.saveData(); openinv.unload(p); + processedPlayerCount.incrementAndGet(); } }, TIMEOUT, TimeUnit.SECONDS); + Thread.sleep(50); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; } catch (TimeoutException e) { plugin.getLogger().warning("failed to process inventory of " + p.getName() + " in " + TIMEOUT + "s"); } catch (ExecutionException e) { - throw new RuntimeException(e); + plugin.getLogger().warning("failed to process inventory of " + p.getName()); + e.printStackTrace(); } + if (checkpoint <= System.currentTimeMillis()) { + checkpoint = System.currentTimeMillis() + 15_000; + sender.sendMessage("processed " + processedPlayerCount.get() + " players..."); + } + } + val msg = "Processed " + processedPlayerCount.get() + " players of " + op.length + " (" + (op.length-processedPlayerCount.get()) + " missed)"; + plugin.getLogger().info(msg); + if (!(sender instanceof ConsoleCommandSender)) { + sender.sendMessage(msg); } return true; }); diff --git a/src/main/java/com/teszvesz/remover/MainPlugin.java b/src/main/java/com/teszvesz/remover/MainPlugin.java index 394a5d3..892344a 100644 --- a/src/main/java/com/teszvesz/remover/MainPlugin.java +++ b/src/main/java/com/teszvesz/remover/MainPlugin.java @@ -1,6 +1,7 @@ package com.teszvesz.remover; +import lombok.val; import org.bukkit.Material; import org.bukkit.plugin.java.JavaPlugin; @@ -114,13 +115,13 @@ public class MainPlugin extends JavaPlugin { @Override public void onEnable() { + val cl = new ChunkLoadEv(); - getServer().getPluginManager().registerEvents(new ChunkLoadEv(), this); - - this.getCommand("removefromchunk").setExecutor(new RemoveChunk(this)); - this.getCommand("removefromenderchest").setExecutor(new EnderChestRemove(this)); - this.getCommand("removefrominv").setExecutor(new InvRemove(this)); + getServer().getPluginManager().registerEvents(cl, this); + getCommand("enablechunkremove").setExecutor(cl::enable); + getCommand("removefromchunk").setExecutor(new RemoveChunk(this)); + getCommand("removefrominv").setExecutor(new InvRemove(this)); } } diff --git a/src/main/java/com/teszvesz/remover/TaskUtility.java b/src/main/java/com/teszvesz/remover/TaskUtility.java index b531b85..89f2c5c 100644 --- a/src/main/java/com/teszvesz/remover/TaskUtility.java +++ b/src/main/java/com/teszvesz/remover/TaskUtility.java @@ -29,17 +29,15 @@ public static void waitForBukkitTask(Plugin plugin, Runnable runnable, long time completableFuture.get(timeout, timeUnit); } - public static CompletableFuture runWithTimingsAsync(final String name, final Supplier task) { + public static CompletableFuture runWithTimingsAsync(Plugin plugin, String name, Supplier task) { CompletableFuture completableFuture = new CompletableFuture<>(); - Thread thread = new Thread(() -> withTimings(name, () -> { + plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> withTimings(name, () -> { try { completableFuture.complete(task.get()); } catch (Throwable t) { completableFuture.completeExceptionally(t); } - }), name); - thread.setDaemon(true); - thread.start(); + })); return completableFuture; } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index c3c8a3c..d8e9a2b 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -9,14 +9,12 @@ depend: - OpenInv commands: + enablechunkremove: + permission: asd.yes.lol removefromchunk: description: RemoveStuffFromPlayerChunk usage: /removefromchunk permission: asd.yes.lol - removefromenderchest: - description: RemoveStuffFromPlayersEC - usage: /removefromenderchest - permission: asd.yes.lol removefrominv: description: RemoveStuffFromPlayersinv usage: /removefrominv