From 6c7a0e4207dbe6a804103a1fbbd38ac26d9a0525 Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 19 Feb 2018 02:29:57 +0100 Subject: [PATCH] Refactored entity movement --- src/main/java/cr0s/warpdrive/Commons.java | 39 ++++++++++++++++++- .../cr0s/warpdrive/event/JumpSequencer.java | 31 ++------------- .../cr0s/warpdrive/event/LivingHandler.java | 9 +---- 3 files changed, 44 insertions(+), 35 deletions(-) diff --git a/src/main/java/cr0s/warpdrive/Commons.java b/src/main/java/cr0s/warpdrive/Commons.java index 57e7ebb7..80abb9e8 100644 --- a/src/main/java/cr0s/warpdrive/Commons.java +++ b/src/main/java/cr0s/warpdrive/Commons.java @@ -1,11 +1,14 @@ package cr0s.warpdrive; import cr0s.warpdrive.config.WarpDriveConfig; +import cr0s.warpdrive.data.Vector3; import cr0s.warpdrive.data.VectorI; +import cr0s.warpdrive.world.SpaceTeleporter; import net.minecraft.block.Block; import net.minecraft.command.ICommandSender; import net.minecraft.command.PlayerSelector; +import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; @@ -17,7 +20,9 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntityChest; import net.minecraft.util.ChatComponentText; +import net.minecraft.util.MathHelper; import net.minecraft.world.World; +import net.minecraft.world.WorldServer; import java.io.File; import java.io.FileInputStream; @@ -398,7 +403,7 @@ public class Commons { return yValues[yValues.length - 1]; } - private static double interpolate(final double xMin, final double yMin, final double xMax, final double yMax, final double x) { + public static double interpolate(final double xMin, final double yMin, final double xMax, final double yMax, final double x) { return yMin + (x - xMin) * (yMax - yMin) / (xMax - xMin); } @@ -561,4 +566,36 @@ public class Commons { NEI_hideItemStack(itemStack); } } + + public static void moveEntity(final Entity entity, final World worldDestination, final Vector3 v3Destination) { + // change to another dimension if needed + if (worldDestination != entity.worldObj) { + final World worldSource = entity.worldObj; + final MinecraftServer server = MinecraftServer.getServer(); + final WorldServer from = server.worldServerForDimension(worldSource.provider.dimensionId); + final WorldServer to = server.worldServerForDimension(worldDestination.provider.dimensionId); + final SpaceTeleporter teleporter = new SpaceTeleporter(to, 0, + MathHelper.floor_double(v3Destination.x), + MathHelper.floor_double(v3Destination.y), + MathHelper.floor_double(v3Destination.z)); + + if (entity instanceof EntityPlayerMP) { + final EntityPlayerMP player = (EntityPlayerMP) entity; + server.getConfigurationManager().transferPlayerToDimension(player, worldDestination.provider.dimensionId, teleporter); + player.sendPlayerAbilities(); + } else { + server.getConfigurationManager().transferEntityToWorld(entity, worldSource.provider.dimensionId, from, to, teleporter); + } + } + + // update position + if (entity instanceof EntityPlayerMP) { + final EntityPlayerMP player = (EntityPlayerMP) entity; + player.setPositionAndUpdate(v3Destination.x, v3Destination.y, v3Destination.z); + } else { + // @TODO: force client refresh of non-player entities + entity.setPosition(v3Destination.x, v3Destination.y, v3Destination.z); + } + } + } diff --git a/src/main/java/cr0s/warpdrive/event/JumpSequencer.java b/src/main/java/cr0s/warpdrive/event/JumpSequencer.java index b111a9a0..813d0538 100644 --- a/src/main/java/cr0s/warpdrive/event/JumpSequencer.java +++ b/src/main/java/cr0s/warpdrive/event/JumpSequencer.java @@ -19,7 +19,6 @@ import cr0s.warpdrive.data.Transformation; import cr0s.warpdrive.data.Vector3; import cr0s.warpdrive.data.VectorI; import cr0s.warpdrive.network.PacketHandler; -import cr0s.warpdrive.world.SpaceTeleporter; import java.io.File; import java.text.SimpleDateFormat; @@ -42,11 +41,9 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.AxisAlignedBB; import net.minecraft.util.ChunkCoordinates; -import net.minecraft.util.MathHelper; import net.minecraft.util.Vec3; import net.minecraft.world.ChunkCoordIntPair; import net.minecraft.world.World; -import net.minecraft.world.WorldServer; import net.minecraftforge.common.ForgeChunkManager; import net.minecraftforge.common.ForgeChunkManager.Ticket; @@ -1100,29 +1097,12 @@ public class JumpSequencer extends AbstractSequencer { newEntityX, newEntityY, newEntityZ, entity.toString())); } - // Travel to another dimension if needed - if (betweenWorlds) { - MinecraftServer server = MinecraftServer.getServer(); - WorldServer from = server.worldServerForDimension(sourceWorld.provider.dimensionId); - WorldServer to = server.worldServerForDimension(targetWorld.provider.dimensionId); - SpaceTeleporter teleporter = new SpaceTeleporter(to, 0, - MathHelper.floor_double(newEntityX), - MathHelper.floor_double(newEntityY), - MathHelper.floor_double(newEntityZ)); - - if (entity instanceof EntityPlayerMP) { - EntityPlayerMP player = (EntityPlayerMP) entity; - server.getConfigurationManager().transferPlayerToDimension(player, targetWorld.provider.dimensionId, teleporter); - player.sendPlayerAbilities(); - } else { - server.getConfigurationManager().transferEntityToWorld(entity, sourceWorld.provider.dimensionId, from, to, teleporter); - } - } - - // Update position transformation.rotate(entity); + Commons.moveEntity(entity, targetWorld, new Vector3(newEntityX, newEntityY, newEntityZ)); + + // Update bed position if (entity instanceof EntityPlayerMP) { - EntityPlayerMP player = (EntityPlayerMP) entity; + final EntityPlayerMP player = (EntityPlayerMP) entity; ChunkCoordinates bedLocation = player.getBedLocation(sourceWorld.provider.dimensionId); @@ -1133,9 +1113,6 @@ public class JumpSequencer extends AbstractSequencer { bedLocation = transformation.apply(bedLocation); player.setSpawnChunk(bedLocation, false, targetWorld.provider.dimensionId); } - player.setPositionAndUpdate(newEntityX, newEntityY, newEntityZ); - } else { - entity.setPosition(newEntityX, newEntityY, newEntityZ); } } } diff --git a/src/main/java/cr0s/warpdrive/event/LivingHandler.java b/src/main/java/cr0s/warpdrive/event/LivingHandler.java index 0b63ae84..be4ce690 100644 --- a/src/main/java/cr0s/warpdrive/event/LivingHandler.java +++ b/src/main/java/cr0s/warpdrive/event/LivingHandler.java @@ -9,6 +9,7 @@ import cr0s.warpdrive.config.Dictionary; import cr0s.warpdrive.config.WarpDriveConfig; import cr0s.warpdrive.data.CelestialObject; import cr0s.warpdrive.data.StateAir; +import cr0s.warpdrive.data.Vector3; import cr0s.warpdrive.data.VectorI; import cr0s.warpdrive.world.SpaceTeleporter; @@ -102,13 +103,7 @@ public class LivingHandler { final double newEntityY = entityLivingBase.posY + 0.1D; final double newEntityZ = celestialObject.dimensionCenterX + Math.signum(relativeZ) * newAbsoluteZ; // entity.isAirBorne = true; - // @TODO: force client refresh of non-player entities - if (entityLivingBase instanceof EntityPlayerMP) { - EntityPlayerMP player = (EntityPlayerMP) entityLivingBase; - player.setPositionAndUpdate(newEntityX, newEntityY, newEntityZ); - } else { - entityLivingBase.setPosition(newEntityX, newEntityY, newEntityZ); - } + Commons.moveEntity(entityLivingBase, entityLivingBase.worldObj, new Vector3(newEntityX, newEntityY, newEntityZ)); // spam chat if it's a player if (entityLivingBase instanceof EntityPlayer && !entityLivingBase.isDead && entityLivingBase.deathTime <= 0) {