From c999c818652dbaa381de8d85b81ad9d3a57cf7b6 Mon Sep 17 00:00:00 2001 From: CrazyPants Date: Fri, 24 Oct 2014 14:34:04 +0800 Subject: [PATCH] EnderRail now working with RC linked trains. --- .../enderio/rail/BlockEnderRail.java | 211 +++------------ .../crazypants/enderio/rail/TeleportUtil.java | 242 ++++++++++++++++++ .../enderio/rail/TileEnderRail.java | 63 ++++- 3 files changed, 333 insertions(+), 183 deletions(-) create mode 100644 src/main/java/crazypants/enderio/rail/TeleportUtil.java diff --git a/src/main/java/crazypants/enderio/rail/BlockEnderRail.java b/src/main/java/crazypants/enderio/rail/BlockEnderRail.java index a2dd82c9..22466686 100644 --- a/src/main/java/crazypants/enderio/rail/BlockEnderRail.java +++ b/src/main/java/crazypants/enderio/rail/BlockEnderRail.java @@ -1,5 +1,6 @@ package crazypants.enderio.rail; +import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -28,6 +29,7 @@ import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import crazypants.enderio.EnderIO; import crazypants.enderio.EnderIOTab; +import crazypants.enderio.Log; import crazypants.enderio.ModObject; import crazypants.enderio.conduit.ConduitUtil; import crazypants.enderio.config.Config; @@ -200,10 +202,10 @@ public class BlockEnderRail extends BlockRail { if(isValidDestination(sender, channel, reciever)) { int requiredPower = getPowerRequired(cart, sender, reciever); if(sender.getEnergyStored() >= requiredPower) { - sender.usePower(requiredPower); - teleportCart(world, cart, sender, reciever); - - return; + if(teleportCart(world, cart, sender, reciever)) { + sender.usePower(requiredPower); + return; + } } } } @@ -233,15 +235,8 @@ public class BlockEnderRail extends BlockRail { private int getPowerRequired(EntityMinecart cart, TileTransceiver sender, TileTransceiver reciever) { int powerPerCart = getPowerRequiredForSingleCart(sender, reciever); - // ILinkageManager linkMan = CartTools.getLinkageManager(sender.getWorldObj()); - // if(linkMan == null) { - return powerPerCart; - // } - // int numInLink = linkMan.countCartsInTrain(cart); - // if(numInLink <= 1) { - // return powerPerCart; - // } - // return numInLink * powerPerCart; + int numCarts = TeleportUtil.getNumberOfCartsInTrain(cart); + return powerPerCart * numCarts; } private int getPowerRequiredForSingleCart(TileTransceiver sender, TileTransceiver reciever) { @@ -255,174 +250,52 @@ public class BlockEnderRail extends BlockRail { return powerRequired; } - private void teleportCart(World world, EntityMinecart cart, TileTransceiver sender, TileTransceiver reciever) { - updateCartLinks(world, cart); - teleportSingleCart(world, cart, sender, reciever); - } + private boolean teleportCart(World world, EntityMinecart cart, TileTransceiver sender, TileTransceiver reciever) { - private void teleportSingleCart(World world, EntityMinecart cart, TileTransceiver sender, TileTransceiver reciever) { - - int toDimension = reciever.getWorldObj().provider.dimensionId; - int toX = reciever.xCoord; - int toY = reciever.yCoord + 1; - int toZ = reciever.zCoord; - - - TileEntity te = sender.getWorldObj().getTileEntity(sender.xCoord, sender.yCoord +1 , sender.zCoord); - if(!(te instanceof TileEnderRail)) { - return; - } - TileEnderRail senderRail = (TileEnderRail) te; - - te = reciever.getWorldObj().getTileEntity(toX, toY, toZ); - if(!(te instanceof TileEnderRail)) { - return; - } - TileEnderRail destinationRail = (TileEnderRail) te; - - - - MinecraftServer minecraftserver = MinecraftServer.getServer(); - int j = cart.dimension; - WorldServer worldserver = minecraftserver.worldServerForDimension(j); - WorldServer worldserver1 = minecraftserver.worldServerForDimension(toDimension); - cart.dimension = toDimension; - - if(j == 1 && toDimension == 1) { - worldserver1 = minecraftserver.worldServerForDimension(0); - cart.dimension = 0; + TileEnderRail destRail = getRailTile(reciever); + if(destRail == null) { + return false; } - Entity passenger = cart.riddenByEntity; - if(passenger != null) { - worldserver.removeEntity(passenger); - passenger.isDead = true; + List allCarts = TeleportUtil.getCartsInTrain(cart); + if(allCarts.size() > 1) { + TeleportUtil.updateCartLinks(world, cart); } - ItemStack[] stacks = null; - if(cart instanceof IInventory) { - IInventory cont = (IInventory) cart; - stacks = new ItemStack[cont.getSizeInventory()]; - for (int i = 0; i < stacks.length; i++) { - stacks[i] = cont.getStackInSlot(i); - cont.setInventorySlotContents(i, null); - } - } - - worldserver.removeEntity(cart); - cart.isDead = false; - - EntityMinecart newCart = (EntityMinecart) EntityList.createEntityByName(EntityList.getEntityString(cart), worldserver1); - if(newCart != null) { - - NBTTagCompound nbttagcompound = new NBTTagCompound(); - cart.writeToNBT(nbttagcompound); - newCart.readFromNBT(nbttagcompound); - - newCart.setLocationAndAngles(toX + 0.5, toY, toZ + 0.5, cart.rotationYaw, cart.rotationPitch); - - if(stacks != null && newCart instanceof IInventory) { - IInventory cont = (IInventory) newCart; - for (int i = 0; i < stacks.length; i++) { - cont.setInventorySlotContents(i, stacks[i]); + List> toTeleport = new ArrayList>(allCarts.size()); + List toDespawn = new ArrayList(allCarts.size()); + for (EntityMinecart cartInTrain : allCarts) { + if(cartInTrain != null) { + List entities = TeleportUtil.createEntitiesForReciever(cartInTrain, sender, reciever); + if(entities != null) { + toTeleport.add(entities); + toDespawn.add(cartInTrain); } } - - if(passenger != null) { - Entity newPas = EntityList.createEntityByName(EntityList.getEntityString(passenger), worldserver1); - newPas.copyDataFrom(passenger, true); - newPas.setLocationAndAngles(toX + 0.5, toY, toZ + 0.5, cart.rotationYaw, cart.rotationPitch); - newCart.riddenByEntity = newPas; - newPas.ridingEntity = newCart; - worldserver1.spawnEntityInWorld(newPas); - } - - worldserver1.spawnEntityInWorld(newCart); - - destinationRail.onCartRecieved((EntityMinecart) newCart); - senderRail.onCartSent(cart); - - } - cart.isDead = true; + + for (EntityMinecart despawnCart : toDespawn) { + TeleportUtil.despawn(sender.getWorldObj(), despawnCart); + } + + destRail.onTrainRecieved(toTeleport); + return true; + + } + + private TileEnderRail getRailTile(TileTransceiver reciever) { + if(reciever == null || reciever.getWorldObj() == null) { + return null; + } + TileEntity te = reciever.getWorldObj().getTileEntity(reciever.xCoord, reciever.yCoord + 1, reciever.zCoord); + if(!(te instanceof TileEnderRail)) { + return null; + } + return (TileEnderRail) te; } public boolean isFlexibleRail(IBlockAccess world, int y, int x, int z) { return false; } - //------------ Link Utils - - public static void recreateLink(EntityMinecart existingCart, EntityMinecart newCart) { - if(existingCart == null || newCart == null) { - return; - } - ILinkageManager linkMan = CartTools.getLinkageManager(existingCart.worldObj); - if(linkMan == null) { - return; - } - UUID linkA = getLinkA(newCart); - if(linkA != null && linkA.equals(existingCart.getPersistentID())) { - if(!linkMan.areLinked(existingCart, newCart)) { - boolean res = linkMan.createLink(existingCart, newCart); - System.out.println("BlockEnderRail.recreateLink: A " + res); - } - return; - } - UUID linkB = getLinkB(newCart); - if(linkB != null && linkB.equals(existingCart.getPersistentID())) { - if(!linkMan.areLinked(existingCart, newCart)) { - boolean res = linkMan.createLink(existingCart, newCart); - System.out.println("BlockEnderRail.recreateLink: B " + res); - } - return; - } - } - - public static void updateCartLinks(World world, EntityMinecart cart) { - ILinkageManager linkMan = CartTools.getLinkageManager(cart.worldObj); - if(linkMan == null || linkMan.countCartsInTrain(cart) <= 1) { - return; - } - Iterable allCarts = linkMan.getCartsInTrain(cart); - for (EntityMinecart aCart : allCarts) { - if(aCart != null) { - updateLink("a", aCart, linkMan.getLinkedCartA(aCart)); - updateLink("b", aCart, linkMan.getLinkedCartB(aCart)); - } - } - } - - private static void updateLink(String prefix, EntityMinecart cart, EntityMinecart linkedCart) { - NBTTagCompound data = cart.getEntityData(); - long lastUpdateTime = -1; - String timeKey = prefix + "UpdateTime"; - if(data.hasKey(timeKey)) { - lastUpdateTime = data.getLong(timeKey); - } - long curTime = cart.worldObj.getTotalWorldTime(); - if(lastUpdateTime > 0 && curTime - lastUpdateTime < 100) { - return; - } - data.setLong(timeKey, curTime); - data.setString(prefix + "Link", linkedCart == null ? "null" : linkedCart.getPersistentID().toString()); - } - - public static UUID getLinkA(EntityMinecart cart) { - return getLink("a", cart); - } - - public static UUID getLinkB(EntityMinecart cart) { - return getLink("b", cart); - } - - private static UUID getLink(String prefix, EntityMinecart cart) { - NBTTagCompound data = cart.getEntityData(); - String uuidStr = data.getString(prefix + "Link"); - if(uuidStr == null || uuidStr.trim().isEmpty() || "null".equals(uuidStr)) { - return null; - } - return UUID.fromString(uuidStr); - } - } diff --git a/src/main/java/crazypants/enderio/rail/TeleportUtil.java b/src/main/java/crazypants/enderio/rail/TeleportUtil.java new file mode 100644 index 00000000..7a38658f --- /dev/null +++ b/src/main/java/crazypants/enderio/rail/TeleportUtil.java @@ -0,0 +1,242 @@ +package crazypants.enderio.rail; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.UUID; + +import mods.railcraft.api.carts.CartTools; +import mods.railcraft.api.carts.ILinkageManager; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityList; +import net.minecraft.entity.item.EntityMinecart; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.server.MinecraftServer; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; +import net.minecraft.world.WorldServer; +import crazypants.enderio.machine.transceiver.TileTransceiver; +import crazypants.vecmath.Vector3d; + +public class TeleportUtil { + + public static List createEntitiesForReciever(EntityMinecart cart, TileTransceiver sender, TileTransceiver reciever) { + int toDimension = reciever.getWorldObj().provider.dimensionId; + int toX = reciever.xCoord; + int toY = reciever.yCoord + 1; + int toZ = reciever.zCoord; + + MinecraftServer minecraftserver = MinecraftServer.getServer(); + WorldServer worldserver1 = minecraftserver.worldServerForDimension(toDimension); + EntityMinecart newCart = (EntityMinecart) EntityList.createEntityByName(EntityList.getEntityString(cart), worldserver1); + if(newCart == null) { + return null; + } + + NBTTagCompound nbttagcompound = new NBTTagCompound(); + cart.writeToNBT(nbttagcompound); + newCart.readFromNBT(nbttagcompound); + newCart.dimension = toDimension; + newCart.setLocationAndAngles(toX + 0.5, toY, toZ + 0.5, cart.rotationYaw, cart.rotationPitch); + newCart.isDead = false; + + List result = new ArrayList(); + result.add(newCart); + + Entity passenger = cart.riddenByEntity; + if(passenger != null && !(passenger instanceof EntityPlayer)) { + Entity newPas = EntityList.createEntityByName(EntityList.getEntityString(passenger), worldserver1); + newPas.copyDataFrom(passenger, true); + newPas.dimension = toDimension; + newPas.setLocationAndAngles(toX + 0.5, toY, toZ + 0.5, cart.rotationYaw, cart.rotationPitch); + newCart.riddenByEntity = newPas; + newPas.ridingEntity = newCart; + result.add(newPas); + } + return result; + } + + public static void despawn(World world, EntityMinecart cart) { + + if(cart instanceof IInventory) { + IInventory cont = (IInventory) cart; + for (int i = 0; i < cont.getSizeInventory(); i++) { + cont.setInventorySlotContents(i, null); + } + } + + MinecraftServer minecraftserver = MinecraftServer.getServer(); + WorldServer worldserver = minecraftserver.worldServerForDimension(world.provider.dimensionId); + + Entity passenger = cart.riddenByEntity; + if(passenger != null) { + worldserver.removeEntity(passenger); + passenger.isDead = true; + } + worldserver.removeEntity(cart); + cart.isDead = true; + + } + + public static void spawn(World world, Entity entity ) { + MinecraftServer minecraftserver = MinecraftServer.getServer(); + WorldServer worldserver = minecraftserver.worldServerForDimension(world.provider.dimensionId); + if(entity != null) { + worldserver.spawnEntityInWorld(entity); + } + } + + //------------ Link Utils + + public static void recreateLink(EntityMinecart existingCart, EntityMinecart newCart) { + if(existingCart == null || newCart == null) { + return; + } + ILinkageManager linkMan = CartTools.getLinkageManager(existingCart.worldObj); + if(linkMan == null) { + return; + } + UUID linkA = getLinkA(newCart); + if(linkA != null && linkA.equals(existingCart.getPersistentID())) { + if(!linkMan.areLinked(existingCart, newCart)) { + boolean res = linkMan.createLink(existingCart, newCart); + } + return; + } + UUID linkB = getLinkB(newCart); + if(linkB != null && linkB.equals(existingCart.getPersistentID())) { + if(!linkMan.areLinked(existingCart, newCart)) { + boolean res = linkMan.createLink(existingCart, newCart); + } + return; + } + } + + public static void updateCartLinks(World world, EntityMinecart cart) { + ILinkageManager linkMan = CartTools.getLinkageManager(cart.worldObj); + if(linkMan == null || linkMan.countCartsInTrain(cart) <= 1) { + return; + } + Iterable allCarts = linkMan.getCartsInTrain(cart); + for (EntityMinecart aCart : allCarts) { + if(aCart != null) { + updateLink("a", aCart, linkMan.getLinkedCartA(aCart)); + updateLink("b", aCart, linkMan.getLinkedCartB(aCart)); + } + } + } + + private static void updateLink(String prefix, EntityMinecart cart, EntityMinecart linkedCart) { + NBTTagCompound data = cart.getEntityData(); + long lastUpdateTime = -1; + String timeKey = prefix + "UpdateTime"; + if(data.hasKey(timeKey)) { + lastUpdateTime = data.getLong(timeKey); + } + long curTime = cart.worldObj.getTotalWorldTime(); + if(lastUpdateTime > 0 && curTime - lastUpdateTime < 100) { + return; + } + data.setLong(timeKey, curTime); + data.setString(prefix + "Link", linkedCart == null ? "null" : linkedCart.getPersistentID().toString()); + } + + public static UUID getLinkA(EntityMinecart cart) { + return getLink("a", cart); + } + + public static UUID getLinkB(EntityMinecart cart) { + return getLink("b", cart); + } + + private static UUID getLink(String prefix, EntityMinecart cart) { + NBTTagCompound data = cart.getEntityData(); + String uuidStr = data.getString(prefix + "Link"); + if(uuidStr == null || uuidStr.trim().isEmpty() || "null".equals(uuidStr)) { + return null; + } + return UUID.fromString(uuidStr); + } + + public static int getNumberOfCartsInTrain(EntityMinecart cart) { + if(cart == null) { + return 0; + } + ILinkageManager linkMan = CartTools.getLinkageManager(cart.worldObj); + if(linkMan == null) { + return 1; + } + return linkMan.countCartsInTrain(cart); + } + + public static List getCartsInTrain(EntityMinecart cart) { + if(cart == null) { + Collections.emptyList(); + } + List result = new ArrayList(); + ILinkageManager linkMan = CartTools.getLinkageManager(cart.worldObj); + if(linkMan == null) { + result.add(cart); + return result; + } + + Iterable iter = linkMan.getCartsInTrain(cart); + for (EntityMinecart cartInTrain : iter) { + if(cartInTrain != null) { + result.add(cartInTrain); + } + } + Collections.sort(result, new TrainOrderComparator(cart)); + return result; + } + + public static void recreateLinks(EntityMinecart cart) { + ILinkageManager linkMan = CartTools.getLinkageManager(cart.worldObj); + if(linkMan == null) { + return; + } + recreateLink(cart, linkMan, getLinkA(cart)); + recreateLink(cart, linkMan, getLinkB(cart)); + } + + private static void recreateLink(EntityMinecart cart, ILinkageManager linkMan, UUID uuid) { + if(uuid != null) { + EntityMinecart linkTo = linkMan.getCartFromUUID(uuid); + if(!linkMan.areLinked(cart, linkTo)) { + linkMan.createLink(cart, linkTo); + } + } + } + + private static class TrainOrderComparator implements Comparator { + + private final Vector3d refPoint = new Vector3d(); + private final Vector3d loc1 = new Vector3d(); + private final Vector3d loc2 = new Vector3d(); + private double d1; + private double d2; + + TrainOrderComparator(EntityMinecart head) { + set(refPoint, head); + } + + @Override + public int compare(EntityMinecart o1, EntityMinecart o2) { + set(loc1, o1); + set(loc2, o2); + d1 = refPoint.distanceSquared(loc1); + d2 = refPoint.distanceSquared(loc2); + return Double.compare(d1, d2); + } + + private void set(Vector3d point, EntityMinecart cart) { + point.set(cart.posX, cart.posY, cart.posZ); + } + + } + +} diff --git a/src/main/java/crazypants/enderio/rail/TileEnderRail.java b/src/main/java/crazypants/enderio/rail/TileEnderRail.java index 5279d965..1aa8b0bd 100644 --- a/src/main/java/crazypants/enderio/rail/TileEnderRail.java +++ b/src/main/java/crazypants/enderio/rail/TileEnderRail.java @@ -2,11 +2,14 @@ package crazypants.enderio.rail; import java.util.ArrayList; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.UUID; +import crazypants.util.ForgeDirectionOffsets; import crazypants.util.MetadataUtil; +import crazypants.vecmath.Vector3d; import mods.railcraft.api.carts.CartTools; import mods.railcraft.api.carts.ILinkageManager; @@ -20,23 +23,55 @@ import net.minecraftforge.common.util.ForgeDirection; public class TileEnderRail extends TileEntity { - private Set recievedCarts = new HashSet(); - private EntityMinecart lastCartRecieved; + //keep track of which carts we have received have left the track as they can't be teleported again until they have + private final Set newlySpawnedCarts = new HashSet(); + + private final LinkedList> entitiesToSpawn = new LinkedList>(); + + private int ticksFailedToSpawn = 0; @Override public void updateEntity() { + + spawnRecievedCart(); + List carts = getMinecartsAt(getWorldObj(), xCoord, yCoord, zCoord); List toRemove = new ArrayList(); - for (EntityMinecart recievedCart : recievedCarts) { + for (EntityMinecart recievedCart : newlySpawnedCarts) { if(!carts.contains(recievedCart)) { toRemove.add(recievedCart); } } for (EntityMinecart recievedCart : toRemove) { - recievedCarts.remove(recievedCart); + newlySpawnedCarts.remove(recievedCart); } } + private void spawnRecievedCart() { + if(entitiesToSpawn.isEmpty()) { + ticksFailedToSpawn = 0; + return; + } + if(!isClear()) { + ticksFailedToSpawn++; + if(ticksFailedToSpawn < 60) { + return; + } + } + + List spawnThisTick = entitiesToSpawn.removeFirst(); + for(Entity ent : spawnThisTick) { + TeleportUtil.spawn(getWorldObj(), ent); + if(ent instanceof EntityMinecart) { + EntityMinecart cart = (EntityMinecart)ent; + newlySpawnedCarts.add(cart); + TeleportUtil.recreateLinks(cart); + } + } + ticksFailedToSpawn = 0; + + } + public boolean isReverse() { return BlockEnderRail.isReverse(getBlockMetadata()); } @@ -45,22 +80,22 @@ public class TileEnderRail extends TileEntity { return BlockEnderRail.getDirection(getBlockMetadata()); } - public void onCartRecieved(EntityMinecart cart) { - recievedCarts.add(cart); - BlockEnderRail.recreateLink(lastCartRecieved, cart); - lastCartRecieved = cart; + public void onTrainRecieved(List> toTeleport) { + entitiesToSpawn.addAll(toTeleport); } - public void onCartSent(EntityMinecart cart) { - recievedCarts.remove(cart); - } - public boolean isRecievedCart(EntityMinecart mc) { - return recievedCarts.contains(mc); + return newlySpawnedCarts.contains(mc); } public boolean isClear() { - List res = worldObj.getEntitiesWithinAABB(Entity.class, AxisAlignedBB.getBoundingBox(xCoord, yCoord + 1, zCoord, xCoord + 1, yCoord + 2, zCoord + 1)); + double buf = 1; + ForgeDirection dir = BlockEnderRail.getDirection(getBlockMetadata()); + Vector3d offset = ForgeDirectionOffsets.forDirCopy(dir); + offset.scale(buf); + offset.x = Math.abs(offset.x); + offset.z = Math.abs(offset.z); + List res = worldObj.getEntitiesWithinAABB(Entity.class, AxisAlignedBB.getBoundingBox(xCoord - offset.x, yCoord, zCoord - offset.z, xCoord + 1 + offset.x, yCoord + 1, zCoord + 1 + offset.z)); return res == null || res.isEmpty(); }