Search in sources :

Example 6 with ITeleporter

use of net.minecraftforge.common.util.ITeleporter in project Mekanism by mekanism.

the class TileEntityTeleporter method teleportEntityTo.

@Nullable
public static Entity teleportEntityTo(Entity entity, World targetWorld, BlockPos target) {
    if (entity.getCommandSenderWorld().dimension() == targetWorld.dimension()) {
        entity.teleportTo(target.getX() + 0.5, target.getY(), target.getZ() + 0.5);
        if (!entity.getPassengers().isEmpty()) {
            // Force re-apply any passengers so that players don't get "stuck" outside what they may be riding
            ((ServerChunkProvider) entity.getCommandSenderWorld().getChunkSource()).broadcast(entity, new SSetPassengersPacket(entity));
            Entity controller = entity.getControllingPassenger();
            if (controller != entity && controller instanceof ServerPlayerEntity && !(controller instanceof FakePlayer)) {
                ServerPlayerEntity player = (ServerPlayerEntity) controller;
                if (player.connection != null) {
                    // Force sync the fact that the vehicle moved to the client that is controlling it
                    // so that it makes sure to use the correct positions when sending move packets
                    // back to the server instead of running into moved wrongly issues
                    player.connection.send(new SMoveVehiclePacket(entity));
                }
            }
        }
        return entity;
    }
    Vector3d destination = new Vector3d(target.getX() + 0.5, target.getY(), target.getZ() + 0.5);
    // Note: We grab the passengers here instead of in placeEntity as changeDimension starts by removing any passengers
    List<Entity> passengers = entity.getPassengers();
    return entity.changeDimension((ServerWorld) targetWorld, new ITeleporter() {

        @Override
        public Entity placeEntity(Entity entity, ServerWorld currentWorld, ServerWorld destWorld, float yaw, Function<Boolean, Entity> repositionEntity) {
            Entity repositionedEntity = repositionEntity.apply(false);
            if (repositionedEntity != null) {
                // Teleport all passengers to the other dimension and then make them start riding the entity again
                for (Entity passenger : passengers) {
                    teleportPassenger(destWorld, destination, repositionedEntity, passenger);
                }
            }
            return repositionedEntity;
        }

        @Override
        public PortalInfo getPortalInfo(Entity entity, ServerWorld destWorld, Function<ServerWorld, PortalInfo> defaultPortalInfo) {
            return new PortalInfo(destination, entity.getDeltaMovement(), entity.yRot, entity.xRot);
        }

        @Override
        public boolean playTeleportSound(ServerPlayerEntity player, ServerWorld sourceWorld, ServerWorld destWorld) {
            return false;
        }
    });
}
Also used : ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) Entity(net.minecraft.entity.Entity) PartEntity(net.minecraftforge.entity.PartEntity) SSetPassengersPacket(net.minecraft.network.play.server.SSetPassengersPacket) PortalInfo(net.minecraft.block.PortalInfo) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) SMoveVehiclePacket(net.minecraft.network.play.server.SMoveVehiclePacket) FakePlayer(net.minecraftforge.common.util.FakePlayer) ServerWorld(net.minecraft.world.server.ServerWorld) Vector3d(net.minecraft.util.math.vector.Vector3d) ITeleporter(net.minecraftforge.common.util.ITeleporter) ServerChunkProvider(net.minecraft.world.server.ServerChunkProvider) Nullable(javax.annotation.Nullable)

Aggregations

Entity (net.minecraft.entity.Entity)6 ITeleporter (net.minecraftforge.common.util.ITeleporter)6 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)4 ServerWorld (net.minecraft.world.server.ServerWorld)4 PortalInfo (net.minecraft.block.PortalInfo)3 Nullable (javax.annotation.Nullable)2 PlayerEntity (net.minecraft.entity.player.PlayerEntity)2 Vector3d (net.minecraft.util.math.vector.Vector3d)2 World (net.minecraft.world.World)2 PartEntity (net.minecraftforge.entity.PartEntity)2 Vector3d (com.enderio.core.common.vecmath.Vector3d)1 ServerPlayNetHandlerBridge (io.izzel.arclight.common.bridge.network.play.ServerPlayNetHandlerBridge)1 TeleporterBridge (io.izzel.arclight.common.bridge.world.TeleporterBridge)1 ServerWorldBridge (io.izzel.arclight.common.bridge.world.server.ServerWorldBridge)1 List (java.util.List)1 BlockPattern (net.minecraft.block.pattern.BlockPattern)1 CreatureEntity (net.minecraft.entity.CreatureEntity)1 MobEntity (net.minecraft.entity.MobEntity)1 ItemEntity (net.minecraft.entity.item.ItemEntity)1 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)1