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;
}
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;
}
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;
}
}
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;
}
}
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);
}
Aggregations