From 228c8cffe5ffa8fac528b792d68e6c90faf96fc8 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Fri, 10 May 2024 14:19:04 +0200 Subject: [PATCH 1/2] EnergyRegulator GUI --- .../items/electric/EnergyRegulator.java | 163 +++++++++++++++++- 1 file changed, 162 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/EnergyRegulator.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/EnergyRegulator.java index 2377d02d09..ec0e643cff 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/EnergyRegulator.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/EnergyRegulator.java @@ -3,7 +3,16 @@ import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; +import io.github.thebusybiscuit.slimefun4.core.attributes.MachineProcessHolder; +import io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler; +import io.github.thebusybiscuit.slimefun4.core.machines.MachineOperation; +import io.github.thebusybiscuit.slimefun4.core.machines.MachineProcessor; +import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; +import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; +import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu; +import org.bukkit.Location; import org.bukkit.block.Block; +import org.bukkit.entity.Player; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.inventory.ItemStack; @@ -20,6 +29,9 @@ import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config; import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.*; /** * The {@link EnergyRegulator} is a special type of {@link SlimefunItem} which serves as the heart of every @@ -33,6 +45,13 @@ */ public class EnergyRegulator extends SlimefunItem implements HologramOwner { + private static final int[] BORDER = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; + private static final int[] END_BORDER = {45, 46, 48, 49, 50, 52, 53}; + private static final int PREV = 47; + private static final int NEXT = 51; + + private static int AMOUNT = 36; + @ParametersAreNonnullByDefault public EnergyRegulator(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { super(itemGroup, item, recipeType, recipe); @@ -56,7 +75,7 @@ private BlockPlaceHandler onPlace() { return new BlockPlaceHandler(false) { @Override - public void onPlayerPlace(BlockPlaceEvent e) { + public void onPlayerPlace(@Nonnull BlockPlaceEvent e) { updateHologram(e.getBlock(), "&7Connecting..."); } @@ -67,6 +86,28 @@ public void onPlayerPlace(BlockPlaceEvent e) { public void preRegister() { addItemHandler(onPlace()); + BlockUseHandler handler = blockUserHandler -> { + Location location; + Player player = blockUserHandler.getPlayer(); + + try { + location = blockUserHandler.getClickedBlock().orElseThrow().getLocation(); + } catch (NoSuchElementException e) { + blockUserHandler.cancel(); + return; + } + + EnergyNet network = getEnergyNetwork(location); + + if (network == null) + return; + + + getGUI(player, 0, network).open(player); + }; + + addItemHandler(handler); + addItemHandler(new BlockTicker() { @Override @@ -86,4 +127,124 @@ private void tick(@Nonnull Block b) { network.tick(b); } + private EnergyNet getEnergyNetwork(Location location) { + EnergyNet network; + + try { + network = Slimefun.getNetworkManager().getNetworkFromLocation(location, EnergyNet.class).orElseThrow(); + } catch (NoSuchElementException e) { + return null; + } + + return network; + } + + + private List getDisplayMachines(EnergyNet network) { + + Map consumers = network.getConsumers(); + + List print = new ArrayList<>(); + + for (var entry : consumers.entrySet()) { + EnergyNetComponent cmp = entry.getValue(); + Location location = entry.getKey(); + + if (!(cmp instanceof SlimefunItem sf)) { + continue; + } + + ItemStack element = sf.getItem().clone(); + ItemMeta meta = element.getItemMeta(); + + if (meta == null) { + continue; + } + + List lore = element.getItemMeta().getLore(); + + if (lore == null) { + continue; + } + + if (sf instanceof MachineProcessHolder holder) { + //This isn't very reliable as end-game machines which are faster will show as offline if clicked when a recipe just completed + MachineProcessor processor = (MachineProcessor) holder.getMachineProcessor(); + MachineOperation op = processor.getOperation(location); + + String msg = op == null ? "§4§lOffline" : "§a§lOnline"; + lore.add(""); + lore.add("§bStored Energy:§3 " + cmp.getCharge(location) +" J"); + lore.add(msg); + meta.setLore(lore); + element.setItemMeta(meta); + } + + print.add(element); + } + + print.sort(Comparator.comparing(ItemStack::getType)); + + return print; + } + + private ChestMenu getGUI(Player p, int page, EnergyNet network) { + ChestMenu chestMenu = new ChestMenu(getItemName()); + + ChestMenu.MenuClickHandler blank = ChestMenuUtils.getEmptyClickHandler(); + ItemStack background = ChestMenuUtils.getBackground(); + + for (int i : BORDER) { + chestMenu.addItem(i, background, blank); + } + + List displayMachines = getDisplayMachines(network); + int length = displayMachines.size(); + int pages = length / AMOUNT; + + int start = page * AMOUNT; + int end = start + AMOUNT; + + for (int i = start; i < end; i++) { + int slot = i % AMOUNT + 9; + + ItemStack item; + try { + item = displayMachines.get(i); + } catch (IndexOutOfBoundsException e) { + break; + } + + if (item == null) { + break; + } + + chestMenu.addItem(slot, displayMachines.get(i), blank); + } + + chestMenu.addItem(PREV, ChestMenuUtils.getPreviousButton(p, page, pages), (p1, slot, item, action) -> { + int prev = page - 1; + if (prev == -1) + return false; + + getGUI(p1, prev, network).open(p1); + return true; + }); + + chestMenu.addItem(NEXT, ChestMenuUtils.getNextButton(p, page, pages), (p1, slot, item, action) -> { + int next = page + 1; + if (next > pages) + return false; + + getGUI(p1, next, network).open(p1); + return true; + }); + + for (int i : END_BORDER) { + chestMenu.addItem(i, background, blank); + } + + return chestMenu; + } + } From 2517c0f03df6593e59546d29593da4ba5048e992 Mon Sep 17 00:00:00 2001 From: Intybyte Date: Fri, 10 May 2024 14:24:40 +0200 Subject: [PATCH 2/2] Removed Wildcard import --- .../implementation/items/electric/EnergyRegulator.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/EnergyRegulator.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/EnergyRegulator.java index ec0e643cff..57575c2e8c 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/EnergyRegulator.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/EnergyRegulator.java @@ -31,7 +31,11 @@ import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker; import org.bukkit.inventory.meta.ItemMeta; -import java.util.*; +import java.util.Map; +import java.util.Comparator; +import java.util.ArrayList; +import java.util.List; +import java.util.NoSuchElementException; /** * The {@link EnergyRegulator} is a special type of {@link SlimefunItem} which serves as the heart of every