Search in sources :

Example 1 with DoubleTag

use of com.github.steveice10.opennbt.tag.builtin.DoubleTag in project ViaVersion by ViaVersion.

the class InventoryPackets method registerPackets.

@Override
public void registerPackets() {
    registerSetCooldown(ClientboundPackets1_13.COOLDOWN);
    registerAdvancements(ClientboundPackets1_13.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
    protocol.registerClientbound(ClientboundPackets1_13.OPEN_WINDOW, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Short windowId = wrapper.read(Type.UNSIGNED_BYTE);
                    String type = wrapper.read(Type.STRING);
                    JsonElement title = wrapper.read(Type.COMPONENT);
                    COMPONENT_REWRITER.processText(title);
                    Short slots = wrapper.read(Type.UNSIGNED_BYTE);
                    if (type.equals("EntityHorse")) {
                        wrapper.setId(0x1F);
                        int entityId = wrapper.read(Type.INT);
                        wrapper.write(Type.UNSIGNED_BYTE, windowId);
                        wrapper.write(Type.VAR_INT, slots.intValue());
                        wrapper.write(Type.INT, entityId);
                    } else {
                        wrapper.setId(0x2E);
                        wrapper.write(Type.VAR_INT, windowId.intValue());
                        int typeId = -1;
                        switch(type) {
                            case "minecraft:crafting_table":
                                typeId = 11;
                                break;
                            case "minecraft:furnace":
                                typeId = 13;
                                break;
                            case "minecraft:dropper":
                            case "minecraft:dispenser":
                                typeId = 6;
                                break;
                            case "minecraft:enchanting_table":
                                typeId = 12;
                                break;
                            case "minecraft:brewing_stand":
                                typeId = 10;
                                break;
                            case "minecraft:villager":
                                typeId = 18;
                                break;
                            case "minecraft:beacon":
                                typeId = 8;
                                break;
                            case "minecraft:anvil":
                                typeId = 7;
                                break;
                            case "minecraft:hopper":
                                typeId = 15;
                                break;
                            case "minecraft:shulker_box":
                                typeId = 19;
                                break;
                            case "minecraft:container":
                            case "minecraft:chest":
                            default:
                                if (slots > 0 && slots <= 54) {
                                    typeId = slots / 9 - 1;
                                }
                                break;
                        }
                        if (typeId == -1) {
                            Via.getPlatform().getLogger().warning("Can't open inventory for 1.14 player! Type: " + type + " Size: " + slots);
                        }
                        wrapper.write(Type.VAR_INT, typeId);
                        wrapper.write(Type.COMPONENT, title);
                    }
                }
            });
        }
    });
    registerWindowItems(ClientboundPackets1_13.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
    registerSetSlot(ClientboundPackets1_13.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
    protocol.registerClientbound(ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Channel
            map(Type.STRING);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    String channel = wrapper.get(Type.STRING, 0);
                    if (channel.equals("minecraft:trader_list") || channel.equals("trader_list")) {
                        wrapper.setId(0x27);
                        wrapper.resetReader();
                        // Remove channel
                        wrapper.read(Type.STRING);
                        int windowId = wrapper.read(Type.INT);
                        EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
                        tracker.setLatestTradeWindowId(windowId);
                        wrapper.write(Type.VAR_INT, windowId);
                        int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
                        for (int i = 0; i < size; i++) {
                            // Input Item
                            handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
                            // Output Item
                            handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
                            // Has second item
                            boolean secondItem = wrapper.passthrough(Type.BOOLEAN);
                            if (secondItem) {
                                // Second Item
                                handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
                            }
                            // Trade disabled
                            wrapper.passthrough(Type.BOOLEAN);
                            // Number of tools uses
                            wrapper.passthrough(Type.INT);
                            // Maximum number of trade uses
                            wrapper.passthrough(Type.INT);
                            wrapper.write(Type.INT, 0);
                            wrapper.write(Type.INT, 0);
                            wrapper.write(Type.FLOAT, 0f);
                        }
                        wrapper.write(Type.VAR_INT, 0);
                        wrapper.write(Type.VAR_INT, 0);
                        wrapper.write(Type.BOOLEAN, false);
                    } else if (channel.equals("minecraft:book_open") || channel.equals("book_open")) {
                        int hand = wrapper.read(Type.VAR_INT);
                        wrapper.clearPacket();
                        wrapper.setId(0x2D);
                        wrapper.write(Type.VAR_INT, hand);
                    }
                }
            });
        }
    });
    registerEntityEquipment(ClientboundPackets1_13.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
    RecipeRewriter recipeRewriter = new RecipeRewriter1_13_2(protocol);
    protocol.registerClientbound(ClientboundPackets1_13.DECLARE_RECIPES, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                int size = wrapper.passthrough(Type.VAR_INT);
                int deleted = 0;
                for (int i = 0; i < size; i++) {
                    // Recipe Identifier
                    String id = wrapper.read(Type.STRING);
                    String type = wrapper.read(Type.STRING);
                    if (REMOVED_RECIPE_TYPES.contains(type)) {
                        deleted++;
                        continue;
                    }
                    wrapper.write(Type.STRING, type);
                    wrapper.write(Type.STRING, id);
                    recipeRewriter.handle(wrapper, type);
                }
                wrapper.set(Type.VAR_INT, 0, size - deleted);
            });
        }
    });
    registerClickWindow(ServerboundPackets1_14.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
    protocol.registerServerbound(ServerboundPackets1_14.SELECT_TRADE, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // Selecting trade now moves the items, we need to resync the inventory
                    PacketWrapper resyncPacket = wrapper.create(0x08);
                    EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
                    // 0 - Window ID
                    resyncPacket.write(Type.UNSIGNED_BYTE, ((short) tracker.getLatestTradeWindowId()));
                    // 1 - Slot
                    resyncPacket.write(Type.SHORT, ((short) -999));
                    // 2 - Button - End left click
                    resyncPacket.write(Type.BYTE, (byte) 2);
                    // 3 - Action number
                    resyncPacket.write(Type.SHORT, ((short) ThreadLocalRandom.current().nextInt()));
                    // 4 - Mode - Drag
                    resyncPacket.write(Type.VAR_INT, 5);
                    CompoundTag tag = new CompoundTag();
                    // Tags with NaN are not equal
                    tag.put("force_resync", new DoubleTag(Double.NaN));
                    // 5 - Clicked Item
                    resyncPacket.write(Type.FLAT_VAR_INT_ITEM, new DataItem(1, (byte) 1, (short) 0, tag));
                    resyncPacket.scheduleSendToServer(Protocol1_14To1_13_2.class);
                }
            });
        }
    });
    registerCreativeInvAction(ServerboundPackets1_14.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
    registerSpawnParticle(ClientboundPackets1_13.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.FLOAT);
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) ComponentRewriter(com.viaversion.viaversion.rewriter.ComponentRewriter) JsonObject(com.google.gson.JsonObject) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Item(com.viaversion.viaversion.api.minecraft.item.Item) JsonElement(com.google.gson.JsonElement) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Protocol1_14To1_13_2(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) ItemRewriter(com.viaversion.viaversion.rewriter.ItemRewriter) EntityTracker1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) Set(java.util.Set) RecipeRewriter1_13_2(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.RecipeRewriter1_13_2) Via(com.viaversion.viaversion.api.Via) ServerboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14) Sets(com.google.common.collect.Sets) RecipeRewriter(com.viaversion.viaversion.rewriter.RecipeRewriter) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) ClientboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) ChatRewriter(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter) DoubleTag(com.github.steveice10.opennbt.tag.builtin.DoubleTag) EntityTracker1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) RecipeRewriter(com.viaversion.viaversion.rewriter.RecipeRewriter) RecipeRewriter1_13_2(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.RecipeRewriter1_13_2) DoubleTag(com.github.steveice10.opennbt.tag.builtin.DoubleTag) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) JsonElement(com.google.gson.JsonElement) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 2 with DoubleTag

use of com.github.steveice10.opennbt.tag.builtin.DoubleTag in project DragonProxy by DragonetMC.

the class ItemBlockTranslator method translateRawNBT.

@SuppressWarnings("unchecked")
public static org.dragonet.common.data.nbt.tag.CompoundTag translateRawNBT(int id, Tag pcTag, org.dragonet.common.data.nbt.tag.CompoundTag target) {
    if (pcTag != null) {
        String name = pcTag.getName() != null ? pcTag.getName() : "";
        if (target == null)
            target = new org.dragonet.common.data.nbt.tag.CompoundTag(name);
        switch(pcTag.getClass().getSimpleName()) {
            case "ByteArrayTag":
                target.putByteArray(name, (byte[]) pcTag.getValue());
                break;
            case "ByteTag":
                target.putByte(name, (byte) pcTag.getValue());
                break;
            case "DoubleTag":
                target.putDouble(name, (double) pcTag.getValue());
                break;
            case "FloatTag":
                target.putFloat(name, (float) pcTag.getValue());
                break;
            case "IntArrayTag":
                target.putIntArray(name, (int[]) pcTag.getValue());
                break;
            case "IntTag":
                target.putInt(name, (int) pcTag.getValue());
                break;
            case "LongTag":
                target.putLong(name, (long) pcTag.getValue());
                break;
            case "ShortTag":
                target.putShort(name, (short) pcTag.getValue());
                break;
            case "StringTag":
                target.putString(name, (String) pcTag.getValue());
                break;
            case "CompoundTag":
                for (String subName : ((CompoundTag) pcTag).getValue().keySet()) translateRawNBT(0, ((CompoundTag) pcTag).getValue().get(subName), target);
                break;
            case "ListTag":
                ListTag listTag = new ListTag();
                for (Tag subTag : (List<Tag>) pcTag.getValue()) listTag.add(translateRawNBT(0, subTag, new org.dragonet.common.data.nbt.tag.CompoundTag()));
                target.putList(listTag);
                break;
            default:
                System.out.println("TAG not implemented : " + pcTag.getClass().getSimpleName());
                break;
        }
    }
    return target;
}
Also used : List(java.util.List) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) ListTag(org.dragonet.common.data.nbt.tag.ListTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) ListTag(org.dragonet.common.data.nbt.tag.ListTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 3 with DoubleTag

use of com.github.steveice10.opennbt.tag.builtin.DoubleTag in project ViaVersion by ViaVersion.

the class TagStringReader method scalar.

/**
 * A tag that is definitely some sort of scalar.
 *
 * <p>Does not detect quoted strings, so those should have been parsed already.</p>
 *
 * @return a parsed tag
 */
private Tag scalar() {
    final StringBuilder builder = new StringBuilder();
    int noLongerNumericAt = -1;
    while (this.buffer.hasMore()) {
        char current = this.buffer.peek();
        if (current == '\\') {
            // escape -- we are significantly more lenient than original format at the moment
            this.buffer.advance();
            current = this.buffer.take();
        } else if (Tokens.id(current)) {
            this.buffer.advance();
        } else {
            // end of value
            break;
        }
        builder.append(current);
        if (noLongerNumericAt == -1 && !Tokens.numeric(current)) {
            noLongerNumericAt = builder.length();
        }
    }
    final int length = builder.length();
    final String built = builder.toString();
    if (noLongerNumericAt == length) {
        final char last = built.charAt(length - 1);
        try {
            switch(// try to read and return as a number
            Character.toLowerCase(last)) {
                case Tokens.TYPE_BYTE:
                    return new ByteTag(Byte.parseByte(built.substring(0, length - 1)));
                case Tokens.TYPE_SHORT:
                    return new ShortTag(Short.parseShort(built.substring(0, length - 1)));
                case Tokens.TYPE_INT:
                    return new IntTag(Integer.parseInt(built.substring(0, length - 1)));
                case Tokens.TYPE_LONG:
                    return new LongTag(Long.parseLong(built.substring(0, length - 1)));
                case Tokens.TYPE_FLOAT:
                    final float floatValue = Float.parseFloat(built.substring(0, length - 1));
                    if (Float.isFinite(floatValue)) {
                        // don't accept NaN and Infinity
                        return new FloatTag(floatValue);
                    }
                    break;
                case Tokens.TYPE_DOUBLE:
                    final double doubleValue = Double.parseDouble(built.substring(0, length - 1));
                    if (Double.isFinite(doubleValue)) {
                        // don't accept NaN and Infinity
                        return new DoubleTag(doubleValue);
                    }
                    break;
            }
        } catch (final NumberFormatException ignored) {
        }
    } else if (noLongerNumericAt == -1) {
        // if we run out of content without an explicit value separator, then we're either an integer or string tag -- all others have a character at the end
        try {
            return new IntTag(Integer.parseInt(built));
        } catch (final NumberFormatException ex) {
            if (built.indexOf('.') != -1) {
                try {
                    return new DoubleTag(Double.parseDouble(built));
                } catch (final NumberFormatException ex2) {
                // ignore
                }
            }
        }
    }
    if (built.equalsIgnoreCase(Tokens.LITERAL_TRUE)) {
        return new ByteTag((byte) 1);
    } else if (built.equalsIgnoreCase(Tokens.LITERAL_FALSE)) {
        return new ByteTag((byte) 0);
    }
    return new StringTag(built);
}
Also used : StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) DoubleTag(com.github.steveice10.opennbt.tag.builtin.DoubleTag) ShortTag(com.github.steveice10.opennbt.tag.builtin.ShortTag) FloatTag(com.github.steveice10.opennbt.tag.builtin.FloatTag) ByteTag(com.github.steveice10.opennbt.tag.builtin.ByteTag) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag) LongTag(com.github.steveice10.opennbt.tag.builtin.LongTag)

Aggregations

StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)3 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)2 DoubleTag (com.github.steveice10.opennbt.tag.builtin.DoubleTag)2 IntTag (com.github.steveice10.opennbt.tag.builtin.IntTag)2 Tag (com.github.steveice10.opennbt.tag.builtin.Tag)2 ByteTag (com.github.steveice10.opennbt.tag.builtin.ByteTag)1 FloatTag (com.github.steveice10.opennbt.tag.builtin.FloatTag)1 ListTag (com.github.steveice10.opennbt.tag.builtin.ListTag)1 LongTag (com.github.steveice10.opennbt.tag.builtin.LongTag)1 ShortTag (com.github.steveice10.opennbt.tag.builtin.ShortTag)1 Sets (com.google.common.collect.Sets)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 Via (com.viaversion.viaversion.api.Via)1 DataItem (com.viaversion.viaversion.api.minecraft.item.DataItem)1 Item (com.viaversion.viaversion.api.minecraft.item.Item)1 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)1 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)1 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)1 Type (com.viaversion.viaversion.api.type.Type)1