Search in sources :

Example 1 with Dimension

use of org.spongepowered.api.world.Dimension in project SpongeCommon by SpongePowered.

the class EntityUtil method getPlayerRespawnLocation.

// Internal to MixinPlayerList. has side effects
public static Location<World> getPlayerRespawnLocation(EntityPlayerMP playerIn, @Nullable WorldServer targetWorld) {
    final Location<World> location = ((World) playerIn.world).getSpawnLocation();
    tempIsBedSpawn = false;
    if (targetWorld == null) {
        // Target world doesn't exist? Use global
        return location;
    }
    final Dimension targetDimension = (Dimension) targetWorld.provider;
    int targetDimensionId = ((IMixinWorldServer) targetWorld).getDimensionId();
    // that world. (Usually overworld unless a mod says otherwise).
    if (!targetDimension.allowsPlayerRespawns()) {
        targetDimensionId = SpongeImplHooks.getRespawnDimension((WorldProvider) targetDimension, playerIn);
        targetWorld = targetWorld.getMinecraftServer().getWorld(targetDimensionId);
    }
    Vector3d targetSpawnVec = VecHelper.toVector3d(targetWorld.getSpawnPoint());
    BlockPos bedPos = SpongeImplHooks.getBedLocation(playerIn, targetDimensionId);
    if (bedPos != null) {
        // Player has a bed
        boolean forceBedSpawn = SpongeImplHooks.isSpawnForced(playerIn, targetDimensionId);
        BlockPos bedSpawnLoc = EntityPlayer.getBedSpawnLocation(targetWorld, bedPos, forceBedSpawn);
        if (bedSpawnLoc != null) {
            // The bed exists and is not obstructed
            tempIsBedSpawn = true;
            targetSpawnVec = new Vector3d(bedSpawnLoc.getX() + 0.5D, bedSpawnLoc.getY() + 0.1D, bedSpawnLoc.getZ() + 0.5D);
        } else {
            // Bed invalid
            playerIn.connection.sendPacket(new SPacketChangeGameState(0, 0.0F));
            // Vanilla behaviour - Delete the known bed location if invalid
            // null = remove location
            bedPos = null;
        }
        // Set the new bed location for the new dimension
        // Temporarily for setSpawnPoint
        int prevDim = playerIn.dimension;
        playerIn.dimension = targetDimensionId;
        playerIn.setSpawnPoint(bedPos, forceBedSpawn);
        playerIn.dimension = prevDim;
    }
    return new Location<>((World) targetWorld, targetSpawnVec);
}
Also used : Vector3d(com.flowpowered.math.vector.Vector3d) WorldProvider(net.minecraft.world.WorldProvider) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) BlockPos(net.minecraft.util.math.BlockPos) Dimension(org.spongepowered.api.world.Dimension) World(org.spongepowered.api.world.World) SPacketChangeGameState(net.minecraft.network.play.server.SPacketChangeGameState) Location(org.spongepowered.api.world.Location) ResourceLocation(net.minecraft.util.ResourceLocation)

Aggregations

Vector3d (com.flowpowered.math.vector.Vector3d)1 SPacketChangeGameState (net.minecraft.network.play.server.SPacketChangeGameState)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 BlockPos (net.minecraft.util.math.BlockPos)1 WorldProvider (net.minecraft.world.WorldProvider)1 Dimension (org.spongepowered.api.world.Dimension)1 Location (org.spongepowered.api.world.Location)1 World (org.spongepowered.api.world.World)1 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)1