Search in sources :

Example 1 with PortalInfo

use of net.minecraft.block.PortalInfo in project Mekanism by mekanism.

the class TileEntityTeleporter method teleportPassenger.

private static void teleportPassenger(ServerWorld destWorld, Vector3d destination, Entity repositionedEntity, Entity passenger) {
    if (!passenger.canChangeDimensions()) {
        // If the passenger can't change dimensions just let it peacefully stay after dismounting rather than trying to teleport it
        return;
    }
    // Note: We grab the passengers here instead of in placeEntity as changeDimension starts by removing any passengers
    List<Entity> passengers = passenger.getPassengers();
    passenger.changeDimension(destWorld, new ITeleporter() {

        @Override
        public Entity placeEntity(Entity entity, ServerWorld currentWorld, ServerWorld destWorld, float yaw, Function<Boolean, Entity> repositionEntity) {
            boolean invulnerable = entity.isInvulnerable();
            // Make the entity invulnerable so that when we teleport it, it doesn't take damage
            // we revert this state to the previous state after teleporting
            entity.setInvulnerable(true);
            Entity repositionedPassenger = repositionEntity.apply(false);
            if (repositionedPassenger != null) {
                // Force our passenger to start riding the new entity again
                repositionedPassenger.startRiding(repositionedEntity, true);
                // Teleport "nested" passengers
                for (Entity passenger : passengers) {
                    teleportPassenger(destWorld, destination, repositionedPassenger, passenger);
                }
                repositionedPassenger.setInvulnerable(invulnerable);
            }
            entity.setInvulnerable(invulnerable);
            return repositionedPassenger;
        }

        @Override
        public PortalInfo getPortalInfo(Entity entity, ServerWorld destWorld, Function<ServerWorld, PortalInfo> defaultPortalInfo) {
            // This is needed to ensure the passenger starts getting tracked after teleporting
            return new PortalInfo(destination, entity.getDeltaMovement(), entity.yRot, entity.xRot);
        }

        @Override
        public boolean playTeleportSound(ServerPlayerEntity player, ServerWorld sourceWorld, ServerWorld destWorld) {
            return false;
        }
    });
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) Entity(net.minecraft.entity.Entity) PartEntity(net.minecraftforge.entity.PartEntity) ITeleporter(net.minecraftforge.common.util.ITeleporter) PortalInfo(net.minecraft.block.PortalInfo) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity)

Example 2 with PortalInfo

use of net.minecraft.block.PortalInfo in project ChaosAwakens by ChaosAwakens.

the class HeightmapTeleporter method getPortalInfo.

@Nullable
public PortalInfo getPortalInfo(Entity entity, ServerWorld targetWorld, Function<ServerWorld, PortalInfo> defaultPortalInfo) {
    // ChaosAwakens.debug("TELEPORTER", entity+" "+targetWorld);
    WorldBorder border = targetWorld.getWorldBorder();
    double coordDiff = DimensionType.getTeleportationScale(entity.level.dimensionType(), targetWorld.dimensionType());
    // You know how walking a block on the nether equals to X in the overworld, this
    // is checking for it
    Vector3d newPosVector = new Vector3d(MathHelper.clamp(entity.getX() * coordDiff * 1.0, Math.max(-2.9999872E7D, border.getMinX() + 16.0D), Math.min(2.9999872E7D, border.getMaxX() - 16.0D)), entity.getY(), MathHelper.clamp(entity.getZ() * coordDiff * 1.0, Math.max(-2.9999872E7D, border.getMinZ() + 16.0D), Math.min(2.9999872E7D, border.getMaxZ() - 16.0D)));
    // Load target chunk
    targetWorld.getChunk(new BlockPos(newPosVector));
    entity.forcedLoading = true;
    // Get a valid Y pos for the targeted block
    BlockPos targetPos;
    int tries = 0;
    do {
        targetPos = new BlockPos(newPosVector.x(), targetWorld.getHeight(Heightmap.Type.WORLD_SURFACE, (int) newPosVector.x(), (int) newPosVector.z()), newPosVector.z());
        tries++;
    } while (targetPos.getY() <= 0 && tries < 30);
    return new PortalInfo(new Vector3d(newPosVector.x(), targetPos.getY(), newPosVector.z()), new Vector3d(0, 0, 0), entity.yRot, entity.xRot);
}
Also used : Vector3d(net.minecraft.util.math.vector.Vector3d) WorldBorder(net.minecraft.world.border.WorldBorder) PortalInfo(net.minecraft.block.PortalInfo) BlockPos(net.minecraft.util.math.BlockPos) Nullable(javax.annotation.Nullable)

Example 3 with PortalInfo

use of net.minecraft.block.PortalInfo in project Mekanism by mekanism.

the class EntityRobit method goHome.

public void goHome() {
    if (level.isClientSide()) {
        return;
    }
    setFollowing(false);
    if (level.dimension() == homeLocation.dimension) {
        setDeltaMovement(0, 0, 0);
        teleportTo(homeLocation.getX() + 0.5, homeLocation.getY() + 0.3, homeLocation.getZ() + 0.5);
    } else {
        ServerWorld newWorld = ((ServerWorld) this.level).getServer().getLevel(homeLocation.dimension);
        if (newWorld != null) {
            Vector3d destination = new Vector3d(homeLocation.getX() + 0.5, homeLocation.getY() + 0.3, homeLocation.getZ() + 0.5);
            changeDimension(newWorld, new ITeleporter() {

                @Override
                public Entity placeEntity(Entity entity, ServerWorld currentWorld, ServerWorld destWorld, float yaw, Function<Boolean, Entity> repositionEntity) {
                    return repositionEntity.apply(false);
                }

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

                @Override
                public boolean playTeleportSound(ServerPlayerEntity player, ServerWorld sourceWorld, ServerWorld destWorld) {
                    return false;
                }
            });
        }
    }
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) CreatureEntity(net.minecraft.entity.CreatureEntity) MobEntity(net.minecraft.entity.MobEntity) Entity(net.minecraft.entity.Entity) ItemEntity(net.minecraft.entity.item.ItemEntity) Vector3d(net.minecraft.util.math.vector.Vector3d) ITeleporter(net.minecraftforge.common.util.ITeleporter) PortalInfo(net.minecraft.block.PortalInfo) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity)

Example 4 with PortalInfo

use of net.minecraft.block.PortalInfo 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

PortalInfo (net.minecraft.block.PortalInfo)4 Entity (net.minecraft.entity.Entity)3 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)3 Vector3d (net.minecraft.util.math.vector.Vector3d)3 ServerWorld (net.minecraft.world.server.ServerWorld)3 ITeleporter (net.minecraftforge.common.util.ITeleporter)3 Nullable (javax.annotation.Nullable)2 PartEntity (net.minecraftforge.entity.PartEntity)2 CreatureEntity (net.minecraft.entity.CreatureEntity)1 MobEntity (net.minecraft.entity.MobEntity)1 ItemEntity (net.minecraft.entity.item.ItemEntity)1 PlayerEntity (net.minecraft.entity.player.PlayerEntity)1 SMoveVehiclePacket (net.minecraft.network.play.server.SMoveVehiclePacket)1 SSetPassengersPacket (net.minecraft.network.play.server.SSetPassengersPacket)1 BlockPos (net.minecraft.util.math.BlockPos)1 WorldBorder (net.minecraft.world.border.WorldBorder)1 ServerChunkProvider (net.minecraft.world.server.ServerChunkProvider)1 FakePlayer (net.minecraftforge.common.util.FakePlayer)1