Search in sources :

Example 1 with RawItemStack

use of org.lanternpowered.server.network.objects.RawItemStack in project LanternServer by LanternPowered.

the class CodecPlayOutSetWindowSlot method encode.

@Override
public ByteBuffer encode(CodecContext context, MessagePlayOutSetWindowSlot message) throws CodecException {
    final ByteBuffer buf = context.byteBufAlloc().buffer();
    buf.writeByte((byte) message.getWindow());
    buf.writeShort((short) message.getIndex());
    final Object item = message.getItem();
    if (item instanceof ItemStack) {
        buf.write(Types.ITEM_STACK, (ItemStack) item);
    } else if (item instanceof RawItemStack || item == null) {
        buf.write(Types.RAW_ITEM_STACK, (RawItemStack) item);
    } else {
        throw new EncoderException("Invalid item type:" + item.getClass().getName());
    }
    return buf;
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) RawItemStack(org.lanternpowered.server.network.objects.RawItemStack) RawItemStack(org.lanternpowered.server.network.objects.RawItemStack) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer)

Example 2 with RawItemStack

use of org.lanternpowered.server.network.objects.RawItemStack 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 3 with RawItemStack

use of org.lanternpowered.server.network.objects.RawItemStack in project LanternServer by LanternPowered.

the class LanternPlayer method sendBookView.

@Override
public void sendBookView(BookView bookView) {
    checkNotNull(bookView, "bookView");
    final DataView dataView = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
    WrittenBookItemTypeObjectSerializer.writeBookData(dataView, bookView, this.locale);
    // Written book internal id
    final RawItemStack rawItemStack = new RawItemStack(387, 0, 1, dataView);
    final int slot = this.inventory.getHotbar().getSelectedSlotIndex();
    this.session.send(new MessagePlayOutSetWindowSlot(-2, slot, rawItemStack));
    this.session.send(new MessagePlayOutOpenBook(HandTypes.MAIN_HAND));
    this.session.send(new MessagePlayOutSetWindowSlot(-2, slot, this.inventory.getHotbar().getSelectedSlot().peek().orElse(null)));
}
Also used : DataView(org.spongepowered.api.data.DataView) MessagePlayOutSetWindowSlot(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSetWindowSlot) RawItemStack(org.lanternpowered.server.network.objects.RawItemStack) MessagePlayOutOpenBook(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutOpenBook)

Example 4 with RawItemStack

use of org.lanternpowered.server.network.objects.RawItemStack in project LanternServer by LanternPowered.

the class CodecPlayOutEntityEquipment method encode.

@Override
public ByteBuffer encode(CodecContext context, MessagePlayOutEntityEquipment message) throws CodecException {
    final ByteBuffer buf = context.byteBufAlloc().buffer();
    buf.writeVarInt(message.getEntityId());
    buf.writeVarInt(message.getSlotIndex());
    final Object item = message.getItem();
    if (item instanceof ItemStack) {
        buf.write(Types.ITEM_STACK, (ItemStack) item);
    } else if (item instanceof RawItemStack || item == null) {
        buf.write(Types.RAW_ITEM_STACK, (RawItemStack) item);
    } else {
        throw new EncoderException("Invalid item type:" + item.getClass().getName());
    }
    return buf;
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) RawItemStack(org.lanternpowered.server.network.objects.RawItemStack) RawItemStack(org.lanternpowered.server.network.objects.RawItemStack) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer)

Aggregations

RawItemStack (org.lanternpowered.server.network.objects.RawItemStack)4 EncoderException (io.netty.handler.codec.EncoderException)2 ByteBuffer (org.lanternpowered.server.network.buffer.ByteBuffer)2 DataView (org.spongepowered.api.data.DataView)2 ItemStack (org.spongepowered.api.item.inventory.ItemStack)2 Vector3i (com.flowpowered.math.vector.Vector3i)1 CodecException (io.netty.handler.codec.CodecException)1 List (java.util.List)1 MessagePlayInAcceptBeaconEffects (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInAcceptBeaconEffects)1 MessagePlayInChangeItemName (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInChangeItemName)1 MessagePlayInChangeOffer (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInChangeOffer)1 MessagePlayInEditBook (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInEditBook)1 MessagePlayInEditCommandBlock (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInEditCommandBlock)1 MessagePlayInOutBrand (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutBrand)1 MessagePlayInOutChannelPayload (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload)1 MessagePlayInPickItem (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPickItem)1 MessagePlayInSignBook (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInSignBook)1 MessagePlayOutOpenBook (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutOpenBook)1 MessagePlayOutSetWindowSlot (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSetWindowSlot)1 PotionEffectType (org.spongepowered.api.effect.potion.PotionEffectType)1