From 5b33673956f4d06d03b606576fea6aaaa7177969 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Mon, 25 Aug 2014 01:54:08 -0400 Subject: [PATCH] Fixes and drop items on break --- .../storagedrawers/block/BlockDrawers.java | 71 ++++++++++++++++++- .../block/tile/TileEntityDrawers.java | 8 +++ .../renderer/TileEntityDrawersRenderer.java | 8 +-- .../core/ForgeEventHandler.java | 2 +- 4 files changed, 83 insertions(+), 6 deletions(-) diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java index 992a711..30de07f 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java @@ -12,6 +12,7 @@ import net.minecraft.block.BlockContainer; import net.minecraft.block.BlockWood; import net.minecraft.block.material.Material; import net.minecraft.client.Minecraft; +import net.minecraft.client.particle.EffectRenderer; import net.minecraft.client.renderer.texture.IIconRegister; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.Entity; @@ -20,6 +21,7 @@ import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.AxisAlignedBB; import net.minecraft.util.IIcon; @@ -29,6 +31,7 @@ import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import java.util.List; +import java.util.Random; public class BlockDrawers extends BlockContainer implements IExtendedBlockClickHandler { @@ -57,7 +60,7 @@ public class BlockDrawers extends BlockContainer implements IExtendedBlockClickH this.halfDepth = halfDepth; setCreativeTab(ModCreativeTabs.tabStorageDrawers); - setHardness(2.5f); + setHardness(5f); setStepSound(Block.soundTypeWood); setBlockName(blockName); } @@ -244,6 +247,52 @@ public class BlockDrawers extends BlockContainer implements IExtendedBlockClickH return super.removedByPlayer(world, player, x, y, z); } + @Override + public void breakBlock (World world, int x, int y, int z, Block block, int meta) { + TileEntityDrawers tile = getTileEntity(world, x, y, z); + if (tile != null) { + for (int i = 0; i < tile.getDrawerCount(); i++) { + while (tile.getItemCount(i) > 0) { + ItemStack stack = tile.takeItemsFromSlot(i, tile.getStackSize(i)); + if (stack == null || stack.stackSize == 0) + break; + + dropStackInBatches(world, x, y, z, stack); + } + } + + world.func_147453_f(x, y, z, block); + } + + super.breakBlock(world, x, y, z, block, meta); + } + + private void dropStackInBatches (World world, int x, int y, int z, ItemStack stack) { + Random rand = world.rand; + + float ex = rand.nextFloat() * .8f + .1f; + float ey = rand.nextFloat() * .8f + .1f; + float ez = rand.nextFloat() * .8f + .1f; + + EntityItem entity; + for (; stack.stackSize > 0; world.spawnEntityInWorld(entity)) { + int stackPartSize = rand.nextInt(21) + 10; + if (stackPartSize > stack.stackSize) + stackPartSize = stack.stackSize; + + stack.stackSize -= stackPartSize; + entity = new EntityItem(world, x + ex, y + ey, z + ez, new ItemStack(stack.getItem(), stackPartSize, stack.getItemDamage())); + + float motionUnit = .05f; + entity.motionX = rand.nextGaussian() * motionUnit; + entity.motionY = rand.nextGaussian() * motionUnit + .2f; + entity.motionZ = rand.nextGaussian() * motionUnit; + + if (stack.hasTagCompound()) + entity.getEntityItem().setTagCompound((NBTTagCompound)stack.getTagCompound().copy()); + } + } + @Override public TileEntityDrawers createNewTileEntity (World world, int meta) { return new TileEntityDrawers(); @@ -264,6 +313,26 @@ public class BlockDrawers extends BlockContainer implements IExtendedBlockClickH return tile; } + @Override + @SideOnly(Side.CLIENT) + public boolean addHitEffects (World worldObj, MovingObjectPosition target, EffectRenderer effectRenderer) { + TileEntity tile = worldObj.getTileEntity(target.blockX, target.blockY, target.blockZ); + if (tile instanceof TileEntityDrawers) { + if (((TileEntityDrawers) tile).getDirection() == target.sideHit) + return true; + } + + return super.addHitEffects(worldObj, target, effectRenderer); + } + + /*@Override + @SideOnly(Side.CLIENT) + public boolean addDestroyEffects (World world, int x, int y, int z, int meta, EffectRenderer effectRenderer) { + TileEntity tile = world.getTileEntity(x, y, z); + if (tile instanceof TileEntityDrawers) + return super.addDestroyEffects(world, x, y, z, meta, effectRenderer); + }*/ + @Override public void getSubBlocks (Item item, CreativeTabs creativeTabs, List list) { for (int i = 0; i < BlockWood.field_150096_a.length; i++) diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java index a2e4633..7e58850 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java @@ -89,6 +89,10 @@ public class TileEntityDrawers extends TileEntity this.direction = direction % 6; } + public int getDrawerCount () { + return drawerCount; + } + public void setDrawerCount (int count) { drawerCount = count; @@ -113,6 +117,10 @@ public class TileEntityDrawers extends TileEntity return data[slot].meta; } + public int getItemCount (int slot) { + return data[slot].count; + } + public int getStackSize (int slot) { if (data[slot].item == null) return 0; diff --git a/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java b/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java index 2e325b9..4b8fee9 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java +++ b/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java @@ -51,15 +51,15 @@ public class TileEntityDrawersRenderer extends TileEntitySpecialRenderer GL11.glEnable(GL11.GL_LIGHTING); - int drawerCount = 2; + int drawerCount = tileDrawers.getDrawerCount(); double depth = 1; double unit = .0625; Block block = tile.getWorldObj().getBlock(tile.xCoord, tile.yCoord, tile.zCoord); - if (block instanceof BlockDrawers) { + if (block instanceof BlockDrawers) depth = ((BlockDrawers) block).halfDepth ? .5 : 1; - drawerCount = ((BlockDrawers) block).drawerCount; - } + else + return; itemRenderer.setRenderManager(RenderManager.instance); diff --git a/src/com/jaquadro/minecraft/storagedrawers/core/ForgeEventHandler.java b/src/com/jaquadro/minecraft/storagedrawers/core/ForgeEventHandler.java index eb2c4e1..1a6cc17 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/core/ForgeEventHandler.java +++ b/src/com/jaquadro/minecraft/storagedrawers/core/ForgeEventHandler.java @@ -9,7 +9,7 @@ public class ForgeEventHandler { @SubscribeEvent public void playerInteracts (PlayerInteractEvent event) { - if (event.action == PlayerInteractEvent.Action.LEFT_CLICK_BLOCK) { + if (event.action == PlayerInteractEvent.Action.LEFT_CLICK_BLOCK && event.entityPlayer.capabilities.isCreativeMode) { TileEntity tile = event.world.getTileEntity(event.x, event.y, event.z); if (tile instanceof TileEntityDrawers) { int dir = ((TileEntityDrawers) tile).getDirection();