Search in sources :

Example 1 with DirtyChestMenu

use of me.mrCookieSlime.Slimefun.api.inventory.DirtyChestMenu in project FluffyMachines by NCBPFluffyBear.

the class AutoCrafter method constructMenu.

private void constructMenu(String displayName) {
    new BlockMenuPreset(getId(), displayName) {

        @Override
        public void init() {
            constructMenu(this);
        }

        @Override
        public void newInstance(@Nonnull BlockMenu menu, @Nonnull Block b) {
            if (!BlockStorage.hasBlockInfo(b) || BlockStorage.getLocationInfo(b.getLocation(), "enabled") == null || BlockStorage.getLocationInfo(b.getLocation(), "enabled").equals(String.valueOf(false))) {
                menu.replaceExistingItem(6, new CustomItemStack(Material.GUNPOWDER, "&7Enabled: &4\u2718", "", "&e> Click to enable this Machine"));
                menu.addMenuClickHandler(6, (p, slot, item, action) -> {
                    BlockStorage.addBlockInfo(b, "enabled", String.valueOf(true));
                    newInstance(menu, b);
                    return false;
                });
            } else {
                menu.replaceExistingItem(6, new CustomItemStack(Material.REDSTONE, "&7Enabled: &2\u2714", "", "&e> Click to disable this Machine"));
                menu.addMenuClickHandler(6, (p, slot, item, action) -> {
                    BlockStorage.addBlockInfo(b, "enabled", String.valueOf(false));
                    newInstance(menu, b);
                    return false;
                });
            }
        }

        @Override
        public boolean canOpen(@Nonnull Block b, @Nonnull Player p) {
            return p.hasPermission("slimefun.inventory.bypass") || Slimefun.getProtectionManager().hasPermission(p, b.getLocation(), Interaction.INTERACT_BLOCK);
        }

        @Override
        public int[] getSlotsAccessedByItemTransport(ItemTransportFlow flow) {
            return new int[0];
        }

        @Override
        public int[] getSlotsAccessedByItemTransport(DirtyChestMenu menu, ItemTransportFlow flow, ItemStack item) {
            return getCustomItemTransport(menu, flow, item);
        }
    };
}
Also used : Player(org.bukkit.entity.Player) BlockMenu(me.mrCookieSlime.Slimefun.api.inventory.BlockMenu) Nonnull(javax.annotation.Nonnull) CustomItemStack(io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack) Block(org.bukkit.block.Block) DirtyChestMenu(me.mrCookieSlime.Slimefun.api.inventory.DirtyChestMenu) CustomItemStack(io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack) ItemStack(org.bukkit.inventory.ItemStack) SlimefunItemStack(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack) BlockMenuPreset(me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset) ItemTransportFlow(me.mrCookieSlime.Slimefun.api.item_transport.ItemTransportFlow)

Example 2 with DirtyChestMenu

use of me.mrCookieSlime.Slimefun.api.inventory.DirtyChestMenu in project Slimefun4 by Slimefun.

the class CargoUtils method withdraw.

@Nullable
static ItemStack withdraw(AbstractItemNetwork network, Map<Location, Inventory> inventories, Block node, Block target, ItemStack template) {
    DirtyChestMenu menu = getChestMenu(target);
    if (menu == null) {
        if (hasInventory(target)) {
            Inventory inventory = inventories.get(target.getLocation());
            if (inventory != null) {
                return withdrawFromVanillaInventory(network, node, template, inventory);
            }
            BlockState state = PaperLib.getBlockState(target, false).getState();
            if (state instanceof InventoryHolder) {
                inventory = ((InventoryHolder) state).getInventory();
                inventories.put(target.getLocation(), inventory);
                return withdrawFromVanillaInventory(network, node, template, inventory);
            }
        }
        return null;
    }
    ItemStackWrapper wrapperTemplate = ItemStackWrapper.wrap(template);
    for (int slot : menu.getPreset().getSlotsAccessedByItemTransport(menu, ItemTransportFlow.WITHDRAW, null)) {
        ItemStack is = menu.getItemInSlot(slot);
        ItemStackWrapper wrapperItemInSlot = ItemStackWrapper.wrap(is);
        if (SlimefunUtils.isItemSimilar(wrapperItemInSlot, wrapperTemplate, true) && matchesFilter(network, node, wrapperItemInSlot)) {
            if (is.getAmount() > template.getAmount()) {
                is.setAmount(is.getAmount() - template.getAmount());
                menu.replaceExistingItem(slot, is);
                return template;
            } else {
                menu.replaceExistingItem(slot, null);
                return is;
            }
        }
    }
    return null;
}
Also used : BlockState(org.bukkit.block.BlockState) DirtyChestMenu(me.mrCookieSlime.Slimefun.api.inventory.DirtyChestMenu) ItemStackWrapper(io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper) ItemStack(org.bukkit.inventory.ItemStack) InventoryHolder(org.bukkit.inventory.InventoryHolder) BrewerInventory(org.bukkit.inventory.BrewerInventory) Inventory(org.bukkit.inventory.Inventory) FurnaceInventory(org.bukkit.inventory.FurnaceInventory) Nullable(javax.annotation.Nullable)

Example 3 with DirtyChestMenu

use of me.mrCookieSlime.Slimefun.api.inventory.DirtyChestMenu in project Slimefun4 by Slimefun.

the class CargoUtils method insert.

@Nullable
static ItemStack insert(AbstractItemNetwork network, Map<Location, Inventory> inventories, Block node, Block target, boolean smartFill, ItemStack stack, ItemStackWrapper wrapper) {
    Debug.log(TestCase.CARGO_INPUT_TESTING, "CargoUtils#insert");
    if (!matchesFilter(network, node, stack)) {
        return stack;
    }
    DirtyChestMenu menu = getChestMenu(target);
    if (menu == null) {
        if (hasInventory(target)) {
            Inventory inventory = inventories.get(target.getLocation());
            if (inventory != null) {
                return insertIntoVanillaInventory(stack, wrapper, smartFill, inventory);
            }
            BlockState state = PaperLib.getBlockState(target, false).getState();
            if (state instanceof InventoryHolder) {
                inventory = ((InventoryHolder) state).getInventory();
                inventories.put(target.getLocation(), inventory);
                return insertIntoVanillaInventory(stack, wrapper, smartFill, inventory);
            }
        }
        return stack;
    }
    for (int slot : menu.getPreset().getSlotsAccessedByItemTransport(menu, ItemTransportFlow.INSERT, wrapper)) {
        ItemStack itemInSlot = menu.getItemInSlot(slot);
        if (itemInSlot == null) {
            menu.replaceExistingItem(slot, stack);
            return null;
        }
        int maxStackSize = itemInSlot.getType().getMaxStackSize();
        int currentAmount = itemInSlot.getAmount();
        if (!smartFill && currentAmount == maxStackSize) {
            // Skip full stacks - Performance optimization for non-smartfill nodes
            continue;
        }
        if (SlimefunUtils.isItemSimilar(itemInSlot, wrapper, true, false)) {
            if (currentAmount < maxStackSize) {
                int amount = currentAmount + stack.getAmount();
                itemInSlot.setAmount(Math.min(amount, maxStackSize));
                if (amount > maxStackSize) {
                    stack.setAmount(amount - maxStackSize);
                } else {
                    stack = null;
                }
                menu.replaceExistingItem(slot, itemInSlot);
                return stack;
            } else if (smartFill) {
                return stack;
            }
        }
    }
    return stack;
}
Also used : BlockState(org.bukkit.block.BlockState) DirtyChestMenu(me.mrCookieSlime.Slimefun.api.inventory.DirtyChestMenu) ItemStack(org.bukkit.inventory.ItemStack) InventoryHolder(org.bukkit.inventory.InventoryHolder) BrewerInventory(org.bukkit.inventory.BrewerInventory) Inventory(org.bukkit.inventory.Inventory) FurnaceInventory(org.bukkit.inventory.FurnaceInventory) Nullable(javax.annotation.Nullable)

Aggregations

DirtyChestMenu (me.mrCookieSlime.Slimefun.api.inventory.DirtyChestMenu)3 ItemStack (org.bukkit.inventory.ItemStack)3 Nullable (javax.annotation.Nullable)2 BlockState (org.bukkit.block.BlockState)2 BrewerInventory (org.bukkit.inventory.BrewerInventory)2 FurnaceInventory (org.bukkit.inventory.FurnaceInventory)2 Inventory (org.bukkit.inventory.Inventory)2 InventoryHolder (org.bukkit.inventory.InventoryHolder)2 SlimefunItemStack (io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack)1 CustomItemStack (io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack)1 ItemStackWrapper (io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper)1 Nonnull (javax.annotation.Nonnull)1 BlockMenu (me.mrCookieSlime.Slimefun.api.inventory.BlockMenu)1 BlockMenuPreset (me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset)1 ItemTransportFlow (me.mrCookieSlime.Slimefun.api.item_transport.ItemTransportFlow)1 Block (org.bukkit.block.Block)1 Player (org.bukkit.entity.Player)1