diff --git a/src/main/java/forestry/arboriculture/blocks/BlockArbFence.java b/src/main/java/forestry/arboriculture/blocks/BlockArbFence.java index b11eddbb3..d9710bd7a 100644 --- a/src/main/java/forestry/arboriculture/blocks/BlockArbFence.java +++ b/src/main/java/forestry/arboriculture/blocks/BlockArbFence.java @@ -106,7 +106,7 @@ public class BlockArbFence extends BlockFence implements IWoodTyped, ITileEntity @Override public IIcon getIcon(IBlockAccess world, int x, int y, int z, int side) { - TileWood wood = BlockWood.getWoodTile(world, x, y, z); + TileWood wood = TileWood.getWoodTile(world, x, y, z); EnumWoodType woodType = wood.getWoodType(); return IconProviderWood.getPlankIcon(woodType); } @@ -125,30 +125,37 @@ public class BlockArbFence extends BlockFence implements IWoodTyped, ITileEntity @Override public final ItemStack getPickBlock(MovingObjectPosition target, World world, int x, int y, int z) { ItemStack itemStack = new ItemStack(this); - NBTTagCompound nbt = BlockWood.getTagCompound(world, x, y, z); + NBTTagCompound nbt = TileWood.getTagCompound(world, x, y, z); itemStack.setTagCompound(nbt); return itemStack; } - @Override - public boolean removedByPlayer(World world, EntityPlayer player, int x, int y, int z) { - return TileWood.blockRemovedByPlayer(this, world, player, x, y, z); - } + /* DROP HANDLING */ + // Hack: When harvesting we need to get the drops in onBlockHarvested, + // because Mojang destroys the block and tile before calling getDrops. + private final ThreadLocal> drops = new ThreadLocal<>(); @Override - public void breakBlock(World world, int x, int y, int z, Block block, int meta) { - world.removeTileEntity(x, y, z); - super.breakBlock(world, x, y, z, block, meta); + public void onBlockHarvested(World world, int x, int y, int z, int meta, EntityPlayer playerProfile) { + drops.set(TileWood.getDrops(this, world, x, y, z)); } @Override public ArrayList getDrops(World world, int x, int y, int z, int metadata, int fortune) { - return new ArrayList<>(); + ArrayList ret = drops.get(); + drops.remove(); + + // not harvested, get drops normally + if (ret == null) { + ret = TileWood.getDrops(this, world, x, y, z); + } + + return ret; } @Override public final float getBlockHardness(World world, int x, int y, int z) { - TileWood wood = BlockWood.getWoodTile(world, x, y, z); + TileWood wood = TileWood.getWoodTile(world, x, y, z); if (wood == null) { return EnumWoodType.DEFAULT_HARDNESS; } diff --git a/src/main/java/forestry/arboriculture/blocks/BlockArbStairs.java b/src/main/java/forestry/arboriculture/blocks/BlockArbStairs.java index 581198abc..f29c60399 100644 --- a/src/main/java/forestry/arboriculture/blocks/BlockArbStairs.java +++ b/src/main/java/forestry/arboriculture/blocks/BlockArbStairs.java @@ -67,26 +67,33 @@ public class BlockArbStairs extends BlockStairs implements IWoodTyped, ITileEnti } } - @Override - public boolean removedByPlayer(World world, EntityPlayer player, int x, int y, int z) { - return TileWood.blockRemovedByPlayer(this, world, player, x, y, z); - } + /* DROP HANDLING */ + // Hack: When harvesting we need to get the drops in onBlockHarvested, + // because Mojang destroys the block and tile before calling getDrops. + private final ThreadLocal> drops = new ThreadLocal<>(); @Override - public void breakBlock(World world, int x, int y, int z, Block block, int meta) { - world.removeTileEntity(x, y, z); - super.breakBlock(world, x, y, z, block, meta); + public void onBlockHarvested(World world, int x, int y, int z, int meta, EntityPlayer playerProfile) { + drops.set(TileWood.getDrops(this, world, x, y, z)); } @Override public ArrayList getDrops(World world, int x, int y, int z, int metadata, int fortune) { - return new ArrayList<>(); + ArrayList ret = drops.get(); + drops.remove(); + + // not harvested, get drops normally + if (ret == null) { + ret = TileWood.getDrops(this, world, x, y, z); + } + + return ret; } @Override public ItemStack getPickBlock(MovingObjectPosition target, World world, int x, int y, int z) { ItemStack itemStack = super.getPickBlock(target, world, x, y, z); - NBTTagCompound stairsNBT = BlockWood.getTagCompound(world, x, y, z); + NBTTagCompound stairsNBT = TileWood.getTagCompound(world, x, y, z); itemStack.setTagCompound(stairsNBT); return itemStack; } @@ -107,7 +114,7 @@ public class BlockArbStairs extends BlockStairs implements IWoodTyped, ITileEnti @Override public IIcon getIcon(IBlockAccess world, int x, int y, int z, int side) { - TileWood wood = BlockWood.getWoodTile(world, x, y, z); + TileWood wood = TileWood.getWoodTile(world, x, y, z); EnumWoodType woodType = wood.getWoodType(); return IconProviderWood.getPlankIcon(woodType); } diff --git a/src/main/java/forestry/arboriculture/blocks/BlockLog.java b/src/main/java/forestry/arboriculture/blocks/BlockLog.java index 58dd7479e..2fc3bb4fe 100644 --- a/src/main/java/forestry/arboriculture/blocks/BlockLog.java +++ b/src/main/java/forestry/arboriculture/blocks/BlockLog.java @@ -45,6 +45,7 @@ public class BlockLog extends BlockWood { @Override public void breakBlock(World world, int x, int y, int z, Block block, int meta) { + super.breakBlock(world, x, y, z, block, meta); byte radius = 4; int boundary = radius + 1; @@ -101,7 +102,7 @@ public class BlockLog extends BlockWood { @Override public IIcon getIcon(IBlockAccess world, int x, int y, int z, int side) { int meta = world.getBlockMetadata(x, y, z); - TileWood wood = getWoodTile(world, x, y, z); + TileWood wood = TileWood.getWoodTile(world, x, y, z); EnumWoodType type = wood.getWoodType(); if (type == null) { return getIcon(side, meta); diff --git a/src/main/java/forestry/arboriculture/blocks/BlockPlanks.java b/src/main/java/forestry/arboriculture/blocks/BlockPlanks.java index cb76c1f2f..df7a14dc4 100644 --- a/src/main/java/forestry/arboriculture/blocks/BlockPlanks.java +++ b/src/main/java/forestry/arboriculture/blocks/BlockPlanks.java @@ -50,7 +50,7 @@ public class BlockPlanks extends BlockWood { @Override public IIcon getIcon(IBlockAccess world, int x, int y, int z, int side) { - TileWood wood = getWoodTile(world, x, y, z); + TileWood wood = TileWood.getWoodTile(world, x, y, z); EnumWoodType woodType = wood.getWoodType(); return IconProviderWood.getPlankIcon(woodType); } diff --git a/src/main/java/forestry/arboriculture/blocks/BlockSlab.java b/src/main/java/forestry/arboriculture/blocks/BlockSlab.java index 4358c0e18..e578a0859 100644 --- a/src/main/java/forestry/arboriculture/blocks/BlockSlab.java +++ b/src/main/java/forestry/arboriculture/blocks/BlockSlab.java @@ -85,7 +85,7 @@ public class BlockSlab extends net.minecraft.block.BlockSlab implements IWoodTyp @Override public IIcon getIcon(IBlockAccess world, int x, int y, int z, int side) { - TileWood wood = BlockWood.getWoodTile(world, x, y, z); + TileWood wood = TileWood.getWoodTile(world, x, y, z); EnumWoodType woodType = wood.getWoodType(); return IconProviderWood.getPlankIcon(woodType); } @@ -122,30 +122,37 @@ public class BlockSlab extends net.minecraft.block.BlockSlab implements IWoodTyp @Override public final ItemStack getPickBlock(MovingObjectPosition target, World world, int x, int y, int z) { ItemStack itemStack = new ItemStack(this); - NBTTagCompound nbt = BlockWood.getTagCompound(world, x, y, z); + NBTTagCompound nbt = TileWood.getTagCompound(world, x, y, z); itemStack.setTagCompound(nbt); return itemStack; } - @Override - public boolean removedByPlayer(World world, EntityPlayer player, int x, int y, int z) { - return TileWood.blockRemovedByPlayer(this, world, player, x, y, z); - } + /* DROP HANDLING */ + // Hack: When harvesting we need to get the drops in onBlockHarvested, + // because Mojang destroys the block and tile before calling getDrops. + private final ThreadLocal> drops = new ThreadLocal<>(); @Override - public void breakBlock(World world, int x, int y, int z, Block block, int meta) { - world.removeTileEntity(x, y, z); - super.breakBlock(world, x, y, z, block, meta); + public void onBlockHarvested(World world, int x, int y, int z, int meta, EntityPlayer playerProfile) { + drops.set(TileWood.getDrops(this, world, x, y, z)); } @Override public ArrayList getDrops(World world, int x, int y, int z, int metadata, int fortune) { - return new ArrayList<>(); + ArrayList ret = drops.get(); + drops.remove(); + + // not harvested, get drops normally + if (ret == null) { + ret = TileWood.getDrops(this, world, x, y, z); + } + + return ret; } @Override public final float getBlockHardness(World world, int x, int y, int z) { - TileWood wood = BlockWood.getWoodTile(world, x, y, z); + TileWood wood = TileWood.getWoodTile(world, x, y, z); if (wood == null) { return EnumWoodType.DEFAULT_HARDNESS; } diff --git a/src/main/java/forestry/arboriculture/blocks/BlockWood.java b/src/main/java/forestry/arboriculture/blocks/BlockWood.java index f0e7ca861..6b12fe9c9 100644 --- a/src/main/java/forestry/arboriculture/blocks/BlockWood.java +++ b/src/main/java/forestry/arboriculture/blocks/BlockWood.java @@ -37,7 +37,6 @@ import forestry.arboriculture.IWoodTyped; import forestry.arboriculture.render.IconProviderWood; import forestry.arboriculture.tiles.TileWood; import forestry.core.render.ParticleHelper; -import forestry.core.tiles.TileUtil; public abstract class BlockWood extends Block implements ITileEntityProvider, IWoodTyped { @@ -66,21 +65,6 @@ public abstract class BlockWood extends Block implements ITileEntityProvider, IW return fireproof; } - public static TileWood getWoodTile(IBlockAccess world, int x, int y, int z) { - return TileUtil.getTile(world, x, y, z, TileWood.class); - } - - protected static NBTTagCompound getTagCompound(IBlockAccess world, int x, int y, int z) { - TileWood wood = getWoodTile(world, x, y, z); - NBTTagCompound nbttagcompound = new NBTTagCompound(); - if (wood == null) { - return nbttagcompound; - } - EnumWoodType woodType = wood.getWoodType(); - woodType.saveToCompound(nbttagcompound); - return nbttagcompound; - } - @SideOnly(Side.CLIENT) @Override public final void registerBlockIcons(IIconRegister register) { @@ -95,30 +79,37 @@ public abstract class BlockWood extends Block implements ITileEntityProvider, IW @Override public final ItemStack getPickBlock(MovingObjectPosition target, World world, int x, int y, int z) { ItemStack itemStack = new ItemStack(this); - NBTTagCompound nbt = getTagCompound(world, x, y, z); + NBTTagCompound nbt = TileWood.getTagCompound(world, x, y, z); itemStack.setTagCompound(nbt); return itemStack; } - @Override - public boolean removedByPlayer(World world, EntityPlayer player, int x, int y, int z) { - return TileWood.blockRemovedByPlayer(this, world, player, x, y, z); - } + /* DROP HANDLING */ + // Hack: When harvesting we need to get the drops in onBlockHarvested, + // because Mojang destroys the block and tile before calling getDrops. + private final ThreadLocal> drops = new ThreadLocal<>(); @Override - public void breakBlock(World world, int x, int y, int z, Block block, int meta) { - world.removeTileEntity(x, y, z); - super.breakBlock(world, x, y, z, block, meta); + public void onBlockHarvested(World world, int x, int y, int z, int meta, EntityPlayer playerProfile) { + drops.set(TileWood.getDrops(this, world, x, y, z)); } @Override public ArrayList getDrops(World world, int x, int y, int z, int metadata, int fortune) { - return new ArrayList<>(); + ArrayList ret = drops.get(); + drops.remove(); + + // not harvested, get drops normally + if (ret == null) { + ret = TileWood.getDrops(this, world, x, y, z); + } + + return ret; } @Override public final float getBlockHardness(World world, int x, int y, int z) { - TileWood wood = getWoodTile(world, x, y, z); + TileWood wood = TileWood.getWoodTile(world, x, y, z); if (wood == null) { return EnumWoodType.DEFAULT_HARDNESS; } diff --git a/src/main/java/forestry/arboriculture/tiles/TileWood.java b/src/main/java/forestry/arboriculture/tiles/TileWood.java index fd0441382..a1864093b 100644 --- a/src/main/java/forestry/arboriculture/tiles/TileWood.java +++ b/src/main/java/forestry/arboriculture/tiles/TileWood.java @@ -11,13 +11,14 @@ package forestry.arboriculture.tiles; import java.io.IOException; +import java.util.ArrayList; import net.minecraft.block.Block; -import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.Packet; import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import forestry.api.arboriculture.EnumWoodType; @@ -26,7 +27,7 @@ import forestry.core.network.DataInputStreamForestry; import forestry.core.network.DataOutputStreamForestry; import forestry.core.network.IStreamable; import forestry.core.network.PacketTileStream; -import forestry.core.utils.ItemStackUtil; +import forestry.core.tiles.TileUtil; public class TileWood extends TileEntity implements IStreamable { private EnumWoodType woodType; @@ -84,23 +85,33 @@ public class TileWood extends TileEntity implements IStreamable { } } - public static boolean blockRemovedByPlayer(T block, World world, EntityPlayer player, int x, int y, int z) { - int meta = world.getBlockMetadata(x, y, z); - if (!world.isRemote && block.canHarvestBlock(player, meta) && !player.capabilities.isCreativeMode) { - - TileEntity tile = world.getTileEntity(x, y, z); - if (tile instanceof TileWood) { - TileWood wood = (TileWood) tile; - - ItemStack stack = new ItemStack(block); - NBTTagCompound compound = new NBTTagCompound(); - wood.getWoodType().saveToCompound(compound); - stack.setTagCompound(compound); - ItemStackUtil.dropItemStackAsEntity(stack, world, x, y, z); - } + public static NBTTagCompound getTagCompound(IBlockAccess world, int x, int y, int z) { + TileWood wood = getWoodTile(world, x, y, z); + NBTTagCompound nbttagcompound = new NBTTagCompound(); + if (wood == null) { + return nbttagcompound; } - - return world.setBlockToAir(x, y, z); + EnumWoodType woodType = wood.getWoodType(); + woodType.saveToCompound(nbttagcompound); + return nbttagcompound; } + public static TileWood getWoodTile(IBlockAccess world, int x, int y, int z) { + return TileUtil.getTile(world, x, y, z, TileWood.class); + } + + public static ArrayList getDrops(T block, World world, int x, int y, int z) { + ArrayList drops = new ArrayList<>(); + + TileWood wood = getWoodTile(world, x, y, z); + if (wood != null) { + ItemStack stack = new ItemStack(block); + NBTTagCompound compound = new NBTTagCompound(); + wood.getWoodType().saveToCompound(compound); + stack.setTagCompound(compound); + drops.add(stack); + } + + return drops; + } }