diff --git a/src/main/java/com/brandon3055/draconicevolution/common/utills/PortalHelper.java b/src/main/java/com/brandon3055/draconicevolution/common/utills/PortalHelper.java index a8381b63..94c1f195 100644 --- a/src/main/java/com/brandon3055/draconicevolution/common/utills/PortalHelper.java +++ b/src/main/java/com/brandon3055/draconicevolution/common/utills/PortalHelper.java @@ -2,8 +2,8 @@ package com.brandon3055.draconicevolution.common.utills; import net.minecraft.block.Block; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; -import net.minecraft.world.chunk.Chunk; import net.minecraftforge.common.util.ForgeDirection; import com.brandon3055.draconicevolution.common.ModBlocks; @@ -15,7 +15,7 @@ import com.brandon3055.draconicevolution.common.tileentities.multiblocktiles.Til */ public class PortalHelper { - static int iterationNow = 0; + public static final int MAXIMUM_PORTAL_SIZE = 150; public static boolean isFrame(Block block) { return block == ModBlocks.infusedObsidian; @@ -30,13 +30,17 @@ public class PortalHelper { } public static PortalStructure getValidStructure(World world, int x, int y, int z) { - if (world.isRemote) return null; + if (world.isRemote) { + return null; + } for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) { for (ForgeDirection plane : ForgeDirection.VALID_DIRECTIONS) { if (plane != direction && plane != direction.getOpposite()) { PortalStructure structure = traceFrame(world, x, y, z, direction, plane); - if (structure != null && structure.scanPortal(world, x, y, z, false, false)) return structure; + if (structure != null && structure.scanPortal(world, x, y, z, false, false)) { + return structure; + } } } } @@ -46,67 +50,45 @@ public class PortalHelper { public static PortalStructure traceFrame(World world, int x, int y, int z, ForgeDirection startDir, ForgeDirection plane) { - int MAX_SIZE = 150; int startX = x + startDir.offsetX; int startY = y + startDir.offsetY; int startZ = z + startDir.offsetZ; - // Check that the trace is starting from a receptacle - if (!world.isAirBlock(startX, startY, startZ)) return null; - - int xSize = 0; - int ySize = 0; - int yOffset = 0; - - // Get X size - for (int i = 0; i <= MAX_SIZE; i++) { - Block block = world.getBlock( - startX + i * startDir.offsetX, - startY + i * startDir.offsetY, - startZ + i * startDir.offsetZ); - if (isFrame(block)) { - xSize = i; - break; - } else if (!world.isAirBlock( - startX + i * startDir.offsetX, - startY + i * startDir.offsetY, - startZ + i * startDir.offsetZ)) - return null; + if (!world.isAirBlock(startX, startY, startZ)) { + return null; } - // Get Y size above receptacle - for (int i = 0; i <= MAX_SIZE; i++) { - Block block = world - .getBlock(startX + i * plane.offsetX, startY + i * plane.offsetY, startZ + i * plane.offsetZ); - if (isFrame(block)) { - ySize = i; - break; - } else if (!world - .isAirBlock(startX + i * plane.offsetX, startY + i * plane.offsetY, startZ + i * plane.offsetZ)) - return null; - } + int xSize = findDistanceToFrame(world, startX, startY, startZ, startDir); + int ySize = findDistanceToFrame(world, startX, startY, startZ, plane); + int yOffset = findDistanceToFrame(world, startX, startY, startZ, plane.getOpposite()); + ySize += yOffset - 1; - // Get Y size below receptacle and get y offset - for (int i = 0; i <= MAX_SIZE; i++) { - Block block = world - .getBlock(startX - i * plane.offsetX, startY - i * plane.offsetY, startZ - i * plane.offsetZ); - if (isFrame(block)) { - ySize += i - 1; - yOffset = i; - break; - } else if (!world - .isAirBlock(startX - i * plane.offsetX, startY - i * plane.offsetY, startZ - i * plane.offsetZ)) - return null; + if (xSize == 0 || ySize == 0 || yOffset == 0 || ySize > PortalHelper.MAXIMUM_PORTAL_SIZE) { + return null; } - if (xSize == 0 || ySize == 0 || ySize > MAX_SIZE) return null; - PortalStructure structure = new PortalStructure(xSize, ySize, yOffset, startDir, plane); + if (structure.checkFrameIsValid(world, x, y, z) && structure.scanPortal(world, x, y, z, false, false)) { + return structure; + } + return null; - if (!structure.checkFrameIsValid(world, x, y, z) || !structure.scanPortal(world, x, y, z, false, false)) - return null; + } - return structure; + private static int findDistanceToFrame(World world, int startX, int startY, int startZ, ForgeDirection direction) { + for (int distance = 1; distance <= PortalHelper.MAXIMUM_PORTAL_SIZE; distance++) { + int targetX = startX + direction.offsetX * distance; + int targetY = startY + direction.offsetY * distance; + int targetZ = startZ + direction.offsetZ * distance; + Block block = world.getBlock(targetX, targetY, targetZ); + if (isFrame(block)) { + return distance; + } + if (!world.isAirBlock(targetX, targetY, targetZ)) { + return 0; + } + } + return 0; } public static class PortalStructure { @@ -128,77 +110,68 @@ public class PortalHelper { } public boolean checkFrameIsValid(World world, int x, int y, int z) { - - int startX = x + startDir.offsetX; - int startY = y + startDir.offsetY; - int startZ = z + startDir.offsetZ; - - // Check structure for y size - for (int y1 = 1; y1 <= ySize; y1++) { - int y2 = y1 - yOffset; - - int inX = startX + y2 * plane.offsetX - startDir.offsetX; - int inY = startY + y2 * plane.offsetY - startDir.offsetY; - int inZ = startZ + y2 * plane.offsetZ - startDir.offsetZ; - - int outX = startX + y2 * plane.offsetX + (xSize) * startDir.offsetX; - int outY = startY + y2 * plane.offsetY + (xSize) * startDir.offsetY; - int outZ = startZ + y2 * plane.offsetZ + (xSize) * startDir.offsetZ; - - if (!isFrame(world.getBlock(inX, inY, inZ)) && !(inX == x && inY == y && inZ == z)) return false; - if (!isFrame(world.getBlock(outX, outY, outZ))) return false; + for (int xDistance = 1; xDistance <= xSize; xDistance++) { + if (isFrameMissing(world, x, y, z, xDistance, -yOffset) + || isFrameMissing(world, x, y, z, xDistance, ySize - yOffset + 1)) { + return false; + } } - - // Check structure for x size - for (int x1 = 0; x1 < xSize; x1++) { - int upX = startX + x1 * startDir.offsetX - yOffset * plane.offsetX; - int upY = startY + x1 * startDir.offsetY - yOffset * plane.offsetY; - int upZ = startZ + x1 * startDir.offsetZ - yOffset * plane.offsetZ; - - int downX = startX + x1 * startDir.offsetX + (ySize - yOffset + 1) * plane.offsetX; - int downY = startY + x1 * startDir.offsetY + (ySize - yOffset + 1) * plane.offsetY; - int downZ = startZ + x1 * startDir.offsetZ + (ySize - yOffset + 1) * plane.offsetZ; - - if (!isFrame(world.getBlock(upX, upY, upZ))) return false; - if (!isFrame(world.getBlock(downX, downY, downZ))) return false; + for (int yDistance = 1 - yOffset; yDistance <= ySize - yOffset; yDistance++) { + if (yDistance != 0 && isFrameMissing(world, x, y, z, 0, yDistance) + || isFrameMissing(world, x, y, z, xSize + 1, yDistance)) { + return false; + } } - return true; } + private boolean isFrameMissing(World world, int x, int y, int z, int xDistance, int yDistance) { + int targetX = x + startDir.offsetX * xDistance + plane.offsetX * yDistance; + int targetY = y + startDir.offsetY * xDistance + plane.offsetY * yDistance; + int targetZ = z + startDir.offsetZ * xDistance + plane.offsetZ * yDistance; + return !isFrame(world.getBlock(targetX, targetY, targetZ)); + } + public boolean scanPortal(World world, int x, int y, int z, boolean setPortalBlocks, boolean checkPortalBlocks) { - int startX = x + startDir.offsetX; - int startY = y + startDir.offsetY; - int startZ = z + startDir.offsetZ; + TileEntity tile = world.getTileEntity(x, y, z); + if (!(tile instanceof TileDislocatorReceptacle)) { + return false; + } + TileDislocatorReceptacle receptacle = (TileDislocatorReceptacle) tile; + if (setPortalBlocks) { + receptacle.updating = true; + } - TileDislocatorReceptacle receptacle = (TileDislocatorReceptacle) world.getTileEntity(x, y, z); - if (receptacle == null) return false; - if (setPortalBlocks) receptacle.updating = true; - - for (int x1 = 0; x1 < xSize; x1++) { - for (int y1 = 1; y1 <= ySize; y1++) { - int y2 = y1 - yOffset; - int X = (startX + x1 * startDir.offsetX) + y2 * plane.offsetX; - int Y = (startY + x1 * startDir.offsetY) + y2 * plane.offsetY; - int Z = (startZ + x1 * startDir.offsetZ) + y2 * plane.offsetZ; - - Block block = world.getBlock(X, Y, Z); + for (int xDistance = 1; xDistance <= xSize; xDistance++) { + for (int yDistance = 1 - yOffset; yDistance <= ySize - yOffset; yDistance++) { + int targetX = x + xDistance * startDir.offsetX + yDistance * plane.offsetX; + int targetY = y + xDistance * startDir.offsetY + yDistance * plane.offsetY; + int targetZ = z + xDistance * startDir.offsetZ + yDistance * plane.offsetZ; + Block block = world.getBlock(targetX, targetY, targetZ); if (checkPortalBlocks) { - if (!isPortal(block)) return false; + if (!isPortal(block)) { + return false; + } } else if (setPortalBlocks) { - world.setBlock(X, Y, Z, ModBlocks.portal); - Chunk chunk = world.getChunkFromBlockCoords(X, Z); - TilePortalBlock tile = (TilePortalBlock) chunk.func_150806_e(X & 15, Y, Z & 15); - tile.masterX = x; - tile.masterY = y; - tile.masterZ = z; - } else if (!world.isAirBlock(X, Y, Z)) return false; + world.setBlock(targetX, targetY, targetZ, ModBlocks.portal); + tile = world.getTileEntity(targetX, targetY, targetZ); + if (tile instanceof TilePortalBlock) { + TilePortalBlock portal = (TilePortalBlock) tile; + portal.masterX = x; + portal.masterY = y; + portal.masterZ = z; + } + } else if (!world.isAirBlock(targetX, targetY, targetZ)) { + return false; + } } } - if (setPortalBlocks) receptacle.updating = false; + if (setPortalBlocks) { + receptacle.updating = false; + } return true; }