use of crazypants.enderio.api.teleport.TeleportEntityEvent in project EnderIO by SleepyTrousers.
the class TeleportUtil method serverTeleport.
public static boolean serverTeleport(@Nonnull Entity entity, @Nonnull BlockPos pos, int targetDim, boolean conserveMotion, @Nonnull TravelSource source) {
TeleportEntityEvent evt = new TeleportEntityEvent(entity, source, pos, targetDim);
if (MinecraftForge.EVENT_BUS.post(evt)) {
return false;
}
EntityPlayerMP player = null;
if (entity instanceof EntityPlayerMP) {
player = (EntityPlayerMP) entity;
}
int x = pos.getX();
int y = pos.getY();
int z = pos.getZ();
int from = entity.dimension;
if (from != targetDim) {
MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
WorldServer fromDim = server.getWorld(from);
WorldServer toDim = server.getWorld(targetDim);
Teleporter teleporter = new TeleporterEIO(toDim);
// play sound at the dimension we are leaving for others to hear
SoundHelper.playSound(server.getWorld(entity.dimension), entity, source.sound, 1.0F, 1.0F);
if (player != null) {
server.getPlayerList().transferPlayerToDimension(player, targetDim, teleporter);
if (from == 1 && entity.isEntityAlive()) {
// get around vanilla End
// hacks
toDim.spawnEntity(entity);
toDim.updateEntityWithOptionalForce(entity, false);
}
} else {
NBTTagCompound tagCompound = new NBTTagCompound();
float rotationYaw = entity.rotationYaw;
float rotationPitch = entity.rotationPitch;
entity.writeToNBT(tagCompound);
Class<? extends Entity> entityClass = entity.getClass();
fromDim.removeEntity(entity);
try {
Entity newEntity = entityClass.getConstructor(World.class).newInstance(toDim);
newEntity.readFromNBT(tagCompound);
newEntity.setLocationAndAngles(x, y, z, rotationYaw, rotationPitch);
newEntity.forceSpawn = true;
toDim.spawnEntity(newEntity);
// necessary?
newEntity.forceSpawn = false;
} catch (Exception e) {
// Throwables.propagate(e);
Log.error("serverTeleport: Error creating a entity to be created in new dimension.");
return false;
}
}
}
// Force the chunk to load
if (!entity.world.isBlockLoaded(pos)) {
entity.world.getChunkFromBlockCoords(pos);
}
if (player != null) {
player.connection.setPlayerLocation(x + 0.5, y + 1.1, z + 0.5, player.rotationYaw, player.rotationPitch);
} else {
entity.setPositionAndUpdate(x + 0.5, y + 1.1, z + 0.5);
}
entity.fallDistance = 0;
SoundHelper.playSound(entity.world, entity, source.sound, 1.0F, 1.0F);
if (player != null) {
if (conserveMotion) {
Vector3d velocityVex = Util.getLookVecEio(player);
SPacketEntityVelocity p = new SPacketEntityVelocity(entity.getEntityId(), velocityVex.x, velocityVex.y, velocityVex.z);
player.connection.sendPacket(p);
}
}
return true;
}
use of crazypants.enderio.api.teleport.TeleportEntityEvent in project EnderIO by SleepyTrousers.
the class TravelController method doClientTeleport.
// Note: This is restricted to the current player
public boolean doClientTeleport(@Nonnull Entity entity, @Nonnull EnumHand hand, @Nonnull BlockPos bc, @Nonnull TravelSource source, int powerUse, boolean conserveMomentum) {
TeleportEntityEvent evt = new TeleportEntityEvent(entity, source, bc, entity.dimension);
if (MinecraftForge.EVENT_BUS.post(evt)) {
return false;
}
PacketTravelEvent p = new PacketTravelEvent(evt.getTarget(), powerUse, conserveMomentum, source, hand);
PacketHandler.INSTANCE.sendToServer(p);
return true;
}
Aggregations