From 2daca017c2856c67af08a69dff8146149ff909b1 Mon Sep 17 00:00:00 2001 From: YoyoNow Date: Tue, 29 Jul 2025 14:52:03 +0200 Subject: [PATCH] Add InventoryFillerCommand.gui Closes: #27 Fix PistonCalculator movement --- .../BauSystem_Main/src/BauSystem.properties | 3 + .../InventoryFillerCommand.java | 143 +++++++++++++++++- .../features/util/PistonCalculator.java | 26 ++-- .../de/steamwar/inventory/SWInventory.java | 4 + .../src/de/steamwar/inventory/SWItem.java | 5 + 5 files changed, 170 insertions(+), 11 deletions(-) diff --git a/BauSystem/BauSystem_Main/src/BauSystem.properties b/BauSystem/BauSystem_Main/src/BauSystem.properties index e1cce9bf..c03b2605 100644 --- a/BauSystem/BauSystem_Main/src/BauSystem.properties +++ b/BauSystem/BauSystem_Main/src/BauSystem.properties @@ -390,6 +390,9 @@ INVENTORY_FILL_HELP=§8/§einventoryfill §8- §7Toggles InventoryFill INVENTORY_FILL_INFO=§7Helps you fill containers by looking at them while sneaking and dropping the item. Or just scroll on a container to change the amount of the item inside. INVENTORY_FILL_ENABLE=§aInventoryFiller activated INVENTORY_FILL_DISABLE=§cInventoryFiller deactivated +INVENTORY_FILL_GUI_NAME=Inventory Filler +INVENTORY_FILL_GUI_POWER=§ePower§8:§7 {0} +INVENTORY_FILL_GUI_TNT=§eTNT # Killchecker KILLCHECKER_HELP_ENABLE=§8/§ekillchecker enable §8- §7Enables Killchecker / Recalculates kills KILLCHECKER_HELP_DISABLE=§8/§ekillchecker disable §8- §7Disables Killchecker diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/inventoryfiller/InventoryFillerCommand.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/inventoryfiller/InventoryFillerCommand.java index 4804a9d7..e467e3eb 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/inventoryfiller/InventoryFillerCommand.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/inventoryfiller/InventoryFillerCommand.java @@ -4,12 +4,45 @@ import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.SWUtils; import de.steamwar.bausystem.configplayer.Config; import de.steamwar.command.SWCommand; +import de.steamwar.inventory.SWInventory; +import de.steamwar.inventory.SWItem; import de.steamwar.linkage.Linked; +import org.bukkit.Material; +import org.bukkit.block.Barrel; +import org.bukkit.block.BlockState; +import org.bukkit.block.Chest; +import org.bukkit.block.ShulkerBox; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.BlockStateMeta; + +import java.util.HashMap; +import java.util.Map; @Linked public class InventoryFillerCommand extends SWCommand { + private static final Map POWER_TO_FILLLEVEL = new HashMap<>(); + + static { + POWER_TO_FILLLEVEL.put(0, 0); + POWER_TO_FILLLEVEL.put(1, 1); + POWER_TO_FILLLEVEL.put(2, 1 * 64 + 60); + POWER_TO_FILLLEVEL.put(3, 3 * 64 + 55); + POWER_TO_FILLLEVEL.put(4, 5 * 64 + 51); + POWER_TO_FILLLEVEL.put(5, 7 * 64 + 46); + POWER_TO_FILLLEVEL.put(6, 9 * 64 + 42); + POWER_TO_FILLLEVEL.put(7, 11 * 64 + 37); + POWER_TO_FILLLEVEL.put(8, 13 * 64 + 32); + POWER_TO_FILLLEVEL.put(9, 15 * 64 + 28); + POWER_TO_FILLLEVEL.put(10, 17 * 64 + 23); + POWER_TO_FILLLEVEL.put(11, 19 * 64 + 19); + POWER_TO_FILLLEVEL.put(12, 21 * 64 + 14); + POWER_TO_FILLLEVEL.put(13, 23 * 64 + 10); + POWER_TO_FILLLEVEL.put(14, 25 * 64 + 5); + POWER_TO_FILLLEVEL.put(15, 27 * 64 + 0); + } + public InventoryFillerCommand() { super("inventoryfill"); } @@ -21,8 +54,116 @@ public class InventoryFillerCommand extends SWCommand { if (!inventoryFill) { SWUtils.sendToActionbar(player, BauSystem.MESSAGE.parse("INVENTORY_FILL_ENABLE", player)); BauSystem.MESSAGE.send("INVENTORY_FILL_INFO", player); - }else { + } else { SWUtils.sendToActionbar(player, BauSystem.MESSAGE.parse("INVENTORY_FILL_DISABLE", player)); } } + + @Register("gui") + public void gui(Player player) { + SWInventory inventory = new SWInventory(player, 18, BauSystem.MESSAGE.parse("INVENTORY_FILL_GUI_NAME", player)); + inventory.setItem(0, new SWItem(Material.REDSTONE, BauSystem.MESSAGE.parse("INVENTORY_FILL_GUI_POWER", player, 0)).setAmount(1)); + inventory.setItem(1, new SWItem(Material.REDSTONE_TORCH, BauSystem.MESSAGE.parse("INVENTORY_FILL_GUI_POWER", player, 1)).setAmount(1)); + inventory.setItem(2, new SWItem(Material.REDSTONE_TORCH, BauSystem.MESSAGE.parse("INVENTORY_FILL_GUI_POWER", player, 2)).setAmount(2)); + inventory.setItem(3, new SWItem(Material.REDSTONE_TORCH, BauSystem.MESSAGE.parse("INVENTORY_FILL_GUI_POWER", player, 3)).setAmount(3)); + inventory.setItem(4, new SWItem(Material.REDSTONE_TORCH, BauSystem.MESSAGE.parse("INVENTORY_FILL_GUI_POWER", player, 4)).setAmount(4)); + inventory.setItem(5, new SWItem(Material.REDSTONE_TORCH, BauSystem.MESSAGE.parse("INVENTORY_FILL_GUI_POWER", player, 5)).setAmount(5)); + inventory.setItem(6, new SWItem(Material.REDSTONE_TORCH, BauSystem.MESSAGE.parse("INVENTORY_FILL_GUI_POWER", player, 6)).setAmount(6)); + inventory.setItem(7, new SWItem(Material.REDSTONE_TORCH, BauSystem.MESSAGE.parse("INVENTORY_FILL_GUI_POWER", player, 7)).setAmount(7)); + inventory.setItem(8, new SWItem(Material.REDSTONE_TORCH, BauSystem.MESSAGE.parse("INVENTORY_FILL_GUI_POWER", player, 8)).setAmount(8)); + inventory.setItem(9, new SWItem(Material.REDSTONE_TORCH, BauSystem.MESSAGE.parse("INVENTORY_FILL_GUI_POWER", player, 9)).setAmount(9)); + inventory.setItem(10, new SWItem(Material.REDSTONE_TORCH, BauSystem.MESSAGE.parse("INVENTORY_FILL_GUI_POWER", player, 10)).setAmount(10)); + inventory.setItem(11, new SWItem(Material.REDSTONE_TORCH, BauSystem.MESSAGE.parse("INVENTORY_FILL_GUI_POWER", player, 11)).setAmount(11)); + inventory.setItem(12, new SWItem(Material.REDSTONE_TORCH, BauSystem.MESSAGE.parse("INVENTORY_FILL_GUI_POWER", player, 12)).setAmount(12)); + inventory.setItem(13, new SWItem(Material.REDSTONE_TORCH, BauSystem.MESSAGE.parse("INVENTORY_FILL_GUI_POWER", player, 13)).setAmount(13)); + inventory.setItem(14, new SWItem(Material.REDSTONE_TORCH, BauSystem.MESSAGE.parse("INVENTORY_FILL_GUI_POWER", player, 14)).setAmount(14)); + inventory.setItem(15, new SWItem(Material.REDSTONE_TORCH, BauSystem.MESSAGE.parse("INVENTORY_FILL_GUI_POWER", player, 15)).setAmount(15)); + inventory.setItem(17, new SWItem(Material.TNT, BauSystem.MESSAGE.parse("INVENTORY_FILL_GUI_TNT", player))); + + for (int i = 0; i < 16; i++) { + inventory.setEventCallback(i, inventoryClickEvent -> { + selectInventoryType(player, inventoryClickEvent.getCurrentItem().getItemMeta().getDisplayName(), Material.DANDELION, POWER_TO_FILLLEVEL.get(inventoryClickEvent.getCurrentItem().getAmount())); + }); + } + inventory.setEventCallback(17, inventoryClickEvent -> { + selectInventoryType(player, inventoryClickEvent.getCurrentItem().getItemMeta().getDisplayName(), Material.TNT, POWER_TO_FILLLEVEL.get(15)); + }); + + inventory.open(); + } + + private final Material[] SHULKER_BOX_MATERIALS = new Material[]{ + Material.SHULKER_BOX, + Material.WHITE_SHULKER_BOX, + Material.LIGHT_GRAY_SHULKER_BOX, + Material.GRAY_SHULKER_BOX, + Material.BLACK_SHULKER_BOX, + Material.BROWN_SHULKER_BOX, + Material.RED_SHULKER_BOX, + Material.ORANGE_SHULKER_BOX, + Material.YELLOW_SHULKER_BOX, + Material.LIME_SHULKER_BOX, + Material.GREEN_SHULKER_BOX, + Material.CYAN_SHULKER_BOX, + Material.LIGHT_BLUE_SHULKER_BOX, + Material.BLUE_SHULKER_BOX, + Material.PURPLE_SHULKER_BOX, + Material.MAGENTA_SHULKER_BOX, + Material.PINK_SHULKER_BOX + }; + + private void selectInventoryType(Player player, String name, Material material, int count) { + ItemStack[] itemStacks = new ItemStack[27]; + int index = 0; + int amount = count; + while (amount > 0) { + ItemStack stack; + if (amount > 64) { + stack = new ItemStack(material, 64); + amount -= 64; + } else { + stack = new ItemStack(material, amount); + amount = 0; + } + itemStacks[index++] = stack; + } + + SWInventory inventory = new SWInventory(player, 27, BauSystem.MESSAGE.parse("INVENTORY_FILL_GUI_NAME", player)); + int i = 0; + for (Material type : SHULKER_BOX_MATERIALS) { + inventory.setItemEvent(i++, generateFilledInventory(name, type, itemStacks), inventoryClickEvent -> { + SWUtils.giveItemToPlayer(player, inventoryClickEvent.getCurrentItem()); + }); + } + inventory.setItemEvent(27 - 6, generateFilledInventory(name, Material.CHEST, itemStacks), inventoryClickEvent -> { + SWUtils.giveItemToPlayer(player, inventoryClickEvent.getCurrentItem()); + }); + inventory.setItemEvent(27 - 4, generateFilledInventory(name, Material.BARREL, itemStacks), inventoryClickEvent -> { + SWUtils.giveItemToPlayer(player, inventoryClickEvent.getCurrentItem()); + }); + inventory.open(); + } + + private ItemStack generateFilledInventory(String name, Material material, ItemStack[] itemStacks) { + ItemStack itemStack = new ItemStack(material); + BlockStateMeta meta = (BlockStateMeta) itemStack.getItemMeta(); + BlockState state = meta.getBlockState(); + + if (state instanceof ShulkerBox box) { + box.getInventory().setContents(itemStacks); + box.update(); + meta.setBlockState(box); + } else if (state instanceof Chest chest) { + chest.getInventory().setContents(itemStacks); + chest.update(); + meta.setBlockState(chest); + } else if (state instanceof Barrel barrel) { + barrel.getInventory().setContents(itemStacks); + barrel.update(); + meta.setBlockState(barrel); + } + meta.setDisplayName(name); + itemStack.setItemMeta(meta); + return itemStack; + } } diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/util/PistonCalculator.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/util/PistonCalculator.java index e759f031..ddc98cd5 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/util/PistonCalculator.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/util/PistonCalculator.java @@ -87,25 +87,29 @@ public class PistonCalculator implements Listener { @EventHandler public void onBlockPistonExtend(BlockPistonExtendEvent event) { Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> { + movePistOrders(event.getDirection(), event.getBlocks()); pistOrders.values().forEach(PistOrder::calculate); }, 3); - event.getBlocks().forEach(block -> { - PistOrder pistOrder = pistOrders.remove(block.getLocation()); - if (pistOrder == null) return; - pistOrder.piston = pistOrder.piston.getRelative(event.getDirection()); - pistOrders.put(pistOrder.piston.getLocation(), pistOrder); - }); } @EventHandler public void onBlockPistonRetract(BlockPistonRetractEvent event) { Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> { + movePistOrders(event.getDirection(), event.getBlocks()); pistOrders.values().forEach(PistOrder::calculate); }, 3); - event.getBlocks().forEach(block -> { - PistOrder pistOrder = pistOrders.remove(block.getLocation()); + } + + private void movePistOrders(BlockFace direction, List blocks) { + Set orders = new HashSet<>(); + blocks.forEach(block -> { + PistOrder pistOrder = pistOrders.get(block.getLocation()); if (pistOrder == null) return; - pistOrder.piston = pistOrder.piston.getRelative(event.getDirection()); + pistOrders.remove(block.getLocation()); + pistOrder.piston = pistOrder.piston.getRelative(direction); + orders.add(pistOrder); + }); + orders.forEach(pistOrder -> { pistOrders.put(pistOrder.piston.getLocation(), pistOrder); }); } @@ -173,7 +177,9 @@ public class PistonCalculator implements Listener { return; } - Piston pistonData = (Piston) piston.getBlockData(); + BlockData blockData = piston.getBlockData(); + if (!(blockData instanceof Piston)) return; + Piston pistonData = (Piston) blockData; if (piston.getType() == Material.PISTON && pistonData.isExtended()) { visualize(); return; diff --git a/SpigotCore/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java b/SpigotCore/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java index 1b4529d3..6b009004 100644 --- a/SpigotCore/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java +++ b/SpigotCore/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java @@ -86,6 +86,10 @@ public class SWInventory implements Listener { } } + public boolean hasItem(int pos) { + return inventory.getItem(pos) != null; + } + public void setItem(int pos, SWItem item) { setItem(pos, item.getItemStack(), item.getCallback()); } diff --git a/SpigotCore/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java b/SpigotCore/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java index b368332d..7fb27810 100644 --- a/SpigotCore/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java +++ b/SpigotCore/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java @@ -220,4 +220,9 @@ public class SWItem { itemStack.setItemMeta(itemMeta); return this; } + + public SWItem setAmount(int count) { + itemStack.setAmount(count); + return this; + } }