Add getSlotLimit(slot) to IItemHandler (#3445)

This commit is contained in:
Vincent Lee 2016-11-28 18:03:47 -06:00 committed by LexManos
parent b048c1a4b5
commit c17b40790b
9 changed files with 99 additions and 5 deletions

View File

@ -80,4 +80,12 @@ public interface IItemHandler
**/
@Nonnull
ItemStack extractItem(int slot, int amount, boolean simulate);
/**
* Retrieves the maximum stack size allowed to exist in the given slot.
*
* @param slot Slot to query.
* @return The maximum stack size allowed in the slot.
*/
int getSlotLimit(int slot);
}

View File

@ -25,10 +25,12 @@ import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.items.wrapper.PlayerMainInvWrapper;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class ItemHandlerHelper
{
@ -195,4 +197,37 @@ public class ItemHandlerHelper
world.spawnEntityInWorld(entityitem);
}
}
/**
* This method uses the standard vanilla algorithm to calculate a comparator output for how "full" the inventory is.
* This method is an adaptation of Container#calcRedstoneFromInventory(IInventory).
* @param inv The inventory handler to test.
* @return A redstone value in the range [0,15] representing how "full" this inventory is.
*/
public static int calcRedstoneFromInventory(@Nullable IItemHandler inv)
{
if (inv == null)
{
return 0;
}
else
{
int itemsFound = 0;
float proportion = 0.0F;
for (int j = 0; j < inv.getSlots(); ++j)
{
ItemStack itemstack = inv.getStackInSlot(j);
if (!itemstack.func_190926_b())
{
proportion += (float)itemstack.func_190916_E() / (float)Math.min(inv.getSlotLimit(j), itemstack.getMaxStackSize());
++itemsFound;
}
}
proportion = proportion / (float)inv.getSlots();
return MathHelper.floor_float(proportion * 14.0F) + (itemsFound > 0 ? 1 : 0);
}
}
}

View File

@ -154,9 +154,15 @@ public class ItemStackHandler implements IItemHandler, IItemHandlerModifiable, I
}
}
@Override
public int getSlotLimit(int slot)
{
return 64;
}
protected int getStackLimit(int slot, @Nonnull ItemStack stack)
{
return stack.getMaxStackSize();
return Math.min(getSlotLimit(slot), stack.getMaxStackSize());
}
@Override

View File

@ -149,6 +149,13 @@ public class VanillaDoubleChestItemHandler extends WeakReference<TileEntityChest
return chest != null ? chest.getSingleChestHandler().extractItem(targetSlot, amount, simulate) : ItemStack.field_190927_a;
}
@Override
public int getSlotLimit(int slot)
{
boolean accessingUpperChest = slot < 27;
return getChest(accessingUpperChest).getInventoryStackLimit();
}
@Override
public boolean equals(Object o)
{

View File

@ -123,4 +123,13 @@ public class CombinedInvWrapper implements IItemHandlerModifiable
slot = getSlotFromIndex(slot, index);
return handler.extractItem(slot, amount, simulate);
}
@Override
public int getSlotLimit(int slot)
{
int index = getIndexForSlot(slot);
IItemHandlerModifiable handler = getHandlerFromIndex(index);
int localSlot = getSlotFromIndex(slot, index);
return handler.getSlotLimit(localSlot);
}
}

View File

@ -61,4 +61,10 @@ public class EmptyHandler implements IItemHandlerModifiable
{
// nothing to do here
}
@Override
public int getSlotLimit(int slot)
{
return 0;
}
}

View File

@ -86,7 +86,7 @@ public class InvWrapper implements IItemHandlerModifiable
if (!ItemHandlerHelper.canItemStacksStack(stack, stackInSlot))
return stack;
m = Math.min(stack.getMaxStackSize(), getInv().getInventoryStackLimit()) - stackInSlot.func_190916_E();
m = Math.min(stack.getMaxStackSize(), getSlotLimit(slot)) - stackInSlot.func_190916_E();
if (stack.func_190916_E() <= m)
{
@ -121,7 +121,7 @@ public class InvWrapper implements IItemHandlerModifiable
}
else
{
m = Math.min(stack.getMaxStackSize(), getInv().getInventoryStackLimit());
m = Math.min(stack.getMaxStackSize(), getSlotLimit(slot));
if (m < stack.func_190916_E())
{
// copy the stack to not modify the original one
@ -192,6 +192,12 @@ public class InvWrapper implements IItemHandlerModifiable
getInv().setInventorySlotContents(slot, stack);
}
@Override
public int getSlotLimit(int slot)
{
return getInv().getInventoryStackLimit();
}
public IInventory getInv()
{
return inv;

View File

@ -94,6 +94,17 @@ public class RangedWrapper implements IItemHandlerModifiable {
}
}
@Override
public int getSlotLimit(int slot)
{
if (checkSlot(slot))
{
return compose.getSlotLimit(slot + minSlot);
}
return 0;
}
private boolean checkSlot(int localSlot)
{
return localSlot + minSlot < maxSlot;

View File

@ -104,7 +104,7 @@ public class SidedInvWrapper implements IItemHandlerModifiable
if (!ItemHandlerHelper.canItemStacksStack(stack, stackInSlot))
return stack;
m = Math.min(stack.getMaxStackSize(), inv.getInventoryStackLimit()) - stackInSlot.func_190916_E();
m = Math.min(stack.getMaxStackSize(), getSlotLimit(slot)) - stackInSlot.func_190916_E();
if (stack.func_190916_E() <= m)
{
@ -137,7 +137,7 @@ public class SidedInvWrapper implements IItemHandlerModifiable
}
else
{
m = Math.min(stack.getMaxStackSize(), inv.getInventoryStackLimit());
m = Math.min(stack.getMaxStackSize(), getSlotLimit(slot));
if (m < stack.func_190916_E())
{
// copy the stack to not modify the original one
@ -211,4 +211,10 @@ public class SidedInvWrapper implements IItemHandlerModifiable
return inv.decrStackSize(slot1, m);
}
}
@Override
public int getSlotLimit(int slot)
{
return inv.getInventoryStackLimit();
}
}