Bin block-based inventory work

This commit is contained in:
Aidan C. Brady 2013-11-12 19:18:22 -05:00
parent e6ed263aa3
commit 1e336047d2
4 changed files with 142 additions and 23 deletions

View file

@ -46,7 +46,7 @@ public class RenderBin extends TileEntitySpecialRenderer
if(itemStack != null)
{
amount = Integer.toString(itemStack.stackSize);
amount = Integer.toString(tileEntity.itemCount);
}
Object3D obj = Object3D.get(tileEntity).getFromSide(ForgeDirection.getOrientation(tileEntity.facing));

View file

@ -220,7 +220,7 @@ public class BlockBasic extends Block
if(bin.getStack() != null)
{
world.spawnEntityInWorld(new EntityItem(world, player.posX, player.posY, player.posZ, bin.getStack().copy()));
bin.setInventorySlotContents(0, null);
bin.removeStack();
}
}
}
@ -244,7 +244,19 @@ public class BlockBasic extends Block
return true;
}
if(metadata == 9 || metadata == 10 || metadata == 11)
if(metadata == 6)
{
TileEntityBin bin = (TileEntityBin)world.getBlockTileEntity(x, y, z);
if(entityplayer.getCurrentEquippedItem() != null)
{
ItemStack remain = bin.add(entityplayer.getCurrentEquippedItem());
entityplayer.setCurrentItemOrArmor(0, remain);
}
return true;
}
else if(metadata == 9 || metadata == 10 || metadata == 11)
{
if(!entityplayer.isSneaking() && ((TileEntityDynamicTank)world.getBlockTileEntity(x, y, z)).structure != null)
{

View file

@ -5,6 +5,8 @@ import net.minecraft.nbt.NBTTagCompound;
public class InventoryBin
{
public final int MAX_STORAGE = 4096;
public ItemStack bin;
public InventoryBin(ItemStack stack)
@ -17,7 +19,7 @@ public class InventoryBin
if(getItemCount() > 0)
{
ItemStack ret = getItemType().copy();
ret.stackSize = Math.min(64, getItemCount());
ret.stackSize = Math.min(getItemType().getMaxStackSize(), getItemCount());
return ret;
}
@ -40,17 +42,53 @@ public class InventoryBin
public ItemStack add(ItemStack stack)
{
if(stack != null && stack.stackSize > 0 && stack.isStackable())
if(isValid(stack) && getItemCount() != MAX_STORAGE)
{
if(getItemType() == null)
{
setItemType(stack);
}
setItemCount(getItemCount() + stack.stackSize);
if(getItemCount() + stack.stackSize <= MAX_STORAGE)
{
setItemCount(getItemCount() + stack.stackSize);
}
else {
ItemStack rejects = getItemType().copy();
rejects.stackSize = (getItemCount()+stack.stackSize) - MAX_STORAGE;
setItemCount(MAX_STORAGE);
return rejects;
}
}
return null;
return stack;
}
public boolean isValid(ItemStack stack)
{
if(stack == null || stack.stackSize <= 0)
{
return false;
}
if(stack.isItemStackDamageable() && stack.isItemDamaged())
{
return false;
}
if(getItemType() == null)
{
return true;
}
if(!stack.isItemEqual(getItemType()) || !ItemStack.areItemStackTagsEqual(stack, getItemType()))
{
return false;
}
return true;
}
public int getItemCount()

View file

@ -30,6 +30,8 @@ public class TileEntityBin extends TileEntityBasicBlock implements ISidedInvento
public int itemCount;
public final int MAX_STORAGE = 4096;
public ItemStack itemType;
public ItemStack getStack()
@ -37,7 +39,7 @@ public class TileEntityBin extends TileEntityBasicBlock implements ISidedInvento
if(itemCount > 0)
{
ItemStack ret = itemType.copy();
ret.stackSize = Math.min(64, itemCount);
ret.stackSize = Math.min(itemType.getMaxStackSize(), itemCount);
return ret;
}
@ -45,6 +47,83 @@ public class TileEntityBin extends TileEntityBasicBlock implements ISidedInvento
return null;
}
public ItemStack getInsertStack()
{
int remain = MAX_STORAGE-itemCount;
if(itemType == null)
{
return null;
}
if(remain >= itemType.getMaxStackSize())
{
return null;
}
else {
ItemStack ret = itemType.copy();
ret.stackSize = itemType.getMaxStackSize()-remain;
return ret;
}
}
public boolean isValid(ItemStack stack)
{
if(stack == null || stack.stackSize <= 0)
{
return false;
}
if(stack.isItemStackDamageable() && stack.isItemDamaged())
{
return false;
}
if(itemType == null)
{
return true;
}
if(!stack.isItemEqual(itemType) || !ItemStack.areItemStackTagsEqual(stack, itemType))
{
return false;
}
return true;
}
public ItemStack add(ItemStack stack)
{
if(isValid(stack))
{
if(itemType == null)
{
setItemType(stack);
}
setItemCount(itemCount + stack.stackSize);
onInventoryChanged();
}
return null;
}
public ItemStack removeStack()
{
ItemStack stack = getStack();
if(stack == null)
{
return null;
}
setItemCount(itemCount - stack.stackSize);
onInventoryChanged();
return stack;
}
@Override
public void onUpdate()
{
@ -135,7 +214,7 @@ public class TileEntityBin extends TileEntityBasicBlock implements ISidedInvento
{
if(slotID == 1)
{
return null;
return getInsertStack();
}
else {
return getStack();
@ -182,11 +261,6 @@ public class TileEntityBin extends TileEntityBasicBlock implements ISidedInvento
@Override
public void setInventorySlotContents(int i, ItemStack itemstack)
{
if(itemType != null && itemstack != null && !itemstack.isItemEqual(itemType))
{
return;
}
if(i == 0)
{
if(itemCount == 0)
@ -204,14 +278,9 @@ public class TileEntityBin extends TileEntityBasicBlock implements ISidedInvento
}
else if(i == 1)
{
if(itemstack != null && itemstack.stackSize > 0)
if(isValid(itemstack))
{
if(itemType == null)
{
setItemType(itemstack);
}
setItemCount(itemCount + itemstack.stackSize);
add(itemstack);
}
}
@ -285,7 +354,7 @@ public class TileEntityBin extends TileEntityBasicBlock implements ISidedInvento
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack)
{
return itemType == null || itemType.isItemEqual(itemstack);
return i == 1 ? isValid(itemstack) : false;
}
@Override
@ -309,7 +378,7 @@ public class TileEntityBin extends TileEntityBasicBlock implements ISidedInvento
@Override
public boolean canExtractItem(int i, ItemStack itemstack, int j)
{
return itemType != null && itemType.isItemEqual(itemstack);
return i == 0 ? isValid(itemstack) : false;
}
@Override