diff --git a/src/main/java/crazypants/enderio/config/Config.java b/src/main/java/crazypants/enderio/config/Config.java index b596f7e7..dbdceaf5 100644 --- a/src/main/java/crazypants/enderio/config/Config.java +++ b/src/main/java/crazypants/enderio/config/Config.java @@ -376,6 +376,7 @@ public final class Config { public static int enderRailPowerRequiredPerBlock = 10; public static boolean enderRailCapSameDimensionPowerAtCrossDimensionCost = true; public static int enderRailTicksBeforeForceSpawningLinkedCarts = 60; + public static boolean enderRailTeleportPlayers = false; public static void load(FMLPreInitializationEvent event) { @@ -996,7 +997,7 @@ public final class Config { "When set to true the RF cost of sending a cart within the same dimension will be capped to the cross dimension cost"); enderRailTicksBeforeForceSpawningLinkedCarts = config.get(sectionRailConfig.name, "enderRailTicksBeforeForceSpawningLinkedCarts", enderRailTicksBeforeForceSpawningLinkedCarts, "The number of ticks to wait for the track to clear before force spawning the next cart in a (RailCraft) linked set").getInt(enderRailTicksBeforeForceSpawningLinkedCarts); - + enderRailTeleportPlayers = config.getBoolean("enderRailTeleportPlayers", sectionRailConfig.name, enderRailTeleportPlayers, "If true player in minecarts will be teleported. WARN: WIP, seems to cause a memory leak."); dumpMobNames = config.getBoolean("dumpMobNames", sectionMobConfig.name, dumpMobNames, "When set to true a list of all registered mobs will be dumped to config/enderio/mobTypes.txt The names are in the format required by EIOs mob blacklists."); diff --git a/src/main/java/crazypants/enderio/rail/BlockEnderRail.java b/src/main/java/crazypants/enderio/rail/BlockEnderRail.java index fcdcfb12..a0027e8f 100644 --- a/src/main/java/crazypants/enderio/rail/BlockEnderRail.java +++ b/src/main/java/crazypants/enderio/rail/BlockEnderRail.java @@ -2,46 +2,34 @@ package crazypants.enderio.rail; import java.util.ArrayList; import java.util.List; -import java.util.UUID; -import mods.railcraft.api.carts.CartTools; -import mods.railcraft.api.carts.ILinkageManager; import net.minecraft.block.Block; import net.minecraft.block.BlockRail; import net.minecraft.client.renderer.texture.IIconRegister; import net.minecraft.entity.Entity; -import net.minecraft.entity.EntityList; -import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.item.EntityMinecart; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.inventory.IInventory; +import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.server.MinecraftServer; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.IIcon; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; -import net.minecraft.world.WorldServer; import net.minecraftforge.common.util.ForgeDirection; +import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.registry.GameRegistry; -import cpw.mods.fml.relauncher.ReflectionHelper; 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; import crazypants.enderio.gui.IResourceTooltipProvider; -import crazypants.enderio.machine.AbstractMachineEntity; import crazypants.enderio.machine.transceiver.Channel; import crazypants.enderio.machine.transceiver.ChannelType; import crazypants.enderio.machine.transceiver.ServerChannelRegister; import crazypants.enderio.machine.transceiver.TileTransceiver; import crazypants.enderio.network.PacketHandler; -import crazypants.enderio.teleport.packet.PacketConfigSync; import crazypants.util.MetadataUtil; import crazypants.util.RoundRobinIterator; @@ -72,6 +60,10 @@ public class BlockEnderRail extends BlockRail implements IResourceTooltipProvide PacketHandler.INSTANCE.registerMessage(PacketTeleportEffects.class, PacketTeleportEffects.class, PacketHandler.nextID(), Side.CLIENT); BlockEnderRail res = new BlockEnderRail(); res.init(); + + if(Config.enderRailTeleportPlayers) { + FMLCommonHandler.instance().bus().register(PlayerTeleportHandler.instance); + } return res; } @@ -153,6 +145,15 @@ public class BlockEnderRail extends BlockRail implements IResourceTooltipProvide return canPlaceBlockAt(world, x, y, z); } + public boolean isFlexibleRail(IBlockAccess world, int y, int x, int z) { + return false; + } + + @Override + public String getUnlocalizedNameForTooltip(ItemStack itemStack) { + return getUnlocalizedName(); + } + public void onNeighborBlockChange(World world, int x, int y, int z, Block block) { if(world.isRemote) { return; @@ -240,7 +241,7 @@ public class BlockEnderRail extends BlockRail implements IResourceTooltipProvide private int getPowerRequired(EntityMinecart cart, TileTransceiver sender, TileTransceiver reciever) { int powerPerCart = getPowerRequiredForSingleCart(sender, reciever); - int numCarts = TeleportUtil.getNumberOfCartsInTrain(cart); + int numCarts = CartLinkUtil.getNumberOfCartsInTrain(cart); return powerPerCart * numCarts; } @@ -259,40 +260,47 @@ public class BlockEnderRail extends BlockRail implements IResourceTooltipProvide private boolean teleportCart(World world, EntityMinecart cart, TileTransceiver sender, TileTransceiver reciever) { - List allCarts = TeleportUtil.getCartsInTrain(cart); + List allCarts = CartLinkUtil.getCartsInTrain(cart); if(allCarts.size() > 1) { - TeleportUtil.updateCartLinks(world, cart); + CartLinkUtil.updateCartLinks(world, cart); } List> toTeleport = new ArrayList>(allCarts.size()); List toDespawn = new ArrayList(allCarts.size()); + EntityPlayerMP playerToTP = null; + EntityMinecart playerToMount = null; for (EntityMinecart cartInTrain : allCarts) { if(cartInTrain != null) { List entities = TeleportUtil.createEntitiesForReciever(cartInTrain, sender, reciever); if(entities != null) { toTeleport.add(entities); toDespawn.add(cartInTrain); + if(Config.enderRailTeleportPlayers && cartInTrain.riddenByEntity instanceof EntityPlayerMP) { + playerToTP = (EntityPlayerMP) cartInTrain.riddenByEntity; + playerToMount = getCart(entities); + } } } } - for (EntityMinecart despawnCart : toDespawn) { TeleportUtil.spawnTeleportEffects(world, despawnCart); TeleportUtil.despawn(sender.getWorldObj(), despawnCart); } - reciever.getRailController().onTrainRecieved(toTeleport); + if(playerToTP != null) { + PlayerTeleportHandler.instance.teleportPlayer(reciever, playerToTP, playerToMount); + } return true; } - public boolean isFlexibleRail(IBlockAccess world, int y, int x, int z) { - return false; - } - - @Override - public String getUnlocalizedNameForTooltip(ItemStack itemStack) { - return getUnlocalizedName(); + private EntityMinecart getCart(List entities) { + for (Entity ent : entities) { + if(ent instanceof EntityMinecart) { + return (EntityMinecart) ent; + } + } + return null; } } diff --git a/src/main/java/crazypants/enderio/rail/CartLinkUtil.java b/src/main/java/crazypants/enderio/rail/CartLinkUtil.java new file mode 100644 index 00000000..3cd381f3 --- /dev/null +++ b/src/main/java/crazypants/enderio/rail/CartLinkUtil.java @@ -0,0 +1,187 @@ +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.item.EntityMinecart; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.world.World; +import crazypants.vecmath.Vector3d; + +public class CartLinkUtil { + + private CartLinkUtil() { + } + + public static void breakLinks(World world, Entity cart) { + if(cart instanceof EntityMinecart) { + breakLinks(world, (EntityMinecart) cart); + } + } + + public static void breakLinks(World world, EntityMinecart cart) { + if(world == null || cart == null) { + return; + } + ILinkageManager linkMan = CartTools.getLinkageManager(world); + if(linkMan == null) { + return; + } + linkMan.breakLinks(cart); + } + + 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/EnderRailController.java b/src/main/java/crazypants/enderio/rail/EnderRailController.java index 31a7021a..3778c0e9 100644 --- a/src/main/java/crazypants/enderio/rail/EnderRailController.java +++ b/src/main/java/crazypants/enderio/rail/EnderRailController.java @@ -5,15 +5,16 @@ import java.util.Collection; import java.util.HashSet; import java.util.LinkedList; import java.util.List; +import java.util.ListIterator; import java.util.Set; import java.util.UUID; -import codechicken.nei.WorldOverlayRenderer; - import net.minecraft.block.Block; 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.entity.player.EntityPlayerMP; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTTagString; @@ -25,6 +26,7 @@ import net.minecraftforge.common.util.ForgeDirection; import crazypants.enderio.config.Config; import crazypants.enderio.machine.transceiver.TileTransceiver; import crazypants.util.BlockCoord; +import crazypants.util.EntityUtil; import crazypants.util.ForgeDirectionOffsets; import crazypants.vecmath.Vector3d; @@ -34,6 +36,7 @@ public class EnderRailController { private final Set newlySpawnedCarts = new HashSet(); private final LinkedList> cartsToSpawn = new LinkedList>(); + private final List playersToRemount = new ArrayList(); private int ticksFailedToSpawn = 0; @@ -54,7 +57,7 @@ public class EnderRailController { spawnRecievedCart(); - List carts = getMinecartsAt(transciever.getWorldObj(), transciever.xCoord, transciever.yCoord + 1, transciever.zCoord); + List carts = getMinecartsOnTrack(); List toRemove = new ArrayList(); //any cart in the newly spawned list no longer on the track needs to be removed @@ -80,6 +83,16 @@ public class EnderRailController { private void spawnRecievedCart() { + if(Config.enderRailTeleportPlayers) { + ListIterator li = playersToRemount.listIterator(); + while (li.hasNext()) { + PlayerTpInfo tpi = li.next(); + if(tpi.attemptMount() || tpi.attemptsRemaining <= 0) { + li.remove(); + } + } + } + if(cartsToSpawn.isEmpty()) { ticksFailedToSpawn = 0; return; @@ -101,10 +114,11 @@ public class EnderRailController { if(ent instanceof EntityMinecart) { EntityMinecart cart = (EntityMinecart) ent; newlySpawnedCarts.add(cart.getPersistentID()); - TeleportUtil.recreateLinks(cart); + CartLinkUtil.recreateLinks(cart); } } } + ticksFailedToSpawn = 0; } @@ -134,6 +148,10 @@ public class EnderRailController { return res == null || res.isEmpty(); } + public List getMinecartsOnTrack() { + return getMinecartsAt(transciever.getWorldObj(), transciever.xCoord, transciever.yCoord + 1, transciever.zCoord); + } + public static List getMinecartsAt(World world, int x, int y, int z) { List entities = world.getEntitiesWithinAABB(EntityMinecart.class, AxisAlignedBB.getBoundingBox(x, y, z, x + 1, y + 1, z + 1)); List carts = new ArrayList(); @@ -208,7 +226,7 @@ public class EnderRailController { } } - public void dropNonSpawnedCarts() { + public void dropNonSpawnedCarts() { if(cartsToSpawn.isEmpty()) { return; } @@ -239,7 +257,8 @@ public class EnderRailController { if(world.canPlaceEntityOnSide(b, x, y, z, false, ForgeDirection.UP.ordinal(), entity, null)) { resetForRandomRandomSpawn(entity); if(worldserver.spawnEntityInWorld(entity)) { - entity.onUpdate(); + //entity.onUpdate(); + worldserver.updateEntity(entity); return; } } @@ -250,23 +269,64 @@ public class EnderRailController { } private void resetForRandomRandomSpawn(Entity entity) { - TeleportUtil.breakLinks(transciever.getWorldObj(), entity); + CartLinkUtil.breakLinks(transciever.getWorldObj(), entity); entity.riddenByEntity = null; entity.ridingEntity = null; entity.motionX = 0; - entity.motionY = 0; + entity.motionY = 0; entity.motionZ = 0; -// if(entity instanceof EntityMinecart) { -// entity.posY -= 0.3; -// } - entity.prevPosX = entity.posX; + // if(entity instanceof EntityMinecart) { + // entity.posY -= 0.3; + // } + entity.prevPosX = entity.posX; entity.prevPosY = entity.posY; - entity.prevPosZ = entity.posZ; - entity.rotationYaw = (float) (transciever.getWorldObj().rand.nextDouble() * 360); + entity.prevPosZ = entity.posZ; + entity.rotationYaw = (float) (transciever.getWorldObj().rand.nextDouble() * 360); } private int randOffset(int spread) { return (int) Math.round((transciever.getWorldObj().rand.nextDouble() - 0.5) * spread * 2); } + public void onPlayerTeleported(EntityPlayerMP playerToTP, EntityMinecart toMount) { + if(playerToTP != null) { + playersToRemount.add(new PlayerTpInfo(playerToTP.getCommandSenderName(), toMount.getPersistentID(), 20)); + } + } + + private class PlayerTpInfo { + String playerName; + UUID cartId; + int attemptsRemaining; + + PlayerTpInfo(String playerName, UUID cartId, int attemptsRemaining) { + this.playerName = playerName; + this.cartId = cartId; + this.attemptsRemaining = attemptsRemaining; + } + + boolean attemptMount() { + attemptsRemaining--; + List carts = getMinecartsOnTrack(); + for (EntityMinecart cart : carts) { + if(cart != null && cart.getPersistentID().equals(cartId)) { + EntityPlayer player = transciever.getWorldObj().getPlayerEntityByName(playerName); + if(player != null) { + Vector3d playerPos = EntityUtil.getEntityPosition(player); + Vector3d cartPos = EntityUtil.getEntityPosition(cart); + if(playerPos.distanceSquared(cartPos) <= 9) { + player.posX = cart.posX; + player.posY = cart.posY; + player.posZ = cart.posZ; + player.mountEntity(cart); + return true; + } + } + } + } + return false; + } + + } + } diff --git a/src/main/java/crazypants/enderio/rail/PlayerTeleportHandler.java b/src/main/java/crazypants/enderio/rail/PlayerTeleportHandler.java new file mode 100644 index 00000000..75e01b8f --- /dev/null +++ b/src/main/java/crazypants/enderio/rail/PlayerTeleportHandler.java @@ -0,0 +1,85 @@ +package crazypants.enderio.rail; + +import java.util.ArrayList; +import java.util.List; + +import net.minecraft.entity.item.EntityMinecart; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.util.AxisAlignedBB; +import net.minecraftforge.common.util.ForgeDirection; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.gameevent.TickEvent; +import crazypants.enderio.EnderIO; +import crazypants.enderio.machine.transceiver.TileTransceiver; +import crazypants.util.BlockCoord; +import crazypants.util.EntityUtil; + +public class PlayerTeleportHandler { + + static PlayerTeleportHandler instance = new PlayerTeleportHandler(); + + List queue = new ArrayList(); + + @SubscribeEvent + public void onServerTick(TickEvent.ServerTickEvent event) { + if(event.phase != TickEvent.Phase.END) { + return; + } + for (TeleportAction action : queue) { + action.doTeleport(); + } + queue.clear(); + } + + public void teleportPlayer(TileTransceiver reciever, EntityPlayerMP playerToTP, EntityMinecart playerToMount) { + //Need to quete these as we cant perform the operation during a tile entities update phase as + //we need to remove the player from being tracked + queue.add(new TeleportAction(reciever, playerToTP, playerToMount)); + } + + private static class TeleportAction { + + TileTransceiver reciever; + EntityPlayerMP playerToTP; + EntityMinecart playerToMount; + + private TeleportAction(TileTransceiver reciever, EntityPlayerMP playerToTP, EntityMinecart playerToMount) { + this.reciever = reciever; + this.playerToTP = playerToTP; + this.playerToMount = playerToMount; + } + + void doTeleport() { + int toDim = reciever.getWorldObj().provider.dimensionId; + int meta = reciever.getBlockMetadata(); + + //Make sure player not on the track and is in a safe position + ForgeDirection railDir = EnderIO.blockEnderRail.getDirection(meta); + int xOffset = Math.abs(railDir.offsetX); + int zOffset = Math.abs(railDir.offsetZ); + BlockCoord startPos = new BlockCoord(reciever).getLocation(ForgeDirection.UP); + boolean foundSpot = false; + for (int i = 1; i < 3 && !foundSpot; i++) { + //try each side of the track + playerToTP.setPosition(startPos.x + 0.5 - (xOffset * i), startPos.y, startPos.z + 0.5 - (zOffset * i)); + List collides = EntityUtil.getCollidingBlockGeometry(reciever.getWorldObj(), playerToTP); + foundSpot = collides == null || collides.isEmpty(); + if(!foundSpot) { + playerToTP.setPosition(startPos.x + 0.5 + (xOffset * i), startPos.y, startPos.z + 0.5 + (zOffset * i)); + collides = EntityUtil.getCollidingBlockGeometry(reciever.getWorldObj(), playerToTP); + foundSpot = collides == null || collides.isEmpty(); + } + } + if(!foundSpot) { + //If not space each side will have to spawn on the track + playerToTP.setPosition(startPos.x + 0.5, startPos.y, startPos.z + 0.5); + } +// ChunkCoordinates spawn = new ChunkCoordinates((int) playerToTP.posX, (int) playerToTP.posY, (int) playerToTP.posZ); +// TeleportUtil.teleportPlayer((WorldServer) reciever.getWorldObj(), playerToTP, toDim, spawn); + + reciever.getRailController().onPlayerTeleported(playerToTP, playerToMount); + playerToTP.mcServer.getConfigurationManager().transferPlayerToDimension(playerToTP, toDim, new TeleporterEIO( playerToTP.mcServer.worldServerForDimension(toDim))); + } + + } +} diff --git a/src/main/java/crazypants/enderio/rail/TeleportUtil.java b/src/main/java/crazypants/enderio/rail/TeleportUtil.java index 4663766b..5f649ccd 100644 --- a/src/main/java/crazypants/enderio/rail/TeleportUtil.java +++ b/src/main/java/crazypants/enderio/rail/TeleportUtil.java @@ -1,32 +1,21 @@ package crazypants.enderio.rail; import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; import java.util.List; -import java.util.Random; -import java.util.UUID; -import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint; - -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.EnderIO; +import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint; import crazypants.enderio.config.Config; import crazypants.enderio.machine.transceiver.TileTransceiver; import crazypants.enderio.network.PacketHandler; -import crazypants.vecmath.Vector3d; public class TeleportUtil { @@ -88,185 +77,76 @@ public class TeleportUtil { } - public static void spawn(World world, Entity entity ) { - if(entity != null) { + public static void spawn(World world, Entity entity) { + if(entity != null) { MinecraftServer minecraftserver = MinecraftServer.getServer(); WorldServer worldserver = minecraftserver.worldServerForDimension(world.provider.dimensionId); worldserver.spawnEntityInWorld(entity); - } + } } - - public static void spawnTeleportEffects(World world, Entity entity) { - PacketHandler.INSTANCE.sendToAllAround(new PacketTeleportEffects(entity),new TargetPoint(world.provider.dimensionId, entity.posX, entity.posY, entity.posZ, 64)); + + public static void spawnTeleportEffects(World world, Entity entity) { + PacketHandler.INSTANCE.sendToAllAround(new PacketTeleportEffects(entity), new TargetPoint(world.provider.dimensionId, entity.posX, entity.posY, + entity.posZ, 64)); if(Config.machineSoundsEnabled) { world.playSoundEffect(entity.posX, entity.posY, entity.posZ, "mob.endermen.portal", 0.5F, 0.25F); - } - } + } + } - //------------ Link Utils +// public static void teleportPlayer(WorldServer teleportTo, EntityPlayerMP player, int dimension, ChunkCoordinates spawn) { +// WorldServer originalWorld = (WorldServer) player.worldObj; +// if(player.ridingEntity != null) { +// player.mountEntity(null); +// } +// boolean changeDimension = originalWorld != teleportTo; +// +// player.closeScreen(); +// player.setLocationAndAngles(spawn.posX + 0.5D, spawn.posY, spawn.posZ + 0.5D, player.rotationYaw, player.rotationPitch); +// +// if(changeDimension) { +// player.dimension = dimension; +// player.playerNetServerHandler.sendPacket(new S07PacketRespawn(player.dimension, player.worldObj.difficultySetting, teleportTo.getWorldInfo() +// .getTerrainType(), player.theItemInWorldManager.getGameType())); +// removePlayerFromWorld(originalWorld, player); +// teleportTo.spawnEntityInWorld(player); +// player.setWorld(teleportTo); +// player.mcServer.getConfigurationManager().func_72375_a(player, teleportTo); +// player.theItemInWorldManager.setWorld((WorldServer) teleportTo); +// player.mcServer.getConfigurationManager().updateTimeAndWeatherForPlayer(player, (WorldServer) teleportTo); +// player.mcServer.getConfigurationManager().syncPlayerInventory(player); +// Iterator iter = player.getActivePotionEffects().iterator(); +// while (iter.hasNext()) { +// PotionEffect effect = (PotionEffect) iter.next(); +// player.playerNetServerHandler.sendPacket(new S1DPacketEntityEffect(player.getEntityId(), effect)); +// } +// player.playerNetServerHandler.sendPacket(new S1FPacketSetExperience(player.experience, player.experienceTotal, player.experienceLevel)); +// +// } else { +// teleportTo.theChunkProviderServer.loadChunk(spawn.posX >> 4, spawn.posZ >> 4); +// } +// +// player.playerNetServerHandler.setPlayerLocation(spawn.posX + 0.5D, spawn.posY, spawn.posZ + 0.5D, player.rotationYaw, player.rotationPitch); +// player.motionX = 0; +// player.motionY = 0; +// player.motionZ = 0; +// teleportTo.updateEntityWithOptionalForce(player, false); +// } +// +// private static void removePlayerFromWorld(WorldServer world, EntityPlayerMP player) { +// world.removePlayerEntityDangerously(player); +//// world.getPlayerManager().removePlayer(player); +//// world.playerEntities.remove(player); +//// world.updateAllPlayersSleepingFlag(); +//// int cx = player.chunkCoordX; +//// int cy = player.chunkCoordZ; +//// if((player.addedToChunk) && (world.getChunkProvider().chunkExists(cx, cy))) { +//// world.getChunkFromChunkCoords(cx, cy).removeEntity(player); +//// world.getChunkFromChunkCoords(cx, cy).isModified = true; +//// } +//// world.loadedEntityList.remove(player); +//// world.getEntityTracker().removeEntityFromAllTrackingPlayers(player); +// } - public static void breakLinks(World world, Entity cart) { - if(cart instanceof EntityMinecart) { - breakLinks(world, (EntityMinecart)cart); - } - } - - public static void breakLinks(World world, EntityMinecart cart) { - if(world == null || cart == null) { - return; - } - ILinkageManager linkMan = CartTools.getLinkageManager(world); - if(linkMan == null) { - return; - } - linkMan.breakLinks(cart); - } - - 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/TeleporterEIO.java b/src/main/java/crazypants/enderio/rail/TeleporterEIO.java new file mode 100644 index 00000000..89a04945 --- /dev/null +++ b/src/main/java/crazypants/enderio/rail/TeleporterEIO.java @@ -0,0 +1,36 @@ +package crazypants.enderio.rail; + +import net.minecraft.entity.Entity; +import net.minecraft.world.Teleporter; +import net.minecraft.world.WorldServer; + +public class TeleporterEIO extends Teleporter { + + public TeleporterEIO(WorldServer p_i1963_1_) { + super(p_i1963_1_); + } + + @Override + public boolean makePortal(Entity p_makePortal_1_) { + return true; + } + + @Override + public boolean placeInExistingPortal(Entity p_placeInExistingPortal_1_, double p_placeInExistingPortal_2_, double p_placeInExistingPortal_4_, + double p_placeInExistingPortal_6_, float p_placeInExistingPortal_8_) { + return true; + } + + @Override + public void placeInPortal(Entity entity, double x, double y, double z, float p_77185_8_) { + entity.setLocationAndAngles(x, y, z, entity.rotationPitch, entity.rotationYaw); + entity.motionX = 0; + entity.motionY = 0; + entity.motionZ = 0; + } + + @Override + public void removeStalePortalLocations(long p_removeStalePortalLocations_1_) { + } + +} diff --git a/src/main/java/crazypants/util/EntityUtil.java b/src/main/java/crazypants/util/EntityUtil.java index 02ac8299..024978e2 100644 --- a/src/main/java/crazypants/util/EntityUtil.java +++ b/src/main/java/crazypants/util/EntityUtil.java @@ -5,10 +5,18 @@ import java.util.List; import java.util.Map; import java.util.Set; +import crazypants.vecmath.Vector3d; + +import net.minecraft.block.Block; +import net.minecraft.entity.Entity; import net.minecraft.entity.EntityList; import net.minecraft.entity.EntityLiving; import net.minecraft.entity.boss.IBossDisplayData; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.util.AxisAlignedBB; +import net.minecraft.util.MathHelper; import net.minecraft.util.StatCollector; +import net.minecraft.world.World; public class EntityUtil { @@ -31,5 +39,31 @@ public class EntityUtil { private EntityUtil() { } + + public static Vector3d getEntityPosition(Entity ent) { + return new Vector3d(ent.posX, ent.posY, ent.posZ); + } + + public static List getCollidingBlockGeometry(World world, Entity entity) { + AxisAlignedBB entityBounds = entity.boundingBox; + ArrayList collidingBoundingBoxes = new ArrayList(); + int minX = MathHelper.floor_double(entityBounds.minX); + int minY = MathHelper.floor_double(entityBounds.minY); + int minZ = MathHelper.floor_double(entityBounds.minZ); + int maxX = MathHelper.floor_double(entityBounds.maxX + 1.0D); + int maxY = MathHelper.floor_double(entityBounds.maxY + 1.0D); + int maxZ = MathHelper.floor_double(entityBounds.maxZ + 1.0D); + for (int x = minX; x < maxX; x++) { + for (int z = minZ; z < maxZ; z++) { + for (int y = minY; y < maxY; y++) { + Block block = world.getBlock(x, y, z); + if(block != null) { + block.addCollisionBoxesToList(world, x, y, z, entityBounds, collidingBoundingBoxes, entity); + } + } + } + } + return collidingBoundingBoxes; + } }