Refined item drop logic

This commit is contained in:
Taylor Shuler 2014-08-03 21:48:47 +01:00
parent 717c3f5fcc
commit 2a127a0871
3 changed files with 33 additions and 55 deletions

View File

@ -33,15 +33,12 @@ import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly; import cpw.mods.fml.relauncher.SideOnly;
public class BlockIronChest extends BlockContainer { public class BlockIronChest extends BlockContainer {
private Random random;
public BlockIronChest() public BlockIronChest()
{ {
super(Material.iron); super(Material.iron);
setBlockName("IronChest"); setBlockName("IronChest");
setHardness(3.0F); setHardness(3.0F);
setBlockBounds(0.0625F, 0F, 0.0625F, 0.9375F, 0.875F, 0.9375F); setBlockBounds(0.0625F, 0F, 0.0625F, 0.9375F, 0.875F, 0.9375F);
random = new Random();
setCreativeTab(CreativeTabs.tabDecorations); setCreativeTab(CreativeTabs.tabDecorations);
} }
@ -96,17 +93,7 @@ public class BlockIronChest extends BlockContainer {
{ {
TileEntity te = world.getTileEntity(i, j, k); TileEntity te = world.getTileEntity(i, j, k);
if (te == null || !(te instanceof TileEntityIronChest)) if (te == null || !(te instanceof TileEntityIronChest) || world.isSideSolid(i, j + 1, k, ForgeDirection.DOWN) || world.isRemote)
{
return true;
}
if (world.isSideSolid(i, j + 1, k, ForgeDirection.DOWN))
{
return true;
}
if (world.isRemote)
{ {
return true; return true;
} }
@ -166,42 +153,43 @@ public class BlockIronChest extends BlockContainer {
if (tileentitychest != null) if (tileentitychest != null)
{ {
tileentitychest.removeAdornments(); tileentitychest.removeAdornments();
dropContent(0, tileentitychest, world, tileentitychest.xCoord, tileentitychest.yCoord, tileentitychest.zCoord); dropItems(0, tileentitychest, world, tileentitychest.xCoord, tileentitychest.yCoord, tileentitychest.zCoord);
} }
super.breakBlock(world, i, j, k, i1, i2); super.breakBlock(world, i, j, k, i1, i2);
} }
public void dropContent(int newSize, IInventory chest, World world, int xCoord, int yCoord, int zCoord) public void dropItems(int newSize, IInventory chest, World world, int i, int j, int k)
{ {
for (int l = newSize; l < chest.getSizeInventory(); l++) for (int i1 = 0; i1 < chest.getSizeInventory(); ++i1)
{ {
ItemStack itemstack = chest.getStackInSlot(l); Random rand = new Random();
if (itemstack == null) ItemStack is = chest.getStackInSlot(i1);
if (is != null)
{ {
continue; EntityItem entityitem;
}
float f = random.nextFloat() * 0.8F + 0.1F; for (float f = rand.nextFloat() * 0.8F + 0.1F; is.stackSize > 0; world.spawnEntityInWorld(entityitem))
float f1 = random.nextFloat() * 0.8F + 0.1F;
float f2 = random.nextFloat() * 0.8F + 0.1F;
while (itemstack.stackSize > 0)
{
int i1 = random.nextInt(21) + 10;
if (i1 > itemstack.stackSize)
{ {
i1 = itemstack.stackSize; int j1 = rand.nextInt(21) + 10;
if (j1 > is.stackSize)
{
j1 = is.stackSize;
}
is.stackSize -= j1;
entityitem = new EntityItem(world, (double)((float)i + f), (double)((float)j + f), (double)((float)k + f), new ItemStack(is.getItem(), j1, is.getItemDamage()));
float f2 = 0.05F;
entityitem.motionX = (double)((float)rand.nextGaussian() * f2);
entityitem.motionY = (double)((float)rand.nextGaussian() * f2 + 0.2F);
entityitem.motionZ = (double)((float)rand.nextGaussian() * f2);
if (is.hasTagCompound())
{
entityitem.getEntityItem().setTagCompound((NBTTagCompound)is.getTagCompound().copy());
}
} }
itemstack.stackSize -= i1;
EntityItem entityitem = new EntityItem(world, (float) xCoord + f, (float) yCoord + (newSize > 0 ? 1 : 0) + f1, (float) zCoord + f2,
new ItemStack(itemstack.getItem(), i1, itemstack.getItemDamage()));
float f3 = 0.05F;
entityitem.motionX = (float) random.nextGaussian() * f3;
entityitem.motionY = (float) random.nextGaussian() * f3 + 0.2F;
entityitem.motionZ = (float) random.nextGaussian() * f3;
if (itemstack.hasTagCompound())
{
entityitem.getEntityItem().setTagCompound((NBTTagCompound) itemstack.getTagCompound().copy());
}
world.spawnEntityInWorld(entityitem);
} }
} }
} }

View File

@ -64,7 +64,7 @@ public class ItemChestChanger extends Item {
ItemStack[] chestContents = ObfuscationReflectionHelper.getPrivateValue(TileEntityChest.class, tec, 0); ItemStack[] chestContents = ObfuscationReflectionHelper.getPrivateValue(TileEntityChest.class, tec, 0);
System.arraycopy(chestContents, 0, newchest.chestContents, 0, Math.min(newSize, chestContents.length)); System.arraycopy(chestContents, 0, newchest.chestContents, 0, Math.min(newSize, chestContents.length));
BlockIronChest block = IronChest.ironChestBlock; BlockIronChest block = IronChest.ironChestBlock;
block.dropContent(newSize, tec, world, tec.xCoord, tec.yCoord, tec.zCoord); block.dropItems(newSize, tec, world, tec.xCoord, tec.yCoord, tec.zCoord);
newchest.setFacing((byte) tec.getBlockMetadata()); newchest.setFacing((byte) tec.getBlockMetadata());
newchest.sortTopStacks(); newchest.sortTopStacks();
for (int i = 0; i < Math.min(newSize, chestContents.length); i++) for (int i = 0; i < Math.min(newSize, chestContents.length); i++)
@ -75,8 +75,7 @@ public class ItemChestChanger extends Item {
world.setBlock(X, Y, Z, Blocks.air, 0, 3); world.setBlock(X, Y, Z, Blocks.air, 0, 3);
// Force the Chest TE to reset it's knowledge of neighbouring blocks // Force the Chest TE to reset it's knowledge of neighbouring blocks
tec.updateContainingBlockInfo(); tec.updateContainingBlockInfo();
// Force the Chest TE to update any neighbours so they update next // Force the Chest TE to update any neighbours so they update next tick
// tick
tec.checkForAdjacentChests(); tec.checkForAdjacentChests();
// And put in our block instead // And put in our block instead
world.setBlock(X, Y, Z, block, newchest.getType().ordinal(), 3); world.setBlock(X, Y, Z, block, newchest.getType().ordinal(), 3);

View File

@ -373,11 +373,7 @@ public class TileEntityIronChest extends TileEntity implements IInventory {
public TileEntityIronChest applyUpgradeItem(ItemChestChanger itemChestChanger) public TileEntityIronChest applyUpgradeItem(ItemChestChanger itemChestChanger)
{ {
if (numUsingPlayers > 0) if (numUsingPlayers > 0 || !itemChestChanger.getType().canUpgrade(this.getType()))
{
return null;
}
if (!itemChestChanger.getType().canUpgrade(this.getType()))
{ {
return null; return null;
} }
@ -385,7 +381,7 @@ public class TileEntityIronChest extends TileEntity implements IInventory {
int newSize = newEntity.chestContents.length; int newSize = newEntity.chestContents.length;
System.arraycopy(chestContents, 0, newEntity.chestContents, 0, Math.min(newSize, chestContents.length)); System.arraycopy(chestContents, 0, newEntity.chestContents, 0, Math.min(newSize, chestContents.length));
BlockIronChest block = IronChest.ironChestBlock; BlockIronChest block = IronChest.ironChestBlock;
block.dropContent(newSize, this, this.worldObj, this.xCoord, this.yCoord, this.zCoord); block.dropItems(newSize, this, this.worldObj, this.xCoord, this.yCoord, this.zCoord);
newEntity.setFacing(facing); newEntity.setFacing(facing);
newEntity.sortTopStacks(); newEntity.sortTopStacks();
newEntity.ticksSinceSync = -1; newEntity.ticksSinceSync = -1;
@ -488,11 +484,6 @@ public class TileEntityIronChest extends TileEntity implements IInventory {
} }
} }
public void setMaxStackSize(int size)
{
}
@Override @Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) public boolean isItemValidForSlot(int i, ItemStack itemstack)
{ {