Search in sources :

Example 11 with DataItem

use of com.viaversion.viaversion.api.minecraft.item.DataItem in project ViaVersion by ViaVersion.

the class MetadataRewriter1_11To1_10 method handleMetadata.

@Override
protected void handleMetadata(int entityId, com.viaversion.viaversion.api.minecraft.entities.EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) {
    if (metadata.getValue() instanceof DataItem) {
        // Apply rewrite
        EntityIdRewriter.toClientItem((Item) metadata.getValue());
    }
    if (type == null)
        return;
    if (type.is(EntityType.ELDER_GUARDIAN) || type.is(EntityType.GUARDIAN)) {
        // Guardians
        int oldid = metadata.id();
        if (oldid == 12) {
            boolean val = (((byte) metadata.getValue()) & 0x02) == 0x02;
            metadata.setTypeAndValue(MetaType1_9.Boolean, val);
        }
    }
    if (type.isOrHasParent(EntityType.ABSTRACT_SKELETON)) {
        // Skeletons
        int oldid = metadata.id();
        if (oldid == 12) {
            metadatas.remove(metadata);
        }
        if (oldid == 13) {
            metadata.setId(12);
        }
    }
    if (type.isOrHasParent(EntityType.ZOMBIE)) {
        // Zombie | Zombie Villager | Husk
        if (type.is(EntityType.ZOMBIE, EntityType.HUSK) && metadata.id() == 14) {
            metadatas.remove(metadata);
        } else {
            if (metadata.id() == 15) {
                metadata.setId(14);
            } else {
                if (metadata.id() == 14) {
                    metadata.setId(15);
                }
            }
        }
    }
    if (type.isOrHasParent(EntityType.ABSTRACT_HORSE)) {
        // Horses
        // Remap metadata id
        int oldid = metadata.id();
        if (oldid == 14) {
            // Type
            metadatas.remove(metadata);
        }
        if (oldid == 16) {
            // Owner
            metadata.setId(14);
        }
        if (oldid == 17) {
            // Armor
            metadata.setId(16);
        }
        // Process per type
        if (type.is(EntityType.HORSE)) {
        // Normal Horse
        } else {
            // Remove 15, 16
            if (metadata.id() == 15 || metadata.id() == 16) {
                metadatas.remove(metadata);
            }
        }
        if (type.is(EntityType.DONKEY, EntityType.MULE)) {
            // Chested Horse
            if (metadata.id() == 13) {
                if ((((byte) metadata.getValue()) & 0x08) == 0x08) {
                    metadatas.add(new Metadata(15, MetaType1_9.Boolean, true));
                } else {
                    metadatas.add(new Metadata(15, MetaType1_9.Boolean, false));
                }
            }
        }
    }
    if (type.is(EntityType.ARMOR_STAND) && Via.getConfig().isHologramPatch()) {
        Metadata flags = metaByIndex(11, metadatas);
        Metadata customName = metaByIndex(2, metadatas);
        Metadata customNameVisible = metaByIndex(3, metadatas);
        if (metadata.id() == 0 && flags != null && customName != null && customNameVisible != null) {
            byte data = (byte) metadata.getValue();
            // Check invisible | Check small | Check if custom name is empty | Check if custom name visible is true
            if ((data & 0x20) == 0x20 && ((byte) flags.getValue() & 0x01) == 0x01 && !((String) customName.getValue()).isEmpty() && (boolean) customNameVisible.getValue()) {
                EntityTracker1_11 tracker = tracker(connection);
                if (tracker.addHologram(entityId)) {
                    try {
                        // Send movement
                        PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9_3.ENTITY_POSITION, null, connection);
                        wrapper.write(Type.VAR_INT, entityId);
                        wrapper.write(Type.SHORT, (short) 0);
                        wrapper.write(Type.SHORT, (short) (128D * (-Via.getConfig().getHologramYOffset() * 32D)));
                        wrapper.write(Type.SHORT, (short) 0);
                        wrapper.write(Type.BOOLEAN, true);
                        wrapper.send(Protocol1_11To1_10.class);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
Also used : EntityTracker1_11(com.viaversion.viaversion.protocols.protocol1_11to1_10.storage.EntityTracker1_11) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata)

Example 12 with DataItem

use of com.viaversion.viaversion.api.minecraft.item.DataItem in project ViaVersion by ViaVersion.

the class Protocol1_17_1To1_17 method registerPackets.

@Override
protected void registerPackets() {
    registerClientbound(ClientboundPackets1_17.REMOVE_ENTITY, ClientboundPackets1_17_1.REMOVE_ENTITIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Aaaaand back to an array again!
                int entityId = wrapper.read(Type.VAR_INT);
                wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { entityId });
            });
        }
    });
    registerClientbound(ClientboundPackets1_17.SET_SLOT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Container id
            map(Type.UNSIGNED_BYTE);
            // Add arbitrary state id
            create(Type.VAR_INT, 0);
        }
    });
    registerClientbound(ClientboundPackets1_17.WINDOW_ITEMS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Container id
            map(Type.UNSIGNED_BYTE);
            // Add arbitrary state id
            create(Type.VAR_INT, 0);
            handler(wrapper -> {
                // Length encoded as var int now
                wrapper.write(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT, wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY));
                // Carried item - should work like this
                wrapper.write(Type.FLAT_VAR_INT_ITEM, null);
            });
        }
    });
    registerServerbound(ServerboundPackets1_17.CLICK_WINDOW, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Container id
            map(Type.UNSIGNED_BYTE);
            // Remove state id
            read(Type.VAR_INT);
        }
    });
    registerServerbound(ServerboundPackets1_17.EDIT_BOOK, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                CompoundTag tag = new CompoundTag();
                // Magic value for writable books
                Item item = new DataItem(942, (byte) 1, (short) 0, tag);
                // Write the item, edit the tag down the line
                wrapper.write(Type.FLAT_VAR_INT_ITEM, item);
                int slot = wrapper.read(Type.VAR_INT);
                // Save pages to tag
                int pages = wrapper.read(Type.VAR_INT);
                ListTag pagesTag = new ListTag(StringTag.class);
                for (int i = 0; i < pages; i++) {
                    String page = wrapper.read(PAGE_STRING_TYPE);
                    pagesTag.add(new StringTag(page));
                }
                // Legacy servers don't like an empty pages list
                if (pagesTag.size() == 0) {
                    pagesTag.add(new StringTag(""));
                }
                tag.put("pages", pagesTag);
                if (wrapper.read(Type.BOOLEAN)) {
                    // Save the title to tag
                    String title = wrapper.read(TITLE_STRING_TYPE);
                    tag.put("title", new StringTag(title));
                    // Even if unused, legacy servers check for the author tag
                    tag.put("author", new StringTag(wrapper.user().getProtocolInfo().getUsername()));
                    // Write signing
                    wrapper.write(Type.BOOLEAN, true);
                } else {
                    wrapper.write(Type.BOOLEAN, false);
                }
                // Write the slot
                wrapper.write(Type.VAR_INT, slot);
            });
        }
    });
}
Also used : StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17) Type(com.viaversion.viaversion.api.type.Type) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) ServerboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPackets1_17) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) StringType(com.viaversion.viaversion.api.type.types.StringType) Item(com.viaversion.viaversion.api.minecraft.item.Item) AbstractProtocol(com.viaversion.viaversion.api.protocol.AbstractProtocol) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) Item(com.viaversion.viaversion.api.minecraft.item.Item) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 13 with DataItem

use of com.viaversion.viaversion.api.minecraft.item.DataItem in project ViaFabric by ViaVersion.

the class VRHandItemProvider method fromNative.

private Item fromNative(ItemStack original) {
    Identifier iid = Registry.ITEM.getId(original.getItem());
    int id = RemappingUtil.swordId(iid.toString());
    return new DataItem(id, (byte) original.getCount(), (short) original.getDamage(), null);
}
Also used : Identifier(net.minecraft.util.Identifier) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem)

Example 14 with DataItem

use of com.viaversion.viaversion.api.minecraft.item.DataItem in project ViaFabric by ViaVersion.

the class VRHandItemProvider method fromNative.

private Item fromNative(ItemStack original) {
    Identifier iid = Registry.ITEM.getId(original.getItem());
    int id = RemappingUtil.swordId(iid.toString());
    return new DataItem(id, (byte) original.getCount(), (short) original.getDamage(), null);
}
Also used : Identifier(net.minecraft.util.Identifier) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem)

Example 15 with DataItem

use of com.viaversion.viaversion.api.minecraft.item.DataItem in project ViaVersion by ViaVersion.

the class ItemTypeTest method testNormalItemRead.

@Test
public void testNormalItemRead() throws Exception {
    // Test item read
    Assertions.assertEquals(new DataItem((int) Short.MAX_VALUE, (byte) -128, (short) 257, null), Type.ITEM.read(Unpooled.wrappedBuffer(new byte[] { 127, -1, -128, 1, 1, 0 })));
    Assertions.assertEquals(new DataItem(420, (byte) 53, (short) 0, null), Type.FLAT_ITEM.read(Unpooled.wrappedBuffer(new byte[] { 1, (byte) 164, 53, 0 })));
    Assertions.assertEquals(new DataItem(268435456, (byte) 127, (short) 0, null), Type.FLAT_VAR_INT_ITEM.read(Unpooled.wrappedBuffer(new byte[] { 1, -128, -128, -128, -128, 1, 127, 0 })));
}
Also used : DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) Test(org.junit.jupiter.api.Test)

Aggregations

DataItem (com.viaversion.viaversion.api.minecraft.item.DataItem)23 Item (com.viaversion.viaversion.api.minecraft.item.Item)12 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)7 Identifier (net.minecraft.util.Identifier)6 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)5 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)4 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)4 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)4 Type (com.viaversion.viaversion.api.type.Type)4 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)3 JsonObject (com.google.gson.JsonObject)3 Via (com.viaversion.viaversion.api.Via)3 ListTag (com.github.steveice10.opennbt.tag.builtin.ListTag)2 Sets (com.google.common.collect.Sets)2 JsonElement (com.google.gson.JsonElement)2 Position (com.viaversion.viaversion.api.minecraft.Position)2 AbstractProtocol (com.viaversion.viaversion.api.protocol.AbstractProtocol)2 Protocol1_14To1_13_2 (com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2)2 EntityTracker1_14 (com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14)2 EntityTracker1_9 (com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9)2