Search in sources :

Example 1 with PositionRotationMessage

use of net.glowstone.net.message.play.game.PositionRotationMessage in project Glowstone by GlowstoneMC.

the class GlowPlayer method spawnAt.

/**
 * Spawn the player at the given location after they have already joined.
 *
 * <p>Used for changing worlds and respawning after death.
 *
 * @param location The location to place the player.
 */
private void spawnAt(Location location) {
    GlowWorld oldWorld;
    // switch worlds
    worldLock.writeLock().lock();
    try {
        oldWorld = world;
        world.getEntityManager().unregister(this);
        world = (GlowWorld) location.getWorld();
        world.getEntityManager().register(this);
        updateBossBars();
    } finally {
        worldLock.writeLock().unlock();
    }
    // switch chunk set
    // no need to send chunk unload messages - respawn unloads all chunks
    knownChunks.clear();
    chunkLock.clear();
    chunkLock = world.newChunkLock(getName());
    // spawn into world
    session.send(new RespawnMessage(world.getName(), world.getSeedHash(), getGameMode().getValue(), -1, false, world.getWorldType() == WorldType.FLAT, oldWorld.getEnvironment() != world.getEnvironment()));
    // take us to spawn position
    setRawLocation(location, false);
    session.send(new PositionRotationMessage(location));
    teleportedTo = location.clone();
    // set our compass target
    setCompassTarget(world.getSpawnLocation());
    // stream blocks
    streamBlocks();
    sendWeather();
    sendRainDensity();
    sendSkyDarkness();
    sendTime();
    updateInventory();
    // fire world change if needed
    if (oldWorld != world) {
        session.send(((GlowWorldBorder) world.getWorldBorder()).createMessage());
        EventFactory.getInstance().callEvent(new PlayerChangedWorldEvent(this, oldWorld));
    }
}
Also used : RespawnMessage(net.glowstone.net.message.play.game.RespawnMessage) PlayerChangedWorldEvent(org.bukkit.event.player.PlayerChangedWorldEvent) PositionRotationMessage(net.glowstone.net.message.play.game.PositionRotationMessage) GlowWorld(net.glowstone.GlowWorld)

Example 2 with PositionRotationMessage

use of net.glowstone.net.message.play.game.PositionRotationMessage in project Glowstone by GlowstoneMC.

the class GlowPlayer method teleport.

@Override
public boolean teleport(Location location, TeleportCause cause) {
    // NON-NLS
    checkNotNull(location, "location cannot be null");
    // NON-NLS
    checkNotNull(location.getWorld(), "location's world cannot be null");
    // NON-NLS
    checkNotNull(cause, "cause cannot be null");
    if (this.location != null && this.location.getWorld() != null) {
        PlayerTeleportEvent event = new PlayerTeleportEvent(this, this.location, location, cause);
        if (EventFactory.getInstance().callEvent(event).isCancelled()) {
            return false;
        }
        location = event.getTo();
        closeInventory();
    }
    worldLock.writeLock().lock();
    try {
        if (location.getWorld() != world) {
            spawnAt(location);
        } else {
            world.getEntityManager().move(this, location);
            // Position.copyLocation(location, this.previousLocation);
            // Position.copyLocation(location, this.location);
            session.send(new PositionRotationMessage(location));
            teleportedTo = location.clone();
        }
    } finally {
        worldLock.writeLock().unlock();
    }
    teleportedTo = location.clone();
    return true;
}
Also used : PositionRotationMessage(net.glowstone.net.message.play.game.PositionRotationMessage) PlayerTeleportEvent(org.bukkit.event.player.PlayerTeleportEvent)

Example 3 with PositionRotationMessage

use of net.glowstone.net.message.play.game.PositionRotationMessage in project Glowstone by GlowstoneMC.

the class GlowPlayer method join.

/**
 * Loads the player's state and sends the messages that are necessary on login.
 *
 * @param session the player's session
 * @param reader  the source of the player's saved state
 */
public void join(GlowSession session, PlayerReader reader) {
    String type = world.getWorldType().getName().toLowerCase();
    reader.readData(this);
    reader.close();
    int gameMode = getGameMode().getValue();
    session.send(new JoinGameMessage(getEntityId(), world.isHardcore(), gameMode, // TODO: determine previous gamemode
    -1, server.getWorlds().stream().map(World::getName).toArray(String[]::new), world.getName(), world.getSeedHash(), server.getMaxPlayers(), world.getViewDistance(), world.getGameRuleMap().getBoolean(GameRules.REDUCED_DEBUG_INFO), !world.getGameRuleMap().getBoolean(GameRules.DO_IMMEDIATE_RESPAWN), // TODO: Debug worlds
    false, world.getWorldType() == WorldType.FLAT));
    // send server brand and supported plugin channels
    Message pluginMessage = PluginMessage.fromString("minecraft:brand", server.getName());
    if (pluginMessage != null) {
        session.send(pluginMessage);
    }
    sendSupportedChannels();
    joinTime = System.currentTimeMillis();
    // Add player to list of online players
    getServer().setPlayerOnline(this, true);
    // save data back out
    saveData();
    // stream the initial set of blocks
    streamBlocks();
    sendWeather();
    sendRainDensity();
    sendSkyDarkness();
    getServer().sendPlayerAbilities(this);
    // send initial location
    session.send(new PositionRotationMessage(location));
    // send initial velocity
    session.send(new EntityVelocityMessage(getEntityId(), velocity));
    // send initial health
    sendHealth();
    // send gamemode defaults
    setGameModeDefaults();
    // send held item
    getSession().send(new HeldItemMessage(getInventory().getHeldItemSlot()));
    // send xp bar
    sendExperience();
    session.send(world.getWorldBorder().createMessage());
    sendTime();
    // set our compass target
    setCompassTarget(world.getSpawnLocation());
    scoreboard = server.getScoreboardManager().getMainScoreboard();
    scoreboard.subscribe(this);
    invMonitor = new InventoryMonitor(getOpenInventory());
    // send inventory contents
    updateInventory();
    session.send(recipeMonitor.createInitMessage());
    if (!server.getResourcePackUrl().isEmpty()) {
        setResourcePack(server.getResourcePackUrl(), server.getResourcePackHash());
    }
}
Also used : JoinGameMessage(net.glowstone.net.message.play.game.JoinGameMessage) TitleMessage(net.glowstone.net.message.play.game.TitleMessage) EntityMetadataMessage(net.glowstone.net.message.play.entity.EntityMetadataMessage) OpenWindowMessage(net.glowstone.net.message.play.inv.OpenWindowMessage) BlockBreakAnimationMessage(net.glowstone.net.message.play.game.BlockBreakAnimationMessage) WindowPropertyMessage(net.glowstone.net.message.play.inv.WindowPropertyMessage) UpdateBlockEntityMessage(net.glowstone.net.message.play.game.UpdateBlockEntityMessage) RespawnMessage(net.glowstone.net.message.play.game.RespawnMessage) EntityVelocityMessage(net.glowstone.net.message.play.entity.EntityVelocityMessage) PositionRotationMessage(net.glowstone.net.message.play.game.PositionRotationMessage) HealthMessage(net.glowstone.net.message.play.game.HealthMessage) PluginMessage(net.glowstone.net.message.play.game.PluginMessage) SetWindowSlotMessage(net.glowstone.net.message.play.inv.SetWindowSlotMessage) PlayEffectMessage(net.glowstone.net.message.play.game.PlayEffectMessage) BlockChangeMessage(net.glowstone.net.message.play.game.BlockChangeMessage) MultiBlockChangeMessage(net.glowstone.net.message.play.game.MultiBlockChangeMessage) UnloadChunkMessage(net.glowstone.net.message.play.game.UnloadChunkMessage) StateChangeMessage(net.glowstone.net.message.play.game.StateChangeMessage) SpawnPositionMessage(net.glowstone.net.message.play.game.SpawnPositionMessage) ExperienceMessage(net.glowstone.net.message.play.game.ExperienceMessage) NamedSoundEffectMessage(net.glowstone.net.message.play.game.NamedSoundEffectMessage) UserListHeaderFooterMessage(net.glowstone.net.message.play.game.UserListHeaderFooterMessage) HeldItemMessage(net.glowstone.net.message.play.inv.HeldItemMessage) MapDataMessage(net.glowstone.net.message.play.game.MapDataMessage) StopSoundMessage(net.glowstone.net.message.play.game.StopSoundMessage) CloseWindowMessage(net.glowstone.net.message.play.inv.CloseWindowMessage) ChunkDataMessage(net.glowstone.net.message.play.game.ChunkDataMessage) TimeMessage(net.glowstone.net.message.play.game.TimeMessage) Message(com.flowpowered.network.Message) SetWindowContentsMessage(net.glowstone.net.message.play.inv.SetWindowContentsMessage) PlayParticleMessage(net.glowstone.net.message.play.game.PlayParticleMessage) ResourcePackSendMessage(net.glowstone.net.message.play.player.ResourcePackSendMessage) DestroyEntitiesMessage(net.glowstone.net.message.play.entity.DestroyEntitiesMessage) JoinGameMessage(net.glowstone.net.message.play.game.JoinGameMessage) SignEditorMessage(net.glowstone.net.message.play.game.SignEditorMessage) SetPassengerMessage(net.glowstone.net.message.play.entity.SetPassengerMessage) EntityAnimationMessage(net.glowstone.net.message.play.entity.EntityAnimationMessage) TextMessage(net.glowstone.util.TextMessage) UpdateSignMessage(net.glowstone.net.message.play.game.UpdateSignMessage) UserListItemMessage(net.glowstone.net.message.play.game.UserListItemMessage) ChatMessage(net.glowstone.net.message.play.game.ChatMessage) PositionRotationMessage(net.glowstone.net.message.play.game.PositionRotationMessage) InventoryMonitor(net.glowstone.inventory.InventoryMonitor) EntityVelocityMessage(net.glowstone.net.message.play.entity.EntityVelocityMessage) HeldItemMessage(net.glowstone.net.message.play.inv.HeldItemMessage)

Example 4 with PositionRotationMessage

use of net.glowstone.net.message.play.game.PositionRotationMessage in project Glowstone by GlowstoneMC.

the class PlayerUpdateHandler method handle.

@Override
public void handle(GlowSession session, PlayerUpdateMessage message) {
    GlowPlayer player = session.getPlayer();
    Location oldLocation = player.getLocation();
    Location newLocation = oldLocation.clone();
    message.update(newLocation);
    // don't let players reach an illegal position
    if (Math.abs(newLocation.getBlockX()) > 32000000 || Math.abs(newLocation.getBlockZ()) > 32000000) {
        session.getPlayer().kickPlayer("Illegal position");
        return;
    }
    /*
          don't let players move more than 100 blocks in a single packet
          if they move greater than 10 blocks, but less than 100, just warn
          this is NOT robust hack prevention - only to prevent client
          confusion about where its actual location is (e.g. during login)
        */
    if (message.moved() && !player.isDead()) {
        if (player.teleportedTo != null) {
            if (newLocation.equals(player.teleportedTo)) {
                player.endTeleport();
                return;
            } else {
                // outdated location, so skip packet
                return;
            }
        } else {
            double distance = newLocation.distanceSquared(oldLocation);
            if (distance > 100 * 100) {
                player.kickPlayer("You moved too quickly :( (Hacking?)");
                return;
            } else if (distance > 100) {
                GlowServer.logger.warning(player.getName() + " moved too quickly!");
            }
        }
    }
    // call move event if movement actually occurred and there are handlers registered
    if (!oldLocation.equals(newLocation) && PlayerMoveEvent.getHandlerList().getRegisteredListeners().length > 0) {
        PlayerMoveEvent event = EventFactory.getInstance().callEvent(new PlayerMoveEvent(player, oldLocation, newLocation));
        if (event.isCancelled()) {
            // tell client they're back where they started
            session.send(new PositionRotationMessage(oldLocation));
            return;
        }
        if (!event.getTo().equals(newLocation)) {
            // teleport to the set destination: fires PlayerTeleportEvent and
            // handles if the destination is in another world
            player.teleport(event.getTo(), TeleportCause.PLUGIN);
            return;
        }
        if (!Objects.equals(player.getLocation(), oldLocation)) {
            // plugin changed location on move event
            return;
        }
    }
    // move event was not fired or did nothing, simply update location
    player.setRawLocation(newLocation);
    if (Position.hasRotated(oldLocation, newLocation)) {
        player.setHeadYaw(newLocation.getYaw());
    }
    // do stuff with onGround if we need to
    if (player.isOnGround() != message.isOnGround()) {
        if (message.isOnGround() && player.getVelocity().getY() > 0) {
            // jump
            player.incrementStatistic(Statistic.JUMP);
            if (player.isSprinting()) {
                player.addExhaustion(0.2f);
            } else {
                player.addExhaustion(0.05f);
            }
        }
        player.setOnGround(message.isOnGround());
    }
    // Checks if the player is still wearing the Elytra
    ItemStack chestplate = player.getInventory().getChestplate();
    boolean hasElytra = chestplate != null && chestplate.getType() == Material.ELYTRA && chestplate.getDurability() < chestplate.getType().getMaxDurability();
    if (player.isGliding() && (player.isOnGround() || !hasElytra)) {
        player.setGliding(false);
    }
    player.addMoveExhaustion(newLocation);
    // track movement stats
    Vector delta = newLocation.clone().subtract(oldLocation).toVector();
    delta.setX(Math.abs(delta.getX()));
    delta.setY(Math.abs(delta.getY()));
    delta.setZ(Math.abs(delta.getZ()));
    int flatDistance = (int) Math.round(Math.hypot(delta.getX(), delta.getZ()) * 100.0);
    if (flatDistance <= 0) {
        return;
    }
    if (player.isInsideVehicle()) {
        final GlowEntity vehicle = player.getVehicle();
        if (vehicle != null) {
            switch(vehicle.getType()) {
                case BOAT:
                    player.incrementStatistic(Statistic.BOAT_ONE_CM, flatDistance);
                    break;
                case MINECART:
                    player.incrementStatistic(Statistic.MINECART_ONE_CM, flatDistance);
                    break;
                default:
                    break;
            }
        }
    } else if (message.isOnGround()) {
        if (player.isSprinting()) {
            player.incrementStatistic(Statistic.SPRINT_ONE_CM, flatDistance);
        } else if (player.isSneaking()) {
            player.incrementStatistic(Statistic.CROUCH_ONE_CM, flatDistance);
        } else {
            player.incrementStatistic(Statistic.WALK_ONE_CM, flatDistance);
        }
    } else if (player.isFlying()) {
        player.incrementStatistic(Statistic.FLY_ONE_CM, flatDistance);
    } else if (player.isInWater()) {
        player.incrementStatistic(Statistic.SWIM_ONE_CM, flatDistance);
    }
}
Also used : PositionRotationMessage(net.glowstone.net.message.play.game.PositionRotationMessage) GlowPlayer(net.glowstone.entity.GlowPlayer) GlowEntity(net.glowstone.entity.GlowEntity) PlayerMoveEvent(org.bukkit.event.player.PlayerMoveEvent) ItemStack(org.bukkit.inventory.ItemStack) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Aggregations

PositionRotationMessage (net.glowstone.net.message.play.game.PositionRotationMessage)4 RespawnMessage (net.glowstone.net.message.play.game.RespawnMessage)2 Message (com.flowpowered.network.Message)1 GlowWorld (net.glowstone.GlowWorld)1 GlowEntity (net.glowstone.entity.GlowEntity)1 GlowPlayer (net.glowstone.entity.GlowPlayer)1 InventoryMonitor (net.glowstone.inventory.InventoryMonitor)1 DestroyEntitiesMessage (net.glowstone.net.message.play.entity.DestroyEntitiesMessage)1 EntityAnimationMessage (net.glowstone.net.message.play.entity.EntityAnimationMessage)1 EntityMetadataMessage (net.glowstone.net.message.play.entity.EntityMetadataMessage)1 EntityVelocityMessage (net.glowstone.net.message.play.entity.EntityVelocityMessage)1 SetPassengerMessage (net.glowstone.net.message.play.entity.SetPassengerMessage)1 BlockBreakAnimationMessage (net.glowstone.net.message.play.game.BlockBreakAnimationMessage)1 BlockChangeMessage (net.glowstone.net.message.play.game.BlockChangeMessage)1 ChatMessage (net.glowstone.net.message.play.game.ChatMessage)1 ChunkDataMessage (net.glowstone.net.message.play.game.ChunkDataMessage)1 ExperienceMessage (net.glowstone.net.message.play.game.ExperienceMessage)1 HealthMessage (net.glowstone.net.message.play.game.HealthMessage)1 JoinGameMessage (net.glowstone.net.message.play.game.JoinGameMessage)1 MapDataMessage (net.glowstone.net.message.play.game.MapDataMessage)1