Search in sources :

Example 1 with MessagePlayInOutChannelPayload

use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload in project LanternServer by LanternPowered.

the class CodecPlayInOutCustomPayload method decode0.

@Override
protected Message decode0(CodecContext context, String channel, ByteBuffer content) throws CodecException {
    if ("MC|ItemName".equals(channel)) {
        return new MessagePlayInChangeItemName(content.readString());
    } else if ("MC|TrSel".equals(channel)) {
        return new MessagePlayInChangeOffer(content.readInteger());
    } else if ("MC|Brand".equals(channel)) {
        return new MessagePlayInOutBrand(content.readString());
    } else if ("MC|Beacon".equals(channel)) {
        final PotionEffectType primary = PotionEffectTypeRegistryModule.get().getByInternalId(content.readInteger()).orElse(null);
        final PotionEffectType secondary = PotionEffectTypeRegistryModule.get().getByInternalId(content.readInteger()).orElse(null);
        return new MessagePlayInAcceptBeaconEffects(primary, secondary);
    } else if ("MC|AdvCdm".equals(channel)) {
        final byte type = content.readByte();
        Vector3i pos = null;
        int entityId = 0;
        if (type == 0) {
            int x = content.readInteger();
            int y = content.readInteger();
            int z = content.readInteger();
            pos = new Vector3i(x, y, z);
        } else if (type == 1) {
            entityId = content.readInteger();
        } else {
            throw new CodecException("Unknown modify command message type: " + type);
        }
        final String command = content.readString();
        final boolean shouldTrackOutput = content.readBoolean();
        if (pos != null) {
            return new MessagePlayInEditCommandBlock.Block(pos, command, shouldTrackOutput);
        } else {
            return new MessagePlayInEditCommandBlock.Entity(entityId, command, shouldTrackOutput);
        }
    } else if ("MC|AutoCmd".equals(channel)) {
        final int x = content.readInteger();
        final int y = content.readInteger();
        final int z = content.readInteger();
        final String command = content.readString();
        final boolean shouldTrackOutput = content.readBoolean();
        final MessagePlayInEditCommandBlock.AdvancedBlock.Mode mode = MessagePlayInEditCommandBlock.AdvancedBlock.Mode.valueOf(content.readString());
        final boolean conditional = content.readBoolean();
        final boolean automatic = content.readBoolean();
        return new MessagePlayInEditCommandBlock.AdvancedBlock(new Vector3i(x, y, z), command, shouldTrackOutput, mode, conditional, automatic);
    } else if ("MC|BSign".equals(channel)) {
        final RawItemStack rawItemStack = content.read(Types.RAW_ITEM_STACK);
        // noinspection ConstantConditions
        if (rawItemStack == null) {
            throw new CodecException("Signed book may not be null!");
        }
        final DataView dataView = rawItemStack.getDataView();
        if (dataView == null) {
            throw new CodecException("Signed book data view (nbt tag) may not be null!");
        }
        final String author = dataView.getString(AUTHOR).orElseThrow(() -> new CodecException("Signed book author missing!"));
        final String title = dataView.getString(TITLE).orElseThrow(() -> new CodecException("Signed book title missing!"));
        final List<String> pages = dataView.getStringList(PAGES).orElseThrow(() -> new CodecException("Signed book pages missing!"));
        return new MessagePlayInSignBook(author, title, pages);
    } else if ("MC|BEdit".equals(channel)) {
        final RawItemStack rawItemStack = content.read(Types.RAW_ITEM_STACK);
        // noinspection ConstantConditions
        if (rawItemStack == null) {
            throw new CodecException("Edited book may not be null!");
        }
        final DataView dataView = rawItemStack.getDataView();
        if (dataView == null) {
            throw new CodecException("Edited book data view (nbt tag) may not be null!");
        }
        final List<String> pages = dataView.getStringList(PAGES).orElseThrow(() -> new CodecException("Edited book pages missing!"));
        return new MessagePlayInEditBook(pages);
    } else if ("MC|Struct".equals(channel)) {
    // Something related to structure placing in minecraft 1.9,
    // seems like it's something mojang doesn't want to share with use,
    // they used this channel to build and save structures
    } else if ("MC|PickItem".equals(channel)) {
        return new MessagePlayInPickItem(content.readVarInt());
    } else if ("FML|HS".equals(channel)) {
        throw new CodecException("Received and unexpected message with channel: " + channel);
    } else if ("FML".equals(channel)) {
    // Fml channel
    } else if (channel.startsWith("FML")) {
    // A unknown/ignored fml channel
    } else {
        return new MessagePlayInOutChannelPayload(channel, content);
    }
    return NullMessage.INSTANCE;
}
Also used : MessagePlayInChangeOffer(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInChangeOffer) MessagePlayInEditBook(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInEditBook) MessagePlayInChangeItemName(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInChangeItemName) MessagePlayInAcceptBeaconEffects(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInAcceptBeaconEffects) MessagePlayInSignBook(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInSignBook) PotionEffectType(org.spongepowered.api.effect.potion.PotionEffectType) DataView(org.spongepowered.api.data.DataView) MessagePlayInPickItem(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPickItem) MessagePlayInOutChannelPayload(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload) RawItemStack(org.lanternpowered.server.network.objects.RawItemStack) Vector3i(com.flowpowered.math.vector.Vector3i) MessagePlayInEditCommandBlock(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInEditCommandBlock) CodecException(io.netty.handler.codec.CodecException) List(java.util.List) MessagePlayInOutBrand(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutBrand) MessagePlayInEditCommandBlock(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInEditCommandBlock)

Example 2 with MessagePlayInOutChannelPayload

use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload in project LanternServer by LanternPowered.

the class ProcessorForgeHandshakeOutRegistryData method process.

@Override
public void process(CodecContext context, MessageForgeHandshakeOutRegistryData message, List<Message> output) throws CodecException {
    final Iterator<Entry> it = message.getEntries().iterator();
    if (!it.hasNext()) {
        throw new CodecException("There must be at least one entry present!");
    }
    while (it.hasNext()) {
        final Entry entry = it.next();
        final ByteBuffer buf = context.byteBufAlloc().buffer();
        buf.writeByte((byte) CodecPlayInOutCustomPayload.FML_HANDSHAKE_REGISTRY_DATA);
        buf.writeBoolean(it.hasNext());
        buf.writeString(entry.getName());
        final Map<String, Integer> ids = entry.getIds();
        buf.writeVarInt(ids.size());
        for (Map.Entry<String, Integer> en : ids.entrySet()) {
            buf.writeString(en.getKey());
            buf.writeVarInt(en.getValue());
        }
        final List<String> substitutions = entry.getSubstitutions();
        buf.writeVarInt(substitutions.size());
        substitutions.forEach(buf::writeString);
        output.add(new MessagePlayInOutChannelPayload("FML|HS", buf));
    }
}
Also used : Entry(org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeOutRegistryData.Entry) MessagePlayInOutChannelPayload(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload) CodecException(io.netty.handler.codec.CodecException) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) Map(java.util.Map)

Example 3 with MessagePlayInOutChannelPayload

use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload in project LanternServer by LanternPowered.

the class AbstractCodecPlayInOutCustomPayload method encode.

@Override
public ByteBuffer encode(CodecContext context, Message message) throws CodecException {
    final ByteBuffer buf = context.byteBufAlloc().buffer();
    final String channel;
    final ByteBuffer content;
    if (message instanceof MessagePlayInOutChannelPayload) {
        final MessagePlayInOutChannelPayload message1 = (MessagePlayInOutChannelPayload) message;
        content = message1.getContent();
        channel = message1.getChannel();
    } else if (message instanceof MessagePlayInOutRegisterChannels) {
        content = encodeChannels(((MessagePlayInOutRegisterChannels) message).getChannels());
        channel = "REGISTER";
    } else if (message instanceof MessagePlayInOutUnregisterChannels) {
        content = encodeChannels(((MessagePlayInOutUnregisterChannels) message).getChannels());
        channel = "UNREGISTER";
    } else {
        final MessageResult result = encode0(context, message);
        channel = result.channel;
        content = result.byteBuf;
    }
    buf.writeString(channel);
    buf.writeBytes(content);
    return buf;
}
Also used : MessagePlayInOutRegisterChannels(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutRegisterChannels) MessagePlayInOutUnregisterChannels(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutUnregisterChannels) MessagePlayInOutChannelPayload(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer)

Example 4 with MessagePlayInOutChannelPayload

use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload in project LanternServer by LanternPowered.

the class LanternChannelRegistrar method sendPayload.

void sendPayload(Player player, String channel, Consumer<ByteBuffer> payload) {
    checkNotNull(player, "player");
    checkNotNull(payload, "payload");
    final NetworkSession session = ((LanternPlayer) player).getConnection();
    if (session.getRegisteredChannels().contains(channel)) {
        final ByteBuffer buf = ByteBufferAllocator.unpooled().buffer();
        payload.accept(buf);
        session.send(new MessagePlayInOutChannelPayload(channel, buf));
    }
}
Also used : NetworkSession(org.lanternpowered.server.network.NetworkSession) MessagePlayInOutChannelPayload(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 5 with MessagePlayInOutChannelPayload

use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload in project LanternServer by LanternPowered.

the class LanternChannelRegistrar method sendPayloadToAll.

void sendPayloadToAll(String channel, Consumer<ByteBuffer> payload) {
    checkNotNull(payload, "payload");
    final Iterator<Player> players = this.server.getOnlinePlayers().stream().filter(player -> ((LanternPlayer) player).getConnection().getRegisteredChannels().contains(channel)).iterator();
    if (players.hasNext()) {
        final ByteBuffer buf = ByteBufferAllocator.unpooled().buffer();
        payload.accept(buf);
        final Message msg = new MessagePlayInOutChannelPayload(channel, buf);
        players.forEachRemaining(player -> ((LanternPlayer) player).getConnection().send(msg));
    }
}
Also used : RemoteConnection(org.spongepowered.api.network.RemoteConnection) ByteBufferAllocator(org.lanternpowered.server.network.buffer.ByteBufferAllocator) IndexedMessageChannel(org.spongepowered.api.network.ChannelBinding.IndexedMessageChannel) Inject(com.google.inject.Inject) MessagePlayInOutChannelPayload(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload) Platform(org.spongepowered.api.Platform) MessagePlayInOutUnregisterChannels(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutUnregisterChannels) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) NetworkSession(org.lanternpowered.server.network.NetworkSession) ChannelRegistrar(org.spongepowered.api.network.ChannelRegistrar) Map(java.util.Map) MessagePlayInOutRegisterChannels(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutRegisterChannels) Server(org.spongepowered.api.Server) Conditions.checkNotNullOrEmpty(org.lanternpowered.server.util.Conditions.checkNotNullOrEmpty) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) PluginContainer(org.spongepowered.api.plugin.PluginContainer) ImmutableSet(com.google.common.collect.ImmutableSet) Iterator(java.util.Iterator) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Sets(com.google.common.collect.Sets) Message(org.lanternpowered.server.network.message.Message) ChannelBinding(org.spongepowered.api.network.ChannelBinding) Consumer(java.util.function.Consumer) ChannelRegistrationException(org.spongepowered.api.network.ChannelRegistrationException) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) Conditions.checkPlugin(org.lanternpowered.server.util.Conditions.checkPlugin) Collections(java.util.Collections) Singleton(com.google.inject.Singleton) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) Player(org.spongepowered.api.entity.living.player.Player) Message(org.lanternpowered.server.network.message.Message) MessagePlayInOutChannelPayload(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Aggregations

MessagePlayInOutChannelPayload (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload)5 ByteBuffer (org.lanternpowered.server.network.buffer.ByteBuffer)4 CodecException (io.netty.handler.codec.CodecException)2 Map (java.util.Map)2 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)2 NetworkSession (org.lanternpowered.server.network.NetworkSession)2 MessagePlayInOutRegisterChannels (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutRegisterChannels)2 MessagePlayInOutUnregisterChannels (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutUnregisterChannels)2 Vector3i (com.flowpowered.math.vector.Vector3i)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Sets (com.google.common.collect.Sets)1 Inject (com.google.inject.Inject)1 Singleton (com.google.inject.Singleton)1 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1