Search in sources :

Example 6 with LanternPlayer

use of org.lanternpowered.server.entity.living.player.LanternPlayer in project LanternServer by LanternPowered.

the class HandlerPlayInChatMessage method handle.

@Override
public void handle(NetworkContext context, MessagePlayInChatMessage message) {
    final NetworkSession session = context.getSession();
    final LanternPlayer player = session.getPlayer();
    player.resetIdleTimeoutCounter();
    final String message0 = message.getMessage();
    // Check for a valid click action callback
    final Matcher matcher = LanternClickActionCallbacks.COMMAND_PATTERN.matcher(message0);
    if (matcher.matches()) {
        final UUID uniqueId = UUID.fromString(matcher.group(1));
        final Optional<Consumer<CommandSource>> callback = LanternClickActionCallbacks.get().getCallbackForUUID(uniqueId);
        if (callback.isPresent()) {
            callback.get().accept(player);
        } else {
            player.sendMessage(error(t("The callback you provided was not valid. Keep in mind that callbacks will expire " + "after 10 minutes, so you might want to consider clicking faster next time!")));
        }
        return;
    }
    String message1 = StringUtils.normalizeSpace(message0);
    if (!isAllowedString(message0)) {
        session.disconnect(t("multiplayer.disconnect.illegal_characters"));
        return;
    }
    if (message1.startsWith("/")) {
        Lantern.getSyncExecutorService().submit(() -> Sponge.getCommandManager().process(player, message1.substring(1)));
    } else {
        final Text nameText = player.get(Keys.DISPLAY_NAME).get();
        final Text rawMessageText = Text.of(message0);
        final GlobalConfig.Chat.Urls urls = Lantern.getGame().getGlobalConfig().getChat().getUrls();
        final Text messageText;
        if (urls.isEnabled() && player.hasPermission(Permissions.Chat.FORMAT_URLS)) {
            messageText = newTextWithLinks(message0, urls.getTemplate(), false);
        } else {
            messageText = rawMessageText;
        }
        final MessageChannel channel = player.getMessageChannel();
        final CauseStack causeStack = CauseStack.current();
        try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
            frame.addContext(EventContextKeys.PLAYER, player);
            final MessageChannelEvent.Chat event = SpongeEventFactory.createMessageChannelEventChat(causeStack.getCurrentCause(), channel, Optional.of(channel), new MessageEvent.MessageFormatter(nameText, messageText), rawMessageText, false);
            if (!Sponge.getEventManager().post(event) && !event.isMessageCancelled()) {
                event.getChannel().ifPresent(c -> c.send(player, event.getMessage(), ChatTypes.CHAT));
            }
        }
    }
    final Attribute<ChatData> attr = context.getChannel().attr(CHAT_DATA);
    ChatData chatData = attr.get();
    if (chatData == null) {
        chatData = new ChatData();
        final ChatData chatData1 = attr.setIfAbsent(chatData);
        if (chatData1 != null) {
            chatData = chatData1;
        }
    }
    // noinspection SynchronizationOnLocalVariableOrMethodParameter
    synchronized (chatData) {
        final long currentTime = LanternGame.currentTimeTicks();
        if (chatData.lastChatTime != -1L) {
            chatData.chatThrottle = (int) Math.max(0, chatData.chatThrottle - (currentTime - chatData.lastChatTime));
        }
        chatData.lastChatTime = currentTime;
        chatData.chatThrottle += 20;
        if (chatData.chatThrottle > Lantern.getGame().getGlobalConfig().getChatSpamThreshold()) {
            session.disconnect(t("disconnect.spam"));
        }
    }
}
Also used : CauseStack(org.lanternpowered.server.event.CauseStack) NetworkSession(org.lanternpowered.server.network.NetworkSession) Matcher(java.util.regex.Matcher) MessageChannelEvent(org.spongepowered.api.event.message.MessageChannelEvent) MessageEvent(org.spongepowered.api.event.message.MessageEvent) Text(org.spongepowered.api.text.Text) Consumer(java.util.function.Consumer) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) UUID(java.util.UUID) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 7 with LanternPlayer

use of org.lanternpowered.server.entity.living.player.LanternPlayer in project LanternServer by LanternPowered.

the class HandlerPlayInClientSettings method handle.

@Override
public void handle(NetworkContext context, MessagePlayInClientSettings message) {
    final LanternPlayer player = context.getSession().getPlayer();
    final Cause cause = Cause.of(EventContext.empty(), player);
    final PlayerChangeClientSettingsEvent event = SpongeEventFactory.createPlayerChangeClientSettingsEvent(cause, message.getChatVisibility(), LanternSkinPart.fromBitPattern(message.getSkinPartsBitPattern()), message.getLocale(), player, message.getEnableColors(), message.getViewDistance());
    Sponge.getEventManager().post(event);
    player.setLocale(event.getLocale());
    player.setViewDistance(event.getViewDistance());
    player.setChatVisibility(event.getChatVisibility());
    player.setChatColorsEnabled(message.getEnableColors());
    player.offer(LanternKeys.DISPLAYED_SKIN_PARTS, event.getDisplayedSkinParts());
    player.offer(Keys.DOMINANT_HAND, message.getDominantHand());
}
Also used : PlayerChangeClientSettingsEvent(org.spongepowered.api.event.entity.living.humanoid.player.PlayerChangeClientSettingsEvent) Cause(org.spongepowered.api.event.cause.Cause) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 8 with LanternPlayer

use of org.lanternpowered.server.entity.living.player.LanternPlayer in project LanternServer by LanternPowered.

the class HandlerPlayInCraftingBookState method handle.

@Override
public void handle(NetworkContext context, MessagePlayInCraftingBookState message) {
    final LanternPlayer player = context.getSession().getPlayer();
    player.offer(LanternKeys.RECIPE_BOOK_FILTER_ACTIVE, message.hasFilter());
    player.offer(LanternKeys.RECIPE_BOOK_GUI_OPEN, message.isCurrentlyOpen());
}
Also used : LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 9 with LanternPlayer

use of org.lanternpowered.server.entity.living.player.LanternPlayer in project LanternServer by LanternPowered.

the class HandlerPlayInPlayerMovement method handle.

@Override
public void handle(NetworkContext context, MessagePlayInPlayerMovement message) {
    final LanternPlayer player = context.getSession().getPlayer();
    player.setRawPosition(new Vector3d(message.getX(), message.getY(), message.getZ()));
    player.handleOnGroundState(message.isOnGround());
}
Also used : Vector3d(com.flowpowered.math.vector.Vector3d) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 10 with LanternPlayer

use of org.lanternpowered.server.entity.living.player.LanternPlayer in project LanternServer by LanternPowered.

the class HandlerPlayInPlayerMovementAndLook method handle.

@Override
public void handle(NetworkContext context, MessagePlayInPlayerMovementAndLook message) {
    final LanternPlayer player = context.getSession().getPlayer();
    player.setRawPosition(new Vector3d(message.getX(), message.getY(), message.getZ()));
    player.setRawRotation(HandlerPlayInPlayerLook.toRotation(message.getPitch(), message.getYaw()));
    player.handleOnGroundState(message.isOnGround());
}
Also used : Vector3d(com.flowpowered.math.vector.Vector3d) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Aggregations

LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)48 ArrayList (java.util.ArrayList)13 CauseStack (org.lanternpowered.server.event.CauseStack)12 AbstractSlot (org.lanternpowered.server.inventory.AbstractSlot)11 ItemStack (org.spongepowered.api.item.inventory.ItemStack)11 World (org.spongepowered.api.world.World)11 Optional (java.util.Optional)10 Vector3d (com.flowpowered.math.vector.Vector3d)9 List (java.util.List)9 Collectors (java.util.stream.Collectors)9 Nullable (javax.annotation.Nullable)9 Sponge (org.spongepowered.api.Sponge)9 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)9 Lantern (org.lanternpowered.server.game.Lantern)8 LanternItemStack (org.lanternpowered.server.inventory.LanternItemStack)8 Player (org.spongepowered.api.entity.living.player.Player)8 ChangeInventoryEvent (org.spongepowered.api.event.item.inventory.ChangeInventoryEvent)8 AbstractInventory (org.lanternpowered.server.inventory.AbstractInventory)7 PlayerInventoryContainer (org.lanternpowered.server.inventory.PlayerInventoryContainer)7 ClientContainer (org.lanternpowered.server.inventory.client.ClientContainer)7