2016-11-16 16:59:00 +01:00
|
|
|
/*
|
|
|
|
* This file ("StackUtil.java") is part of the Actually Additions mod for Minecraft.
|
|
|
|
* It is created and owned by Ellpeck and distributed
|
|
|
|
* under the Actually Additions License to be found at
|
|
|
|
* http://ellpeck.de/actaddlicense
|
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 Ellpeck
|
2016-11-16 16:59:00 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package de.ellpeck.actuallyadditions.mod.util;
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.util.compat.SlotlessableItemHandlerWrapper;
|
2016-11-16 16:59:00 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2016-11-20 20:13:26 +01:00
|
|
|
import net.minecraft.util.NonNullList;
|
2018-05-31 22:41:13 +02:00
|
|
|
import net.minecraftforge.items.IItemHandler;
|
2021-02-26 22:15:48 +01:00
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
2016-11-16 16:59:00 +01:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
public final class StackUtil {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pretty much just a check for {@link ItemStack#isEmpty()} but exists in case Mojang does some more refactoring.
|
2021-02-26 22:15:48 +01:00
|
|
|
*
|
2018-06-23 01:39:30 +02:00
|
|
|
* @param stack The stack
|
2021-02-26 22:15:48 +01:00
|
|
|
*
|
2018-06-23 01:39:30 +02:00
|
|
|
* @return If the stack is not empty, or if it's an IDisableableItem, if its enabled.
|
|
|
|
*/
|
2021-03-01 17:25:30 +01:00
|
|
|
@Deprecated
|
2018-06-23 01:39:30 +02:00
|
|
|
public static boolean isValid(ItemStack stack) {
|
2021-02-26 22:15:48 +01:00
|
|
|
return stack != null && !stack.isEmpty();
|
|
|
|
// if (stack == null) AwfulUtil.callTheFuckinPolice("Null ItemStack detected", stack);
|
|
|
|
// Item i = stack.getItem();
|
|
|
|
// if (i instanceof IDisableableItem) return !((IDisableableItem) i).isDisabled();
|
|
|
|
// return !stack.isEmpty();
|
2016-11-16 16:59:00 +01:00
|
|
|
}
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
/**
|
|
|
|
* @return The empty itemstack instance.
|
|
|
|
*/
|
2021-03-01 17:25:30 +01:00
|
|
|
@Deprecated
|
2018-06-23 01:39:30 +02:00
|
|
|
public static ItemStack getEmpty() {
|
|
|
|
return ItemStack.EMPTY;
|
2016-11-16 16:59:00 +01:00
|
|
|
}
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
/**
|
|
|
|
* Checks if a collection of stacks are empty, as {@link Collection#isEmpty()} does not care about empty stacks.
|
2021-02-26 22:15:48 +01:00
|
|
|
*
|
2018-06-23 01:39:30 +02:00
|
|
|
* @param stacks Some ItemStacks
|
2021-02-26 22:15:48 +01:00
|
|
|
*
|
2018-06-23 01:39:30 +02:00
|
|
|
* @return If all stacks in the collection return true for {@link ItemStack#isEmpty()}
|
|
|
|
*/
|
2021-03-01 17:25:30 +01:00
|
|
|
@Deprecated
|
2018-06-23 01:39:30 +02:00
|
|
|
public static boolean isEmpty(Collection<ItemStack> stacks) {
|
2021-02-26 22:15:48 +01:00
|
|
|
if (stacks.isEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (ItemStack s : stacks) {
|
|
|
|
if (!s.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
return true;
|
2016-11-16 16:59:00 +01:00
|
|
|
}
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
/**
|
|
|
|
* Checks if all provided itemstacks will fit in the AA handler. Use addAll below to actually add the stacks. This is strictly a check function.
|
2021-02-26 22:15:48 +01:00
|
|
|
*
|
|
|
|
* @param inv The AA Item handler
|
|
|
|
* @param stacks The stacks to add
|
2018-06-23 01:39:30 +02:00
|
|
|
* @param fromAutomation If these stacks are coming from a pipe or other external source, or internally, like from the TE's update() method.
|
2021-02-26 22:15:48 +01:00
|
|
|
*
|
2018-06-23 01:39:30 +02:00
|
|
|
* @return If all stacks fit fully. If even one item would not fit, the method returns false.
|
|
|
|
*/
|
|
|
|
public static boolean canAddAll(ItemStackHandlerAA inv, List<ItemStack> stacks, boolean fromAutomation) {
|
|
|
|
int counter = 0;
|
2018-08-10 05:04:07 +02:00
|
|
|
ItemStackHandlerAA dummy = testDummy(inv, 0, inv.getSlots());
|
|
|
|
for (ItemStack s : stacks) {
|
|
|
|
for (int i = 0; i < dummy.getSlots(); i++) {
|
|
|
|
s = dummy.insertItem(i, s, false, fromAutomation);
|
2021-02-26 22:15:48 +01:00
|
|
|
if (s.isEmpty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (s.isEmpty()) {
|
|
|
|
counter++;
|
2016-11-16 16:59:00 +01:00
|
|
|
}
|
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
return counter == stacks.size();
|
2016-11-16 16:59:00 +01:00
|
|
|
}
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
/**
|
|
|
|
* Adds all itemstacks in a list to an AA item handler. Must be an AA item handler to support the automation bool.
|
2021-02-26 22:15:48 +01:00
|
|
|
*
|
|
|
|
* @param inv The AA Item handler
|
|
|
|
* @param stacks The stacks to add
|
2018-06-23 01:39:30 +02:00
|
|
|
* @param fromAutomation If these stacks are coming from a pipe or other external source, or internally, like from the TE's update() method.
|
|
|
|
*/
|
|
|
|
public static void addAll(ItemStackHandlerAA inv, List<ItemStack> stacks, boolean fromAutomation) {
|
|
|
|
int slotMax = inv.getSlots();
|
|
|
|
for (ItemStack s : stacks) {
|
|
|
|
for (int i = 0; i < slotMax; i++) {
|
|
|
|
s = inv.insertItem(i, s, false, fromAutomation);
|
2021-02-26 22:15:48 +01:00
|
|
|
if (s.isEmpty()) {
|
|
|
|
break;
|
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-01 18:07:42 +01:00
|
|
|
}
|
2018-08-05 21:39:36 +02:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
/**
|
|
|
|
* Checks if all provided itemstacks will fit in the AA handler. Use addAll below to actually add the stacks. This is strictly a check function.
|
2021-02-26 22:15:48 +01:00
|
|
|
*
|
|
|
|
* @param inv The AA Item handler
|
|
|
|
* @param stacks The stacks to add
|
|
|
|
* @param slot The starting slot.
|
|
|
|
* @param endSlot The ending slot, exclusive.
|
2018-06-23 01:39:30 +02:00
|
|
|
* @param fromAutomation If these stacks are coming from a pipe or other external source, or internally, like from the TE's update() method.
|
2021-02-26 22:15:48 +01:00
|
|
|
*
|
2018-06-23 01:39:30 +02:00
|
|
|
* @return If all stacks fit fully. If even one item would not fit, the method returns false.
|
|
|
|
*/
|
|
|
|
public static boolean canAddAll(ItemStackHandlerAA inv, List<ItemStack> stacks, int slot, int endSlot, boolean fromAutomation) {
|
|
|
|
int counter = 0;
|
2018-08-10 05:04:07 +02:00
|
|
|
ItemStackHandlerAA dummy = testDummy(inv, slot, endSlot);
|
|
|
|
for (ItemStack s : stacks) {
|
|
|
|
for (int i = 0; i < dummy.getSlots(); i++) {
|
|
|
|
s = dummy.insertItem(i, s, false, fromAutomation);
|
2021-02-26 22:15:48 +01:00
|
|
|
if (s.isEmpty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (s.isEmpty()) {
|
|
|
|
counter++;
|
2018-06-23 01:39:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return counter == stacks.size();
|
2016-11-16 16:59:00 +01:00
|
|
|
}
|
2016-11-19 21:11:17 +01:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
/**
|
|
|
|
* Adds all itemstacks in a list to an AA item handler. Must be an AA item handler to support the automation bool.
|
2021-02-26 22:15:48 +01:00
|
|
|
*
|
|
|
|
* @param inv The AA Item handler
|
|
|
|
* @param stacks The stacks to add
|
|
|
|
* @param slot The starting slot.
|
|
|
|
* @param endSlot The ending slot, exclusive.
|
2018-06-23 01:39:30 +02:00
|
|
|
* @param fromAutomation If these stacks are coming from a pipe or other external source, or internally, like from the TE's update() method.
|
|
|
|
*/
|
|
|
|
public static void addAll(ItemStackHandlerAA inv, List<ItemStack> stacks, int slot, int endSlot, boolean fromAutomation) {
|
|
|
|
for (ItemStack s : stacks) {
|
|
|
|
for (int i = slot; i < endSlot; i++) {
|
|
|
|
s = inv.insertItem(i, s, false, fromAutomation);
|
2021-02-26 22:15:48 +01:00
|
|
|
if (s.isEmpty()) {
|
|
|
|
break;
|
|
|
|
}
|
2016-11-19 21:11:17 +01:00
|
|
|
}
|
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
}
|
2016-11-19 21:11:17 +01:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
/**
|
|
|
|
* Util method to find the first filled item in a handler. Searches from slot 0 to the end.
|
2021-02-26 22:15:48 +01:00
|
|
|
*
|
2018-06-23 01:39:30 +02:00
|
|
|
* @param inv The IItemHandler to search.
|
2021-02-26 22:15:48 +01:00
|
|
|
*
|
2018-06-23 01:39:30 +02:00
|
|
|
* @return The first filled slot, or -1 if all slots are empty.
|
|
|
|
*/
|
|
|
|
public static int findFirstFilled(IItemHandler inv) {
|
|
|
|
for (int i = 0; i < inv.getSlots(); i++) {
|
2021-02-26 22:15:48 +01:00
|
|
|
if (!inv.getStackInSlot(i).isEmpty()) {
|
|
|
|
return i;
|
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
}
|
|
|
|
return -1;
|
2016-11-19 21:11:17 +01:00
|
|
|
}
|
2016-11-20 20:13:26 +01:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
/**
|
|
|
|
* Helper method to add stack size and return the stack.
|
|
|
|
*/
|
|
|
|
public static ItemStack grow(ItemStack s, int i) {
|
|
|
|
s.grow(i);
|
|
|
|
return s;
|
2016-11-20 20:13:26 +01:00
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to remove stack size and return the stack.
|
|
|
|
*/
|
|
|
|
public static ItemStack shrink(ItemStack s, int i) {
|
|
|
|
s.shrink(i);
|
|
|
|
return s;
|
2017-12-06 22:25:10 +01:00
|
|
|
}
|
2018-08-05 21:39:36 +02:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
/**
|
|
|
|
* Helper method to remove stack size and return the stack.
|
|
|
|
*/
|
|
|
|
public static ItemStack shrinkForContainer(ItemStack s, int i) {
|
2018-09-26 09:53:48 +02:00
|
|
|
ItemStack sc = s.copy();
|
2018-06-23 01:39:30 +02:00
|
|
|
s.shrink(i);
|
2021-02-26 22:15:48 +01:00
|
|
|
if (s.isEmpty()) {
|
|
|
|
return sc.getItem().getContainerItem(sc);
|
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
return s;
|
|
|
|
}
|
2018-05-31 22:41:13 +02:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
/**
|
|
|
|
* Interaction method for working with Common Capabilities.
|
2021-02-26 22:15:48 +01:00
|
|
|
*
|
|
|
|
* @param wrapper The wrapper holding at least one instance
|
|
|
|
* @param stack The stack to insert. Should not be empty.
|
|
|
|
* @param simulate If this is a simulation
|
2018-06-23 01:39:30 +02:00
|
|
|
* @param slotStart Start range
|
2021-02-26 22:15:48 +01:00
|
|
|
* @param slotEnd End range
|
|
|
|
*
|
2018-06-23 01:39:30 +02:00
|
|
|
* @return The remainder that was not inserted.
|
|
|
|
*/
|
|
|
|
public static ItemStack insertItem(SlotlessableItemHandlerWrapper wrapper, ItemStack stack, boolean simulate, int slotStart, int slotEnd) {
|
2021-02-26 22:15:48 +01:00
|
|
|
if (stack.isEmpty()) {
|
|
|
|
return stack;
|
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
ItemStack remain = stack.copy();
|
|
|
|
|
|
|
|
if (ActuallyAdditions.commonCapsLoaded) {
|
|
|
|
Object handler = wrapper.getSlotlessHandler();
|
2021-03-01 17:25:30 +01:00
|
|
|
// if (handler instanceof ISlotlessItemHandler) {
|
|
|
|
// remain = ((ISlotlessItemHandler) handler).insertItem(remain, simulate);
|
|
|
|
// if (!ItemStack.areItemStacksEqual(remain, stack)) {
|
|
|
|
// return remain;
|
|
|
|
// }
|
|
|
|
// }
|
2018-05-31 22:41:13 +02:00
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
|
|
|
|
IItemHandler handler = wrapper.getNormalHandler();
|
|
|
|
if (handler != null) {
|
|
|
|
for (int i = Math.max(0, slotStart); i < Math.min(slotEnd, handler.getSlots()); i++) {
|
|
|
|
remain = handler.insertItem(i, remain, simulate);
|
2018-05-31 22:41:13 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-21 17:50:33 +01:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
return remain;
|
|
|
|
}
|
2016-11-21 17:50:33 +01:00
|
|
|
|
2018-08-05 21:39:36 +02:00
|
|
|
/**
|
2018-08-10 05:04:07 +02:00
|
|
|
* Constructs a clone of the given item handler, from the given slots. The new item handler will have the provided slot as slot 0.
|
|
|
|
* This is used for testing the ability to add all itemstacks, and should not be used for anything else.
|
2018-08-05 21:39:36 +02:00
|
|
|
*/
|
2018-08-10 05:04:07 +02:00
|
|
|
public static ItemStackHandlerAA testDummy(ItemStackHandlerAA inv, int slot, int endSlot) {
|
|
|
|
NonNullList<ItemStack> stacks = NonNullList.withSize(endSlot - slot, getEmpty());
|
2021-02-26 22:15:48 +01:00
|
|
|
for (int i = slot; i < endSlot; i++) {
|
2018-08-10 05:04:07 +02:00
|
|
|
stacks.set(i - slot, inv.getStackInSlot(i).copy());
|
2021-02-26 22:15:48 +01:00
|
|
|
}
|
2018-08-10 05:04:07 +02:00
|
|
|
return new ItemStackHandlerAA(stacks, inv.getAcceptor(), inv.getRemover());
|
2018-08-05 21:39:36 +02:00
|
|
|
}
|
|
|
|
|
2016-11-16 16:59:00 +01:00
|
|
|
}
|