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;
|
|
|
|
|
2017-12-06 22:25:10 +01:00
|
|
|
import java.util.Collection;
|
|
|
|
|
2017-08-02 04:20:47 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.misc.IDisableableItem;
|
|
|
|
import net.minecraft.item.Item;
|
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;
|
2016-11-16 16:59:00 +01:00
|
|
|
|
|
|
|
public final class StackUtil{
|
|
|
|
|
|
|
|
public static ItemStack validateCopy(ItemStack stack){
|
|
|
|
if(isValid(stack)){
|
|
|
|
return stack.copy();
|
|
|
|
}
|
|
|
|
else{
|
2017-11-02 22:49:53 +01:00
|
|
|
return getEmpty();
|
2016-11-16 16:59:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack validateCheck(ItemStack stack){
|
|
|
|
if(isValid(stack)){
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
else{
|
2017-11-02 22:49:53 +01:00
|
|
|
return getEmpty();
|
2016-11-16 16:59:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 22:49:53 +01:00
|
|
|
public static boolean isValid(ItemStack stack){
|
|
|
|
if(stack == null) AwfulUtil.callTheFuckinPolice("Null ItemStack detected", stack);
|
2017-08-02 04:20:47 +02:00
|
|
|
Item i = stack.getItem();
|
|
|
|
if(i instanceof IDisableableItem) return !((IDisableableItem) i).isDisabled();
|
|
|
|
return !stack.isEmpty();
|
2016-11-16 16:59:00 +01:00
|
|
|
}
|
|
|
|
|
2017-11-02 22:49:53 +01:00
|
|
|
public static ItemStack getEmpty(){
|
2016-11-26 21:32:27 +01:00
|
|
|
return ItemStack.EMPTY;
|
2016-11-16 16:59:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static int getStackSize(ItemStack stack){
|
|
|
|
if(!isValid(stack)){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else{
|
2016-11-26 21:32:27 +01:00
|
|
|
return stack.getCount();
|
2016-11-16 16:59:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack setStackSize(ItemStack stack, int size){
|
2016-12-01 18:07:42 +01:00
|
|
|
return setStackSize(stack, size, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack setStackSize(ItemStack stack, int size, boolean containerOnEmpty){
|
2016-11-16 16:59:00 +01:00
|
|
|
if(size <= 0){
|
2016-12-01 18:07:42 +01:00
|
|
|
if(isValid(stack) && containerOnEmpty){
|
2016-11-16 16:59:00 +01:00
|
|
|
return stack.getItem().getContainerItem(stack);
|
|
|
|
}
|
|
|
|
else{
|
2017-11-02 22:49:53 +01:00
|
|
|
return getEmpty();
|
2016-11-16 16:59:00 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-26 21:32:27 +01:00
|
|
|
stack.setCount(size);
|
2016-11-16 16:59:00 +01:00
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack addStackSize(ItemStack stack, int size){
|
2016-12-01 18:07:42 +01:00
|
|
|
return addStackSize(stack, size, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack addStackSize(ItemStack stack, int size, boolean containerOnEmpty){
|
|
|
|
return setStackSize(stack, getStackSize(stack)+size, containerOnEmpty);
|
2016-11-16 16:59:00 +01:00
|
|
|
}
|
2016-11-19 21:11:17 +01:00
|
|
|
|
2016-11-20 20:13:26 +01:00
|
|
|
public static boolean isIInvEmpty(NonNullList<ItemStack> slots){
|
2016-11-19 21:11:17 +01:00
|
|
|
for(ItemStack stack : slots){
|
|
|
|
if(StackUtil.isValid(stack)){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-20 20:13:26 +01:00
|
|
|
|
|
|
|
public static NonNullList<ItemStack> createSlots(int size){
|
2017-11-02 22:49:53 +01:00
|
|
|
return NonNullList.withSize(size, getEmpty());
|
2016-11-20 20:13:26 +01:00
|
|
|
}
|
2017-12-06 22:25:10 +01:00
|
|
|
|
|
|
|
public static boolean isEmpty(Collection<ItemStack> stacks) {
|
|
|
|
if(stacks.isEmpty()) return true;
|
|
|
|
else for(ItemStack s : stacks) if (!s.isEmpty()) return false;
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-21 17:50:33 +01:00
|
|
|
|
|
|
|
|
2016-11-16 16:59:00 +01:00
|
|
|
}
|