diff --git a/src/main/java/forestry/core/blocks/BlockBase.java b/src/main/java/forestry/core/blocks/BlockBase.java index fbdbe3079..dd598e79f 100644 --- a/src/main/java/forestry/core/blocks/BlockBase.java +++ b/src/main/java/forestry/core/blocks/BlockBase.java @@ -26,6 +26,8 @@ import net.minecraft.tileentity.TileEntity; import net.minecraft.util.AxisAlignedBB; import net.minecraft.util.ChatComponentTranslation; import net.minecraft.util.IIcon; +import net.minecraft.util.MovingObjectPosition; +import net.minecraft.util.Vec3; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; @@ -125,6 +127,16 @@ public class BlockBase extends BlockForestry { return definition.getBoundingBox(x, y, z); } + @Override + public MovingObjectPosition collisionRayTrace(World world, int x, int y, int z, Vec3 startVec, Vec3 endVec) { + MachineDefinition definition = definitions.get(world.getBlockMetadata(x, y, z)); + if (definition == null) { + return super.collisionRayTrace(world, x, y, z, startVec, endVec); + } else { + return definition.collisionRayTrace(world, x, y, z, startVec, endVec); + } + } + /* CREATIVE INVENTORY */ @SuppressWarnings("rawtypes") @SideOnly(Side.CLIENT) diff --git a/src/main/java/forestry/core/tiles/MachineDefinition.java b/src/main/java/forestry/core/tiles/MachineDefinition.java index 53f7d46ab..6baeaf9cd 100644 --- a/src/main/java/forestry/core/tiles/MachineDefinition.java +++ b/src/main/java/forestry/core/tiles/MachineDefinition.java @@ -125,6 +125,10 @@ public class MachineDefinition { return AxisAlignedBB.getBoundingBox(x + minX, y + minY, z + minZ, x + maxX, y + maxY, z + maxZ); } + public MovingObjectPosition collisionRayTrace(World world, int x, int y, int z, Vec3 startVec, Vec3 endVec) { + return BlockUtil.collisionRayTrace(world, x, y, z, startVec, endVec, minX, minY, minZ, maxX, maxY, maxZ); + } + /** * Registers the tile entity with MC. */ diff --git a/src/main/java/forestry/core/utils/BlockUtil.java b/src/main/java/forestry/core/utils/BlockUtil.java index a11f38cbe..ac4a85df8 100644 --- a/src/main/java/forestry/core/utils/BlockUtil.java +++ b/src/main/java/forestry/core/utils/BlockUtil.java @@ -19,6 +19,8 @@ import net.minecraft.init.Blocks; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.Direction; +import net.minecraft.util.MovingObjectPosition; +import net.minecraft.util.Vec3; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; @@ -109,4 +111,121 @@ public abstract class BlockUtil { return block == Blocks.vine || block == Blocks.tallgrass || block == Blocks.deadbush || block == Blocks.snow_layer || block.getMaterial().isReplaceable(); } + + /** + * Ray traces through the blocks collision from start vector to end vector returning a ray trace hit. + */ + public static MovingObjectPosition collisionRayTrace(World world, int x, int y, int z, Vec3 startVec, Vec3 endVec, float minX, float minY, float minZ, float maxX, float maxY, float maxZ) { + startVec = startVec.addVector((double) (-x), (double) (-y), (double) (-z)); + endVec = endVec.addVector((double) (-x), (double) (-y), (double) (-z)); + Vec3 vec32 = startVec.getIntermediateWithXValue(endVec, minX); + Vec3 vec33 = startVec.getIntermediateWithXValue(endVec, maxX); + Vec3 vec34 = startVec.getIntermediateWithYValue(endVec, minY); + Vec3 vec35 = startVec.getIntermediateWithYValue(endVec, maxY); + Vec3 vec36 = startVec.getIntermediateWithZValue(endVec, minZ); + Vec3 vec37 = startVec.getIntermediateWithZValue(endVec, maxZ); + + if (!isVecInsideYZBounds(vec32, minY, minZ, maxY, maxZ)) { + vec32 = null; + } + + if (!isVecInsideYZBounds(vec33, minY, minZ, maxY, maxZ)) { + vec33 = null; + } + + if (!isVecInsideXZBounds(vec34, minX, minZ, maxX, maxZ)) { + vec34 = null; + } + + if (!isVecInsideXZBounds(vec35, minX, minZ, maxX, maxZ)) { + vec35 = null; + } + + if (!isVecInsideXYBounds(vec36, minX, minY, maxX, maxY)) { + vec36 = null; + } + + if (!isVecInsideXYBounds(vec37, minX, minY, maxX, maxY)) { + vec37 = null; + } + + Vec3 minHit = null; + + if (vec32 != null && (minHit == null || startVec.squareDistanceTo(vec32) < startVec.squareDistanceTo(minHit))) { + minHit = vec32; + } + + if (vec33 != null && (minHit == null || startVec.squareDistanceTo(vec33) < startVec.squareDistanceTo(minHit))) { + minHit = vec33; + } + + if (vec34 != null && (minHit == null || startVec.squareDistanceTo(vec34) < startVec.squareDistanceTo(minHit))) { + minHit = vec34; + } + + if (vec35 != null && (minHit == null || startVec.squareDistanceTo(vec35) < startVec.squareDistanceTo(minHit))) { + minHit = vec35; + } + + if (vec36 != null && (minHit == null || startVec.squareDistanceTo(vec36) < startVec.squareDistanceTo(minHit))) { + minHit = vec36; + } + + if (vec37 != null && (minHit == null || startVec.squareDistanceTo(vec37) < startVec.squareDistanceTo(minHit))) { + minHit = vec37; + } + + if (minHit == null) { + return null; + } else { + byte sideHit = -1; + + if (minHit == vec32) { + sideHit = 4; + } + + if (minHit == vec33) { + sideHit = 5; + } + + if (minHit == vec34) { + sideHit = 0; + } + + if (minHit == vec35) { + sideHit = 1; + } + + if (minHit == vec36) { + sideHit = 2; + } + + if (minHit == vec37) { + sideHit = 3; + } + + return new MovingObjectPosition(x, y, z, sideHit, minHit.addVector((double) x, (double) y, (double) z)); + } + } + + /** + * Checks if a vector is within the Y and Z bounds of the block. + */ + private static boolean isVecInsideYZBounds(Vec3 vec, float minY, float minZ, float maxY, float maxZ) { + return vec != null && (vec.yCoord >= minY && vec.yCoord <= maxY && vec.zCoord >= minZ && vec.zCoord <= maxZ); + } + + /** + * Checks if a vector is within the X and Z bounds of the block. + */ + private static boolean isVecInsideXZBounds(Vec3 vec, float minX, float minZ, float maxX, float maxZ) { + return vec != null && (vec.xCoord >= minX && vec.xCoord <= maxX && vec.zCoord >= minZ && vec.zCoord <= maxZ); + } + + /** + * Checks if a vector is within the X and Y bounds of the block. + */ + private static boolean isVecInsideXYBounds(Vec3 vec, float minX, float minY, float maxX, float maxY) { + return vec != null && (vec.xCoord >= minX && vec.xCoord <= maxX && vec.yCoord >= minY && vec.yCoord <= maxY); + } }