Search in sources :

Example 1 with PlayerReader

use of net.glowstone.io.PlayerDataService.PlayerReader in project Glowstone by GlowstoneMC.

the class GlowSession method setPlayer.

// //////////////////////////////////////////////////////////////////////////
// Player and state management
/**
 * Sets the player associated with this session.
 *
 * @param profile The player's profile with name and UUID information.
 * @throws IllegalStateException if there is already a player associated with this
 *                               session.
 */
public void setPlayer(GlowPlayerProfile profile) {
    if (player != null) {
        throw new IllegalStateException("Cannot set player twice");
    }
    // isActive check here in case player disconnected during authentication
    if (!isActive()) {
        // no need to call onDisconnect() since it only does anything if there's a player set
        return;
    }
    // initialize the player
    PlayerReader reader = server.getPlayerDataService().beginReadingData(profile.getId());
    player = new GlowPlayer(this, profile, reader);
    finalizeLogin(profile);
    // but before the GlowPlayer initialization was completed
    if (!isActive()) {
        reader.close();
        onDisconnect();
        return;
    }
    // Kick other players with the same UUID
    for (GlowPlayer other : getServer().getRawOnlinePlayers()) {
        if (other != player && other.getUniqueId().equals(player.getUniqueId())) {
            other.getSession().disconnect("You logged in from another location.", true);
            break;
        }
    }
    // login event
    PlayerLoginEvent event = EventFactory.getInstance().onPlayerLogin(player, virtualHost.toString());
    if (event.getResult() != Result.ALLOWED) {
        disconnect(event.getKickMessage(), true);
        return;
    }
    // joins the player
    player.join(this, reader);
    player.getWorld().getRawPlayers().add(player);
    online = true;
    GlowServer.logger.info(player.getName() + " [" + address + "] connected, UUID: " + UuidUtils.toString(player.getUniqueId()));
    // message and user list
    String message = EventFactory.getInstance().onPlayerJoin(player).getJoinMessage();
    if (message != null && !message.isEmpty()) {
        server.broadcastMessage(message);
    }
    Message addMessage = new UserListItemMessage(Action.ADD_PLAYER, player.getUserListEntry());
    List<Entry> entries = new ArrayList<>();
    for (GlowPlayer other : server.getRawOnlinePlayers()) {
        if (other != player && other.canSee(player)) {
            other.getSession().send(addMessage);
        }
        if (player.canSee(other)) {
            entries.add(other.getUserListEntry());
        }
    }
    send(new UserListItemMessage(Action.ADD_PLAYER, entries));
    send(server.createAdvancementsMessage(false, Collections.emptyList(), player));
}
Also used : Entry(net.glowstone.net.message.play.game.UserListItemMessage.Entry) KickMessage(net.glowstone.net.message.KickMessage) PingMessage(net.glowstone.net.message.play.game.PingMessage) LoginSuccessMessage(net.glowstone.net.message.login.LoginSuccessMessage) DestroyEntitiesMessage(net.glowstone.net.message.play.entity.DestroyEntitiesMessage) Message(com.flowpowered.network.Message) SetCompressionMessage(net.glowstone.net.message.SetCompressionMessage) UserListItemMessage(net.glowstone.net.message.play.game.UserListItemMessage) AsyncableMessage(com.flowpowered.network.AsyncableMessage) PlayerReader(net.glowstone.io.PlayerDataService.PlayerReader) GlowPlayer(net.glowstone.entity.GlowPlayer) PlayerLoginEvent(org.bukkit.event.player.PlayerLoginEvent) UserListItemMessage(net.glowstone.net.message.play.game.UserListItemMessage) ArrayList(java.util.ArrayList)

Example 2 with PlayerReader

use of net.glowstone.io.PlayerDataService.PlayerReader in project Glowstone by GlowstoneMC.

the class GlowOfflinePlayer method loadData.

// //////////////////////////////////////////////////////////////////////////
// Core properties
private void loadData() {
    profile.completeCached();
    try (PlayerReader reader = server.getPlayerDataService().beginReadingData(getUniqueId())) {
        hasPlayed = reader.hasPlayedBefore();
        if (hasPlayed) {
            firstPlayed = reader.getFirstPlayed();
            lastPlayed = reader.getLastPlayed();
            lastLogin = reader.getLastLogin();
            bedSpawnLocation = reader.getBedSpawnLocation();
            String lastName = reader.getLastKnownName();
            if (lastName != null) {
                this.lastName = lastName;
            }
        }
    }
}
Also used : PlayerReader(net.glowstone.io.PlayerDataService.PlayerReader)

Example 3 with PlayerReader

use of net.glowstone.io.PlayerDataService.PlayerReader in project Glowstone by GlowstoneMC.

the class GlowSession method setPlayer.

/**
     * Sets the player associated with this session.
     *
     * @param profile The player's profile with name and UUID information.
     * @throws IllegalStateException if there is already a player associated
     *                               with this session.
     */
public void setPlayer(PlayerProfile profile) {
    if (player != null) {
        throw new IllegalStateException("Cannot set player twice");
    }
    // isActive check here in case player disconnected during authentication
    if (!isActive()) {
        // no need to call onDisconnect() since it only does anything if there's a player set
        return;
    }
    // initialize the player
    PlayerReader reader = server.getPlayerDataService().beginReadingData(profile.getUniqueId());
    player = new GlowPlayer(this, profile, reader);
    finalizeLogin(profile);
    // but before the GlowPlayer initialization was completed
    if (!isActive()) {
        reader.close();
        onDisconnect();
        return;
    }
    // Kick other players with the same UUID
    for (GlowPlayer other : getServer().getRawOnlinePlayers()) {
        if (other != player && other.getUniqueId().equals(player.getUniqueId())) {
            other.getSession().disconnect("You logged in from another location.", true);
            break;
        }
    }
    // login event
    PlayerLoginEvent event = EventFactory.onPlayerLogin(player, hostname);
    if (event.getResult() != Result.ALLOWED) {
        disconnect(event.getKickMessage(), true);
        return;
    }
    //joins the player
    player.join(this, reader);
    player.getWorld().getRawPlayers().add(player);
    online = true;
    GlowServer.logger.info(player.getName() + " [" + address + "] connected, UUID: " + player.getUniqueId());
    // message and user list
    String message = EventFactory.onPlayerJoin(player).getJoinMessage();
    if (message != null && !message.isEmpty()) {
        server.broadcastMessage(message);
    }
    Message addMessage = new UserListItemMessage(Action.ADD_PLAYER, player.getUserListEntry());
    List<Entry> entries = new ArrayList<>();
    for (GlowPlayer other : server.getRawOnlinePlayers()) {
        if (other != player && other.canSee(player)) {
            other.getSession().send(addMessage);
        }
        if (player.canSee(other)) {
            entries.add(other.getUserListEntry());
        }
    }
    send(new UserListItemMessage(Action.ADD_PLAYER, entries));
}
Also used : Entry(net.glowstone.net.message.play.game.UserListItemMessage.Entry) KickMessage(net.glowstone.net.message.KickMessage) Message(com.flowpowered.network.Message) PingMessage(net.glowstone.net.message.play.game.PingMessage) LoginSuccessMessage(net.glowstone.net.message.login.LoginSuccessMessage) DestroyEntitiesMessage(net.glowstone.net.message.play.entity.DestroyEntitiesMessage) SetCompressionMessage(net.glowstone.net.message.SetCompressionMessage) BlockPlacementMessage(net.glowstone.net.message.play.player.BlockPlacementMessage) UserListItemMessage(net.glowstone.net.message.play.game.UserListItemMessage) AsyncableMessage(com.flowpowered.network.AsyncableMessage) PlayerReader(net.glowstone.io.PlayerDataService.PlayerReader) GlowPlayer(net.glowstone.entity.GlowPlayer) PlayerLoginEvent(org.bukkit.event.player.PlayerLoginEvent) UserListItemMessage(net.glowstone.net.message.play.game.UserListItemMessage)

Aggregations

PlayerReader (net.glowstone.io.PlayerDataService.PlayerReader)3 AsyncableMessage (com.flowpowered.network.AsyncableMessage)2 Message (com.flowpowered.network.Message)2 GlowPlayer (net.glowstone.entity.GlowPlayer)2 KickMessage (net.glowstone.net.message.KickMessage)2 SetCompressionMessage (net.glowstone.net.message.SetCompressionMessage)2 LoginSuccessMessage (net.glowstone.net.message.login.LoginSuccessMessage)2 DestroyEntitiesMessage (net.glowstone.net.message.play.entity.DestroyEntitiesMessage)2 PingMessage (net.glowstone.net.message.play.game.PingMessage)2 UserListItemMessage (net.glowstone.net.message.play.game.UserListItemMessage)2 Entry (net.glowstone.net.message.play.game.UserListItemMessage.Entry)2 PlayerLoginEvent (org.bukkit.event.player.PlayerLoginEvent)2 ArrayList (java.util.ArrayList)1 BlockPlacementMessage (net.glowstone.net.message.play.player.BlockPlacementMessage)1