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 @Nonnull
ItemStack extractItem(int slot, int amount, boolean simulate); 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.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.SoundCategory; import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.items.wrapper.PlayerMainInvWrapper; import net.minecraftforge.items.wrapper.PlayerMainInvWrapper;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class ItemHandlerHelper public class ItemHandlerHelper
{ {
@ -195,4 +197,37 @@ public class ItemHandlerHelper
world.spawnEntityInWorld(entityitem); 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) protected int getStackLimit(int slot, @Nonnull ItemStack stack)
{ {
return stack.getMaxStackSize(); return Math.min(getSlotLimit(slot), stack.getMaxStackSize());
} }
@Override @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; 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 @Override
public boolean equals(Object o) public boolean equals(Object o)
{ {

View file

@ -123,4 +123,13 @@ public class CombinedInvWrapper implements IItemHandlerModifiable
slot = getSlotFromIndex(slot, index); slot = getSlotFromIndex(slot, index);
return handler.extractItem(slot, amount, simulate); 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 // 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)) if (!ItemHandlerHelper.canItemStacksStack(stack, stackInSlot))
return stack; 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) if (stack.func_190916_E() <= m)
{ {
@ -121,7 +121,7 @@ public class InvWrapper implements IItemHandlerModifiable
} }
else else
{ {
m = Math.min(stack.getMaxStackSize(), getInv().getInventoryStackLimit()); m = Math.min(stack.getMaxStackSize(), getSlotLimit(slot));
if (m < stack.func_190916_E()) if (m < stack.func_190916_E())
{ {
// copy the stack to not modify the original one // copy the stack to not modify the original one
@ -192,6 +192,12 @@ public class InvWrapper implements IItemHandlerModifiable
getInv().setInventorySlotContents(slot, stack); getInv().setInventorySlotContents(slot, stack);
} }
@Override
public int getSlotLimit(int slot)
{
return getInv().getInventoryStackLimit();
}
public IInventory getInv() public IInventory getInv()
{ {
return inv; 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) private boolean checkSlot(int localSlot)
{ {
return localSlot + minSlot < maxSlot; return localSlot + minSlot < maxSlot;

View file

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