ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/util/StackUtil.java

255 lines
9.2 KiB
Java
Raw Normal View History

/*
* 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
*/
package de.ellpeck.actuallyadditions.mod.util;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.util.compat.SlotlessableItemHandlerWrapper;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraftforge.items.IItemHandler;
2021-02-26 22:15:48 +01:00
import java.util.Collection;
import java.util.List;
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
*
* @param stack The stack
2021-02-26 22:15:48 +01:00
*
* @return If the stack is not empty, or if it's an IDisableableItem, if its enabled.
*/
@Deprecated
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();
}
/**
* @return The empty itemstack instance.
*/
@Deprecated
public static ItemStack getEmpty() {
return ItemStack.EMPTY;
}
/**
* 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
*
* @param stacks Some ItemStacks
2021-02-26 22:15:48 +01:00
*
* @return If all stacks in the collection return true for {@link ItemStack#isEmpty()}
*/
@Deprecated
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;
}
}
return true;
}
/**
* 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 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
*
* @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++;
}
}
return counter == stacks.size();
}
/**
* 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 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-08-05 21:39:36 +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.
* @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
*
* @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++;
}
}
return counter == stacks.size();
}
2016-11-19 21:11:17 +01: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.
* @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
}
}
}
2016-11-19 21:11:17 +01: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
*
* @param inv The IItemHandler to search.
2021-02-26 22:15:48 +01: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;
}
}
return -1;
2016-11-19 21:11:17 +01:00
}
/**
* Helper method to add stack size and return the stack.
*/
public static ItemStack grow(ItemStack s, int i) {
s.grow(i);
return s;
}
/**
* 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
/**
* 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();
s.shrink(i);
2021-02-26 22:15:48 +01:00
if (s.isEmpty()) {
return sc.getItem().getContainerItem(sc);
}
return s;
}
/**
* 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
* @param slotStart Start range
2021-02-26 22:15:48 +01:00
* @param slotEnd End range
*
* @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;
}
ItemStack remain = stack.copy();
if (ActuallyAdditions.commonCapsLoaded) {
Object handler = wrapper.getSlotlessHandler();
// if (handler instanceof ISlotlessItemHandler) {
// remain = ((ISlotlessItemHandler) handler).insertItem(remain, simulate);
// if (!ItemStack.areItemStacksEqual(remain, stack)) {
// return remain;
// }
// }
}
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);
}
}
return remain;
}
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
}
}