diff --git a/src/main/java/mekanism/common/block/BlockBasic.java b/src/main/java/mekanism/common/block/BlockBasic.java index 559ca413e..2dc31e4b4 100644 --- a/src/main/java/mekanism/common/block/BlockBasic.java +++ b/src/main/java/mekanism/common/block/BlockBasic.java @@ -1,6 +1,5 @@ package mekanism.common.block; -import java.util.Random; import java.util.UUID; import javax.annotation.Nonnull; import mekanism.api.Coord4D; @@ -8,7 +7,6 @@ import mekanism.api.IMekWrench; import mekanism.api.energy.IEnergizedItem; import mekanism.api.energy.IStrictEnergyStorage; import mekanism.common.Mekanism; -import mekanism.common.tier.BaseTier; import mekanism.common.base.IActiveState; import mekanism.common.base.IBoundingBlock; import mekanism.common.base.ITierItem; @@ -23,6 +21,7 @@ import mekanism.common.inventory.InventoryBin; import mekanism.common.item.ItemBlockBasic; import mekanism.common.multiblock.IMultiblock; import mekanism.common.multiblock.IStructuralMultiblock; +import mekanism.common.tier.BaseTier; import mekanism.common.tile.TileEntityBin; import mekanism.common.tile.TileEntityDynamicTank; import mekanism.common.tile.TileEntityInductionCell; @@ -49,9 +48,7 @@ import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.init.Items; import net.minecraft.init.SoundEvents; -import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.BlockRenderLayer; @@ -85,7 +82,7 @@ import net.minecraftforge.fml.relauncher.SideOnly; * * @author AidanBrady */ -public abstract class BlockBasic extends Block { +public abstract class BlockBasic extends BlockTileDrops { public BlockBasic() { super(Material.IRON); @@ -466,7 +463,7 @@ public abstract class BlockBasic extends Block { wrenchHandler.wrenchUsed(entityplayer, hand, stack, raytrace); if (entityplayer.isSneaking()) { - dismantleBlock(state, world, pos, false); + MekanismUtils.dismantleBlock(this, state, world, pos); return true; } @@ -686,8 +683,7 @@ public abstract class BlockBasic extends Block { @Nonnull @Override - public ItemStack getPickBlock(@Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, - @Nonnull BlockPos pos, EntityPlayer player) { + protected ItemStack getDropItem(@Nonnull IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos) { BasicBlockType type = BasicBlockType.get(state); ItemStack ret = new ItemStack(this, 1, state.getBlock().getMetaFromState(state)); @@ -719,50 +715,6 @@ public abstract class BlockBasic extends Block { return ret; } - @Nonnull - @Override - public Item getItemDropped(IBlockState state, Random random, int fortune) { - return Items.AIR; - } - - @Override - public boolean removedByPlayer(@Nonnull IBlockState state, World world, @Nonnull BlockPos pos, - @Nonnull EntityPlayer player, boolean willHarvest) { - if (!player.capabilities.isCreativeMode && !world.isRemote && willHarvest) { - float motion = 0.7F; - double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - - EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, getPickBlock(state, null, world, pos, player)); - - world.spawnEntity(entityItem); - } - - return world.setBlockToAir(pos); - } - - public ItemStack dismantleBlock(IBlockState state, World world, BlockPos pos, boolean returnBlock) { - ItemStack itemStack = getPickBlock(state, null, world, pos, null); - - world.setBlockToAir(pos); - - if (!returnBlock) { - float motion = 0.7F; - double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - - EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, itemStack); - - world.spawnEntity(entityItem); - } - - return itemStack; - } - @Override @Deprecated @SideOnly(Side.CLIENT) diff --git a/src/main/java/mekanism/common/block/BlockBounding.java b/src/main/java/mekanism/common/block/BlockBounding.java index 9fc6a2a92..e6b184547 100644 --- a/src/main/java/mekanism/common/block/BlockBounding.java +++ b/src/main/java/mekanism/common/block/BlockBounding.java @@ -1,31 +1,31 @@ package mekanism.common.block; -import java.util.Random; import javax.annotation.Nonnull; import javax.annotation.Nullable; import mekanism.common.block.states.BlockStateBounding; import mekanism.common.tile.TileEntityAdvancedBoundingBlock; import mekanism.common.tile.TileEntityBoundingBlock; import net.minecraft.block.Block; +import net.minecraft.block.BlockFlowerPot; import net.minecraft.block.material.Material; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Items; -import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumBlockRenderType; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; +import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.RayTraceResult; +import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; public class BlockBounding extends Block { @Nullable - private static BlockPos getMainBlockPos(World world, BlockPos thisPos) { + private static BlockPos getMainBlockPos(IBlockAccess world, BlockPos thisPos) { TileEntity te = world.getTileEntity(thisPos); if (te instanceof TileEntityBoundingBlock && !thisPos.equals(((TileEntityBoundingBlock) te).mainPos)) { return ((TileEntityBoundingBlock) te).mainPos; @@ -33,6 +33,20 @@ public class BlockBounding extends Block { return null; } + /** + * Removes the main block if it is not already air. + */ + private static void removeMainBlock(World world, BlockPos thisPos) { + BlockPos mainPos = getMainBlockPos(world, thisPos); + if (mainPos != null) { + IBlockState state = world.getBlockState(mainPos); + if (!state.getBlock().isAir(state, world, mainPos)) { + //Set the main block to air, which will invalidate the rest of the bounding blocks + world.setBlockToAir(mainPos); + } + } + } + public BlockBounding() { super(Material.IRON); setHardness(3.5F); @@ -70,10 +84,16 @@ public class BlockBounding extends Block { @Override public void breakBlock(@Nonnull World world, @Nonnull BlockPos pos, @Nonnull IBlockState state) { + //Remove the main block if a bounding block gets broken by being directly replaced + removeMainBlock(world, pos); super.breakBlock(world, pos, state); world.removeTileEntity(pos); } + /** + * {@inheritDoc} Delegate to main {@link Block#getPickBlock(IBlockState, RayTraceResult, World, BlockPos, + * EntityPlayer)}. + */ @Nonnull @Override public ItemStack getPickBlock(@Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, @@ -86,15 +106,63 @@ public class BlockBounding extends Block { return state1.getBlock().getPickBlock(state1, target, world, mainPos, player); } + /** + * {@inheritDoc} Keep tile entity in world until after {@link Block#getDrops(NonNullList, IBlockAccess, BlockPos, + * IBlockState, int)}. Used together with {@link Block#harvestBlock(World, EntityPlayer, BlockPos, IBlockState, + * TileEntity, ItemStack)}. + * + * @author Forge + * @see BlockFlowerPot#removedByPlayer(IBlockState, World, BlockPos, EntityPlayer, boolean) + */ @Override public boolean removedByPlayer(@Nonnull IBlockState state, World world, @Nonnull BlockPos pos, @Nonnull EntityPlayer player, boolean willHarvest) { + if (willHarvest) { + return true; + } + removeMainBlock(world, pos); + return super.removedByPlayer(state, world, pos, player, false); + } + + /** + * {@inheritDoc} Delegate to main {@link Block#getDrops(NonNullList, IBlockAccess, BlockPos, IBlockState, int)}. + */ + @Override + public void getDrops(@Nonnull NonNullList drops, IBlockAccess world, BlockPos pos, + @Nonnull IBlockState state, int fortune) { BlockPos mainPos = getMainBlockPos(world, pos); if (mainPos == null) { - return false; + return; } IBlockState state1 = world.getBlockState(mainPos); - return state1.getBlock().removedByPlayer(state1, world, mainPos, player, willHarvest); + state1.getBlock().getDrops(drops, world, mainPos, state1, fortune); + } + + /** + * {@inheritDoc} Used together with {@link Block#removedByPlayer(IBlockState, World, BlockPos, EntityPlayer, + * boolean)}. + * + * @author Forge + * @see BlockFlowerPot#harvestBlock(World, EntityPlayer, BlockPos, IBlockState, TileEntity, ItemStack) + */ + @Override + public void harvestBlock(@Nonnull World world, EntityPlayer player, @Nonnull BlockPos pos, + @Nonnull IBlockState state, TileEntity te, ItemStack stack) { + super.harvestBlock(world, player, pos, state, te, stack); + world.setBlockToAir(pos); + } + + /** + * Returns that this "cannot" be silk touched. This is so that {@link Block#getSilkTouchDrop(IBlockState)} is not + * called, because only {@link Block#getDrops(NonNullList, IBlockAccess, BlockPos, IBlockState, int)} supports tile + * entities. Our blocks keep their inventory and other behave like they are being silk touched by default anyway. + * + * @return false + */ + @Override + @Deprecated + protected boolean canSilkHarvest() { + return false; } @Override @@ -124,17 +192,6 @@ public class BlockBounding extends Block { return state1.getBlock().getPlayerRelativeBlockHardness(state1, player, world, mainPos); } - @Override - public int quantityDropped(Random random) { - return 0; - } - - @Nonnull - @Override - public Item getItemDropped(IBlockState state, Random random, int fortune) { - return Items.AIR; - } - @Nonnull @Override @Deprecated diff --git a/src/main/java/mekanism/common/block/BlockCardboardBox.java b/src/main/java/mekanism/common/block/BlockCardboardBox.java index 6155274ba..d61e030b9 100644 --- a/src/main/java/mekanism/common/block/BlockCardboardBox.java +++ b/src/main/java/mekanism/common/block/BlockCardboardBox.java @@ -2,7 +2,6 @@ package mekanism.common.block; import static mekanism.common.block.states.BlockStateCardboardBox.storageProperty; -import java.util.Random; import javax.annotation.Nonnull; import mekanism.common.Mekanism; import mekanism.common.MekanismBlocks; @@ -10,13 +9,10 @@ import mekanism.common.block.states.BlockStateCardboardBox; import mekanism.common.item.ItemBlockCardboardBox; import mekanism.common.tile.TileEntityCardboardBox; import net.minecraft.block.Block; -import net.minecraft.block.BlockContainer; import net.minecraft.block.material.Material; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; -import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Items; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -25,7 +21,6 @@ import net.minecraft.util.EnumBlockRenderType; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.RayTraceResult; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import net.minecraftforge.common.MinecraftForge; @@ -33,7 +28,7 @@ import net.minecraftforge.event.entity.player.PlayerInteractEvent; import net.minecraftforge.fml.common.eventhandler.Event; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; -public class BlockCardboardBox extends BlockContainer { +public class BlockCardboardBox extends BlockMekanismContainer { private static boolean testingPlace = false; @@ -72,7 +67,6 @@ public class BlockCardboardBox extends BlockContainer { public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer entityplayer, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) { if (!world.isRemote && entityplayer.isSneaking()) { - ItemStack itemStack = new ItemStack(MekanismBlocks.CardboardBox); TileEntityCardboardBox tileEntity = (TileEntityCardboardBox) world.getTileEntity(pos); if (tileEntity.storedData != null) { @@ -105,15 +99,7 @@ public class BlockCardboardBox extends BlockContainer { new ItemStack(data.block, 1, data.meta)); } - float motion = 0.7F; - double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - - EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, itemStack); - - world.spawnEntity(entityItem); + spawnAsEntity(world, pos, new ItemStack(MekanismBlocks.CardboardBox)); } } @@ -132,72 +118,21 @@ public class BlockCardboardBox extends BlockContainer { return new TileEntityCardboardBox(); } - public ItemStack dismantleBlock(IBlockState state, World world, BlockPos pos, boolean returnBlock) { - ItemStack itemStack = getPickBlock(state, null, world, pos, null); - - world.setBlockToAir(pos); - - if (!returnBlock) { - float motion = 0.7F; - double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - - EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, itemStack); - - world.spawnEntity(entityItem); - } - - return itemStack; - } - @Nonnull @Override - public ItemStack getPickBlock(@Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, - @Nonnull BlockPos pos, EntityPlayer player) { + protected ItemStack getDropItem(@Nonnull IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos) { TileEntityCardboardBox tileEntity = (TileEntityCardboardBox) world.getTileEntity(pos); - ItemStack itemStack = new ItemStack(state.getBlock(), 1, state.getBlock().getMetaFromState(state)); + Item item = Item.getItemFromBlock(state.getBlock()); + ItemStack itemStack = new ItemStack(item, 1, state.getBlock().getMetaFromState(state)); - if (itemStack.getItemDamage() == 1) { - if (tileEntity.storedData != null) { - ((ItemBlockCardboardBox) itemStack.getItem()).setBlockData(itemStack, tileEntity.storedData); - } + if (tileEntity.storedData != null) { + ((ItemBlockCardboardBox) item).setBlockData(itemStack, tileEntity.storedData); } return itemStack; } - @Override - public boolean removedByPlayer(@Nonnull IBlockState state, World world, @Nonnull BlockPos pos, - @Nonnull EntityPlayer player, boolean willHarvest) { - if (!player.capabilities.isCreativeMode && !world.isRemote && willHarvest) { - float motion = 0.7F; - double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - - EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, getPickBlock(state, null, world, pos, player)); - - world.spawnEntity(entityItem); - } - - return world.setBlockToAir(pos); - } - - @Override - public int quantityDropped(Random random) { - return 0; - } - - @Nonnull - @Override - public Item getItemDropped(IBlockState state, Random random, int fortune) { - return Items.AIR; - } - /** * If the player is sneaking and the dest block is a cardboard box, ensure onBlockActivated is called, and that the * item use is not. diff --git a/src/main/java/mekanism/common/block/BlockEnergyCube.java b/src/main/java/mekanism/common/block/BlockEnergyCube.java index 5a02921fd..6c9701fdb 100644 --- a/src/main/java/mekanism/common/block/BlockEnergyCube.java +++ b/src/main/java/mekanism/common/block/BlockEnergyCube.java @@ -1,12 +1,10 @@ package mekanism.common.block; -import java.util.Random; import javax.annotation.Nonnull; import mekanism.api.IMekWrench; import mekanism.api.energy.IEnergizedItem; import mekanism.common.Mekanism; import mekanism.common.MekanismBlocks; -import mekanism.common.tier.EnergyCubeTier; import mekanism.common.base.ISideConfiguration; import mekanism.common.base.ISustainedInventory; import mekanism.common.base.ITierItem; @@ -16,22 +14,19 @@ import mekanism.common.integration.wrenches.Wrenches; import mekanism.common.item.ItemBlockEnergyCube; import mekanism.common.security.ISecurityItem; import mekanism.common.security.ISecurityTile; +import mekanism.common.tier.EnergyCubeTier; import mekanism.common.tile.TileEntityEnergyCube; import mekanism.common.tile.prefab.TileEntityBasicBlock; import mekanism.common.util.ItemDataUtils; import mekanism.common.util.MekanismUtils; import mekanism.common.util.SecurityUtils; import net.minecraft.block.Block; -import net.minecraft.block.BlockContainer; import net.minecraft.block.material.Material; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.EntityLivingBase; -import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Items; -import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; @@ -52,7 +47,7 @@ import net.minecraft.world.World; * * @author AidanBrady */ -public class BlockEnergyCube extends BlockContainer { +public class BlockEnergyCube extends BlockMekanismContainer { public BlockEnergyCube() { super(Material.IRON); @@ -146,17 +141,6 @@ public class BlockEnergyCube extends BlockContainer { tileEntity.redstone = world.getRedstonePowerFromNeighbors(pos) > 0; } - @Override - public int quantityDropped(Random random) { - return 0; - } - - @Nonnull - @Override - public Item getItemDropped(IBlockState state, Random random, int fortune) { - return Items.AIR; - } - @Override public void getSubBlocks(CreativeTabs creativetabs, NonNullList list) { for (EnergyCubeTier tier : EnergyCubeTier.values()) { @@ -200,8 +184,7 @@ public class BlockEnergyCube extends BlockContainer { wrenchHandler.wrenchUsed(entityplayer, hand, stack, raytrace); if (entityplayer.isSneaking()) { - dismantleBlock(state, world, pos, false); - + MekanismUtils.dismantleBlock(this, state, world, pos); return true; } @@ -235,23 +218,6 @@ public class BlockEnergyCube extends BlockContainer { return false; } - @Override - public boolean removedByPlayer(@Nonnull IBlockState state, World world, @Nonnull BlockPos pos, - @Nonnull EntityPlayer player, boolean willHarvest) { - if (!player.capabilities.isCreativeMode && !world.isRemote && willHarvest) { - float motion = 0.7F; - double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - - EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, getPickBlock(state, null, world, pos, player)); - world.spawnEntity(entityItem); - } - - return world.setBlockToAir(pos); - } - @Override public TileEntity createNewTileEntity(@Nonnull World world, int meta) { return new TileEntityEnergyCube(); @@ -272,8 +238,7 @@ public class BlockEnergyCube extends BlockContainer { @Nonnull @Override - public ItemStack getPickBlock(@Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, - @Nonnull BlockPos pos, EntityPlayer player) { + protected ItemStack getDropItem(@Nonnull IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos) { TileEntityEnergyCube tileEntity = (TileEntityEnergyCube) world.getTileEntity(pos); ItemStack itemStack = new ItemStack(MekanismBlocks.EnergyCube); @@ -307,26 +272,6 @@ public class BlockEnergyCube extends BlockContainer { return itemStack; } - public ItemStack dismantleBlock(IBlockState state, World world, BlockPos pos, boolean returnBlock) { - ItemStack itemStack = getPickBlock(state, null, world, pos, null); - - world.setBlockToAir(pos); - - if (!returnBlock) { - float motion = 0.7F; - double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - - EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, itemStack); - - world.spawnEntity(entityItem); - } - - return itemStack; - } - @Override @Deprecated public boolean hasComparatorInputOverride(IBlockState state) { diff --git a/src/main/java/mekanism/common/block/BlockGasTank.java b/src/main/java/mekanism/common/block/BlockGasTank.java index 8dfbc8400..4803fd309 100644 --- a/src/main/java/mekanism/common/block/BlockGasTank.java +++ b/src/main/java/mekanism/common/block/BlockGasTank.java @@ -1,6 +1,5 @@ package mekanism.common.block; -import java.util.Random; import javax.annotation.Nonnull; import mekanism.api.IMekWrench; import mekanism.api.gas.IGasItem; @@ -20,15 +19,11 @@ import mekanism.common.util.ItemDataUtils; import mekanism.common.util.MekanismUtils; import mekanism.common.util.SecurityUtils; import net.minecraft.block.Block; -import net.minecraft.block.BlockContainer; import net.minecraft.block.material.Material; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.EntityLivingBase; -import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Items; -import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; @@ -43,7 +38,7 @@ import net.minecraft.util.math.Vec3d; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; -public class BlockGasTank extends BlockContainer { +public class BlockGasTank extends BlockMekanismContainer { private static final AxisAlignedBB TANK_BOUNDS = new AxisAlignedBB(0.1875F, 0.0F, 0.1875F, 0.8125F, 1.0F, 0.8125F); @@ -162,8 +157,7 @@ public class BlockGasTank extends BlockContainer { wrenchHandler.wrenchUsed(entityplayer, hand, stack, raytrace); if (entityplayer.isSneaking()) { - dismantleBlock(state, world, pos, false); - + MekanismUtils.dismantleBlock(this, state, world, pos); return true; } @@ -197,55 +191,6 @@ public class BlockGasTank extends BlockContainer { return false; } - @Override - public boolean removedByPlayer(@Nonnull IBlockState state, World world, @Nonnull BlockPos pos, - @Nonnull EntityPlayer player, boolean willHarvest) { - if (!player.capabilities.isCreativeMode && !world.isRemote && willHarvest) { - float motion = 0.7F; - double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - - EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, getPickBlock(state, null, world, pos, player)); - - world.spawnEntity(entityItem); - } - - return world.setBlockToAir(pos); - } - - public ItemStack dismantleBlock(IBlockState state, World world, BlockPos pos, boolean returnBlock) { - ItemStack itemStack = getPickBlock(state, null, world, pos, null); - - world.setBlockToAir(pos); - - if (!returnBlock) { - float motion = 0.7F; - double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - - EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, itemStack); - - world.spawnEntity(entityItem); - } - - return itemStack; - } - - @Override - public int quantityDropped(Random random) { - return 0; - } - - @Nonnull - @Override - public Item getItemDropped(IBlockState state, Random random, int fortune) { - return Items.AIR; - } - @Override @Deprecated public boolean isOpaqueCube(IBlockState state) { @@ -273,8 +218,7 @@ public class BlockGasTank extends BlockContainer { @Nonnull @Override - public ItemStack getPickBlock(@Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, - @Nonnull BlockPos pos, EntityPlayer player) { + protected ItemStack getDropItem(@Nonnull IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos) { TileEntityGasTank tileEntity = (TileEntityGasTank) world.getTileEntity(pos); ItemStack itemStack = new ItemStack(MekanismBlocks.GasTank); diff --git a/src/main/java/mekanism/common/block/BlockGlowPanel.java b/src/main/java/mekanism/common/block/BlockGlowPanel.java index 56709396c..5d19aa7de 100644 --- a/src/main/java/mekanism/common/block/BlockGlowPanel.java +++ b/src/main/java/mekanism/common/block/BlockGlowPanel.java @@ -1,6 +1,5 @@ package mekanism.common.block; -import java.util.Random; import javax.annotation.Nonnull; import mekanism.api.Coord4D; import mekanism.common.Mekanism; @@ -17,8 +16,6 @@ import net.minecraft.block.ITileEntityProvider; import net.minecraft.block.material.Material; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; -import net.minecraft.entity.item.EntityItem; -import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.BlockRenderLayer; @@ -26,7 +23,6 @@ import net.minecraft.util.EnumBlockRenderType; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.RayTraceResult; import net.minecraft.util.math.Vec3d; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; @@ -34,10 +30,9 @@ import net.minecraftforge.common.property.IExtendedBlockState; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; -public class BlockGlowPanel extends Block implements ITileEntityProvider { +public class BlockGlowPanel extends BlockTileDrops implements ITileEntityProvider { public static AxisAlignedBB[] bounds = new AxisAlignedBB[6]; - private static Random rand = new Random(); static { AxisAlignedBB cuboid = new AxisAlignedBB(0.25, 0, 0.25, 0.75, 0.125, 0.75); @@ -134,39 +129,11 @@ public class BlockGlowPanel extends Block implements ITileEntityProvider { TileEntityGlowPanel tileEntity = getTileEntityGlowPanel(world, pos); if (tileEntity != null && !world.isRemote && !canStay(world, pos)) { - float motion = 0.7F; - double motionX = (rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - - ItemStack stack = new ItemStack(MekanismBlocks.GlowPanel, 1, tileEntity.colour.getMetaValue()); - EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, stack); - - world.spawnEntity(entityItem); + dropBlockAsItem(world, pos, world.getBlockState(pos), 0); world.setBlockToAir(pos); } } - @Override - public void onNeighborChange(IBlockAccess world, BlockPos pos, BlockPos neighbor) { - TileEntityGlowPanel tileEntity = getTileEntityGlowPanel(world, pos); - - if (tileEntity != null && !tileEntity.getWorld().isRemote && !canStay(world, pos)) { - float motion = 0.7F; - double motionX = (rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - - ItemStack stack = new ItemStack(MekanismBlocks.GlowPanel, 1, tileEntity.colour.getMetaValue()); - EntityItem entityItem = new EntityItem(tileEntity.getWorld(), pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, stack); - - tileEntity.getWorld().spawnEntity(entityItem); - tileEntity.getWorld().setBlockToAir(pos); - } - } - @Nonnull @Override @Deprecated @@ -190,37 +157,13 @@ public class BlockGlowPanel extends Block implements ITileEntityProvider { return 15; } - @Override - public int quantityDropped(Random random) { - return 0; - } - @Nonnull @Override - public ItemStack getPickBlock(@Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, - @Nonnull BlockPos pos, EntityPlayer player) { + protected ItemStack getDropItem(@Nonnull IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos) { TileEntityGlowPanel tileEntity = (TileEntityGlowPanel) world.getTileEntity(pos); return new ItemStack(MekanismBlocks.GlowPanel, 1, tileEntity.colour.getMetaValue()); } - @Override - public boolean removedByPlayer(@Nonnull IBlockState state, World world, @Nonnull BlockPos pos, - @Nonnull EntityPlayer player, boolean willHarvest) { - if (!player.capabilities.isCreativeMode && !world.isRemote && willHarvest) { - float motion = 0.7F; - double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - - EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, getPickBlock(state, null, world, pos, player)); - - world.spawnEntity(entityItem); - } - - return super.removedByPlayer(state, world, pos, player, willHarvest); - } - @Override public TileEntity createNewTileEntity(@Nonnull World worldIn, int meta) { return new TileEntityGlowPanel(); diff --git a/src/main/java/mekanism/common/block/BlockMachine.java b/src/main/java/mekanism/common/block/BlockMachine.java index 95e728cb1..a8339d892 100644 --- a/src/main/java/mekanism/common/block/BlockMachine.java +++ b/src/main/java/mekanism/common/block/BlockMachine.java @@ -51,7 +51,6 @@ import mekanism.common.util.PipeUtils; import mekanism.common.util.SecurityUtils; import mekanism.common.util.StackUtils; import net.minecraft.block.Block; -import net.minecraft.block.BlockContainer; import net.minecraft.block.material.Material; import net.minecraft.block.properties.PropertyEnum; import net.minecraft.block.state.BlockStateContainer; @@ -60,10 +59,8 @@ import net.minecraft.client.particle.ParticleManager; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; -import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.init.Items; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -102,7 +99,7 @@ import net.minecraftforge.fml.relauncher.SideOnly; * * @author AidanBrady */ -public abstract class BlockMachine extends BlockContainer { +public abstract class BlockMachine extends BlockMekanismContainer { private static final AxisAlignedBB CHARGEPAD_BOUNDS = new AxisAlignedBB(0.0F, 0.0F, 0.0F, 1.0F, 0.06F, 1.0F); private static final AxisAlignedBB TANK_BOUNDS = new AxisAlignedBB(0.125F, 0.0F, 0.125F, 0.875F, 1.0F, 0.875F); @@ -372,8 +369,7 @@ public abstract class BlockMachine extends BlockContainer { wrenchHandler.wrenchUsed(entityplayer, hand, stack, raytrace); if (entityplayer.isSneaking()) { - dismantleBlock(state, world, pos, false); - + MekanismUtils.dismantleBlock(this, state, world, pos); return true; } @@ -498,12 +494,6 @@ public abstract class BlockMachine extends BlockContainer { return false; } - @Nonnull - @Override - public Item getItemDropped(IBlockState state, Random random, int fortune) { - return Items.AIR; - } - @Nonnull @Override @Deprecated @@ -539,26 +529,6 @@ public abstract class BlockMachine extends BlockContainer { } } - @Override - public boolean removedByPlayer(@Nonnull IBlockState state, World world, @Nonnull BlockPos pos, - @Nonnull EntityPlayer player, boolean willHarvest) { - if (!player.capabilities.isCreativeMode && !world.isRemote && willHarvest) { - TileEntityBasicBlock tileEntity = (TileEntityBasicBlock) world.getTileEntity(pos); - - float motion = 0.7F; - double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - - EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, getPickBlock(state, null, world, pos, player)); - - world.spawnEntity(entityItem); - } - - return world.setBlockToAir(pos); - } - @Override @Deprecated public boolean hasComparatorInputOverride(IBlockState state) { @@ -711,8 +681,7 @@ public abstract class BlockMachine extends BlockContainer { @Nonnull @Override - public ItemStack getPickBlock(@Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, - @Nonnull BlockPos pos, EntityPlayer player) { + protected ItemStack getDropItem(@Nonnull IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos) { TileEntityBasicBlock tileEntity = (TileEntityBasicBlock) world.getTileEntity(pos); ItemStack itemStack = new ItemStack(this, 1, state.getBlock().getMetaFromState(state)); @@ -814,26 +783,6 @@ public abstract class BlockMachine extends BlockContainer { return type == MachineType.LASER_AMPLIFIER; } - public ItemStack dismantleBlock(IBlockState state, World world, BlockPos pos, boolean returnBlock) { - ItemStack itemStack = getPickBlock(state, null, world, pos, null); - - world.setBlockToAir(pos); - - if (!returnBlock) { - float motion = 0.7F; - double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - - EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, itemStack); - - world.spawnEntity(entityItem); - } - - return itemStack; - } - @Nonnull @Override @Deprecated diff --git a/src/main/java/mekanism/common/block/BlockMekanismContainer.java b/src/main/java/mekanism/common/block/BlockMekanismContainer.java new file mode 100644 index 000000000..fcd567de9 --- /dev/null +++ b/src/main/java/mekanism/common/block/BlockMekanismContainer.java @@ -0,0 +1,97 @@ +package mekanism.common.block; + +import javax.annotation.Nonnull; +import net.minecraft.block.Block; +import net.minecraft.block.BlockContainer; +import net.minecraft.block.BlockFlowerPot; +import net.minecraft.block.material.Material; +import net.minecraft.block.state.IBlockState; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.stats.StatBase; +import net.minecraft.stats.StatList; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.NonNullList; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.RayTraceResult; +import net.minecraft.world.IBlockAccess; +import net.minecraft.world.IWorldNameable; +import net.minecraft.world.World; + +public abstract class BlockMekanismContainer extends BlockContainer { + + protected BlockMekanismContainer(Material materialIn) { + super(materialIn); + } + + @Nonnull + protected abstract ItemStack getDropItem(@Nonnull IBlockState state, @Nonnull IBlockAccess world, + @Nonnull BlockPos pos); + + /** + * {@inheritDoc} Used together with {@link Block#removedByPlayer(IBlockState, World, BlockPos, EntityPlayer, + * boolean)}. + *
+ * This is like Vanilla's {@link BlockContainer#harvestBlock(World, EntityPlayer, BlockPos, IBlockState, TileEntity, + * ItemStack)} except that uses the custom {@link ItemStack} from {@link #getDropItem(IBlockState, IBlockAccess, + * BlockPos)} + * + * @author Forge + * @see BlockFlowerPot#harvestBlock(World, EntityPlayer, BlockPos, IBlockState, TileEntity, ItemStack) + */ + @Override + public void harvestBlock(@Nonnull World world, EntityPlayer player, @Nonnull BlockPos pos, + @Nonnull IBlockState state, TileEntity te, @Nonnull ItemStack stack) { + StatBase blockStats = StatList.getBlockStats(this); + if (blockStats != null) { + player.addStat(blockStats); + } + player.addExhaustion(0.005F); + if (!world.isRemote) { + Block.spawnAsEntity(world, pos, + getDropItem(state, world, pos).setStackDisplayName(((IWorldNameable) te).getName())); + } + //Set it to air like the flower pot's harvestBlock method + world.setBlockToAir(pos); + } + + /** + * Returns that this "cannot" be silk touched. This is so that {@link Block#getSilkTouchDrop(IBlockState)} is not + * called, because only {@link Block#getDrops(NonNullList, IBlockAccess, BlockPos, IBlockState, int)} supports tile + * entities. Our blocks keep their inventory and other behave like they are being silk touched by default anyway. + * + * @return false + */ + @Override + @Deprecated + protected boolean canSilkHarvest() { + return false; + } + + @Override + public void getDrops(@Nonnull NonNullList drops, IBlockAccess world, BlockPos pos, + @Nonnull IBlockState state, int fortune) { + drops.add(getDropItem(state, world, pos)); + } + + /** + * {@inheritDoc} Keep tile entity in world until after {@link Block#getDrops(NonNullList, IBlockAccess, BlockPos, + * IBlockState, int)}. Used together with {@link Block#harvestBlock(World, EntityPlayer, BlockPos, IBlockState, + * TileEntity, ItemStack)}. + * + * @author Forge + * @see BlockFlowerPot#removedByPlayer(IBlockState, World, BlockPos, EntityPlayer, boolean) + */ + @Override + public boolean removedByPlayer(@Nonnull IBlockState state, World world, @Nonnull BlockPos pos, + @Nonnull EntityPlayer player, boolean willHarvest) { + return willHarvest || super.removedByPlayer(state, world, pos, player, false); + } + + @Nonnull + @Override + public ItemStack getPickBlock(@Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, + @Nonnull BlockPos pos, EntityPlayer player) { + return getDropItem(state, world, pos); + } +} \ No newline at end of file diff --git a/src/main/java/mekanism/common/block/BlockTileDrops.java b/src/main/java/mekanism/common/block/BlockTileDrops.java new file mode 100644 index 000000000..06fdd6e6b --- /dev/null +++ b/src/main/java/mekanism/common/block/BlockTileDrops.java @@ -0,0 +1,83 @@ +package mekanism.common.block; + +import javax.annotation.Nonnull; +import net.minecraft.block.Block; +import net.minecraft.block.BlockFlowerPot; +import net.minecraft.block.material.Material; +import net.minecraft.block.state.IBlockState; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.NonNullList; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.RayTraceResult; +import net.minecraft.world.IBlockAccess; +import net.minecraft.world.World; + +/** + * Special handling for block drops that need TileEntity data + */ +public abstract class BlockTileDrops extends Block { + + protected BlockTileDrops(Material materialIn) { + super(materialIn); + } + + @Nonnull + protected abstract ItemStack getDropItem(@Nonnull IBlockState state, @Nonnull IBlockAccess world, + @Nonnull BlockPos pos); + + /** + * {@inheritDoc} Used together with {@link Block#removedByPlayer(IBlockState, World, BlockPos, EntityPlayer, + * boolean)}. + * + * @author Forge + * @see BlockFlowerPot#harvestBlock(World, EntityPlayer, BlockPos, IBlockState, TileEntity, ItemStack) + */ + @Override + public void harvestBlock(@Nonnull World world, EntityPlayer player, @Nonnull BlockPos pos, + @Nonnull IBlockState state, TileEntity te, ItemStack stack) { + super.harvestBlock(world, player, pos, state, te, stack); + world.setBlockToAir(pos); + } + + /** + * Returns that this "cannot" be silk touched. This is so that {@link Block#getSilkTouchDrop(IBlockState)} is not + * called, because only {@link Block#getDrops(NonNullList, IBlockAccess, BlockPos, IBlockState, int)} supports tile + * entities. Our blocks keep their inventory and other behave like they are being silk touched by default anyway. + * + * @return false + */ + @Override + @Deprecated + protected boolean canSilkHarvest() { + return false; + } + + @Override + public void getDrops(@Nonnull NonNullList drops, IBlockAccess world, BlockPos pos, + @Nonnull IBlockState state, int fortune) { + drops.add(getDropItem(state, world, pos)); + } + + /** + * {@inheritDoc} Keep tile entity in world until after {@link Block#getDrops(NonNullList, IBlockAccess, BlockPos, + * IBlockState, int)}. Used together with {@link Block#harvestBlock(World, EntityPlayer, BlockPos, IBlockState, + * TileEntity, ItemStack)}. + * + * @author Forge + * @see BlockFlowerPot#removedByPlayer(IBlockState, World, BlockPos, EntityPlayer, boolean) + */ + @Override + public boolean removedByPlayer(@Nonnull IBlockState state, World world, @Nonnull BlockPos pos, + @Nonnull EntityPlayer player, boolean willHarvest) { + return willHarvest || super.removedByPlayer(state, world, pos, player, false); + } + + @Nonnull + @Override + public ItemStack getPickBlock(@Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, + @Nonnull BlockPos pos, EntityPlayer player) { + return getDropItem(state, world, pos); + } +} \ No newline at end of file diff --git a/src/main/java/mekanism/common/block/BlockTransmitter.java b/src/main/java/mekanism/common/block/BlockTransmitter.java index 09d96c8b3..697ebe7db 100644 --- a/src/main/java/mekanism/common/block/BlockTransmitter.java +++ b/src/main/java/mekanism/common/block/BlockTransmitter.java @@ -2,7 +2,6 @@ package mekanism.common.block; import java.util.Collections; import java.util.List; -import java.util.Random; import javax.annotation.Nonnull; import javax.annotation.Nullable; import mekanism.api.IMekWrench; @@ -38,7 +37,6 @@ import net.minecraft.client.particle.ParticleManager; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; -import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -61,7 +59,7 @@ import net.minecraftforge.common.property.IExtendedBlockState; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; -public class BlockTransmitter extends Block implements ITileEntityProvider { +public class BlockTransmitter extends BlockTileDrops implements ITileEntityProvider { public static AxisAlignedBB[] smallSides = new AxisAlignedBB[7]; public static AxisAlignedBB[] largeSides = new AxisAlignedBB[7]; @@ -259,7 +257,7 @@ public class BlockTransmitter extends Block implements ITileEntityProvider { RayTraceResult raytrace = new RayTraceResult(new Vec3d(hitX, hitY, hitZ), facing, pos); if (wrenchHandler.canUseWrench(player, hand, stack, raytrace) && player.isSneaking()) { if (!world.isRemote) { - dismantleBlock(state, world, pos, false); + MekanismUtils.dismantleBlock(this, state, world, pos); } return true; @@ -271,8 +269,7 @@ public class BlockTransmitter extends Block implements ITileEntityProvider { @Nonnull @Override - public ItemStack getPickBlock(@Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, - @Nonnull BlockPos pos, EntityPlayer player) { + protected ItemStack getDropItem(@Nonnull IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos) { ItemStack itemStack = ItemStack.EMPTY; TileEntitySidedPipe tileEntity = getTileEntitySidedPipe(world, pos); if (tileEntity != null) { @@ -299,26 +296,6 @@ public class BlockTransmitter extends Block implements ITileEntityProvider { super.addHitEffects(state, world, target, manager); } - public ItemStack dismantleBlock(IBlockState state, World world, BlockPos pos, boolean returnBlock) { - ItemStack itemStack = getPickBlock(state, null, world, pos, null); - - world.setBlockToAir(pos); - - if (!returnBlock) { - float motion = 0.7F; - double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - - EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, itemStack); - - world.spawnEntity(entityItem); - } - - return itemStack; - } - @Override public void getSubBlocks(CreativeTabs creativetabs, NonNullList list) { for (TransmitterType type : TransmitterType.values()) { @@ -407,29 +384,6 @@ public class BlockTransmitter extends Block implements ITileEntityProvider { return false; } - @Override - public int quantityDropped(Random random) { - return 0; - } - - @Override - public boolean removedByPlayer(@Nonnull IBlockState state, World world, @Nonnull BlockPos pos, - @Nonnull EntityPlayer player, boolean willHarvest) { - if (!player.capabilities.isCreativeMode && !world.isRemote && willHarvest) { - float motion = 0.7F; - double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - - EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, getPickBlock(state, null, world, pos, player)); - - world.spawnEntity(entityItem); - } - - return super.removedByPlayer(state, world, pos, player, willHarvest); - } - @Override public TileEntity createNewTileEntity(@Nonnull World worldIn, int meta) { TransmitterType type = TransmitterType.get(meta); diff --git a/src/main/java/mekanism/common/integration/multipart/MultipartGlowPanel.java b/src/main/java/mekanism/common/integration/multipart/MultipartGlowPanel.java index b2af421e7..b8e768df5 100644 --- a/src/main/java/mekanism/common/integration/multipart/MultipartGlowPanel.java +++ b/src/main/java/mekanism/common/integration/multipart/MultipartGlowPanel.java @@ -1,6 +1,5 @@ package mekanism.common.integration.multipart; -import java.util.Random; import mcmultipart.api.container.IPartInfo; import mcmultipart.api.multipart.IMultipart; import mcmultipart.api.slot.EnumFaceSlot; @@ -13,7 +12,6 @@ import mekanism.common.tile.TileEntityGlowPanel; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.EntityLivingBase; -import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; @@ -24,8 +22,6 @@ import net.minecraft.world.World; public class MultipartGlowPanel implements IMultipart { - private static Random rand = new Random(); - @Override public IPartSlot getSlotForPlacement(World world, BlockPos pos, IBlockState state, EnumFacing facing, float hitX, float hitY, float hitZ, EntityLivingBase placer) { @@ -54,18 +50,10 @@ public class MultipartGlowPanel implements IMultipart { public void onPartRemoved(IPartInfo part, IPartInfo otherPart) { TileEntity tile = part.getTile().getTileEntity(); if (tile instanceof TileEntityGlowPanel && !BlockGlowPanel.canStay(part.getPartWorld(), part.getPartPos())) { - float motion = 0.7F; - double motionX = (rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - BlockPos pos = part.getPartPos(); TileEntityGlowPanel glowPanel = (TileEntityGlowPanel) tile; ItemStack stack = new ItemStack(MekanismBlocks.GlowPanel, 1, glowPanel.colour.getMetaValue()); - EntityItem entityItem = new EntityItem(glowPanel.getWorld(), pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, stack); - - part.getActualWorld().spawnEntity(entityItem); + Block.spawnAsEntity(part.getActualWorld(), pos, stack); part.remove(); } } diff --git a/src/main/java/mekanism/common/item/ItemConfigurator.java b/src/main/java/mekanism/common/item/ItemConfigurator.java index 973501f4b..73c6e6b9a 100644 --- a/src/main/java/mekanism/common/item/ItemConfigurator.java +++ b/src/main/java/mekanism/common/item/ItemConfigurator.java @@ -34,7 +34,6 @@ import net.minecraft.block.Block; import net.minecraft.client.util.ITooltipFlag; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; -import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; @@ -163,22 +162,7 @@ public class ItemConfigurator extends ItemEnergized implements IMekWrench, ITool break; } - float xRandom = random.nextFloat() * 0.8F + 0.1F; - float yRandom = random.nextFloat() * 0.8F + 0.1F; - float zRandom = random.nextFloat() * 0.8F + 0.1F; - - EntityItem item = new EntityItem(world, pos.getX() + xRandom, pos.getY() + yRandom, - pos.getZ() + zRandom, slotStack.copy()); - - if (slotStack.hasTagCompound()) { - item.getItem().setTagCompound(slotStack.getTagCompound().copy()); - } - - float k = 0.05F; - item.motionX = random.nextGaussian() * k; - item.motionY = random.nextGaussian() * k + 0.2F; - item.motionZ = random.nextGaussian() * k; - world.spawnEntity(item); + Block.spawnAsEntity(world, pos, slotStack.copy()); inv.setInventorySlotContents(i, ItemStack.EMPTY); setEnergy(stack, getEnergy(stack) - ENERGY_PER_ITEM_DUMP); diff --git a/src/main/java/mekanism/common/tile/TileEntityLaserTractorBeam.java b/src/main/java/mekanism/common/tile/TileEntityLaserTractorBeam.java index 0822f2881..0c95f0f92 100644 --- a/src/main/java/mekanism/common/tile/TileEntityLaserTractorBeam.java +++ b/src/main/java/mekanism/common/tile/TileEntityLaserTractorBeam.java @@ -17,8 +17,8 @@ import mekanism.common.tile.component.TileComponentSecurity; import mekanism.common.tile.prefab.TileEntityContainerBlock; import mekanism.common.util.InventoryUtils; import mekanism.common.util.StackUtils; +import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; -import net.minecraft.entity.item.EntityItem; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; @@ -162,20 +162,10 @@ public class TileEntityLaserTractorBeam extends TileEntityContainerBlock impleme } } - dropItem(drop); + Block.spawnAsEntity(world, pos, drop); } } - public void dropItem(ItemStack stack) { - EntityItem item = new EntityItem(world, getPos().getX() + 0.5, getPos().getY() + 1, getPos().getZ() + 0.5, - stack); - item.motionX = world.rand.nextGaussian() * 0.05; - item.motionY = world.rand.nextGaussian() * 0.05 + 0.2; - item.motionZ = world.rand.nextGaussian() * 0.05; - item.setPickupDelay(10); - world.spawnEntity(item); - } - @Override public boolean canInsertItem(int i, @Nonnull ItemStack itemStack, @Nonnull EnumFacing side) { return false; diff --git a/src/main/java/mekanism/common/util/MekanismUtils.java b/src/main/java/mekanism/common/util/MekanismUtils.java index 69c5caef3..b444ca603 100644 --- a/src/main/java/mekanism/common/util/MekanismUtils.java +++ b/src/main/java/mekanism/common/util/MekanismUtils.java @@ -19,9 +19,6 @@ import mekanism.common.Mekanism; import mekanism.common.MekanismBlocks; import mekanism.common.MekanismFluids; import mekanism.common.SideData; -import mekanism.common.tier.BaseTier; -import mekanism.common.tier.FactoryTier; -import mekanism.common.tier.GasTankTier; import mekanism.common.Upgrade; import mekanism.common.base.IActiveState; import mekanism.common.base.IFactory; @@ -38,6 +35,9 @@ import mekanism.common.item.ItemBlockGasTank; import mekanism.common.item.ItemBlockTransmitter; import mekanism.common.network.PacketPersonalChest.PersonalChestMessage; import mekanism.common.network.PacketPersonalChest.PersonalChestPacketType; +import mekanism.common.tier.BaseTier; +import mekanism.common.tier.FactoryTier; +import mekanism.common.tier.GasTankTier; import mekanism.common.tile.TileEntityAdvancedBoundingBlock; import mekanism.common.tile.TileEntityBoundingBlock; import mekanism.common.tile.TileEntityPersonalChest; @@ -1142,6 +1142,14 @@ public final class MekanismUtils { .getTileEntity(pos, Chunk.EnumCreateEntityType.CHECK) : worldIn.getTileEntity(pos); } + /** + * Dismantles a block, dropping it and removing it from the world. + */ + public static void dismantleBlock(Block block, IBlockState state, World world, BlockPos pos) { + block.dropBlockAsItem(world, pos, state, 0); + world.setBlockToAir(pos); + } + /** * Clamp a double to int without using Math.min due to double representation issues. Primary use: power systems that * use int, where Mek uses doubles internally diff --git a/src/main/java/mekanism/common/util/TransporterUtils.java b/src/main/java/mekanism/common/util/TransporterUtils.java index 41efad938..fcb30ad87 100644 --- a/src/main/java/mekanism/common/util/TransporterUtils.java +++ b/src/main/java/mekanism/common/util/TransporterUtils.java @@ -11,9 +11,10 @@ import mekanism.common.content.transporter.TransitRequest.TransitResponse; import mekanism.common.content.transporter.TransporterManager; import mekanism.common.content.transporter.TransporterStack; import mekanism.common.tile.TileEntityLogisticalSorter; -import net.minecraft.entity.item.EntityItem; +import net.minecraft.block.Block; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; +import net.minecraft.util.math.BlockPos; public final class TransporterUtils { @@ -69,15 +70,9 @@ public final class TransporterUtils { } TransporterManager.remove(stack); - - EntityItem entityItem = new EntityItem(tileEntity.world(), tileEntity.coord().x + pos[0], - tileEntity.coord().y + pos[1], tileEntity.coord().z + pos[2], stack.itemStack); - - entityItem.motionX = 0; - entityItem.motionY = 0; - entityItem.motionZ = 0; - - tileEntity.world().spawnEntity(entityItem); + BlockPos blockPos = new BlockPos(tileEntity.coord().x + pos[0], tileEntity.coord().y + pos[1], + tileEntity.coord().z + pos[2]); + Block.spawnAsEntity(tileEntity.world(), blockPos, stack.itemStack); } public static float[] getStackPosition(ILogisticalTransporter tileEntity, TransporterStack stack, float partial) { diff --git a/src/main/java/mekanism/generators/common/block/BlockGenerator.java b/src/main/java/mekanism/generators/common/block/BlockGenerator.java index 4ee66fdc9..794724e51 100644 --- a/src/main/java/mekanism/generators/common/block/BlockGenerator.java +++ b/src/main/java/mekanism/generators/common/block/BlockGenerator.java @@ -10,6 +10,7 @@ import mekanism.common.base.IBoundingBlock; import mekanism.common.base.ISustainedData; import mekanism.common.base.ISustainedInventory; import mekanism.common.base.ISustainedTank; +import mekanism.common.block.BlockMekanismContainer; import mekanism.common.block.states.BlockStateFacing; import mekanism.common.config.MekanismConfig; import mekanism.common.integration.wrenches.Wrenches; @@ -31,7 +32,6 @@ import mekanism.generators.common.block.states.BlockStateGenerator.GeneratorType import mekanism.generators.common.tile.TileEntitySolarGenerator; import mekanism.generators.common.tile.turbine.TileEntityTurbineRotor; import net.minecraft.block.Block; -import net.minecraft.block.BlockContainer; import net.minecraft.block.material.Material; import net.minecraft.block.properties.PropertyEnum; import net.minecraft.block.state.BlockStateContainer; @@ -39,10 +39,7 @@ import net.minecraft.block.state.IBlockState; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.EntityLiving; import net.minecraft.entity.EntityLivingBase; -import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Items; -import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; @@ -70,7 +67,7 @@ import net.minecraftforge.fml.relauncher.SideOnly; * * @author AidanBrady */ -public abstract class BlockGenerator extends BlockContainer { +public abstract class BlockGenerator extends BlockMekanismContainer { private static final AxisAlignedBB SOLAR_BOUNDS = new AxisAlignedBB(0.0F, 0.0F, 0.0F, 1.0F, 0.7F, 1.0F); private static final AxisAlignedBB ROTOR_BOUNDS = new AxisAlignedBB(0.375F, 0.0F, 0.375F, 0.625F, 1.0F, 0.625F); @@ -320,15 +317,7 @@ public abstract class BlockGenerator extends BlockContainer { int amount = ((TileEntityTurbineRotor) tileEntity).getHousedBlades(); if (amount > 0) { - float motion = 0.7F; - double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - - EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, new ItemStack(GeneratorsItems.TurbineBlade, amount)); - - world.spawnEntity(entityItem); + spawnAsEntity(world, pos, new ItemStack(GeneratorsItems.TurbineBlade, amount)); } } @@ -359,8 +348,7 @@ public abstract class BlockGenerator extends BlockContainer { wrenchHandler.wrenchUsed(entityplayer, hand, stack, raytrace); if (entityplayer.isSneaking()) { - dismantleBlock(state, world, pos, false); - + MekanismUtils.dismantleBlock(this, state, world, pos); return true; } @@ -439,11 +427,6 @@ public abstract class BlockGenerator extends BlockContainer { return false; } - @Override - public int quantityDropped(Random random) { - return 0; - } - @Override public TileEntity createTileEntity(@Nonnull World world, @Nonnull IBlockState state) { int metadata = state.getBlock().getMetaFromState(state); @@ -455,12 +438,6 @@ public abstract class BlockGenerator extends BlockContainer { return GeneratorType.get(getGeneratorBlock(), metadata).create(); } - @Nonnull - @Override - public Item getItemDropped(IBlockState state, Random random, int fortune) { - return Items.AIR; - } - @Nonnull @Override @Deprecated @@ -509,28 +486,9 @@ public abstract class BlockGenerator extends BlockContainer { } } - @Override - public boolean removedByPlayer(@Nonnull IBlockState state, World world, @Nonnull BlockPos pos, - @Nonnull EntityPlayer player, boolean willHarvest) { - if (!player.capabilities.isCreativeMode && !world.isRemote && willHarvest) { - float motion = 0.7F; - double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - - EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, getPickBlock(state, null, world, pos, player)); - - world.spawnEntity(entityItem); - } - - return world.setBlockToAir(pos); - } - @Nonnull @Override - public ItemStack getPickBlock(@Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, - @Nonnull BlockPos pos, EntityPlayer player) { + protected ItemStack getDropItem(@Nonnull IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos) { TileEntityBasicBlock tileEntity = (TileEntityBasicBlock) world.getTileEntity(pos); ItemStack itemStack = new ItemStack(GeneratorsBlocks.Generator, 1, state.getBlock().getMetaFromState(state)); @@ -578,26 +536,6 @@ public abstract class BlockGenerator extends BlockContainer { return itemStack; } - public ItemStack dismantleBlock(IBlockState state, World world, BlockPos pos, boolean returnBlock) { - ItemStack itemStack = getPickBlock(state, null, world, pos, null); - - world.setBlockToAir(pos); - - if (!returnBlock) { - float motion = 0.7F; - double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - - EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, itemStack); - - world.spawnEntity(entityItem); - } - - return itemStack; - } - @Override @Deprecated public boolean isSideSolid(IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos, EnumFacing side) { diff --git a/src/main/java/mekanism/generators/common/block/BlockReactor.java b/src/main/java/mekanism/generators/common/block/BlockReactor.java index 89f39142a..b5205f450 100644 --- a/src/main/java/mekanism/generators/common/block/BlockReactor.java +++ b/src/main/java/mekanism/generators/common/block/BlockReactor.java @@ -22,7 +22,6 @@ import net.minecraft.block.properties.PropertyEnum; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; import net.minecraft.creativetab.CreativeTabs; -import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; @@ -129,7 +128,7 @@ public abstract class BlockReactor extends Block implements ITileEntityProvider if (!stack.isEmpty()) { if (MekanismUtils.isBCWrench(stack.getItem()) && !stack.getTranslationKey().contains("omniwrench")) { if (entityplayer.isSneaking()) { - dismantleBlock(world, pos, false); + MekanismUtils.dismantleBlock(this, state, world, pos); return true; } @@ -267,27 +266,6 @@ public abstract class BlockReactor extends Block implements ITileEntityProvider return type == ReactorBlockType.REACTOR_LOGIC_ADAPTER; } - public ItemStack dismantleBlock(World world, BlockPos pos, boolean returnBlock) { - IBlockState state = world.getBlockState(pos); - ItemStack itemStack = new ItemStack(this, 1, state.getBlock().getMetaFromState(state)); - - world.setBlockToAir(pos); - - if (!returnBlock) { - float motion = 0.7F; - double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; - - EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, - pos.getZ() + motionZ, itemStack); - - world.spawnEntity(entityItem); - } - - return itemStack; - } - public PropertyEnum getTypeProperty() { return getReactorBlock().getProperty(); }