Ensured any linked trains 'stuck' during telporting and unlinked and dropped when the rail or transciever are broken.

This commit is contained in:
CrazyPants committed 2014-10-25 23:35:23 +08:00
1 parent d97e055bda
commit 358e1797f2
5 files changed
+141 -21

No files matched your search

@@ -368,9 +368,10 @@ public final class Config {
public static boolean dumpMobNames = false;
public static boolean enderRailEnabled = true;
public static int enderRailPowerRequireCrossDimensions = 10000;
public static int enderRailPowerRequiredBase = 100;
public static int enderRailPowerRequireCrossDimensions = 10000;
public static int enderRailPowerRequiredPerBlock = 10;
public static boolean enderRailCapSameDimensionPowerAtCrossDimensionCost = true;
public static int enderRailTicksBeforeForceSpawningLinkedCarts = 60;
public static void load(FMLPreInitializationEvent event) {
@@ -978,10 +979,12 @@ public final class Config {
enderRailEnabled = config.getBoolean("enderRailEnabled", sectionRailConfig.name, enderRailEnabled, "Wether Ender Rails are enabled");
enderRailPowerRequireCrossDimensions = config.get(sectionRailConfig.name, "enderRailPowerRequireCrossDimensions", enderRailPowerRequireCrossDimensions,
"The amount of power required to transpoer a cart accross dimensions").getInt(enderRailPowerRequireCrossDimensions);
enderRailPowerRequiredBase = config.get(sectionRailConfig.name, "enderRailPowerRequiredBase", enderRailPowerRequiredBase,
"The minimum amout of power required to teleport a cart in the same dimension").getInt(enderRailPowerRequiredBase);
enderRailPowerRequiredPerBlock = config.get(sectionRailConfig.name, "enderRailPowerRequiredPerBlock", enderRailPowerRequiredPerBlock,
"The amout of power required to teleport a cart per block in the same dimension").getInt(enderRailPowerRequiredPerBlock);
enderRailCapSameDimensionPowerAtCrossDimensionCost = config.getBoolean("enderRailCapSameDimensionPowerAtCrossDimensionCost", sectionRailConfig.name, enderRailCapSameDimensionPowerAtCrossDimensionCost,
"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);
dumpMobNames = config.getBoolean("dumpMobNames", sectionMobConfig.name, dumpMobNames,
@@ -44,6 +44,17 @@ public class BlockTransceiver extends AbstractMachineBlock<TileTransceiver> {
super(ModObject.blockTransceiver, TileTransceiver.class);
}
@Override
public boolean removedByPlayer(World world, EntityPlayer player, int x, int y, int z) {
if(!world.isRemote) {
TileEntity te = world.getTileEntity(x, y, z);
if(te instanceof TileTransceiver) {
((TileTransceiver)te).getRailController().dropNonSpawnedCarts();
}
}
return super.removedByPlayer(world, player, x, y, z);
}
@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
TileEntity te = world.getTileEntity(x, y, z);
@@ -11,6 +11,7 @@ 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;
@@ -33,6 +34,7 @@ import crazypants.enderio.Log;
import crazypants.enderio.ModObject;
import crazypants.enderio.conduit.ConduitUtil;
import crazypants.enderio.config.Config;
import crazypants.enderio.machine.AbstractMachineEntity;
import crazypants.enderio.machine.transceiver.Channel;
import crazypants.enderio.machine.transceiver.ChannelType;
import crazypants.enderio.machine.transceiver.ServerChannelRegister;
@@ -119,6 +121,18 @@ public class BlockEnderRail extends BlockRail {
// }
return false;
}
@Override
public boolean removedByPlayer(World world, EntityPlayer player, int x, int y, int z, boolean willHarvest) {
if(!world.isRemote) {
TileEntity te = world.getTileEntity(x, y - 1, z);
System.out.println("BlockEnderRail.removedByPlayer: " + te);
if(te instanceof TileTransceiver) {
((TileTransceiver)te).getRailController().dropNonSpawnedCarts();
}
}
return super.removedByPlayer(world, player, x, y, z, willHarvest);
}
public int getBasicRailMetadata(IBlockAccess world, EntityMinecart cart, int x, int y, int z) {
//Ignore turning bit, used for receive direction
@@ -227,12 +241,14 @@ public class BlockEnderRail extends BlockRail {
}
private int getPowerRequiredForSingleCart(TileTransceiver sender, TileTransceiver reciever) {
int powerRequired;
int powerRequired = 0;
if(sender.getWorldObj().provider.dimensionId != reciever.getWorldObj().provider.dimensionId) {
powerRequired = Config.enderRailPowerRequireCrossDimensions;
} else {
powerRequired = Config.enderRailPowerRequiredBase;
} else {
powerRequired += sender.getLocation().distance(reciever.getLocation()) * Config.enderRailPowerRequiredPerBlock;
if(Config.enderRailCapSameDimensionPowerAtCrossDimensionCost) {
powerRequired = Math.min(powerRequired, Config.enderRailPowerRequireCrossDimensions);
}
}
return powerRequired;
}
@@ -8,15 +8,21 @@ import java.util.List;
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.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.util.ForgeDirection;
import crazypants.enderio.config.Config;
import crazypants.enderio.machine.transceiver.TileTransceiver;
import crazypants.util.BlockCoord;
import crazypants.util.ForgeDirectionOffsets;
@@ -50,11 +56,11 @@ public class EnderRailController {
List<EntityMinecart> carts = getMinecartsAt(transciever.getWorldObj(), transciever.xCoord, transciever.yCoord + 1, transciever.zCoord);
List<UUID> toRemove = new ArrayList<UUID>();
//any cart in the newly spawned list no longer on the track needs to be removed
for (UUID recievedCartUID : newlySpawnedCarts) {
EntityMinecart minecart = getMinecartWthUUID(recievedCartUID, carts);
if(minecart == null) {
if(minecart == null) {
toRemove.add(recievedCartUID);
}
}
@@ -78,19 +84,25 @@ public class EnderRailController {
ticksFailedToSpawn = 0;
return;
}
boolean failedSpawn = false;
if(!isClear()) {
ticksFailedToSpawn++;
if(ticksFailedToSpawn < 60) {
if(ticksFailedToSpawn < Config.enderRailTicksBeforeForceSpawningLinkedCarts) {
return;
}
failedSpawn = true;
}
List<Entity> spawnThisTick = cartsToSpawn.removeFirst();
for (Entity ent : spawnThisTick) {
TeleportUtil.spawn(transciever.getWorldObj(), ent);
if(ent instanceof EntityMinecart) {
EntityMinecart cart = (EntityMinecart) ent;
newlySpawnedCarts.add(cart.getPersistentID());
TeleportUtil.recreateLinks(cart);
if(failedSpawn) {
doRandomSpawn(ent);
} else {
TeleportUtil.spawn(transciever.getWorldObj(), ent);
if(ent instanceof EntityMinecart) {
EntityMinecart cart = (EntityMinecart) ent;
newlySpawnedCarts.add(cart.getPersistentID());
TeleportUtil.recreateLinks(cart);
}
}
}
ticksFailedToSpawn = 0;
@@ -101,7 +113,7 @@ public class EnderRailController {
cartsToSpawn.addAll(toTeleport);
}
public boolean isRecievedCart(EntityMinecart mc) {
public boolean isRecievedCart(EntityMinecart mc) {
return newlySpawnedCarts.contains(mc.getPersistentID());
}
@@ -164,7 +176,7 @@ public class EnderRailController {
String uuisStr = spawnedCartList.getStringTagAt(i);
newlySpawnedCarts.add(UUID.fromString(uuisStr));
}
}
}
}
public void writeToNBT(NBTTagCompound root) {
@@ -196,4 +208,65 @@ public class EnderRailController {
}
}
public void dropNonSpawnedCarts() {
if(cartsToSpawn.isEmpty()) {
return;
}
for (List<Entity> entList : cartsToSpawn) {
for (Entity entity : entList) {
doRandomSpawn(entity);
}
}
cartsToSpawn.clear();
}
private void doRandomSpawn(Entity entity) {
if(entity == null) {
return;
}
double oX = entity.posX;
double oZ = entity.posZ;
World world = transciever.getWorldObj();
MinecraftServer minecraftserver = MinecraftServer.getServer();
WorldServer worldserver = minecraftserver.worldServerForDimension(world.provider.dimensionId);
for (int i = 0; i < 4; i++) {
int x = transciever.xCoord + randOffset(2);
int y = transciever.yCoord + 1;
int z = transciever.zCoord + randOffset(2);
Block b = world.getBlock(x, y, z);
entity.setPosition(x + 0.5, entity.posY, z + 0.5);
if(world.canPlaceEntityOnSide(b, x, y, z, false, ForgeDirection.UP.ordinal(), entity, null)) {
resetForRandomRandomSpawn(entity);
if(worldserver.spawnEntityInWorld(entity)) {
entity.onUpdate();
return;
}
}
}
entity.setPosition(oX, entity.posY, oZ);
resetForRandomRandomSpawn(entity);
worldserver.spawnEntityInWorld(entity);
}
private void resetForRandomRandomSpawn(Entity entity) {
TeleportUtil.breakLinks(transciever.getWorldObj(), entity);
entity.riddenByEntity = null;
entity.ridingEntity = null;
entity.motionX = 0;
entity.motionY = 0;
entity.motionZ = 0;
// 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);
}
private int randOffset(int spread) {
return (int) Math.round((transciever.getWorldObj().rand.nextDouble() - 0.5) * spread * 2);
}
}
@@ -82,16 +82,33 @@ public class TeleportUtil {
}
public static void spawn(World world, Entity entity ) {
MinecraftServer minecraftserver = MinecraftServer.getServer();
WorldServer worldserver = minecraftserver.worldServerForDimension(world.provider.dimensionId);
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);
}
}
//------------ Link Utils
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;
@@ -99,7 +116,7 @@ public class TeleportUtil {
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)) {