diff --git a/src/main/java/forestry/core/blocks/BlockBase.java b/src/main/java/forestry/core/blocks/BlockBase.java index 6fbf15ce1..093d55308 100644 --- a/src/main/java/forestry/core/blocks/BlockBase.java +++ b/src/main/java/forestry/core/blocks/BlockBase.java @@ -17,7 +17,6 @@ import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.client.renderer.texture.IIconRegister; import net.minecraft.creativetab.CreativeTabs; -import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Items; import net.minecraft.inventory.IInventory; @@ -39,7 +38,6 @@ import cpw.mods.fml.relauncher.SideOnly; import forestry.core.access.IAccessHandler; import forestry.core.circuits.ISocketable; import forestry.core.fluids.FluidHelper; -import forestry.core.items.ItemBlockNBT; import forestry.core.proxy.Proxies; import forestry.core.tiles.MachineDefinition; import forestry.core.tiles.TileBase; @@ -164,22 +162,6 @@ public class BlockBase extends BlockForestry { return definition != null && definition.isSolidOnSide(world, x, y, z, side.ordinal()); } - @Override - public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityLiving, ItemStack stack) { - super.onBlockPlacedBy(world, x, y, z, entityLiving, stack); - - TileForestry tile = (TileForestry) world.getTileEntity(x, y, z); - - if (stack.getItem() instanceof ItemBlockNBT && stack.hasTagCompound()) { - tile.readFromNBT(stack.getTagCompound()); - tile.xCoord = x; - tile.yCoord = y; - tile.zCoord = z; - } - - tile.rotateAfterPlacement(entityLiving); - } - @Override public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float par7, float par8, float par9) { if (getDefinition(world, x, y, z).onBlockActivated(world, x, y, z, player, side)) { diff --git a/src/main/java/forestry/core/items/ItemBlockForestry.java b/src/main/java/forestry/core/items/ItemBlockForestry.java index 4251c06f5..2940ca1de 100644 --- a/src/main/java/forestry/core/items/ItemBlockForestry.java +++ b/src/main/java/forestry/core/items/ItemBlockForestry.java @@ -11,13 +11,18 @@ package forestry.core.items; import net.minecraft.block.Block; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.util.IIcon; +import net.minecraft.world.World; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; +import forestry.core.tiles.TileForestry; +import forestry.core.tiles.TileUtil; + public class ItemBlockForestry extends ItemBlock { public ItemBlockForestry(Block block) { @@ -46,4 +51,23 @@ public class ItemBlockForestry extends ItemBlock { return getBlock().getUnlocalizedName() + "." + itemstack.getItemDamage(); } + @Override + public boolean placeBlockAt(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ, int metadata) { + boolean placed = super.placeBlockAt(stack, player, world, x, y, z, side, hitX, hitY, hitZ, metadata); + + TileForestry tile = TileUtil.getTile(world, x, y, z, TileForestry.class); + + if (tile != null) { + if (stack.getItem() instanceof ItemBlockNBT && stack.hasTagCompound()) { + tile.readFromNBT(stack.getTagCompound()); + tile.xCoord = x; + tile.yCoord = y; + tile.zCoord = z; + } + + tile.rotateAfterPlacement(player, side); + } + + return placed; + } } diff --git a/src/main/java/forestry/core/tiles/TileEngine.java b/src/main/java/forestry/core/tiles/TileEngine.java index 9d95aee71..54f728a45 100644 --- a/src/main/java/forestry/core/tiles/TileEngine.java +++ b/src/main/java/forestry/core/tiles/TileEngine.java @@ -12,7 +12,7 @@ package forestry.core.tiles; import java.io.IOException; -import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.ICrafting; import net.minecraft.nbt.NBTTagCompound; @@ -61,8 +61,14 @@ public abstract class TileEngine extends TileBase implements IEnergyConnection, } @Override - public void rotateAfterPlacement(EntityLivingBase entityLiving) { - rotateEngine(); + public void rotateAfterPlacement(EntityPlayer player, int side) { + ForgeDirection orientation = ForgeDirection.getOrientation(side).getOpposite(); + if (isOrientedAtEnergyReciever(orientation)) { + setOrientation(orientation); + } else { + super.rotateAfterPlacement(player, side); + rotateEngine(); + } } protected void addHeat(int i) { @@ -173,21 +179,20 @@ public abstract class TileEngine extends TileBase implements IEnergyConnection, /* INTERACTION */ public void rotateEngine() { - for (int i = getOrientation().ordinal() + 1; i <= getOrientation().ordinal() + 6; ++i) { - ForgeDirection orient = ForgeDirection.values()[i % 6]; - - TileEntity tile = worldObj.getTileEntity(xCoord + orient.offsetX, yCoord + orient.offsetY, zCoord + orient.offsetZ); - - if (BlockUtil.isEnergyReceiverOrEngine(getOrientation().getOpposite(), tile)) { - setOrientation(orient); - worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, worldObj.getBlock(xCoord, yCoord, zCoord)); - worldObj.func_147479_m(xCoord, yCoord, zCoord); - break; + ForgeDirection orientation = ForgeDirection.values()[i % 6]; + if (isOrientedAtEnergyReciever(orientation)) { + setOrientation(orientation); + return; } } } + private boolean isOrientedAtEnergyReciever(ForgeDirection orientation) { + TileEntity tile = worldObj.getTileEntity(xCoord + orientation.offsetX, yCoord + orientation.offsetY, zCoord + orientation.offsetZ); + return BlockUtil.isEnergyReceiverOrEngine(getOrientation().getOpposite(), tile); + } + // STATE INFORMATION protected double getHeatLevel() { return (double) heat / (double) maxHeat; diff --git a/src/main/java/forestry/core/tiles/TileForestry.java b/src/main/java/forestry/core/tiles/TileForestry.java index 66286dafb..bd1cd0f8a 100644 --- a/src/main/java/forestry/core/tiles/TileForestry.java +++ b/src/main/java/forestry/core/tiles/TileForestry.java @@ -15,7 +15,6 @@ import java.util.Collection; import java.util.Random; import net.minecraft.block.Block; -import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.ItemStack; @@ -82,9 +81,8 @@ public abstract class TileForestry extends TileEntity implements IStreamable, IE super.validate(); } - public void rotateAfterPlacement(EntityLivingBase entityLiving) { - - int l = MathHelper.floor_double(((entityLiving.rotationYaw * 4F) / 360F) + 0.5D) & 3; + public void rotateAfterPlacement(EntityPlayer player, int side) { + int l = MathHelper.floor_double(((player.rotationYaw * 4F) / 360F) + 0.5D) & 3; if (l == 0) { setOrientation(ForgeDirection.NORTH); } @@ -97,7 +95,6 @@ public abstract class TileForestry extends TileEntity implements IStreamable, IE if (l == 3) { setOrientation(ForgeDirection.WEST); } - } // / UPDATING @@ -219,6 +216,8 @@ public abstract class TileForestry extends TileEntity implements IStreamable, IE } this.orientation = orientation; this.setNeedsNetworkUpdate(); + worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, worldObj.getBlock(xCoord, yCoord, zCoord)); + worldObj.func_147479_m(xCoord, yCoord, zCoord); } protected final void setNeedsNetworkUpdate() { diff --git a/src/main/java/forestry/core/tiles/TileUtil.java b/src/main/java/forestry/core/tiles/TileUtil.java index cd90ff816..21a727108 100644 --- a/src/main/java/forestry/core/tiles/TileUtil.java +++ b/src/main/java/forestry/core/tiles/TileUtil.java @@ -21,7 +21,6 @@ import net.minecraftforge.common.util.ForgeDirection; import forestry.core.inventory.wrappers.ChestWrapper; import forestry.core.utils.InventoryUtil; -import forestry.core.utils.Log; public abstract class TileUtil { @@ -48,7 +47,6 @@ public abstract class TileUtil { if (tileClass.isInstance(tileEntity)) { return tileClass.cast(tileEntity); } else { - Log.warning("Failed to cast a tile entity {" + tileEntity + "} to a {" + tileClass.getName() + "} at " + x + '/' + y + '/' + z); return null; } } diff --git a/src/main/java/forestry/energy/blocks/BlockEngine.java b/src/main/java/forestry/energy/blocks/BlockEngine.java new file mode 100644 index 000000000..750906968 --- /dev/null +++ b/src/main/java/forestry/energy/blocks/BlockEngine.java @@ -0,0 +1,114 @@ +/******************************************************************************* + * Copyright (c) 2011-2014 SirSengir. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Lesser Public License v3 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Various Contributors including, but not limited to: + * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges + ******************************************************************************/ +package forestry.energy.blocks; + +import com.google.common.collect.ImmutableList; + +import java.util.EnumMap; +import java.util.List; + +import net.minecraft.block.material.Material; +import net.minecraft.entity.Entity; +import net.minecraft.util.AxisAlignedBB; +import net.minecraft.util.MovingObjectPosition; +import net.minecraft.util.Vec3; +import net.minecraft.world.World; + +import net.minecraftforge.common.util.ForgeDirection; + +import forestry.core.blocks.BlockBase; +import forestry.core.tiles.TileEngine; +import forestry.core.tiles.TileUtil; + +public class BlockEngine extends BlockBase { + private static final EnumMap> boundingBoxesForDirections = new EnumMap<>(ForgeDirection.class); + + static { + boundingBoxesForDirections.put(ForgeDirection.DOWN, ImmutableList.of( + AxisAlignedBB.getBoundingBox(0.0, 0.5, 0.0, 1.0, 1.0, 1.0), AxisAlignedBB.getBoundingBox(0.25, 0.0, 0.25, 0.75, 0.5, 0.75) + )); + boundingBoxesForDirections.put(ForgeDirection.UP, ImmutableList.of( + AxisAlignedBB.getBoundingBox(0.0, 0.0, 0.0, 1.0, 0.5, 1.0), AxisAlignedBB.getBoundingBox(0.25, 0.5, 0.25, 0.75, 1.0, 0.75) + )); + boundingBoxesForDirections.put(ForgeDirection.NORTH, ImmutableList.of( + AxisAlignedBB.getBoundingBox(0.0, 0.0, 0.5, 1.0, 1.0, 1.0), AxisAlignedBB.getBoundingBox(0.25, 0.25, 0.0, 0.75, 0.75, 0.5) + )); + boundingBoxesForDirections.put(ForgeDirection.SOUTH, ImmutableList.of( + AxisAlignedBB.getBoundingBox(0.0, 0.0, 0.0, 1.0, 1.0, 0.5), AxisAlignedBB.getBoundingBox(0.25, 0.25, 0.5, 0.75, 0.75, 1.0) + )); + boundingBoxesForDirections.put(ForgeDirection.WEST, ImmutableList.of( + AxisAlignedBB.getBoundingBox(0.5, 0.0, 0.0, 1.0, 1.0, 1.0), AxisAlignedBB.getBoundingBox(0.0, 0.25, 0.25, 0.5, 0.75, 0.75) + )); + boundingBoxesForDirections.put(ForgeDirection.EAST, ImmutableList.of( + AxisAlignedBB.getBoundingBox(0.0, 0.0, 0.0, 0.5, 1.0, 1.0), AxisAlignedBB.getBoundingBox(0.5, 0.25, 0.25, 1.0, 0.75, 0.75) + )); + } + + public BlockEngine(Material material) { + super(material, true); + } + + @Override + @SuppressWarnings("unchecked") + public void addCollisionBoxesToList(World world, int x, int y, int z, AxisAlignedBB mask, List list, Entity colliding) { + TileEngine tile = TileUtil.getTile(world, x, y, z, TileEngine.class); + if (tile == null) { + super.addCollisionBoxesToList(world, x, y, z, mask, list, colliding); + return; + } + + ForgeDirection orientation = tile.getOrientation(); + List boundingBoxes = boundingBoxesForDirections.get(orientation); + if (boundingBoxes == null) { + return; + } + + for (AxisAlignedBB boundingBoxBase : boundingBoxes) { + AxisAlignedBB boundingBox = boundingBoxBase.getOffsetBoundingBox(x, y, z); + if (mask.intersectsWith(boundingBox)) { + list.add(boundingBox); + } + } + } + + @Override + public MovingObjectPosition collisionRayTrace(World world, int x, int y, int z, Vec3 origin, Vec3 direction) { + TileEngine tile = TileUtil.getTile(world, x, y, z, TileEngine.class); + if (tile == null) { + return super.collisionRayTrace(world, x, y, z, origin, direction); + } + + ForgeDirection orientation = tile.getOrientation(); + List boundingBoxes = boundingBoxesForDirections.get(orientation); + if (boundingBoxes == null) { + return super.collisionRayTrace(world, x, y, z, origin, direction); + } + + MovingObjectPosition nearestIntersection = null; + for (AxisAlignedBB boundingBoxBase : boundingBoxes) { + AxisAlignedBB boundingBox = boundingBoxBase.getOffsetBoundingBox(x, y, z); + MovingObjectPosition intersection = boundingBox.calculateIntercept(origin, direction); + if (intersection != null) { + if (nearestIntersection == null || (intersection.hitVec.distanceTo(origin) < nearestIntersection.hitVec.distanceTo(origin))) { + nearestIntersection = intersection; + } + } + } + + if (nearestIntersection != null) { + nearestIntersection.blockX = x; + nearestIntersection.blockY = y; + nearestIntersection.blockZ = z; + } + + return nearestIntersection; + } +} diff --git a/src/main/java/forestry/energy/tiles/EngineDefinition.java b/src/main/java/forestry/energy/tiles/EngineDefinition.java index 1d945c57c..90b1623ff 100644 --- a/src/main/java/forestry/energy/tiles/EngineDefinition.java +++ b/src/main/java/forestry/energy/tiles/EngineDefinition.java @@ -10,7 +10,6 @@ ******************************************************************************/ package forestry.energy.tiles; -import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.crafting.IRecipe; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.IBlockAccess; @@ -21,7 +20,7 @@ import net.minecraftforge.common.util.ForgeDirection; import forestry.core.render.IBlockRenderer; import forestry.core.tiles.MachineDefinition; import forestry.core.tiles.TileEngine; -import forestry.core.utils.PlayerUtil; +import forestry.core.tiles.TileUtil; public class EngineDefinition extends MachineDefinition { @@ -31,35 +30,22 @@ public class EngineDefinition extends MachineDefinition { @Override public boolean isSolidOnSide(IBlockAccess world, int x, int y, int z, int side) { - TileEntity tile = world.getTileEntity(x, y, z); - if (tile instanceof TileEngine) { - return ((TileEngine) tile).getOrientation().getOpposite().ordinal() == side; - } - return false; - } - - @Override - public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side) { - - if (player.isSneaking()) { + TileEngine tile = TileUtil.getTile(world, x, y, z, TileEngine.class); + if (tile == null) { return false; } - TileEngine tile = (TileEngine) world.getTileEntity(x, y, z); - if (PlayerUtil.canWrench(player, x, y, z)) { - tile.rotateEngine(); - PlayerUtil.useWrench(player, x, y, z); - return true; - } - - return false; + return tile.getOrientation().getOpposite().ordinal() == side; } @Override public boolean rotateBlock(World world, int x, int y, int z, ForgeDirection axis) { - TileEngine tile = (TileEngine) world.getTileEntity(x, y, z); + TileEngine tile = TileUtil.getTile(world, x, y, z, TileEngine.class); + if (tile == null) { + return false; + } + tile.rotateEngine(); return true; } - } diff --git a/src/main/java/forestry/plugins/PluginEnergy.java b/src/main/java/forestry/plugins/PluginEnergy.java index 3e6086e73..d691162fa 100755 --- a/src/main/java/forestry/plugins/PluginEnergy.java +++ b/src/main/java/forestry/plugins/PluginEnergy.java @@ -25,6 +25,7 @@ import forestry.core.items.ItemBlockForestry; import forestry.core.recipes.ShapedRecipeCustom; import forestry.core.tiles.MachineDefinition; import forestry.energy.GuiHandlerEnergy; +import forestry.energy.blocks.BlockEngine; import forestry.energy.proxy.ProxyEnergy; import forestry.energy.tiles.EngineDefinition; import forestry.energy.tiles.TileEngineBiogas; @@ -44,7 +45,7 @@ public class PluginEnergy extends ForestryPlugin { public void preInit() { super.preInit(); - ForestryBlock.engine.registerBlock(new BlockBase(Material.iron, true), ItemBlockForestry.class, "engine"); + ForestryBlock.engine.registerBlock(new BlockEngine(Material.iron), ItemBlockForestry.class, "engine"); definitionEnginePeat = ((BlockBase) ForestryBlock.engine.block()).addDefinition(new EngineDefinition(Constants.DEFINITION_ENGINE_PEAT_META, "forestry.EngineCopper", TileEnginePeat.class, PluginEnergy.proxy.getRenderDefaultEngine(Constants.TEXTURE_PATH_BLOCKS + "/engine_copper_"), ShapedRecipeCustom.createShapedRecipe(