From 29216ee9c29ee9f597dd9c7273ee07b765403020 Mon Sep 17 00:00:00 2001 From: Francesco Macagno Date: Fri, 17 Jul 2015 22:30:04 -0700 Subject: [PATCH] Updated more TileEntities. Nearly done --- .../TileEntityCloakingDeviceCore.java | 43 +- .../warpdrive/machines/TileEntityLaser.java | 1 - .../warpdrive/machines/TileEntityLift.java | 396 ++-- .../machines/TileEntityMiningLaser.java | 1 - .../warpdrive/machines/TileEntityMonitor.java | 8 +- .../machines/TileEntityParticleBooster.java | 17 +- .../machines/TileEntityProtocol.java | 20 +- .../warpdrive/machines/TileEntityRadar.java | 29 +- .../warpdrive/machines/TileEntityReactor.java | 1981 ++++++++--------- .../machines/TileEntityShipScanner.java | 28 +- .../cr0s/warpdrive/machines/WarpEnergyTE.java | 2 +- 11 files changed, 1309 insertions(+), 1217 deletions(-) diff --git a/src/java/cr0s/warpdrive/machines/TileEntityCloakingDeviceCore.java b/src/java/cr0s/warpdrive/machines/TileEntityCloakingDeviceCore.java index 5a12750e..0df5db92 100644 --- a/src/java/cr0s/warpdrive/machines/TileEntityCloakingDeviceCore.java +++ b/src/java/cr0s/warpdrive/machines/TileEntityCloakingDeviceCore.java @@ -4,11 +4,15 @@ import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.util.List; -import jdk.nashorn.internal.runtime.regexp.joni.constants.Arguments; +import li.cil.oc.api.machine.Arguments; +import li.cil.oc.api.machine.Callback; +import li.cil.oc.api.machine.Context; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.init.Blocks; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.network.Packet; import net.minecraft.util.AxisAlignedBB; import net.minecraftforge.common.util.ForgeDirection; import cpw.mods.fml.common.FMLCommonHandler; @@ -18,6 +22,9 @@ import cr0s.warpdrive.WarpDrive; import cr0s.warpdrive.WarpDriveConfig; import cr0s.warpdrive.data.CloakedArea; import cr0s.warpdrive.data.Vector3; +import dan200.computercraft.api.ComputerCraftAPI; +import dan200.computercraft.api.lua.ILuaContext; +import dan200.computercraft.api.peripheral.IComputerAccess; public class TileEntityCloakingDeviceCore extends WarpEnergyTE { private final int MAX_ENERGY_VALUE = 500000000; // 500kk EU @@ -146,7 +153,7 @@ public class TileEntityCloakingDeviceCore extends WarpEnergyTE { public void searchCoilInDirectionAndSetState(byte dx, byte dy, byte dz, boolean enabled) { int coilCount = 0; for (int i = 0; i < WarpDriveConfig.CD_MAX_CLOAKING_FIELD_SIDE; i++) { - if (worldObj.getBlockId(xCoord + i * dx, yCoord + i * dy, zCoord + i * dz) == WarpDriveConfig.cloakCoilID) { + if (worldObj.getBlock(xCoord + i * dx, yCoord + i * dy, zCoord + i * dz).isAssociatedBlock(WarpDrive.cloakCoilBlock)) { coilCount++; if (coilCount > 2) { return; @@ -168,7 +175,7 @@ public class TileEntityCloakingDeviceCore extends WarpEnergyTE { } for (int i = START_LENGTH + 1; i < WarpDriveConfig.CD_MAX_CLOAKING_FIELD_SIDE; i++) { - if (worldObj.getBlockId(xCoord + i * dx, yCoord + i * dy, zCoord + i * dz) == WarpDriveConfig.cloakCoilID) { + if (worldObj.getBlock(xCoord + i * dx, yCoord + i * dy, zCoord + i * dz).isAssociatedBlock(WarpDrive.cloakCoilBlock)) { sendLaserPacket(new Vector3(this).add(0.5), new Vector3(xCoord + i * dx, yCoord + i * dy, zCoord + i * dz).add(0.5), r, g, b, 110, 0, 100); } } @@ -256,7 +263,7 @@ public class TileEntityCloakingDeviceCore extends WarpEnergyTE { for (y = minY; y <= maxY; y++) { for (x = minX; x <= maxX; x++) { for(z = minZ; z <= maxZ; z++) { - if (worldObj.getBlockId(x, y, z) != 0) { + if (!worldObj.getBlock(x, y, z) .isAssociatedBlock(Blocks.air)) { volume++; } } @@ -297,7 +304,9 @@ public class TileEntityCloakingDeviceCore extends WarpEnergyTE { ex.printStackTrace(); } - Packet250CustomPayload packet = new Packet250CustomPayload(); + //TODO: Needs to be re-done according to http://www.minecraftforge.net/forum/index.php/topic,20135.0.html + /* + PacketCustomPayload packet = new Packet250CustomPayload(); packet.channel = "WarpDriveBeam"; packet.data = bos.toByteArray(); packet.length = bos.size(); @@ -309,6 +318,7 @@ public class TileEntityCloakingDeviceCore extends WarpEnergyTE { ((EntityPlayerMP)e).playerNetServerHandler.sendPacketToPlayer(packet); } } + */ } } @@ -330,7 +340,7 @@ public class TileEntityCloakingDeviceCore extends WarpEnergyTE { public int searchCoilInDirection(byte dx, byte dy, byte dz) { for (int i = 3; i < WarpDriveConfig.CD_MAX_CLOAKING_FIELD_SIDE; i++) { - if (worldObj.getBlockId(xCoord + i * dx, yCoord + i * dy, zCoord + i * dz) == WarpDriveConfig.cloakCoilID) { + if (worldObj.getBlock(xCoord + i * dx, yCoord + i * dy, zCoord + i * dz).isAssociatedBlock(WarpDrive.cloakCoilBlock)) { return i; } } @@ -347,7 +357,7 @@ public class TileEntityCloakingDeviceCore extends WarpEnergyTE { byte[] dz = { 0, 0, 0, 0, -1, 1 }; for (int i = 0; i < 6; i++) { - if (worldObj.getBlockId(xCoord + START_LENGTH * dx[i], yCoord + START_LENGTH * dy[i], zCoord + START_LENGTH * dz[i]) != WarpDriveConfig.cloakCoilID) { + if (worldObj.getBlock(xCoord + START_LENGTH * dx[i], yCoord + START_LENGTH * dy[i], zCoord + START_LENGTH * dz[i]).isAssociatedBlock(WarpDrive.cloakCoilBlock)) { return false; } } @@ -441,7 +451,7 @@ public class TileEntityCloakingDeviceCore extends WarpEnergyTE { @Override @Optional.Method(modid = "ComputerCraft") - public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception { + public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) { String methodName = methodsArray[method]; if (methodName.equals("tier")) { if (arguments.length == 1) { @@ -473,14 +483,21 @@ public class TileEntityCloakingDeviceCore extends WarpEnergyTE { public int getMaxEnergyStored() { return MAX_ENERGY_VALUE; } - - @Override - public int getMaxSafeInput() { - return Integer.MAX_VALUE; - } @Override public boolean canInputEnergy(ForgeDirection from) { return true; } + + @Override + public int getSinkTier() { + // TODO Auto-generated method stub + return 3; + } + + @Override + public int getSourceTier() { + // TODO Auto-generated method stub + return 3; + } } diff --git a/src/java/cr0s/warpdrive/machines/TileEntityLaser.java b/src/java/cr0s/warpdrive/machines/TileEntityLaser.java index b924e1f5..3c3cb846 100644 --- a/src/java/cr0s/warpdrive/machines/TileEntityLaser.java +++ b/src/java/cr0s/warpdrive/machines/TileEntityLaser.java @@ -2,7 +2,6 @@ package cr0s.warpdrive.machines; import java.util.List; -import jdk.nashorn.internal.runtime.regexp.joni.constants.Arguments; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.entity.Entity; diff --git a/src/java/cr0s/warpdrive/machines/TileEntityLift.java b/src/java/cr0s/warpdrive/machines/TileEntityLift.java index f550117d..42599dfa 100644 --- a/src/java/cr0s/warpdrive/machines/TileEntityLift.java +++ b/src/java/cr0s/warpdrive/machines/TileEntityLift.java @@ -2,8 +2,12 @@ package cr0s.warpdrive.machines; import java.util.List; -import jdk.nashorn.internal.runtime.regexp.joni.constants.Arguments; +import li.cil.oc.api.machine.Arguments; +import li.cil.oc.api.machine.Callback; +import li.cil.oc.api.machine.Context; +import net.minecraft.block.Block; import net.minecraft.entity.EntityLivingBase; +import net.minecraft.init.Blocks; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.AxisAlignedBB; import net.minecraftforge.common.util.ForgeDirection; @@ -13,155 +17,186 @@ import cr0s.warpdrive.PacketHandler; import cr0s.warpdrive.WarpDrive; import cr0s.warpdrive.WarpDriveConfig; import cr0s.warpdrive.data.Vector3; +import dan200.computercraft.api.lua.ILuaContext; +import dan200.computercraft.api.peripheral.IComputerAccess; public class TileEntityLift extends WarpEnergyTE { - private static final int MODE_REDSTONE = -1; - private static final int MODE_INACTIVE = 0; - private static final int MODE_UP = 1; - private static final int MODE_DOWN = 2; - - private int firstUncoveredY; - private int mode = MODE_INACTIVE; - private boolean isEnabled = false; - private boolean computerEnabled = true; - private int computerMode = MODE_REDSTONE; - - private int tickCount = 0; - - public TileEntityLift() { - super(); - peripheralName = "warpdriveLaserLift"; - methodsArray = new String[] { - "getEnergyLevel", - "mode", - "active", - "help" - }; + private static final int MODE_REDSTONE = -1; + private static final int MODE_INACTIVE = 0; + private static final int MODE_UP = 1; + private static final int MODE_DOWN = 2; + + private int firstUncoveredY; + private int mode = MODE_INACTIVE; + private boolean isEnabled = false; + private boolean computerEnabled = true; + private int computerMode = MODE_REDSTONE; + + private int tickCount = 0; + + public TileEntityLift() { + super(); + peripheralName = "warpdriveLaserLift"; + methodsArray = new String[] { "getEnergyLevel", "mode", "active", + "help" }; } - - @Override - public void updateEntity() { + + @Override + public void updateEntity() { if (FMLCommonHandler.instance().getEffectiveSide().isClient()) { return; } super.updateEntity(); tickCount++; - if (tickCount >= WarpDriveConfig.LL_TICK_RATE) { - tickCount = 0; - - // Switching mode - if (computerMode == MODE_DOWN || (computerMode == MODE_REDSTONE && worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord))) { - mode = MODE_DOWN; - } else { - mode = MODE_UP; - } - - isEnabled = computerEnabled && isPassableBlock(yCoord + 1) && isPassableBlock(yCoord + 2) && isPassableBlock(yCoord - 1) && isPassableBlock(yCoord - 2); - - if (getEnergyStored() < WarpDriveConfig.LL_LIFT_ENERGY || !isEnabled) { - mode = MODE_INACTIVE; - if (getBlockMetadata() != 0) { - worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 0, 2); // disabled - } - return; - } - - if (getBlockMetadata() != mode) { - worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, mode, 2); // current mode - } - - // Launch a beam: search non-air blocks under lift - for (int ny = yCoord - 2; ny > 0; ny--) { - if (!isPassableBlock(ny)) { - firstUncoveredY = ny + 1; - break; - } - } - - if (yCoord - firstUncoveredY >= 2) { - if (mode == MODE_UP) { - PacketHandler.sendBeamPacket(worldObj, new Vector3(xCoord + 0.5D, firstUncoveredY, zCoord + 0.5D), new Vector3(xCoord + 0.5D, yCoord, zCoord + 0.5D), 0f, 1f, 0f, 40, 0, 100); - } else if (mode == MODE_DOWN) { - PacketHandler.sendBeamPacket(worldObj, new Vector3(xCoord + 0.5D, yCoord, zCoord + 0.5D), new Vector3(xCoord + 0.5D, firstUncoveredY, zCoord + 0.5D), 0f, 0f, 1f, 40, 0, 100); - } - - liftEntity(); - } - } - } - - private boolean isPassableBlock(int yPosition) { - int blockId = worldObj.getBlockId(xCoord, yPosition, zCoord); - // 63 & 68 are signs - return blockId == 0 || blockId == 63 || blockId == 68 || WarpDriveConfig.isAirBlock(worldObj, blockId, xCoord, yPosition, zCoord); - } + if (tickCount >= WarpDriveConfig.LL_TICK_RATE) { + tickCount = 0; - private void liftEntity() { - final double CUBE_RADIUS = 0.4; - double xmax, zmax; - double xmin, zmin; - - xmin = xCoord + 0.5 - CUBE_RADIUS; - xmax = xCoord + 0.5 + CUBE_RADIUS; - zmin = zCoord + 0.5 - CUBE_RADIUS; - zmax = zCoord + 0.5 + CUBE_RADIUS; - - // Lift up - if (mode == MODE_UP) { - AxisAlignedBB aabb = AxisAlignedBB.getBoundingBox(xmin, firstUncoveredY, zmin, xmax, yCoord, zmax); - List list = worldObj.getEntitiesWithinAABBExcludingEntity(null, aabb); - if (list != null) { - for (Object o : list) { - if (o != null && o instanceof EntityLivingBase && consumeEnergy(WarpDriveConfig.LL_LIFT_ENERGY, true)) { - ((EntityLivingBase)o).setPositionAndUpdate(xCoord + 0.5D, yCoord + 1.0D, zCoord + 0.5D); - PacketHandler.sendBeamPacket(worldObj, new Vector3(xCoord + 0.5D, firstUncoveredY, zCoord + 0.5D), new Vector3(xCoord + 0.5D, yCoord, zCoord + 0.5D), 1F, 1F, 0F, 40, 0, 100); - worldObj.playSoundEffect(xCoord + 0.5D, yCoord, zCoord + 0.5D, "warpdrive:hilaser", 4F, 1F); - consumeEnergy(WarpDriveConfig.LL_LIFT_ENERGY, false); - } - } - } - } else if (mode == MODE_DOWN) { - AxisAlignedBB aabb = AxisAlignedBB.getBoundingBox(xmin, Math.min(firstUncoveredY + 4.0D, yCoord), zmin, xmax, yCoord + 2.0D, zmax); - List list = worldObj.getEntitiesWithinAABBExcludingEntity(null, aabb); - if (list != null) { - for (Object o : list) { - if (o != null && o instanceof EntityLivingBase && consumeEnergy(WarpDriveConfig.LL_LIFT_ENERGY, true)) { - ((EntityLivingBase)o).setPositionAndUpdate(xCoord + 0.5D, firstUncoveredY, zCoord + 0.5D); - PacketHandler.sendBeamPacket(worldObj, new Vector3(xCoord + 0.5D, yCoord, zCoord + 0.5D), new Vector3(xCoord + 0.5D, firstUncoveredY, zCoord + 0.5D), 1F, 1F, 0F, 40, 0, 100); - worldObj.playSoundEffect(xCoord + 0.5D, yCoord, zCoord + 0.5D, "warpdrive:hilaser", 4F, 1F); + // Switching mode + if (computerMode == MODE_DOWN + || (computerMode == MODE_REDSTONE && worldObj + .isBlockIndirectlyGettingPowered(xCoord, yCoord, + zCoord))) { + mode = MODE_DOWN; + } else { + mode = MODE_UP; + } + + isEnabled = computerEnabled && isPassableBlock(yCoord + 1) + && isPassableBlock(yCoord + 2) + && isPassableBlock(yCoord - 1) + && isPassableBlock(yCoord - 2); + + if (getEnergyStored() < WarpDriveConfig.LL_LIFT_ENERGY + || !isEnabled) { + mode = MODE_INACTIVE; + if (getBlockMetadata() != 0) { + worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, + 0, 2); // disabled + } + return; + } + + if (getBlockMetadata() != mode) { + worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, + mode, 2); // current mode + } + + // Launch a beam: search non-air blocks under lift + for (int ny = yCoord - 2; ny > 0; ny--) { + if (!isPassableBlock(ny)) { + firstUncoveredY = ny + 1; + break; + } + } + + if (yCoord - firstUncoveredY >= 2) { + if (mode == MODE_UP) { + PacketHandler.sendBeamPacket(worldObj, new Vector3( + xCoord + 0.5D, firstUncoveredY, zCoord + 0.5D), + new Vector3(xCoord + 0.5D, yCoord, zCoord + 0.5D), + 0f, 1f, 0f, 40, 0, 100); + } else if (mode == MODE_DOWN) { + PacketHandler.sendBeamPacket(worldObj, new Vector3( + xCoord + 0.5D, yCoord, zCoord + 0.5D), new Vector3( + xCoord + 0.5D, firstUncoveredY, zCoord + 0.5D), 0f, + 0f, 1f, 40, 0, 100); + } + + liftEntity(); + } + } + } + + private boolean isPassableBlock(int yPosition) { + Block block = worldObj.getBlock(xCoord, yPosition, zCoord); + //TODO: Make configurable or less specific + return block.isAssociatedBlock(Blocks.air) + || block.isAssociatedBlock(Blocks.wall_sign) + || block.isAssociatedBlock(Blocks.standing_sign) + || worldObj.isAirBlock(xCoord, yPosition, zCoord); + } + + private void liftEntity() { + final double CUBE_RADIUS = 0.4; + double xmax, zmax; + double xmin, zmin; + + xmin = xCoord + 0.5 - CUBE_RADIUS; + xmax = xCoord + 0.5 + CUBE_RADIUS; + zmin = zCoord + 0.5 - CUBE_RADIUS; + zmax = zCoord + 0.5 + CUBE_RADIUS; + + // Lift up + if (mode == MODE_UP) { + AxisAlignedBB aabb = AxisAlignedBB.getBoundingBox(xmin, + firstUncoveredY, zmin, xmax, yCoord, zmax); + List list = worldObj.getEntitiesWithinAABBExcludingEntity(null, + aabb); + if (list != null) { + for (Object o : list) { + if (o != null + && o instanceof EntityLivingBase + && consumeEnergy(WarpDriveConfig.LL_LIFT_ENERGY, + true)) { + ((EntityLivingBase) o).setPositionAndUpdate( + xCoord + 0.5D, yCoord + 1.0D, zCoord + 0.5D); + PacketHandler.sendBeamPacket(worldObj, new Vector3( + xCoord + 0.5D, firstUncoveredY, zCoord + 0.5D), + new Vector3(xCoord + 0.5D, yCoord, + zCoord + 0.5D), 1F, 1F, 0F, 40, 0, 100); + worldObj.playSoundEffect(xCoord + 0.5D, yCoord, + zCoord + 0.5D, "warpdrive:hilaser", 4F, 1F); consumeEnergy(WarpDriveConfig.LL_LIFT_ENERGY, false); - } - } - } - } - } + } + } + } + } else if (mode == MODE_DOWN) { + AxisAlignedBB aabb = AxisAlignedBB.getBoundingBox(xmin, + Math.min(firstUncoveredY + 4.0D, yCoord), zmin, xmax, + yCoord + 2.0D, zmax); + List list = worldObj.getEntitiesWithinAABBExcludingEntity(null, + aabb); + if (list != null) { + for (Object o : list) { + if (o != null + && o instanceof EntityLivingBase + && consumeEnergy(WarpDriveConfig.LL_LIFT_ENERGY, + true)) { + ((EntityLivingBase) o).setPositionAndUpdate( + xCoord + 0.5D, firstUncoveredY, zCoord + 0.5D); + PacketHandler.sendBeamPacket(worldObj, new Vector3( + xCoord + 0.5D, yCoord, zCoord + 0.5D), + new Vector3(xCoord + 0.5D, firstUncoveredY, + zCoord + 0.5D), 1F, 1F, 0F, 40, 0, 100); + worldObj.playSoundEffect(xCoord + 0.5D, yCoord, + zCoord + 0.5D, "warpdrive:hilaser", 4F, 1F); + consumeEnergy(WarpDriveConfig.LL_LIFT_ENERGY, false); + } + } + } + } + } - @Override - public void readFromNBT(NBTTagCompound tag) { - super.readFromNBT(tag); - } + @Override + public void readFromNBT(NBTTagCompound tag) { + super.readFromNBT(tag); + } - @Override - public void writeToNBT(NBTTagCompound tag) { - super.writeToNBT(tag); - } + @Override + public void writeToNBT(NBTTagCompound tag) { + super.writeToNBT(tag); + } - @Override - public int getMaxEnergyStored() { - return WarpDriveConfig.LL_MAX_ENERGY; - } - - @Override - public boolean canInputEnergy(ForgeDirection from) { - return true; - } - - // IEnergySink methods implementation - @Override - public int getMaxSafeInput() { - return Integer.MAX_VALUE; - } + @Override + public int getMaxEnergyStored() { + return WarpDriveConfig.LL_MAX_ENERGY; + } + + @Override + public boolean canInputEnergy(ForgeDirection from) { + return true; + } // OpenComputer callback methods @Callback @@ -169,7 +204,7 @@ public class TileEntityLift extends WarpEnergyTE { private Object[] mode(Context context, Arguments arguments) { return mode(argumentsOCtoCC(arguments)); } - + @Callback @Optional.Method(modid = "OpenComputers") private Object[] active(Context context, Arguments arguments) { @@ -178,62 +213,75 @@ public class TileEntityLift extends WarpEnergyTE { } return new Object[] { computerEnabled ? false : isEnabled }; } - + private Object[] mode(Object[] arguments) { if (arguments.length == 1) { if (arguments[0].toString().equals("up")) { computerMode = MODE_UP; - } else if(arguments[0].toString().equals("down")) { + } else if (arguments[0].toString().equals("down")) { computerMode = MODE_DOWN; } else { computerMode = MODE_REDSTONE; } } switch (computerMode) { - case -1: - return new Object[] { "redstone" }; - case 1: - return new Object[] { "up" }; - case 2: - return new Object[] { "down" }; + case -1: + return new Object[] { "redstone" }; + case 1: + return new Object[] { "up" }; + case 2: + return new Object[] { "down" }; } return null; } - public String helpStr(Object[] args) { - if (args.length == 1) { - String methodName = args[0].toString().toLowerCase(); - if (methodName.equals("getEnergyLevel")) { - return WarpDrive.defEnergyStr; - } else if (methodName.equals("mode")) { - return "mode(\"up\" or \"down\" or \"redstone\"): sets the mode\nmode(): returns the current mode"; - } else if (methodName.equals("active")) { - return "active(boolean): sets whether the laser is active\nactive(): returns whether the laser is active"; - } - } - return WarpDrive.defHelpStr; - } + public String helpStr(Object[] args) { + if (args.length == 1) { + String methodName = args[0].toString().toLowerCase(); + if (methodName.equals("getEnergyLevel")) { + return WarpDrive.defEnergyStr; + } else if (methodName.equals("mode")) { + return "mode(\"up\" or \"down\" or \"redstone\"): sets the mode\nmode(): returns the current mode"; + } else if (methodName.equals("active")) { + return "active(boolean): sets whether the laser is active\nactive(): returns whether the laser is active"; + } + } + return WarpDrive.defHelpStr; + } - // ComputerCraft IPeripheral methods implementation - @Override + // ComputerCraft IPeripheral methods implementation + @Override @Optional.Method(modid = "ComputerCraft") - public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception { - String methodName = methodsArray[method]; - if (methodName.equals("getEnergyLevel")) { - return getEnergyLevel(); - + public Object[] callMethod(IComputerAccess computer, ILuaContext context, + int method, Object[] arguments) { + String methodName = methodsArray[method]; + if (methodName.equals("getEnergyLevel")) { + return getEnergyLevel(); + } else if (methodName.equals("mode")) { return mode(arguments); - + } else if (methodName.equals("active")) { if (arguments.length == 1) { computerEnabled = toBool(arguments); - } - return new Object[] { computerEnabled ? false : isEnabled }; - - } else if (methodName.equals("help")) { - return new Object[] { helpStr(arguments) }; - } - return null; - } + } + return new Object[] { computerEnabled ? false : isEnabled }; + + } else if (methodName.equals("help")) { + return new Object[] { helpStr(arguments) }; + } + return null; + } + + @Override + public int getSinkTier() { + // TODO Arbitrarily chosen value + return 2; + } + + @Override + public int getSourceTier() { + // TODO Arbitrarily chosen value + return 2; + } } diff --git a/src/java/cr0s/warpdrive/machines/TileEntityMiningLaser.java b/src/java/cr0s/warpdrive/machines/TileEntityMiningLaser.java index be2ad544..f353286a 100644 --- a/src/java/cr0s/warpdrive/machines/TileEntityMiningLaser.java +++ b/src/java/cr0s/warpdrive/machines/TileEntityMiningLaser.java @@ -3,7 +3,6 @@ package cr0s.warpdrive.machines; import java.util.ArrayList; import java.util.List; -import jdk.nashorn.internal.runtime.regexp.joni.constants.Arguments; import net.minecraft.block.Block; import net.minecraft.entity.item.EntityItem; import net.minecraft.inventory.IInventory; diff --git a/src/java/cr0s/warpdrive/machines/TileEntityMonitor.java b/src/java/cr0s/warpdrive/machines/TileEntityMonitor.java index 2b43b26b..299df34e 100644 --- a/src/java/cr0s/warpdrive/machines/TileEntityMonitor.java +++ b/src/java/cr0s/warpdrive/machines/TileEntityMonitor.java @@ -1,11 +1,15 @@ package cr0s.warpdrive.machines; -import jdk.nashorn.internal.runtime.regexp.joni.constants.Arguments; +import li.cil.oc.api.machine.Arguments; +import li.cil.oc.api.machine.Callback; +import li.cil.oc.api.machine.Context; import net.minecraft.nbt.NBTTagCompound; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Optional; import cr0s.warpdrive.PacketHandler; import cr0s.warpdrive.WarpDrive; +import dan200.computercraft.api.lua.ILuaContext; +import dan200.computercraft.api.peripheral.IComputerAccess; public class TileEntityMonitor extends WarpInterfacedTE { private int frequency = -1; @@ -71,7 +75,7 @@ public class TileEntityMonitor extends WarpInterfacedTE { // ComputerCraft IPeripheral methods implementation @Override @Optional.Method(modid = "ComputerCraft") - public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception { + public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) { String methodName = methodsArray[method]; if (methodName.equals("freq")) { if (arguments.length == 1) { diff --git a/src/java/cr0s/warpdrive/machines/TileEntityParticleBooster.java b/src/java/cr0s/warpdrive/machines/TileEntityParticleBooster.java index e29a122b..5ff0aff7 100644 --- a/src/java/cr0s/warpdrive/machines/TileEntityParticleBooster.java +++ b/src/java/cr0s/warpdrive/machines/TileEntityParticleBooster.java @@ -42,13 +42,20 @@ public class TileEntityParticleBooster extends WarpEnergyTE { return WarpDriveConfig.PB_MAX_ENERGY_VALUE; } - @Override - public int getMaxSafeInput() { - return Integer.MAX_VALUE; - } - @Override public boolean canInputEnergy(ForgeDirection from) { return true; } + + @Override + public int getSinkTier() { + // TODO Auto-generated method stub + return 3; + } + + @Override + public int getSourceTier() { + // TODO Auto-generated method stub + return 3; + } } diff --git a/src/java/cr0s/warpdrive/machines/TileEntityProtocol.java b/src/java/cr0s/warpdrive/machines/TileEntityProtocol.java index 6cdad717..ffe5e725 100644 --- a/src/java/cr0s/warpdrive/machines/TileEntityProtocol.java +++ b/src/java/cr0s/warpdrive/machines/TileEntityProtocol.java @@ -2,16 +2,22 @@ package cr0s.warpdrive.machines; import java.util.ArrayList; -import jdk.nashorn.internal.runtime.regexp.joni.constants.Arguments; +import li.cil.oc.api.machine.Arguments; +import li.cil.oc.api.machine.Callback; +import li.cil.oc.api.machine.Context; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.ChatComponentText; import net.minecraft.util.DamageSource; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Optional; import cr0s.warpdrive.WarpDrive; import cr0s.warpdrive.WarpDriveConfig; import cr0s.warpdrive.machines.TileEntityReactor.ReactorMode; +import dan200.computercraft.api.ComputerCraftAPI; +import dan200.computercraft.api.lua.ILuaContext; +import dan200.computercraft.api.peripheral.IComputerAccess; /** * Protocol block tile entity @@ -162,19 +168,19 @@ public class TileEntityProtocol extends WarpInterfacedTE { public void attachPlayer(EntityPlayer entityPlayer) { for (int i = 0; i < players.size(); i++) { - String nick = players.get(i); + String name = players.get(i); - if (entityPlayer.username.equals(nick)) { - entityPlayer.addChatMessage(getBlockType().getLocalizedName() + " Detached."); + if (entityPlayer.getDisplayName().equals(name)) { + entityPlayer.addChatMessage(new ChatComponentText(getBlockType().getLocalizedName() + " Detached.")); players.remove(i); return; } } entityPlayer.attackEntityFrom(DamageSource.generic, 1); - players.add(entityPlayer.username); + players.add(entityPlayer.getDisplayName()); updatePlayersString(); - entityPlayer.addChatMessage(getBlockType().getLocalizedName() + " Successfully attached."); + entityPlayer.addChatMessage(new ChatComponentText(getBlockType().getLocalizedName() + " Successfully attached.")); } public void updatePlayersString() { @@ -735,7 +741,7 @@ public class TileEntityProtocol extends WarpInterfacedTE { @Override @Optional.Method(modid = "ComputerCraft") - public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception { + public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) { String methodName = methodsArray[method]; if (methodName.equals("dim_positive")) {// dim_positive (front, right, up) diff --git a/src/java/cr0s/warpdrive/machines/TileEntityRadar.java b/src/java/cr0s/warpdrive/machines/TileEntityRadar.java index 0229891a..fb9ef3e8 100644 --- a/src/java/cr0s/warpdrive/machines/TileEntityRadar.java +++ b/src/java/cr0s/warpdrive/machines/TileEntityRadar.java @@ -2,13 +2,18 @@ package cr0s.warpdrive.machines; import java.util.ArrayList; -import jdk.nashorn.internal.runtime.regexp.joni.constants.Arguments; +import li.cil.oc.api.machine.Arguments; +import li.cil.oc.api.machine.Callback; +import li.cil.oc.api.machine.Context; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.util.ForgeDirection; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Optional; import cr0s.warpdrive.WarpDrive; import cr0s.warpdrive.WarpDriveConfig; +import dan200.computercraft.api.ComputerCraftAPI; +import dan200.computercraft.api.lua.ILuaContext; +import dan200.computercraft.api.peripheral.IComputerAccess; public class TileEntityRadar extends WarpEnergyTE { private ArrayList results; @@ -130,7 +135,7 @@ public class TileEntityRadar extends WarpEnergyTE { TileEntityReactor res = results.get(index); if (res != null) { - int yAddition = (res.worldObj.provider.dimensionId == WarpDriveConfig.G_SPACE_DIMENSION_ID) ? 256 : (res.worldObj.provider.dimensionId == WarpDriveConfig.G_HYPERSPACE_DIMENSION_ID) ? 512 : 0; + int yAddition = (res.getWorldObj().provider.dimensionId == WarpDriveConfig.G_SPACE_DIMENSION_ID) ? 256 : (res.getWorldObj().provider.dimensionId == WarpDriveConfig.G_HYPERSPACE_DIMENSION_ID) ? 512 : 0; return new Object[] { res.coreFrequency, res.xCoord, res.yCoord + yAddition, res.zCoord }; } } @@ -165,7 +170,7 @@ public class TileEntityRadar extends WarpEnergyTE { @Override @Optional.Method(modid = "ComputerCraft") - public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception { + public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) { String methodName = methodsArray[method]; if (methodName.equals("scanRadius")) {// scanRadius (radius) return scanRadius(arguments); @@ -194,14 +199,20 @@ public class TileEntityRadar extends WarpEnergyTE { return WarpDriveConfig.WR_MAX_ENERGY_VALUE; } - // IEnergySink methods implementation - @Override - public int getMaxSafeInput() { - return Integer.MAX_VALUE; - } - @Override public boolean canInputEnergy(ForgeDirection from) { return true; } + + @Override + public int getSinkTier() { + // TODO Arbitrarily chosen value + return 3; + } + + @Override + public int getSourceTier() { + // TODO Arbitrarily chosen value + return 3; + } } diff --git a/src/java/cr0s/warpdrive/machines/TileEntityReactor.java b/src/java/cr0s/warpdrive/machines/TileEntityReactor.java index 919f083a..39b14a9b 100644 --- a/src/java/cr0s/warpdrive/machines/TileEntityReactor.java +++ b/src/java/cr0s/warpdrive/machines/TileEntityReactor.java @@ -6,6 +6,7 @@ import net.minecraft.block.Block; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.init.Blocks; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.potion.Potion; @@ -14,7 +15,9 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntityChest; import net.minecraft.util.AxisAlignedBB; +import net.minecraft.util.ChatComponentText; import net.minecraft.util.MathHelper; +import net.minecraft.util.Vec3; import net.minecraft.world.WorldServer; import net.minecraftforge.common.DimensionManager; import net.minecraftforge.common.util.ForgeDirection; @@ -28,1082 +31,1074 @@ import cr0s.warpdrive.world.SpaceTeleporter; /** * @author Cr0s */ -public class TileEntityReactor extends WarpEnergyTE -{ - public Boolean ready; +public class TileEntityReactor extends WarpEnergyTE { + public enum ReactorMode { + IDLE(0), BASIC_JUMP(1), // 0-128 + LONG_JUMP(2), // 0-12800 + TELEPORT(3), BEACON_JUMP(4), // Jump ship by beacon + HYPERSPACE(5), // Jump to/from Hyperspace + GATE_JUMP(6); // Jump via jumpgate - public Boolean launchState = false; + private final int code; - public final int JUMP_UP = -1; - public final int JUMP_DOWN = -2; - public int dx, dz; - private int direction; - - public int maxX, maxY, maxZ; - public int minX, minY, minZ; - - public int shipFront, shipBack; - public int shipLeft, shipRight; - public int shipUp, shipDown; - public int shipLength; - public int shipVolume; - private ReactorMode currentMode = ReactorMode.IDLE; - - public enum ReactorMode { - IDLE ( 0 ), - BASIC_JUMP ( 1 ), // 0-128 - LONG_JUMP ( 2 ), // 0-12800 - TELEPORT ( 3 ), - BEACON_JUMP ( 4 ), // Jump ship by beacon - HYPERSPACE ( 5 ), // Jump to/from Hyperspace - GATE_JUMP ( 6 ); // Jump via jumpgate - - private final int code; - ReactorMode(int code) { - this.code = code; + ReactorMode(int code) { + this.code = code; } - - public int getCode() { - return code; - } - } - - private int warmupTime = 0; - private int cooldownTime = 0; - public int randomWarmupAddition = 0; - private int chestTeleportUpdateTicks = 0; - private int registryUpdateTicks = 15; - public String coreFrequency = "default"; - - public int isolationBlocksCount = 0; - public double isolationRate = 0.0D; - public int isolationUpdateTicks = 0; - - public TileEntityProtocol controller; - - private boolean soundPlayed = false; - - @Override - public void updateEntity() { - if (FMLCommonHandler.instance().getEffectiveSide().isClient()) { - return; - } - super.updateEntity(); - - // Always cooldown - if (cooldownTime > 0) { - cooldownTime--; - warmupTime = 0; - } - - // Update state - if (cooldownTime > 0) { // cooling down (2) - if (getBlockMetadata() != 2) { - worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 2, 1 + 2); - } - } else if (controller == null) { // not connected (0) - if (getBlockMetadata() != 0) { - worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 0, 1 + 2); - } - } else if (controller.isJumpFlag() || this.controller.isSummonAllFlag() || !this.controller.getToSummon().isEmpty()) { // active (1) - if (getBlockMetadata() != 1) { - worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 1, 1 + 2); - } - } else { // inactive - if (getBlockMetadata() != 0) { - worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 0, 1 + 2); - } - } - - // Update warp core in cores registry - registryUpdateTicks++; - if (registryUpdateTicks > WarpDriveConfig.WC_CORES_REGISTRY_UPDATE_INTERVAL_SECONDS * 20) { - registryUpdateTicks = 0; - WarpDrive.warpCores.updateInRegistry(this); -// WarpDrive.warpCores.printRegistry(); -// WarpDrive.debugPrint("" + this + " controller is " + controller + ", warmupTime " + warmupTime + ", currentMode " + currentMode + ", jumpFlag " + (controller == null ? "NA" : controller.isJumpFlag()) + ", cooldownTime " + cooldownTime); - - TileEntity c = findControllerBlock(); - if (c == null) { - controller = null; - warmupTime = 0; - soundPlayed = false; - return; - } - controller = (TileEntityProtocol)c; - } - - isolationUpdateTicks++; - if (isolationUpdateTicks > WarpDriveConfig.WC_ISOLATION_UPDATE_INTERVAL_SECONDS * 20) { - isolationUpdateTicks = 0; - updateIsolationState(); - } - - if (controller == null) { - return; - } - - currentMode = controller.getMode(); - - StringBuilder reason = new StringBuilder(); - - if ((controller.isJumpFlag() && (isolationUpdateTicks == 1)) || this.controller.isSummonAllFlag() || !this.controller.getToSummon().isEmpty()) { - if (!validateShipSpatialParameters(reason)) { - if (controller.isJumpFlag()) { - controller.setJumpFlag(false); - messageToAllPlayersOnShip(reason.toString()); - } - warmupTime = 0; - soundPlayed = false; - return; - } - - if (this.controller.isSummonAllFlag()) { - summonPlayers(); - controller.setSummonAllFlag(false); - } else if (!this.controller.getToSummon().isEmpty()) { - summonSinglePlayer(this.controller.getToSummon()); - this.controller.setToSummon(""); - } - } - - switch (currentMode) { - case TELEPORT: - if (worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord)) { - if (isChestSummonMode()) { - chestTeleportUpdateTicks++; - if (chestTeleportUpdateTicks >= 20) { - summonPlayersByChestCode(); - chestTeleportUpdateTicks = 0; - } - } else { - teleportPlayersToSpace(); - } - } else { - chestTeleportUpdateTicks = 0; - } - break; - - case BASIC_JUMP: - case LONG_JUMP: - case BEACON_JUMP: - case HYPERSPACE: - case GATE_JUMP: - if (controller.isJumpFlag()) { - // Compute warm-up time - int targetWarmup = 0; - switch (currentMode) { - case BASIC_JUMP: - case LONG_JUMP: - if (controller.getDistance() < 50) { - targetWarmup = WarpDriveConfig.WC_WARMUP_SHORTJUMP_SECONDS * 20; - } else { - targetWarmup = WarpDriveConfig.WC_WARMUP_LONGJUMP_SECONDS * 20; - } - break; - - case BEACON_JUMP: - case HYPERSPACE: - case GATE_JUMP: - default: - targetWarmup = WarpDriveConfig.WC_WARMUP_LONGJUMP_SECONDS * 20; - break; - } - // Select best sound file and adjust offset - int soundThreshold = 0; - String soundFile = ""; - if (targetWarmup < 10 * 20) { - soundThreshold = targetWarmup - 4 * 20; - soundFile = "warpdrive:warp_4s"; - } else if (targetWarmup > 29 * 20) { - soundThreshold = targetWarmup - 30 * 20; - soundFile = "warpdrive:warp_30s"; - } else { - soundThreshold = targetWarmup - 10 * 20; - soundFile = "warpdrive:warp_10s"; - } - // Add random duration - soundThreshold += randomWarmupAddition; - - // Check cooldown time - if (cooldownTime > 0) { - if (cooldownTime % 20 == 0) { - int seconds = cooldownTime / 20; - if ((seconds < 5) || ((seconds < 30) && (seconds % 5 == 0)) || (seconds % 10 == 0)) { - messageToAllPlayersOnShip("Warp core is cooling down... " + cooldownTime / 20 + "s to go..."); - } - } - return; - } - - // Set up activated animation - if (warmupTime == 0) { - messageToAllPlayersOnShip("Running pre-jump checklist..."); - - // update ship parameters - if (!validateShipSpatialParameters(reason)) { - controller.setJumpFlag(false); - messageToAllPlayersOnShip(reason.toString()); - return; - } - // WarpDrive.debugPrint(this + " Giving warp sickness targetWarmup " + targetWarmup + " distance " + controller.getDistance()); - makePlayersOnShipDrunk(targetWarmup + WarpDriveConfig.WC_WARMUP_RANDOM_TICKS); - } - - if (!soundPlayed && (soundThreshold > warmupTime)) { - // WarpDrive.debugPrint(this + " Playing sound effect '" + soundFile + "' soundThreshold " + soundThreshold + " warmupTime " + warmupTime); - worldObj.playSoundEffect(xCoord + 0.5f, yCoord + 0.5f, zCoord + 0.5f, soundFile, 4F, 1F); - soundPlayed = true; - } - - // Awaiting cool-down time - if (warmupTime < (targetWarmup + randomWarmupAddition)) { - warmupTime++; - return; - } - - warmupTime = 0; - soundPlayed = false; - - if (!validateShipSpatialParameters(reason)) { - controller.setJumpFlag(false); - messageToAllPlayersOnShip(reason.toString()); - return; - } - - if (WarpDrive.warpCores.isWarpCoreIntersectsWithOthers(this)) { - controller.setJumpFlag(false); - messageToAllPlayersOnShip("Warp field intersects with other ship's field. Cannot jump."); - return; - } - - if (WarpDrive.cloaks.isCloaked(worldObj.provider.dimensionId, xCoord, yCoord, zCoord)) { - controller.setJumpFlag(false); - messageToAllPlayersOnShip("Core is inside a cloaking field. Aborting. Disable cloaking field to jump!"); - return; - } - - doJump(); - cooldownTime = WarpDriveConfig.WC_COOLDOWN_INTERVAL_SECONDS * 20; - controller.setJumpFlag(false); - } else { - warmupTime = 0; - } - break; - } - } - - public void messageToAllPlayersOnShip(String msg) { - AxisAlignedBB axisalignedbb = AxisAlignedBB.getBoundingBox(this.minX, this.minY, this.minZ, this.maxX + 0.99D, this.maxY + 0.99D, this.maxZ + 0.99D); - List list = worldObj.getEntitiesWithinAABBExcludingEntity(null, axisalignedbb); - - System.out.println("" + (FMLCommonHandler.instance().getEffectiveSide().isClient() ? "Client":"Server") + this + " messageToAllPlayersOnShip: " + msg); - for (Object o : list) { - if (o == null || !(o instanceof EntityPlayer)) { - continue; - } - - ((EntityPlayer)o).addChatMessage("[" + (coreFrequency.length() > 0 ? coreFrequency : "WarpCore") + "] " + msg); - } - } - - private void updateIsolationState() { - // Search block in cube around core - int xmax, ymax, zmax; - int xmin, ymin, zmin; - xmin = xCoord - WarpDriveConfig.WR_MAX_ISOLATION_RANGE; - xmax = xCoord + WarpDriveConfig.WR_MAX_ISOLATION_RANGE; - - zmin = zCoord - WarpDriveConfig.WR_MAX_ISOLATION_RANGE; - zmax = zCoord + WarpDriveConfig.WR_MAX_ISOLATION_RANGE; - - // scan 1 block higher to encourage putting isolation block on both ground and ceiling - ymin = Math.max( 0, yCoord - WarpDriveConfig.WR_MAX_ISOLATION_RANGE + 1); - ymax = Math.min(255, yCoord + WarpDriveConfig.WR_MAX_ISOLATION_RANGE + 1); - - int newCount = 0; - - // Search for warp isolation blocks - for (int y = ymin; y <= ymax; y++) { - for (int x = xmin; x <= xmax; x++) { - for (int z = zmin; z <= zmax; z++) { - if (worldObj.getBlockId(x, y, z) == WarpDriveConfig.isolationID) { - newCount++; - } - } - } - } - isolationBlocksCount = newCount; - if (isolationBlocksCount >= WarpDriveConfig.WR_MIN_ISOLATION_BLOCKS) { - isolationRate = WarpDriveConfig.WR_MIN_ISOLATION_EFFECT - + (isolationBlocksCount - WarpDriveConfig.WR_MIN_ISOLATION_BLOCKS) // bonus blocks - * (WarpDriveConfig.WR_MAX_ISOLATION_EFFECT - WarpDriveConfig.WR_MIN_ISOLATION_EFFECT) - / (WarpDriveConfig.WR_MAX_ISOLATION_BLOCKS - WarpDriveConfig.WR_MIN_ISOLATION_BLOCKS); - } else { - isolationRate = 0.0D; + public int getCode() { + return code; } - // WarpDrive.debugPrint(this + " Isolation updated to " + isolationBlocksCount + " (" + String.format("%.1f", isolationRate * 100) + "%)"); - } + } - private void makePlayersOnShipDrunk(int tickDuration) { - AxisAlignedBB axisalignedbb = AxisAlignedBB.getBoundingBox(this.minX, this.minY, this.minZ, this.maxX, this.maxY, this.maxZ); - List list = worldObj.getEntitiesWithinAABBExcludingEntity(null, axisalignedbb); + public static int calculateRequiredEnergy(ReactorMode currentMode, int shipVolume, int jumpDistance) { + switch (currentMode) { + case TELEPORT: + return WarpDriveConfig.WC_ENERGY_PER_ENTITY_TO_SPACE; - for (Object o : list) { - if (o == null || !(o instanceof EntityPlayer)) { - continue; - } + case BASIC_JUMP: + return (WarpDriveConfig.WC_ENERGY_PER_BLOCK_MODE1 * shipVolume) + (WarpDriveConfig.WC_ENERGY_PER_DISTANCE_MODE1 * jumpDistance); - // Set "drunk" effect - ((EntityPlayer)o).addPotionEffect(new PotionEffect(Potion.confusion.id, tickDuration, 0, true)); - } - } + case LONG_JUMP: + return (WarpDriveConfig.WC_ENERGY_PER_BLOCK_MODE2 * shipVolume) + (WarpDriveConfig.WC_ENERGY_PER_DISTANCE_MODE2 * jumpDistance); - private void summonPlayers() - { - AxisAlignedBB aabb = AxisAlignedBB.getBoundingBox(this.minX, this.minY, this.minZ, this.maxX, this.maxY, this.maxZ); + case HYPERSPACE: + return WarpDriveConfig.WC_MAX_ENERGY_VALUE / 10; // 10% of maximum - for (int i = 0; i < controller.players.size(); i++) - { - String nick = controller.players.get(i); - EntityPlayerMP player = MinecraftServer.getServer().getConfigurationManager().getPlayerForUsername(nick); + case BEACON_JUMP: + return WarpDriveConfig.WC_MAX_ENERGY_VALUE / 2; // half of maximum - if (player != null && !testBB(aabb, MathHelper.floor_double(player.posX), MathHelper.floor_double(player.posY), MathHelper.floor_double(player.posZ))) - { - summonPlayer(player, xCoord + dx, yCoord, zCoord + dz); - } - } - } + case GATE_JUMP: + return 2 * shipVolume; + default: + break; + } - private void summonSinglePlayer(String nickname) - { - AxisAlignedBB aabb = AxisAlignedBB.getBoundingBox(this.minX, this.minY, this.minZ, this.maxX, this.maxY, this.maxZ); + return WarpDriveConfig.WC_MAX_ENERGY_VALUE; + } - for (int i = 0; i < controller.players.size(); i++) - { - String nick = controller.players.get(i); - EntityPlayerMP player = MinecraftServer.getServer().getConfigurationManager().getPlayerForUsername(nick); + private static boolean checkPlayerInventory(TileEntityChest chest, EntityPlayerMP player) { + Boolean result = false; + final int MIN_KEY_LENGTH = 5; + int keyLength = 0; - if (player != null && nick.equals(nickname) && !testBB(aabb, MathHelper.floor_double(player.posX), MathHelper.floor_double(player.posY), MathHelper.floor_double(player.posZ))) - { - summonPlayer(player, xCoord + dx, yCoord, zCoord + dz); - return; - } - } - } + for (int index = 0; index < chest.getSizeInventory(); index++) { + ItemStack chestItem = chest.getStackInSlot(index); + ItemStack playerItem = player.inventory.getStackInSlot(9 + index); - private void summonPlayer(EntityPlayerMP player, int x, int y, int z) { - if (consumeEnergy(WarpDriveConfig.WC_ENERGY_PER_ENTITY_TO_SPACE, false)) { - player.setPositionAndUpdate(x, y, z); + if (chestItem == null) { + continue; + } - if (player.dimension != worldObj.provider.dimensionId) { - player.mcServer.getConfigurationManager().transferPlayerToDimension(player, this.worldObj.provider.dimensionId, new SpaceTeleporter(DimensionManager.getWorld(this.worldObj.provider.dimensionId), 0, MathHelper.floor_double(player.posX), MathHelper.floor_double(player.posY), MathHelper.floor_double(player.posZ))); - } - } - } - - public boolean validateShipSpatialParameters(StringBuilder reason) { - if (controller == null) { - reason.append("TileEntityReactor.validateShipSpatialParameters: no controller detected!"); - return false; - } - direction = controller.getDirection(); - shipFront = controller.getFront(); - shipRight = controller.getRight(); - shipUp = controller.getUp(); - shipBack = controller.getBack(); - shipLeft = controller.getLeft(); - shipDown = controller.getDown(); - - int x1 = 0, x2 = 0, z1 = 0, z2 = 0; - - if (Math.abs(dx) > 0) { - if (dx == 1) { - x1 = xCoord - shipBack; - x2 = xCoord + shipFront; - z1 = zCoord - shipLeft; - z2 = zCoord + shipRight; - } else { - x1 = xCoord - shipFront; - x2 = xCoord + shipBack; - z1 = zCoord - shipRight; - z2 = zCoord + shipLeft; - } - } else if (Math.abs(dz) > 0) { - if (dz == 1) { - z1 = zCoord - shipBack; - z2 = zCoord + shipFront; - x1 = xCoord - shipRight; - x2 = xCoord + shipLeft; - } else { - z1 = zCoord - shipFront; - z2 = zCoord + shipBack; - x1 = xCoord - shipLeft; - x2 = xCoord + shipRight; - } - } - - if (x1 < x2) { - minX = x1; - maxX = x2; - } else { - minX = x2; - maxX = x1; - } - - if (z1 < z2) { - minZ = z1; - maxZ = z2; - } else { - minZ = z2; - maxZ = z1; - } - - minY = yCoord - shipDown; - maxY = yCoord + shipUp; - shipLength = 0; - - switch (direction) { - case 0: - case 180: - shipLength = shipBack + shipFront; - break; - - case 90: - case 270: - shipLength = shipLeft + shipRight; - break; - - case -1: - case -2: - shipLength = shipDown + shipUp; - break; - - default: - reason.append("Invalid jump direction " + direction); + if (playerItem == null || chestItem.isItemEqual(playerItem) || chestItem.getItemDamage() != playerItem.getItemDamage() + || chestItem.stackSize != playerItem.stackSize) { return false; - } - - // Ship side is too big - if ((shipBack + shipFront) > WarpDriveConfig.WC_MAX_SHIP_SIDE || (shipLeft + shipRight) > WarpDriveConfig.WC_MAX_SHIP_SIDE || (shipDown + shipUp) > WarpDriveConfig.WC_MAX_SHIP_SIDE) { - reason.append("Ship is too big (max is " + WarpDriveConfig.WC_MAX_SHIP_SIDE + " per side)"); - return false; - } + } else { + result = true; + } - this.shipVolume = computeRealShipVolume(); - - if (shipVolume > WarpDriveConfig.WC_MAX_SHIP_VOLUME_ON_SURFACE && worldObj.provider.dimensionId == 0) { - reason.append("Ship is too big for the overworld (max is " + WarpDriveConfig.WC_MAX_SHIP_VOLUME_ON_SURFACE + " blocks)"); - return false; - } - - return true; - } - - private void doBeaconJump() - { - // Search beacon coordinates - String freq = controller.getBeaconFrequency(); - int beaconX = 0, beaconZ = 0; - boolean isBeaconFound = false; - EntityPlayerMP player; - - for (int i = 0; i < MinecraftServer.getServer().getConfigurationManager().playerEntityList.size(); i++) - { - player = (EntityPlayerMP)MinecraftServer.getServer().getConfigurationManager().playerEntityList.get(i); - - // Skip players from other dimensions - if (player.dimension != worldObj.provider.dimensionId) - { - continue; - } - - TileEntity te = worldObj.getBlockTileEntity(MathHelper.floor_double(player.posX), MathHelper.floor_double(player.posY) - 1, MathHelper.floor_double(player.posZ)); - - if (te != null && (te instanceof TileEntityProtocol)) - { - if (((TileEntityProtocol)te).getBeaconFrequency().equals(freq)) - { - beaconX = te.xCoord; - beaconZ = te.zCoord; - isBeaconFound = true; - break; - } - } - } - - // Now make jump to a beacon - if (isBeaconFound) { - // Consume energy - if (consumeEnergy(calculateRequiredEnergy(currentMode, shipVolume, controller.getDistance()), false)) { - System.out.println("" + this + " Moving ship to beacon (" + beaconX + "; " + yCoord + "; " + beaconZ + ")"); - EntityJump jump = new EntityJump(worldObj, xCoord, yCoord, zCoord, dx, dz, this, false, 1, 0, true, beaconX, yCoord, beaconZ); - jump.maxX = maxX; - jump.minX = minX; - jump.maxZ = maxZ; - jump.minZ = minZ; - jump.maxY = maxY; - jump.minY = minY; - jump.shipLength = shipLength; - jump.on = true; - worldObj.spawnEntityInWorld(jump); - } else { - messageToAllPlayersOnShip("Insufficient energy level"); - } - } else { - System.out.println("" + this + " Beacon '" + freq + "' is unknown."); - } - } - - private boolean isShipInJumpgate(Jumpgate jg, StringBuilder reason) - { - AxisAlignedBB aabb = jg.getGateAABB(); - WarpDrive.debugPrint("[TEWarpCore] Jumpgate " + jg.name + " AABB is " + aabb); - int countBlocksInside = 0; - int countBlocksTotal = 0; - - if (aabb.isVecInside(worldObj.getWorldVec3Pool().getVecFromPool(maxX - minX, maxY - minY, maxZ - minZ))) - { - return true; - } - - for (int x = minX; x <= maxX; x++) - { - for (int z = minZ; z <= maxZ; z++) - { - for (int y = minY; y <= maxY; y++) - { - int blockID = worldObj.getBlockId(x, y, z); - - if (worldObj.isAirBlock(x, y, z) && (blockID != WarpDriveConfig.airID)) - { - continue; - } - if (aabb.minX <= x && aabb.maxX >= x && aabb.minY <= y && aabb.maxY >= y && aabb.minZ <= z && aabb.maxZ >= z) - { - countBlocksInside++; - } - countBlocksTotal++; - } - } - } - - float percent = 0F; - if (shipVolume != 0) - { - percent = Math.round((((countBlocksInside * 1.0F) / shipVolume) * 100.0F) * 10.0F) / 10.0F; + keyLength++; } - if (shipVolume != countBlocksTotal) - { - System.out.println("" + this + " Ship volume has changed from " + shipVolume + " to " + countBlocksTotal + " blocks"); - } - WarpDrive.debugPrint("Ship has " + countBlocksInside + " / " + shipVolume + " blocks (" + percent + "%) in jumpgate '" + jg.name + "'"); - // At least 80% of ship must be inside jumpgate - if (percent > 80F) - { - return true; - } - else if (percent <= 0.001) - { - reason.append("Ship is not inside a jumpgate. Jump rejected. Nearest jumpgate is " + jg.toNiceString()); - return false; - } - else - { - reason.append("Ship is only " + percent + "% inside a jumpgate. Sorry, we'll loose too much crew as is, jump rejected."); - return false; - } - } - private boolean isFreePlaceForShip(int destX, int destY, int destZ) - { - int newX, newY, newZ; + if (keyLength < MIN_KEY_LENGTH) { + System.out.println("[ChestCode] Key is too short: " + keyLength + " < " + MIN_KEY_LENGTH); + return false; + } - if (destY + shipUp > 255 || destY - shipDown < 5) - { - return false; - } + return result; + } - int moveX = destX - xCoord; - int moveY = destY - yCoord; - int moveZ = destZ - zCoord; + private static boolean testBB(AxisAlignedBB axisalignedbb, int x, int y, int z) { + return axisalignedbb.minX <= x && axisalignedbb.maxX >= x && axisalignedbb.minY <= y && axisalignedbb.maxY >= y && axisalignedbb.minZ <= z + && axisalignedbb.maxZ >= z; + } - for (int x = minX; x <= maxX; x++) - { - for (int z = minZ; z <= maxZ; z++) - { - for (int y = minY; y <= maxY; y++) - { - if (!worldObj.isAirBlock(x, y, z)) - { - newX = moveX + x; - newY = moveY + y; - newZ = moveZ + z; + public Boolean ready; + public Boolean launchState = false; - if (!worldObj.isAirBlock(newX, newY, newZ)) - { - return false; - } - } - } - } - } + public final int JUMP_UP = -1; + public final int JUMP_DOWN = -2; - return true; - } + public int dx, dz; + private int direction; + public int maxX, maxY, maxZ; + public int minX, minY, minZ; + public int shipFront, shipBack; + public int shipLeft, shipRight; - private void doGateJump() - { - // Search nearest jump-gate - String gateName = controller.getTargetJumpgateName(); - Jumpgate targetGate = WarpDrive.jumpgates.findGateByName(gateName); + public int shipUp, shipDown; - if (targetGate == null) - { - messageToAllPlayersOnShip("Destination jumpgate '" + gateName + "' is unknown. Check jumpgate name."); - this.controller.setJumpFlag(false); - return; - } + public int shipLength; + public int shipVolume; + private ReactorMode currentMode = ReactorMode.IDLE; - // Now make jump to a beacon - int gateX = targetGate.xCoord; - int gateY = targetGate.yCoord; - int gateZ = targetGate.zCoord; - int destX = gateX; - int destY = gateY; - int destZ = gateZ; - Jumpgate nearestGate = WarpDrive.jumpgates.findNearestGate(xCoord, yCoord, zCoord); + private int warmupTime = 0; + private int cooldownTime = 0; + public int randomWarmupAddition = 0; + + private int chestTeleportUpdateTicks = 0; + private int registryUpdateTicks = 15; + public String coreFrequency = "default"; + + public int isolationBlocksCount = 0; + + public double isolationRate = 0.0D; + + public int isolationUpdateTicks = 0; + + public TileEntityProtocol controller; + + private boolean soundPlayed = false; + + @Override + public boolean canInputEnergy(ForgeDirection from) { + return true; + } + + private int computeRealShipVolume() { + int realShipVolume = 0; + + try { + for (int x = minX; x <= maxX; x++) { + for (int z = minZ; z <= maxZ; z++) { + for (int y = minY; y <= maxY; y++) { + Block block = worldObj.getBlock(x, y, z); + + if (worldObj.isAirBlock(x, y, z) && (block.isAssociatedBlock(WarpDrive.airBlock))) { + continue; + } + + realShipVolume++; + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + return realShipVolume; + } + + private void doBeaconJump() { + // Search beacon coordinates + String freq = controller.getBeaconFrequency(); + int beaconX = 0, beaconZ = 0; + boolean isBeaconFound = false; + EntityPlayerMP player; + + for (int i = 0; i < MinecraftServer.getServer().getConfigurationManager().playerEntityList.size(); i++) { + player = (EntityPlayerMP) MinecraftServer.getServer().getConfigurationManager().playerEntityList.get(i); + + // Skip players from other dimensions + if (player.dimension != worldObj.provider.dimensionId) { + continue; + } + + TileEntity te = worldObj.getTileEntity(MathHelper.floor_double(player.posX), MathHelper.floor_double(player.posY) - 1, + MathHelper.floor_double(player.posZ)); + + if (te != null && (te instanceof TileEntityProtocol)) { + if (((TileEntityProtocol) te).getBeaconFrequency().equals(freq)) { + beaconX = te.xCoord; + beaconZ = te.zCoord; + isBeaconFound = true; + break; + } + } + } + + // Now make jump to a beacon + if (isBeaconFound) { + // Consume energy + if (consumeEnergy(calculateRequiredEnergy(currentMode, shipVolume, controller.getDistance()), false)) { + System.out.println("" + this + " Moving ship to beacon (" + beaconX + "; " + yCoord + "; " + beaconZ + ")"); + EntityJump jump = new EntityJump(worldObj, xCoord, yCoord, zCoord, dx, dz, this, false, 1, 0, true, beaconX, yCoord, beaconZ); + jump.maxX = maxX; + jump.minX = minX; + jump.maxZ = maxZ; + jump.minZ = minZ; + jump.maxY = maxY; + jump.minY = minY; + jump.shipLength = shipLength; + jump.on = true; + worldObj.spawnEntityInWorld(jump); + } else { + messageToAllPlayersOnShip("Insufficient energy level"); + } + } else { + System.out.println("" + this + " Beacon '" + freq + "' is unknown."); + } + } + + private void doGateJump() { + // Search nearest jump-gate + String gateName = controller.getTargetJumpgateName(); + Jumpgate targetGate = WarpDrive.jumpgates.findGateByName(gateName); + + if (targetGate == null) { + messageToAllPlayersOnShip("Destination jumpgate '" + gateName + "' is unknown. Check jumpgate name."); + this.controller.setJumpFlag(false); + return; + } + + // Now make jump to a beacon + int gateX = targetGate.xCoord; + int gateY = targetGate.yCoord; + int gateZ = targetGate.zCoord; + int destX = gateX; + int destY = gateY; + int destZ = gateZ; + Jumpgate nearestGate = WarpDrive.jumpgates.findNearestGate(xCoord, yCoord, zCoord); StringBuilder reason = new StringBuilder(); - if (!isShipInJumpgate(nearestGate, reason)) - { - messageToAllPlayersOnShip(reason.toString()); - this.controller.setJumpFlag(false); - return; - } + if (!isShipInJumpgate(nearestGate, reason)) { + messageToAllPlayersOnShip(reason.toString()); + this.controller.setJumpFlag(false); + return; + } - // If gate is blocked by obstacle - if (!isFreePlaceForShip(gateX, gateY, gateZ)) - { - // Randomize destination coordinates and check for collision with obstacles around jumpgate - // Try to find good place for ship - int numTries = 10; // num tries to check for collision - boolean placeFound = false; + // If gate is blocked by obstacle + if (!isFreePlaceForShip(gateX, gateY, gateZ)) { + // Randomize destination coordinates and check for collision with + // obstacles around jumpgate + // Try to find good place for ship + int numTries = 10; // num tries to check for collision + boolean placeFound = false; - for (; numTries > 0; numTries--) - { - // randomize destination coordinates around jumpgate - destX = gateX + ((worldObj.rand.nextBoolean()) ? -1 : 1) * (20 + worldObj.rand.nextInt(100)); - destZ = gateZ + ((worldObj.rand.nextBoolean()) ? -1 : 1) * (20 + worldObj.rand.nextInt(100)); - destY = gateY + ((worldObj.rand.nextBoolean()) ? -1 : 1) * (20 + worldObj.rand.nextInt(50)); + for (; numTries > 0; numTries--) { + // randomize destination coordinates around jumpgate + destX = gateX + ((worldObj.rand.nextBoolean()) ? -1 : 1) * (20 + worldObj.rand.nextInt(100)); + destZ = gateZ + ((worldObj.rand.nextBoolean()) ? -1 : 1) * (20 + worldObj.rand.nextInt(100)); + destY = gateY + ((worldObj.rand.nextBoolean()) ? -1 : 1) * (20 + worldObj.rand.nextInt(50)); - // check for collision - if (isFreePlaceForShip(destX, destY, destZ)) - { - placeFound = true; - break; - } - } + // check for collision + if (isFreePlaceForShip(destX, destY, destZ)) { + placeFound = true; + break; + } + } - if (!placeFound) - { - messageToAllPlayersOnShip("Destination gate is blocked by obstacles. Aborting..."); - this.controller.setJumpFlag(false); - return; - } + if (!placeFound) { + messageToAllPlayersOnShip("Destination gate is blocked by obstacles. Aborting..."); + this.controller.setJumpFlag(false); + return; + } - System.out.println("[GATE] Place found over " + (10 - numTries) + " tries."); - } + System.out.println("[GATE] Place found over " + (10 - numTries) + " tries."); + } - // Consume energy - if (consumeEnergy(calculateRequiredEnergy(currentMode, shipVolume, controller.getDistance()), false)) { - System.out.println("[TE-WC] Moving ship to a place around gate '" + targetGate.name + "' (" + destX + "; " + destY + "; " + destZ + ")"); - EntityJump jump = new EntityJump(worldObj, xCoord, yCoord, zCoord, dx, dz, this, false, 1, 0, true, destX, destY, destZ); - jump.maxX = maxX; - jump.minX = minX; - jump.maxZ = maxZ; - jump.minZ = minZ; - jump.maxY = maxY; - jump.minY = minY; - jump.shipLength = shipLength; - jump.on = true; - worldObj.spawnEntityInWorld(jump); - } else { - messageToAllPlayersOnShip("Insufficient energy level"); - } - } + // Consume energy + if (consumeEnergy(calculateRequiredEnergy(currentMode, shipVolume, controller.getDistance()), false)) { + System.out.println("[TE-WC] Moving ship to a place around gate '" + targetGate.name + "' (" + destX + "; " + destY + "; " + destZ + ")"); + EntityJump jump = new EntityJump(worldObj, xCoord, yCoord, zCoord, dx, dz, this, false, 1, 0, true, destX, destY, destZ); + jump.maxX = maxX; + jump.minX = minX; + jump.maxZ = maxZ; + jump.minZ = minZ; + jump.maxY = maxY; + jump.minY = minY; + jump.shipLength = shipLength; + jump.on = true; + worldObj.spawnEntityInWorld(jump); + } else { + messageToAllPlayersOnShip("Insufficient energy level"); + } + } - private void doJump() { - int distance = controller.getDistance(); - int requiredEnergy = calculateRequiredEnergy(currentMode, shipVolume, distance); + private void doJump() { + int distance = controller.getDistance(); + int requiredEnergy = calculateRequiredEnergy(currentMode, shipVolume, distance); - if (!consumeEnergy(requiredEnergy, true)) { - messageToAllPlayersOnShip("Insufficient energy to jump! Core is currently charged with " + getEnergyStored() + " EU while jump requires " + requiredEnergy + " EU"); - this.controller.setJumpFlag(false); - return; - } + if (!consumeEnergy(requiredEnergy, true)) { + messageToAllPlayersOnShip("Insufficient energy to jump! Core is currently charged with " + getEnergyStored() + " EU while jump requires " + + requiredEnergy + " EU"); + this.controller.setJumpFlag(false); + return; + } - String shipInfo = "" + shipVolume + " blocks inside (" + minX + ", " + minY + ", " + minZ + ") to (" + maxX + ", " + maxY + ", " + maxZ + ")"; - if (currentMode == ReactorMode.GATE_JUMP) { - System.out.println("" + this + " Performing gate jump of " + shipInfo); - doGateJump(); - return; - } else if (currentMode == ReactorMode.BEACON_JUMP) { - System.out.println("" + this + " Performing beacon jump of " + shipInfo); - doBeaconJump(); - return; - } else if (currentMode == ReactorMode.HYPERSPACE) { - System.out.println("" + this + " Performing hyperspace jump of " + shipInfo); + String shipInfo = "" + shipVolume + " blocks inside (" + minX + ", " + minY + ", " + minZ + ") to (" + maxX + ", " + maxY + ", " + maxZ + ")"; + if (currentMode == ReactorMode.GATE_JUMP) { + System.out.println("" + this + " Performing gate jump of " + shipInfo); + doGateJump(); + return; + } else if (currentMode == ReactorMode.BEACON_JUMP) { + System.out.println("" + this + " Performing beacon jump of " + shipInfo); + doBeaconJump(); + return; + } else if (currentMode == ReactorMode.HYPERSPACE) { + System.out.println("" + this + " Performing hyperspace jump of " + shipInfo); - // Check ship size for hyper-space jump - if (shipVolume < WarpDriveConfig.WC_MIN_SHIP_VOLUME_FOR_HYPERSPACE) { - Jumpgate nearestGate = null; - if (WarpDrive.jumpgates == null) { - System.out.println("" + this + " WarpDrive.instance.jumpGates is NULL!"); - } else { - nearestGate = WarpDrive.jumpgates.findNearestGate(xCoord, yCoord, zCoord); - } + // Check ship size for hyper-space jump + if (shipVolume < WarpDriveConfig.WC_MIN_SHIP_VOLUME_FOR_HYPERSPACE) { + Jumpgate nearestGate = null; + if (WarpDrive.jumpgates == null) { + System.out.println("" + this + " WarpDrive.instance.jumpGates is NULL!"); + } else { + nearestGate = WarpDrive.jumpgates.findNearestGate(xCoord, yCoord, zCoord); + } - StringBuilder reason = new StringBuilder(); - if (nearestGate == null || !isShipInJumpgate(nearestGate, reason)) { - this.messageToAllPlayersOnShip("Ship is too small (" + shipVolume + "/" + WarpDriveConfig.WC_MIN_SHIP_VOLUME_FOR_HYPERSPACE + "). Insufficient ship mass to open hyperspace portal. Use a jumpgate to reach or exit hyperspace."); - this.controller.setJumpFlag(false); - return; - } - } - } else if (currentMode == ReactorMode.BASIC_JUMP) { - System.out.println("" + this + " Performing basic jump of " + shipInfo + " toward direction " + direction + " over " + distance + " blocks."); - } else if (currentMode == ReactorMode.LONG_JUMP) { - System.out.println("" + this + " Performing long jump of " + shipInfo + " toward direction " + direction + " over " + distance + " blocks."); - } else { - System.out.println("" + this + " Performing some jump #" + currentMode + " of " + shipInfo); - } - - if (currentMode == ReactorMode.BASIC_JUMP || currentMode == ReactorMode.LONG_JUMP || currentMode == ReactorMode.HYPERSPACE) { - if (!consumeEnergy(requiredEnergy, false)) { - messageToAllPlayersOnShip("Insufficient energy level"); - return; - } + StringBuilder reason = new StringBuilder(); + if (nearestGate == null || !isShipInJumpgate(nearestGate, reason)) { + this.messageToAllPlayersOnShip("Ship is too small (" + shipVolume + "/" + WarpDriveConfig.WC_MIN_SHIP_VOLUME_FOR_HYPERSPACE + + "). Insufficient ship mass to open hyperspace portal. Use a jumpgate to reach or exit hyperspace."); + this.controller.setJumpFlag(false); + return; + } + } + } else if (currentMode == ReactorMode.BASIC_JUMP) { + System.out.println("" + this + " Performing basic jump of " + shipInfo + " toward direction " + direction + " over " + distance + " blocks."); + } else if (currentMode == ReactorMode.LONG_JUMP) { + System.out.println("" + this + " Performing long jump of " + shipInfo + " toward direction " + direction + " over " + distance + " blocks."); + } else { + System.out.println("" + this + " Performing some jump #" + currentMode + " of " + shipInfo); + } - if (this.currentMode == ReactorMode.BASIC_JUMP) { - distance += shipLength; - } + if (currentMode == ReactorMode.BASIC_JUMP || currentMode == ReactorMode.LONG_JUMP || currentMode == ReactorMode.HYPERSPACE) { + if (!consumeEnergy(requiredEnergy, false)) { + messageToAllPlayersOnShip("Insufficient energy level"); + return; + } - if (currentMode == ReactorMode.LONG_JUMP && (direction != -1) && (direction != -2)) { - if (worldObj.provider.dimensionId == WarpDriveConfig.G_HYPERSPACE_DIMENSION_ID) { - distance *= 100; - } - } + if (this.currentMode == ReactorMode.BASIC_JUMP) { + distance += shipLength; + } - WarpDrive.debugPrint("" + this + " Distance adjusted to " + distance + " blocks."); - EntityJump jump = new EntityJump(worldObj, xCoord, yCoord, zCoord, dx, dz, this, (currentMode == ReactorMode.HYPERSPACE), distance, direction, false, 0, 0, 0); - jump.maxX = maxX; - jump.minX = minX; - jump.maxZ = maxZ; - jump.minZ = minZ; - jump.maxY = maxY; - jump.minY = minY; - jump.shipLength = shipLength; - jump.on = true; - worldObj.spawnEntityInWorld(jump); - } - } + if (currentMode == ReactorMode.LONG_JUMP && (direction != -1) && (direction != -2)) { + if (worldObj.provider.dimensionId == WarpDriveConfig.G_HYPERSPACE_DIMENSION_ID) { + distance *= 100; + } + } - private void teleportPlayersToSpace() { - if (worldObj.provider.dimensionId != WarpDriveConfig.G_SPACE_DIMENSION_ID) { - AxisAlignedBB axisalignedbb = AxisAlignedBB.getBoundingBox(xCoord - 2, yCoord - 1, zCoord - 2, xCoord + 2, yCoord + 4, zCoord + 2); - List list = worldObj.getEntitiesWithinAABBExcludingEntity(null, axisalignedbb); + WarpDrive.debugPrint("" + this + " Distance adjusted to " + distance + " blocks."); + EntityJump jump = new EntityJump(worldObj, xCoord, yCoord, zCoord, dx, dz, this, (currentMode == ReactorMode.HYPERSPACE), distance, direction, + false, 0, 0, 0); + jump.maxX = maxX; + jump.minX = minX; + jump.maxZ = maxZ; + jump.minZ = minZ; + jump.maxY = maxY; + jump.minY = minY; + jump.shipLength = shipLength; + jump.on = true; + worldObj.spawnEntityInWorld(jump); + } + } - WorldServer spaceWorld = DimensionManager.getWorld(WarpDriveConfig.G_SPACE_DIMENSION_ID); - for (Object o : list) { - if (!consumeEnergy(WarpDriveConfig.WC_ENERGY_PER_ENTITY_TO_SPACE, false)) { - return; - } + private TileEntity findControllerBlock() { + TileEntity result; + result = worldObj.getTileEntity(xCoord + 1, yCoord, zCoord); - Entity entity = (Entity) o; - int x = MathHelper.floor_double(entity.posX); - int z = MathHelper.floor_double(entity.posZ); - //int y = MathHelper.floor_double(entity.posY); - final int WOOL_BLOCK_ID = 35; - int newY; + if (result != null && result instanceof TileEntityProtocol) { + dx = 1; + dz = 0; + return result; + } - for (newY = 254; newY > 0; newY--) { - if (spaceWorld.getBlockId(x, newY, z) == WOOL_BLOCK_ID) { - break; - } - } + result = worldObj.getTileEntity(xCoord - 1, yCoord, zCoord); - if (newY <= 0) { - newY = 254; - } + if (result != null && result instanceof TileEntityProtocol) { + dx = -1; + dz = 0; + return result; + } - if (entity instanceof EntityPlayerMP) { - ((EntityPlayerMP) entity).mcServer.getConfigurationManager().transferPlayerToDimension(((EntityPlayerMP) entity), WarpDriveConfig.G_SPACE_DIMENSION_ID, new SpaceTeleporter(DimensionManager.getWorld(WarpDriveConfig.G_SPACE_DIMENSION_ID), 0, x, 256, z)); + result = worldObj.getTileEntity(xCoord, yCoord, zCoord + 1); - if (spaceWorld.isAirBlock(x, newY, z)) { - spaceWorld.setBlock(x , newY, z , Block.stone.blockID, 0, 2); - spaceWorld.setBlock(x + 1, newY, z , Block.stone.blockID, 0, 2); - spaceWorld.setBlock(x - 1, newY, z , Block.stone.blockID, 0, 2); - spaceWorld.setBlock(x , newY, z + 1, Block.stone.blockID, 0, 2); - spaceWorld.setBlock(x , newY, z - 1, Block.stone.blockID, 0, 2); - spaceWorld.setBlock(x + 1, newY, z + 1, Block.stone.blockID, 0, 2); - spaceWorld.setBlock(x - 1, newY, z - 1, Block.stone.blockID, 0, 2); - spaceWorld.setBlock(x + 1, newY, z - 1, Block.stone.blockID, 0, 2); - spaceWorld.setBlock(x - 1, newY, z + 1, Block.stone.blockID, 0, 2); - } + if (result != null && result instanceof TileEntityProtocol) { + dx = 0; + dz = 1; + return result; + } - ((EntityPlayerMP) entity).setPositionAndUpdate(x + 0.5D, newY + 2.0D, z + 0.5D); - } - } - } - } + result = worldObj.getTileEntity(xCoord, yCoord, zCoord - 1); - private void summonPlayersByChestCode() - { - if (worldObj.getBlockTileEntity(xCoord, yCoord + 1, zCoord) == null) - { - return; - } + if (result != null && result instanceof TileEntityProtocol) { + dx = 0; + dz = -1; + return result; + } - TileEntityChest chest = (TileEntityChest)worldObj.getBlockTileEntity(xCoord, yCoord + 1, zCoord); - EntityPlayerMP player; + return null; + } - for (int i = 0; i < MinecraftServer.getServer().getConfigurationManager().playerEntityList.size(); i++) - { - player = (EntityPlayerMP)MinecraftServer.getServer().getConfigurationManager().playerEntityList.get(i); + public int getCooldown() { + return cooldownTime; + } - if (checkPlayerInventory(chest, player)) - { - System.out.println("" + this + " Summoning " + player.username); - summonPlayer(player, xCoord, yCoord + 2, zCoord); - } - } - } + @Override + public double getDemandedEnergy() { + if (this.controller != null && controller.getMode() == ReactorMode.IDLE) { + return 0.0D; + } - private static boolean checkPlayerInventory(TileEntityChest chest, EntityPlayerMP player) { - Boolean result = false; - final int MIN_KEY_LENGTH = 5; - int keyLength = 0; + return super.getDemandedEnergy(); + } - for (int index = 0; index < chest.getSizeInventory(); index++) { - ItemStack chestItem = chest.getStackInSlot(index); - ItemStack playerItem = player.inventory.getStackInSlot(9 + index); + @Override + public int getMaxEnergyStored() { + return WarpDriveConfig.WC_MAX_ENERGY_VALUE; + } - if (chestItem == null) { - continue; - } + @Override + public String getStatus() { + return getBlockType().getLocalizedName() + + String.format(" '%s' energy level is %.0f/%.0f EU.", coreFrequency, convertInternalToEU(getEnergyStored()), + convertInternalToEU(getMaxEnergyStored())) + + ((cooldownTime > 0) ? ("\n" + (cooldownTime / 20) + " s left of cooldown.") + : ((isolationBlocksCount > 0) ? ("\n" + isolationBlocksCount + " active isolation blocks") : "")); + } - if (playerItem == null || chestItem.itemID != playerItem.itemID || chestItem.getItemDamage() != playerItem.getItemDamage() || chestItem.stackSize != playerItem.stackSize) { - return false; - } else { - result = true; - } + @Override + public void invalidate() { + WarpDrive.warpCores.removeFromRegistry(this); + super.invalidate(); + } - keyLength++; - } + private Boolean isChestSummonMode() { + TileEntity te = worldObj.getTileEntity(xCoord, yCoord + 1, zCoord); - if (keyLength < MIN_KEY_LENGTH) { - System.out.println("[ChestCode] Key is too short: " + keyLength + " < " + MIN_KEY_LENGTH); - return false; - } + if (te != null) { + return (te instanceof TileEntityChest); + } - return result; - } + return false; + } - private Boolean isChestSummonMode() - { - TileEntity te = worldObj.getBlockTileEntity(xCoord, yCoord + 1, zCoord); + private boolean isFreePlaceForShip(int destX, int destY, int destZ) { + int newX, newY, newZ; - if (te != null) - { - return (te instanceof TileEntityChest); - } + if (destY + shipUp > 255 || destY - shipDown < 5) { + return false; + } - return false; - } + int moveX = destX - xCoord; + int moveY = destY - yCoord; + int moveZ = destZ - zCoord; - private static boolean testBB(AxisAlignedBB axisalignedbb, int x, int y, int z) - { - return axisalignedbb.minX <= x && axisalignedbb.maxX >= x && axisalignedbb.minY <= y && axisalignedbb.maxY >= y && axisalignedbb.minZ <= z && axisalignedbb.maxZ >= z; - } + for (int x = minX; x <= maxX; x++) { + for (int z = minZ; z <= maxZ; z++) { + for (int y = minY; y <= maxY; y++) { + if (!worldObj.isAirBlock(x, y, z)) { + newX = moveX + x; + newY = moveY + y; + newZ = moveZ + z; - @Override - public String getStatus() { - return getBlockType().getLocalizedName() + String.format(" '%s' energy level is %.0f/%.0f EU.", coreFrequency, convertInternalToEU(getEnergyStored()), convertInternalToEU(getMaxEnergyStored())) - + ((cooldownTime > 0) ? ("\n" + (cooldownTime / 20) + " s left of cooldown.") : ((isolationBlocksCount > 0) ? ("\n" + isolationBlocksCount + " active isolation blocks") : "")); - } + if (!worldObj.isAirBlock(newX, newY, newZ)) { + return false; + } + } + } + } + } - public static int calculateRequiredEnergy(ReactorMode currentMode, int shipVolume, int jumpDistance) { - switch (currentMode) { - case TELEPORT: - return WarpDriveConfig.WC_ENERGY_PER_ENTITY_TO_SPACE; - - case BASIC_JUMP: - return (WarpDriveConfig.WC_ENERGY_PER_BLOCK_MODE1 * shipVolume) + (WarpDriveConfig.WC_ENERGY_PER_DISTANCE_MODE1 * jumpDistance); - - case LONG_JUMP: - return (WarpDriveConfig.WC_ENERGY_PER_BLOCK_MODE2 * shipVolume) + (WarpDriveConfig.WC_ENERGY_PER_DISTANCE_MODE2 * jumpDistance); - - case HYPERSPACE: - return WarpDriveConfig.WC_MAX_ENERGY_VALUE / 10; // 10% of maximum - - case BEACON_JUMP: - return WarpDriveConfig.WC_MAX_ENERGY_VALUE / 2; // half of maximum - - case GATE_JUMP: - return 2 * shipVolume; - } - - return WarpDriveConfig.WC_MAX_ENERGY_VALUE; - } - - private int computeRealShipVolume() { - int realShipVolume = 0; - - try { - for (int x = minX; x <= maxX; x++) { - for (int z = minZ; z <= maxZ; z++) { - for (int y = minY; y <= maxY; y++) { - int blockID = worldObj.getBlockId(x, y, z); - - if (WarpDriveConfig.isAirBlock(worldObj, blockID, x, y, z) && (blockID != WarpDriveConfig.airID)) { - continue; - } - - realShipVolume++; - } - } - } - } catch(Exception e) { - e.printStackTrace(); - } - - return realShipVolume; - } - - private TileEntity findControllerBlock() - { - TileEntity result; - result = worldObj.getBlockTileEntity(xCoord + 1, yCoord, zCoord); - - if (result != null && result instanceof TileEntityProtocol) - { - dx = 1; - dz = 0; - return result; - } - - result = worldObj.getBlockTileEntity(xCoord - 1, yCoord, zCoord); - - if (result != null && result instanceof TileEntityProtocol) - { - dx = -1; - dz = 0; - return result; - } - - result = worldObj.getBlockTileEntity(xCoord, yCoord, zCoord + 1); - - if (result != null && result instanceof TileEntityProtocol) - { - dx = 0; - dz = 1; - return result; - } - - result = worldObj.getBlockTileEntity(xCoord, yCoord, zCoord - 1); - - if (result != null && result instanceof TileEntityProtocol) - { - dx = 0; - dz = -1; - return result; - } - - return null; - } - - public int getCooldown() { - return cooldownTime; - } + return true; + } public boolean isHidden() { if (cooldownTime <= 0 && worldObj.rand.nextDouble() < isolationRate) { - // WarpDrive.debugPrint(this + " Core '" + coreFrequency + "' is hidden"); + // WarpDrive.debugPrint(this + " Core '" + coreFrequency + + // "' is hidden"); return true; } return false; } - - @Override - public int getMaxEnergyStored() { - return WarpDriveConfig.WC_MAX_ENERGY_VALUE; + + private boolean isShipInJumpgate(Jumpgate jg, StringBuilder reason) { + AxisAlignedBB aabb = jg.getGateAABB(); + WarpDrive.debugPrint("[TEWarpCore] Jumpgate " + jg.name + " AABB is " + aabb); + int countBlocksInside = 0; + int countBlocksTotal = 0; + + if (aabb.isVecInside(Vec3.createVectorHelper( // TODO: Unknown if will + // work + maxX - minX, maxY - minY, maxZ - minZ))) { + return true; + } + + for (int x = minX; x <= maxX; x++) { + for (int z = minZ; z <= maxZ; z++) { + for (int y = minY; y <= maxY; y++) { + Block block = worldObj.getBlock(x, y, z); + + if (worldObj.isAirBlock(x, y, z) && (!block.isAssociatedBlock(WarpDrive.airBlock))) { + continue; + } + if (aabb.minX <= x && aabb.maxX >= x && aabb.minY <= y && aabb.maxY >= y && aabb.minZ <= z && aabb.maxZ >= z) { + countBlocksInside++; + } + countBlocksTotal++; + } + } + } + + float percent = 0F; + if (shipVolume != 0) { + percent = Math.round((((countBlocksInside * 1.0F) / shipVolume) * 100.0F) * 10.0F) / 10.0F; + } + if (shipVolume != countBlocksTotal) { + System.out.println("" + this + " Ship volume has changed from " + shipVolume + " to " + countBlocksTotal + " blocks"); + } + WarpDrive.debugPrint("Ship has " + countBlocksInside + " / " + shipVolume + " blocks (" + percent + "%) in jumpgate '" + jg.name + "'"); + // At least 80% of ship must be inside jumpgate + if (percent > 80F) { + return true; + } else if (percent <= 0.001) { + reason.append("Ship is not inside a jumpgate. Jump rejected. Nearest jumpgate is " + jg.toNiceString()); + return false; + } else { + reason.append("Ship is only " + percent + "% inside a jumpgate. Sorry, we'll loose too much crew as is, jump rejected."); + return false; + } } - - @Override - public double demandedEnergyUnits() { - if (this.controller != null && controller.getMode() == ReactorMode.IDLE) { - return 0.0D; - } - return super.demandedEnergyUnits(); - } - - @Override - public int getMaxSafeInput() { - return Integer.MAX_VALUE; - } - - @Override - public boolean canInputEnergy(ForgeDirection from) { - return true; - } + private void makePlayersOnShipDrunk(int tickDuration) { + AxisAlignedBB axisalignedbb = AxisAlignedBB.getBoundingBox(this.minX, this.minY, this.minZ, this.maxX, this.maxY, this.maxZ); + List list = worldObj.getEntitiesWithinAABBExcludingEntity(null, axisalignedbb); - @Override - public void readFromNBT(NBTTagCompound tag) - { - super.readFromNBT(tag); - coreFrequency = tag.getString("corefrequency"); - isolationBlocksCount = tag.getInteger("isolation"); - cooldownTime = tag.getInteger("cooldownTime"); - WarpDrive.warpCores.updateInRegistry(this); - } + for (Object o : list) { + if (o == null || !(o instanceof EntityPlayer)) { + continue; + } - @Override - public void writeToNBT(NBTTagCompound tag) - { - super.writeToNBT(tag); - tag.setString("corefrequency", coreFrequency); - tag.setInteger("isolation", isolationBlocksCount); - tag.setInteger("cooldownTime", cooldownTime); - } + // Set "drunk" effect + ((EntityPlayer) o).addPotionEffect(new PotionEffect(Potion.confusion.id, tickDuration, 0, true)); + } + } - @Override - public void onChunkUnload() { - WarpDrive.warpCores.removeFromRegistry(this); - super.onChunkUnload(); - } + public void messageToAllPlayersOnShip(String msg) { + AxisAlignedBB axisalignedbb = AxisAlignedBB.getBoundingBox(this.minX, this.minY, this.minZ, this.maxX + 0.99D, this.maxY + 0.99D, this.maxZ + 0.99D); + List list = worldObj.getEntitiesWithinAABBExcludingEntity(null, axisalignedbb); - @Override - public void validate() { - super.validate(); - WarpDrive.warpCores.updateInRegistry(this); - } + System.out + .println("" + (FMLCommonHandler.instance().getEffectiveSide().isClient() ? "Client" : "Server") + this + " messageToAllPlayersOnShip: " + msg); + for (Object o : list) { + if (o == null || !(o instanceof EntityPlayer)) { + continue; + } - @Override - public void invalidate() { - WarpDrive.warpCores.removeFromRegistry(this); - super.invalidate(); - } + ((EntityPlayer) o).addChatMessage(new ChatComponentText("[" + (coreFrequency.length() > 0 ? coreFrequency : "WarpCore") + "] " + msg)); + } + } - @Override - public String toString() { - return String.format("%s \'%s\' @ \'%s\' %d, %d, %d", new Object[] { - getClass().getSimpleName(), - coreFrequency, - worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName(), - Integer.valueOf(xCoord), Integer.valueOf(yCoord), Integer.valueOf(zCoord)}); - } + @Override + public void onChunkUnload() { + WarpDrive.warpCores.removeFromRegistry(this); + super.onChunkUnload(); + } + + @Override + public void readFromNBT(NBTTagCompound tag) { + super.readFromNBT(tag); + coreFrequency = tag.getString("corefrequency"); + isolationBlocksCount = tag.getInteger("isolation"); + cooldownTime = tag.getInteger("cooldownTime"); + WarpDrive.warpCores.updateInRegistry(this); + } + + private void summonPlayer(EntityPlayerMP player, int x, int y, int z) { + if (consumeEnergy(WarpDriveConfig.WC_ENERGY_PER_ENTITY_TO_SPACE, false)) { + player.setPositionAndUpdate(x, y, z); + + if (player.dimension != worldObj.provider.dimensionId) { + player.mcServer.getConfigurationManager().transferPlayerToDimension( + player, + this.worldObj.provider.dimensionId, + new SpaceTeleporter(DimensionManager.getWorld(this.worldObj.provider.dimensionId), 0, MathHelper.floor_double(player.posX), MathHelper + .floor_double(player.posY), MathHelper.floor_double(player.posZ))); + } + } + } + + private void summonPlayers() { + AxisAlignedBB aabb = AxisAlignedBB.getBoundingBox(this.minX, this.minY, this.minZ, this.maxX, this.maxY, this.maxZ); + + for (int i = 0; i < controller.players.size(); i++) { + String nick = controller.players.get(i); + EntityPlayerMP player = MinecraftServer.getServer().getConfigurationManager().func_152612_a(nick); // getPlayerForUsername + + if (player != null + && !testBB(aabb, MathHelper.floor_double(player.posX), MathHelper.floor_double(player.posY), MathHelper.floor_double(player.posZ))) { + summonPlayer(player, xCoord + dx, yCoord, zCoord + dz); + } + } + } + + private void summonPlayersByChestCode() { + if (worldObj.getTileEntity(xCoord, yCoord + 1, zCoord) == null) { + return; + } + + TileEntityChest chest = (TileEntityChest) worldObj.getTileEntity(xCoord, yCoord + 1, zCoord); + EntityPlayerMP player; + + for (int i = 0; i < MinecraftServer.getServer().getConfigurationManager().playerEntityList.size(); i++) { + player = (EntityPlayerMP) MinecraftServer.getServer().getConfigurationManager().playerEntityList.get(i); + + if (checkPlayerInventory(chest, player)) { + System.out.println("" + this + " Summoning " + player.getDisplayName()); + summonPlayer(player, xCoord, yCoord + 2, zCoord); + } + } + } + + private void summonSinglePlayer(String nickname) { + AxisAlignedBB aabb = AxisAlignedBB.getBoundingBox(this.minX, this.minY, this.minZ, this.maxX, this.maxY, this.maxZ); + + for (int i = 0; i < controller.players.size(); i++) { + String nick = controller.players.get(i); + EntityPlayerMP player = MinecraftServer.getServer().getConfigurationManager().func_152612_a(nick); // getPlayerForUsername + + if (player != null && nick.equals(nickname) + && !testBB(aabb, MathHelper.floor_double(player.posX), MathHelper.floor_double(player.posY), MathHelper.floor_double(player.posZ))) { + summonPlayer(player, xCoord + dx, yCoord, zCoord + dz); + return; + } + } + } + + private void teleportPlayersToSpace() { + if (worldObj.provider.dimensionId != WarpDriveConfig.G_SPACE_DIMENSION_ID) { + AxisAlignedBB axisalignedbb = AxisAlignedBB.getBoundingBox(xCoord - 2, yCoord - 1, zCoord - 2, xCoord + 2, yCoord + 4, zCoord + 2); + List list = worldObj.getEntitiesWithinAABBExcludingEntity(null, axisalignedbb); + + WorldServer spaceWorld = DimensionManager.getWorld(WarpDriveConfig.G_SPACE_DIMENSION_ID); + for (Object o : list) { + if (!consumeEnergy(WarpDriveConfig.WC_ENERGY_PER_ENTITY_TO_SPACE, false)) { + return; + } + + Entity entity = (Entity) o; + int x = MathHelper.floor_double(entity.posX); + int z = MathHelper.floor_double(entity.posZ); + // int y = MathHelper.floor_double(entity.posY); + final int WOOL_BLOCK_ID = 35; + int newY; + + for (newY = 254; newY > 0; newY--) { + if (spaceWorld.getBlock(x, newY, z).isAssociatedBlock(Blocks.wool)) { + break; + } + } + + if (newY <= 0) { + newY = 254; + } + + if (entity instanceof EntityPlayerMP) { + ((EntityPlayerMP) entity).mcServer.getConfigurationManager().transferPlayerToDimension(((EntityPlayerMP) entity), + WarpDriveConfig.G_SPACE_DIMENSION_ID, + new SpaceTeleporter(DimensionManager.getWorld(WarpDriveConfig.G_SPACE_DIMENSION_ID), 0, x, 256, z)); + + if (spaceWorld.isAirBlock(x, newY, z)) { + spaceWorld.setBlock(x, newY, z, Blocks.stone, 0, 2); + spaceWorld.setBlock(x + 1, newY, z, Blocks.stone, 0, 2); + spaceWorld.setBlock(x - 1, newY, z, Blocks.stone, 0, 2); + spaceWorld.setBlock(x, newY, z + 1, Blocks.stone, 0, 2); + spaceWorld.setBlock(x, newY, z - 1, Blocks.stone, 0, 2); + spaceWorld.setBlock(x + 1, newY, z + 1, Blocks.stone, 0, 2); + spaceWorld.setBlock(x - 1, newY, z - 1, Blocks.stone, 0, 2); + spaceWorld.setBlock(x + 1, newY, z - 1, Blocks.stone, 0, 2); + spaceWorld.setBlock(x - 1, newY, z + 1, Blocks.stone, 0, 2); + } + + ((EntityPlayerMP) entity).setPositionAndUpdate(x + 0.5D, newY + 2.0D, z + 0.5D); + } + } + } + } + + @Override + public String toString() { + return String.format( + "%s \'%s\' @ \'%s\' %d, %d, %d", + new Object[] { getClass().getSimpleName(), coreFrequency, worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName(), + Integer.valueOf(xCoord), Integer.valueOf(yCoord), Integer.valueOf(zCoord) }); + } + + @Override + public void updateEntity() { + if (FMLCommonHandler.instance().getEffectiveSide().isClient()) { + return; + } + super.updateEntity(); + + // Always cooldown + if (cooldownTime > 0) { + cooldownTime--; + warmupTime = 0; + } + + // Update state + if (cooldownTime > 0) { // cooling down (2) + if (getBlockMetadata() != 2) { + worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 2, 1 + 2); + } + } else if (controller == null) { // not connected (0) + if (getBlockMetadata() != 0) { + worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 0, 1 + 2); + } + } else if (controller.isJumpFlag() || this.controller.isSummonAllFlag() || !this.controller.getToSummon().isEmpty()) { // active + // (1) + if (getBlockMetadata() != 1) { + worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 1, 1 + 2); + } + } else { // inactive + if (getBlockMetadata() != 0) { + worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 0, 1 + 2); + } + } + + // Update warp core in cores registry + registryUpdateTicks++; + if (registryUpdateTicks > WarpDriveConfig.WC_CORES_REGISTRY_UPDATE_INTERVAL_SECONDS * 20) { + registryUpdateTicks = 0; + WarpDrive.warpCores.updateInRegistry(this); + // WarpDrive.warpCores.printRegistry(); + // WarpDrive.debugPrint("" + this + " controller is " + controller + + // ", warmupTime " + warmupTime + ", currentMode " + currentMode + + // ", jumpFlag " + (controller == null ? "NA" : + // controller.isJumpFlag()) + ", cooldownTime " + cooldownTime); + + TileEntity c = findControllerBlock(); + if (c == null) { + controller = null; + warmupTime = 0; + soundPlayed = false; + return; + } + controller = (TileEntityProtocol) c; + } + + isolationUpdateTicks++; + if (isolationUpdateTicks > WarpDriveConfig.WC_ISOLATION_UPDATE_INTERVAL_SECONDS * 20) { + isolationUpdateTicks = 0; + updateIsolationState(); + } + + if (controller == null) { + return; + } + + currentMode = controller.getMode(); + + StringBuilder reason = new StringBuilder(); + + if ((controller.isJumpFlag() && (isolationUpdateTicks == 1)) || this.controller.isSummonAllFlag() || !this.controller.getToSummon().isEmpty()) { + if (!validateShipSpatialParameters(reason)) { + if (controller.isJumpFlag()) { + controller.setJumpFlag(false); + messageToAllPlayersOnShip(reason.toString()); + } + warmupTime = 0; + soundPlayed = false; + return; + } + + if (this.controller.isSummonAllFlag()) { + summonPlayers(); + controller.setSummonAllFlag(false); + } else if (!this.controller.getToSummon().isEmpty()) { + summonSinglePlayer(this.controller.getToSummon()); + this.controller.setToSummon(""); + } + } + + switch (currentMode) { + case TELEPORT: + if (worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord)) { + if (isChestSummonMode()) { + chestTeleportUpdateTicks++; + if (chestTeleportUpdateTicks >= 20) { + summonPlayersByChestCode(); + chestTeleportUpdateTicks = 0; + } + } else { + teleportPlayersToSpace(); + } + } else { + chestTeleportUpdateTicks = 0; + } + break; + + case BASIC_JUMP: + case LONG_JUMP: + case BEACON_JUMP: + case HYPERSPACE: + case GATE_JUMP: + if (controller.isJumpFlag()) { + // Compute warm-up time + int targetWarmup = 0; + switch (currentMode) { + case BASIC_JUMP: + case LONG_JUMP: + if (controller.getDistance() < 50) { + targetWarmup = WarpDriveConfig.WC_WARMUP_SHORTJUMP_SECONDS * 20; + } else { + targetWarmup = WarpDriveConfig.WC_WARMUP_LONGJUMP_SECONDS * 20; + } + break; + + case BEACON_JUMP: + case HYPERSPACE: + case GATE_JUMP: + default: + targetWarmup = WarpDriveConfig.WC_WARMUP_LONGJUMP_SECONDS * 20; + break; + } + // Select best sound file and adjust offset + int soundThreshold = 0; + String soundFile = ""; + if (targetWarmup < 10 * 20) { + soundThreshold = targetWarmup - 4 * 20; + soundFile = "warpdrive:warp_4s"; + } else if (targetWarmup > 29 * 20) { + soundThreshold = targetWarmup - 30 * 20; + soundFile = "warpdrive:warp_30s"; + } else { + soundThreshold = targetWarmup - 10 * 20; + soundFile = "warpdrive:warp_10s"; + } + // Add random duration + soundThreshold += randomWarmupAddition; + + // Check cooldown time + if (cooldownTime > 0) { + if (cooldownTime % 20 == 0) { + int seconds = cooldownTime / 20; + if ((seconds < 5) || ((seconds < 30) && (seconds % 5 == 0)) || (seconds % 10 == 0)) { + messageToAllPlayersOnShip("Warp core is cooling down... " + cooldownTime / 20 + "s to go..."); + } + } + return; + } + + // Set up activated animation + if (warmupTime == 0) { + messageToAllPlayersOnShip("Running pre-jump checklist..."); + + // update ship parameters + if (!validateShipSpatialParameters(reason)) { + controller.setJumpFlag(false); + messageToAllPlayersOnShip(reason.toString()); + return; + } + // WarpDrive.debugPrint(this + + // " Giving warp sickness targetWarmup " + targetWarmup + + // " distance " + controller.getDistance()); + makePlayersOnShipDrunk(targetWarmup + WarpDriveConfig.WC_WARMUP_RANDOM_TICKS); + } + + if (!soundPlayed && (soundThreshold > warmupTime)) { + // WarpDrive.debugPrint(this + " Playing sound effect '" + + // soundFile + "' soundThreshold " + soundThreshold + + // " warmupTime " + warmupTime); + worldObj.playSoundEffect(xCoord + 0.5f, yCoord + 0.5f, zCoord + 0.5f, soundFile, 4F, 1F); + soundPlayed = true; + } + + // Awaiting cool-down time + if (warmupTime < (targetWarmup + randomWarmupAddition)) { + warmupTime++; + return; + } + + warmupTime = 0; + soundPlayed = false; + + if (!validateShipSpatialParameters(reason)) { + controller.setJumpFlag(false); + messageToAllPlayersOnShip(reason.toString()); + return; + } + + if (WarpDrive.warpCores.isWarpCoreIntersectsWithOthers(this)) { + controller.setJumpFlag(false); + messageToAllPlayersOnShip("Warp field intersects with other ship's field. Cannot jump."); + return; + } + + if (WarpDrive.cloaks.isCloaked(worldObj.provider.dimensionId, xCoord, yCoord, zCoord)) { + controller.setJumpFlag(false); + messageToAllPlayersOnShip("Core is inside a cloaking field. Aborting. Disable cloaking field to jump!"); + return; + } + + doJump(); + cooldownTime = WarpDriveConfig.WC_COOLDOWN_INTERVAL_SECONDS * 20; + controller.setJumpFlag(false); + } else { + warmupTime = 0; + } + break; + default: + break; + } + } + + private void updateIsolationState() { + // Search block in cube around core + int xmax, ymax, zmax; + int xmin, ymin, zmin; + xmin = xCoord - WarpDriveConfig.WR_MAX_ISOLATION_RANGE; + xmax = xCoord + WarpDriveConfig.WR_MAX_ISOLATION_RANGE; + + zmin = zCoord - WarpDriveConfig.WR_MAX_ISOLATION_RANGE; + zmax = zCoord + WarpDriveConfig.WR_MAX_ISOLATION_RANGE; + + // scan 1 block higher to encourage putting isolation block on both + // ground and ceiling + ymin = Math.max(0, yCoord - WarpDriveConfig.WR_MAX_ISOLATION_RANGE + 1); + ymax = Math.min(255, yCoord + WarpDriveConfig.WR_MAX_ISOLATION_RANGE + 1); + + int newCount = 0; + + // Search for warp isolation blocks + for (int y = ymin; y <= ymax; y++) { + for (int x = xmin; x <= xmax; x++) { + for (int z = zmin; z <= zmax; z++) { + if (worldObj.getBlock(x, y, z).isAssociatedBlock(WarpDrive.isolationBlock)) { + newCount++; + } + } + } + } + isolationBlocksCount = newCount; + if (isolationBlocksCount >= WarpDriveConfig.WR_MIN_ISOLATION_BLOCKS) { + isolationRate = WarpDriveConfig.WR_MIN_ISOLATION_EFFECT + + (isolationBlocksCount - WarpDriveConfig.WR_MIN_ISOLATION_BLOCKS) // bonus + // blocks + * (WarpDriveConfig.WR_MAX_ISOLATION_EFFECT - WarpDriveConfig.WR_MIN_ISOLATION_EFFECT) + / (WarpDriveConfig.WR_MAX_ISOLATION_BLOCKS - WarpDriveConfig.WR_MIN_ISOLATION_BLOCKS); + } else { + isolationRate = 0.0D; + } + // WarpDrive.debugPrint(this + " Isolation updated to " + + // isolationBlocksCount + " (" + String.format("%.1f", isolationRate * + // 100) + "%)"); + } + + @Override + public void validate() { + super.validate(); + WarpDrive.warpCores.updateInRegistry(this); + } + + public boolean validateShipSpatialParameters(StringBuilder reason) { + if (controller == null) { + reason.append("TileEntityReactor.validateShipSpatialParameters: no controller detected!"); + return false; + } + direction = controller.getDirection(); + shipFront = controller.getFront(); + shipRight = controller.getRight(); + shipUp = controller.getUp(); + shipBack = controller.getBack(); + shipLeft = controller.getLeft(); + shipDown = controller.getDown(); + + int x1 = 0, x2 = 0, z1 = 0, z2 = 0; + + if (Math.abs(dx) > 0) { + if (dx == 1) { + x1 = xCoord - shipBack; + x2 = xCoord + shipFront; + z1 = zCoord - shipLeft; + z2 = zCoord + shipRight; + } else { + x1 = xCoord - shipFront; + x2 = xCoord + shipBack; + z1 = zCoord - shipRight; + z2 = zCoord + shipLeft; + } + } else if (Math.abs(dz) > 0) { + if (dz == 1) { + z1 = zCoord - shipBack; + z2 = zCoord + shipFront; + x1 = xCoord - shipRight; + x2 = xCoord + shipLeft; + } else { + z1 = zCoord - shipFront; + z2 = zCoord + shipBack; + x1 = xCoord - shipLeft; + x2 = xCoord + shipRight; + } + } + + if (x1 < x2) { + minX = x1; + maxX = x2; + } else { + minX = x2; + maxX = x1; + } + + if (z1 < z2) { + minZ = z1; + maxZ = z2; + } else { + minZ = z2; + maxZ = z1; + } + + minY = yCoord - shipDown; + maxY = yCoord + shipUp; + shipLength = 0; + + switch (direction) { + case 0: + case 180: + shipLength = shipBack + shipFront; + break; + + case 90: + case 270: + shipLength = shipLeft + shipRight; + break; + + case -1: + case -2: + shipLength = shipDown + shipUp; + break; + + default: + reason.append("Invalid jump direction " + direction); + return false; + } + + // Ship side is too big + if ((shipBack + shipFront) > WarpDriveConfig.WC_MAX_SHIP_SIDE || (shipLeft + shipRight) > WarpDriveConfig.WC_MAX_SHIP_SIDE + || (shipDown + shipUp) > WarpDriveConfig.WC_MAX_SHIP_SIDE) { + reason.append("Ship is too big (max is " + WarpDriveConfig.WC_MAX_SHIP_SIDE + " per side)"); + return false; + } + + this.shipVolume = computeRealShipVolume(); + + if (shipVolume > WarpDriveConfig.WC_MAX_SHIP_VOLUME_ON_SURFACE && worldObj.provider.dimensionId == 0) { + reason.append("Ship is too big for the overworld (max is " + WarpDriveConfig.WC_MAX_SHIP_VOLUME_ON_SURFACE + " blocks)"); + return false; + } + + return true; + } + + @Override + public void writeToNBT(NBTTagCompound tag) { + super.writeToNBT(tag); + tag.setString("corefrequency", coreFrequency); + tag.setInteger("isolation", isolationBlocksCount); + tag.setInteger("cooldownTime", cooldownTime); + } + + @Override + public int getSinkTier() { + // TODO Auto-generated method stub + return 3; + } + + @Override + public int getSourceTier() { + // TODO Auto-generated method stub + return 3; + } } diff --git a/src/java/cr0s/warpdrive/machines/TileEntityShipScanner.java b/src/java/cr0s/warpdrive/machines/TileEntityShipScanner.java index 5ab5e1d9..6362d3ee 100644 --- a/src/java/cr0s/warpdrive/machines/TileEntityShipScanner.java +++ b/src/java/cr0s/warpdrive/machines/TileEntityShipScanner.java @@ -6,7 +6,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import jdk.nashorn.internal.runtime.regexp.joni.constants.Arguments; +import li.cil.oc.api.machine.Arguments; import li.cil.oc.api.machine.Callback; import li.cil.oc.api.machine.Context; import net.minecraft.block.Block; @@ -267,16 +267,16 @@ public class TileEntityShipScanner extends WarpEnergyTE { // Do not scan air, bedrock and specified forbidden blocks (like ore or Warp-Cores) if (worldObj.isAirBlock(core.minX + x, core.minY + y, core.minZ + z) || block.isAssociatedBlock(Blocks.bedrock) || WarpDriveConfig.scannerIgnoreBlocks.contains(block)) { - block = 0; + block = Blocks.air; } - localBlocks[x + (y * length + z) * width] = (byte) block; + localBlocks[x + (y * length + z) * width] = (byte) block.getUnlocalizedName(); localMetadata[x + (y * length + z) * width] = (byte) worldObj.getBlockMetadata(core.minX + x, core.minY + y, core.minZ + z); if ((extraBlocks[x + (y * length + z) * width] = (byte) (block >> 8)) > 0) { extra = true; } - if (block != 0) { + if (!block.isAssociatedBlock(Blocks.air)) { TileEntity te = worldObj.getTileEntity(core.minX + x, core.minY + y, core.minZ + z); if (te != null) { try { @@ -625,7 +625,7 @@ public class TileEntityShipScanner extends WarpEnergyTE { // ComputerCraft IPeripheral methods implementation @Override @Optional.Method(modid = "ComputerCraft") - public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception { + public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) { String methodName = methodsArray[method]; if (methodName.equals("scan")) { return scan(arguments); @@ -651,14 +651,20 @@ public class TileEntityShipScanner extends WarpEnergyTE { public int getMaxEnergyStored() { return WarpDriveConfig.SS_MAX_ENERGY_VALUE; } - - @Override - public int getMaxSafeInput() { - return Integer.MAX_VALUE; - } - @Override public boolean canInputEnergy(ForgeDirection from) { return true; } + + @Override + public int getSinkTier() { + // TODO Arbitrarily chosen value + return 3; + } + + @Override + public int getSourceTier() { + // TODO Arbitrarily chosen value + return 3; + } } diff --git a/src/java/cr0s/warpdrive/machines/WarpEnergyTE.java b/src/java/cr0s/warpdrive/machines/WarpEnergyTE.java index c004bb72..4025316f 100644 --- a/src/java/cr0s/warpdrive/machines/WarpEnergyTE.java +++ b/src/java/cr0s/warpdrive/machines/WarpEnergyTE.java @@ -7,7 +7,7 @@ import ic2.api.energy.tile.IEnergySource; import java.util.HashMap; -import jdk.nashorn.internal.runtime.regexp.joni.constants.Arguments; +import li.cil.oc.api.machine.Arguments; import li.cil.oc.api.machine.Callback; import li.cil.oc.api.machine.Context; import net.minecraft.nbt.NBTTagCompound;