Search in sources :

Example 1 with ItemStackWrapper

use of io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper in project Slimefun4 by Slimefun.

the class CargoNetworkTask method distributeItem.

@Nullable
@ParametersAreNonnullByDefault
private ItemStack distributeItem(ItemStack stack, Location inputNode, List<Location> outputNodes) {
    ItemStack item = stack;
    Config cfg = BlockStorage.getLocationInfo(inputNode);
    boolean roundrobin = Objects.equals(cfg.getString("round-robin"), "true");
    boolean smartFill = Objects.equals(cfg.getString("smart-fill"), "true");
    Collection<Location> destinations;
    if (roundrobin) {
        // Use an ArrayDeque to perform round-robin sorting
        // Since the impl for roundRobinSort just does Deque.addLast(Deque#removeFirst)
        // An ArrayDequeue is preferable as opposed to a LinkedList:
        // - The number of elements does not change.
        // - ArrayDequeue has better iterative performance
        Deque<Location> tempDestinations = new ArrayDeque<>(outputNodes);
        roundRobinSort(inputNode, tempDestinations);
        destinations = tempDestinations;
    } else {
        // Using an ArrayList here since we won't need to sort the destinations
        // The ArrayList has the best performance for iteration bar a primitive array
        destinations = new ArrayList<>(outputNodes);
    }
    for (Location output : destinations) {
        Optional<Block> target = network.getAttachedBlock(output);
        if (target.isPresent()) {
            ItemStackWrapper wrapper = ItemStackWrapper.wrap(item);
            item = CargoUtils.insert(network, inventories, output.getBlock(), target.get(), smartFill, item, wrapper);
            if (item == null) {
                break;
            }
        }
    }
    return item;
}
Also used : Config(me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config) Block(org.bukkit.block.Block) ItemStack(org.bukkit.inventory.ItemStack) ItemStackWrapper(io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper) ArrayDeque(java.util.ArrayDeque) Location(org.bukkit.Location) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault) Nullable(javax.annotation.Nullable)

Example 2 with ItemStackWrapper

use of io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper 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 ItemStackWrapper

use of io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper in project Slimefun4 by Slimefun.

the class ItemFilter method test.

@Override
public boolean test(@Nonnull ItemStack item) {
    Debug.log(TestCase.CARGO_INPUT_TESTING, "ItemFilter#test({})", item);
    /*
         * An empty Filter does not need to be iterated over.
         * We can just return our default value in this scenario.
         */
    if (items.isEmpty()) {
        return rejectOnMatch;
    }
    // The amount of potential matches with that item.
    int potentialMatches = 0;
    /*
         * This is a first check for materials to see if we might even have any match.
         * If there is no potential match then we won't need to perform the quite
         * intense operation .getItemMeta()
         */
    for (ItemStackWrapper stack : items) {
        if (stack.getType() == item.getType()) {
            // We found a potential match based on the Material
            potentialMatches++;
        }
    }
    if (potentialMatches == 0) {
        // If there is no match, we can safely assume the default value
        return rejectOnMatch;
    } else {
        /*
             * If there is more than one potential match, create a wrapper to save
             * performance on the ItemMeta otherwise just use the item directly.
             */
        ItemStack subject = potentialMatches == 1 ? item : ItemStackWrapper.wrap(item);
        /*
             * If there is only one match, we won't need to create a Wrapper
             * and thus only perform .getItemMeta() once
             */
        for (ItemStackWrapper stack : items) {
            if (SlimefunUtils.isItemSimilar(subject, stack, checkLore, false)) {
                /*
                     * The filter has found a match, we can return the opposite
                     * of our default value. If we exclude items, this is where we
                     * would return false. Otherwise, we return true.
                     */
                return !rejectOnMatch;
            }
        }
        // If no particular item was matched, we fallback to our default value.
        return rejectOnMatch;
    }
}
Also used : ItemStackWrapper(io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper) ItemStack(org.bukkit.inventory.ItemStack)

Example 4 with ItemStackWrapper

use of io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper in project Slimefun4 by Slimefun.

the class ItemStackAndInteger method initializeItem.

private void initializeItem() {
    if (this.item instanceof ItemStackWrapper) {
        ItemStack copy = new ItemStack(item.getType(), item.getAmount());
        if (this.item.hasItemMeta()) {
            copy.setItemMeta(this.item.getItemMeta());
        }
        this.item = copy;
    }
}
Also used : ItemStackWrapper(io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper) ItemStack(org.bukkit.inventory.ItemStack)

Example 5 with ItemStackWrapper

use of io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper in project Slimefun4 by Slimefun.

the class TestItemStackWrapper method testWrapperChecking.

@Test
@DisplayName("Test if the ItemStackWrapper static method constructors are checking for nested wrapping properly")
void testWrapperChecking() {
    Assertions.assertThrows(IllegalArgumentException.class, () -> ItemStackWrapper.wrap(null));
    Assertions.assertThrows(IllegalArgumentException.class, () -> ItemStackWrapper.forceWrap(null));
    ItemStack item = new CustomItemStack(Material.IRON_INGOT, "A Name", "line 1", "line2");
    ItemStackWrapper wrapper = ItemStackWrapper.wrap(item);
    ItemStackWrapper secondWrap = ItemStackWrapper.wrap(wrapper);
    // We want to check that the wrapper returned is of reference equality
    Assertions.assertSame(wrapper, secondWrap);
    ItemStackWrapper forceSecondWrap = ItemStackWrapper.forceWrap(wrapper);
    // Want to check that the wrapper returned is of different reference equality
    Assertions.assertNotSame(wrapper, forceSecondWrap);
    assertWrapped(wrapper, forceSecondWrap);
}
Also used : CustomItemStack(io.github.bakedlibs.dough.items.CustomItemStack) ItemStack(org.bukkit.inventory.ItemStack) CustomItemStack(io.github.bakedlibs.dough.items.CustomItemStack) ItemStackWrapper(io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

ItemStackWrapper (io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper)14 ItemStack (org.bukkit.inventory.ItemStack)12 CustomItemStack (io.github.bakedlibs.dough.items.CustomItemStack)7 DisplayName (org.junit.jupiter.api.DisplayName)6 Test (org.junit.jupiter.api.Test)6 Nullable (javax.annotation.Nullable)4 ItemMetaSnapshot (io.github.bakedlibs.dough.items.ItemMetaSnapshot)1 SlimefunItemStack (io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack)1 RepairedSpawner (io.github.thebusybiscuit.slimefun4.implementation.items.blocks.RepairedSpawner)1 ArrayDeque (java.util.ArrayDeque)1 Optional (java.util.Optional)1 Nonnull (javax.annotation.Nonnull)1 ParametersAreNonnullByDefault (javax.annotation.ParametersAreNonnullByDefault)1 Config (me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config)1 DirtyChestMenu (me.mrCookieSlime.Slimefun.api.inventory.DirtyChestMenu)1 Location (org.bukkit.Location)1 Block (org.bukkit.block.Block)1 BlockState (org.bukkit.block.BlockState)1 BrewerInventory (org.bukkit.inventory.BrewerInventory)1 FurnaceInventory (org.bukkit.inventory.FurnaceInventory)1