Search in sources :

Example 1 with PlayerList

use of net.minecraft.server.management.PlayerList in project VanillaTeleporter by dyeo.

the class TeleporterUtility method transferPlayerToDimension.

/**
	 * Transfers a player to a different dimension and location, as if they were being teleported by a dimension portal
	 */
private static boolean transferPlayerToDimension(EntityPlayerMP player, double posX, double posY, double posZ, float yaw, float pitch, int dimension) {
    MinecraftServer minecraftServer = FMLCommonHandler.instance().getMinecraftServerInstance();
    WorldServer srcWorld = minecraftServer.worldServerForDimension(player.dimension);
    WorldServer dstWorld = minecraftServer.worldServerForDimension(dimension);
    if (dstWorld != null) {
        // fire player change dimension event and check that action is valid before continuing
        if (!net.minecraftforge.common.ForgeHooks.onTravelToDimension(player, dimension))
            return false;
        // (hard) set the player's dimension to the destination dimension
        player.dimension = dimension;
        // send a player respawn packet to the destination dimension so the player respawns there
        player.connection.sendPacket(new SPacketRespawn(player.dimension, player.world.getDifficulty(), player.world.getWorldInfo().getTerrainType(), player.interactionManager.getGameType()));
        // remove the original player entity
        srcWorld.removeEntity(player);
        // make sure the player isn't dead (removeEntity sets player as dead)
        player.isDead = false;
        PlayerList playerList = player.mcServer.getPlayerList();
        // set player's location (net server handler)
        setPlayerPosition(player, posX, posY, posZ, yaw, pitch);
        // spawn the player in the new world
        dstWorld.spawnEntity(player);
        // update the entity (do not force)
        dstWorld.updateEntityWithOptionalForce(player, false);
        // set the player's world to the new world
        player.setWorld(dstWorld);
        // add the player into the appropriate player list
        playerList.preparePlayer(player, srcWorld);
        // set item in world manager's world to the same as the player
        player.interactionManager.setWorld(dstWorld);
        // update time and weather for the player so that it's the same as the world
        playerList.updateTimeAndWeatherForPlayer(player, dstWorld);
        // sync the player's inventory
        playerList.syncPlayerInventory(player);
        // add no experience (syncs experience)
        player.addExperience(0);
        // update player's health
        player.setPlayerHealthUpdated();
        // fire the dimension changed event so that minecraft swithces dimensions properly
        FMLCommonHandler.instance().firePlayerChangedDimensionEvent(player, srcWorld.provider.getDimension(), dstWorld.provider.getDimension());
        return true;
    } else {
        return false;
    }
}
Also used : PlayerList(net.minecraft.server.management.PlayerList) WorldServer(net.minecraft.world.WorldServer) SPacketRespawn(net.minecraft.network.play.server.SPacketRespawn) MinecraftServer(net.minecraft.server.MinecraftServer)

Aggregations

SPacketRespawn (net.minecraft.network.play.server.SPacketRespawn)1 MinecraftServer (net.minecraft.server.MinecraftServer)1 PlayerList (net.minecraft.server.management.PlayerList)1 WorldServer (net.minecraft.world.WorldServer)1