Search in sources :

Example 26 with Item

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

the class BlockItemPackets1_15 method registerPackets.

@Override
protected void registerPackets() {
    BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14);
    new RecipeRewriter1_14(protocol).registerDefaultHandler(ClientboundPackets1_15.DECLARE_RECIPES);
    protocol.registerServerbound(ServerboundPackets1_14.EDIT_BOOK, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
        }
    });
    registerSetCooldown(ClientboundPackets1_15.COOLDOWN);
    registerWindowItems(ClientboundPackets1_15.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
    registerSetSlot(ClientboundPackets1_15.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
    registerTradeList(ClientboundPackets1_15.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
    registerEntityEquipment(ClientboundPackets1_15.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
    registerAdvancements(ClientboundPackets1_15.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
    registerClickWindow(ServerboundPackets1_14.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
    registerCreativeInvAction(ServerboundPackets1_14.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
    blockRewriter.registerAcknowledgePlayerDigging(ClientboundPackets1_15.ACKNOWLEDGE_PLAYER_DIGGING);
    blockRewriter.registerBlockAction(ClientboundPackets1_15.BLOCK_ACTION);
    blockRewriter.registerBlockChange(ClientboundPackets1_15.BLOCK_CHANGE);
    blockRewriter.registerMultiBlockChange(ClientboundPackets1_15.MULTI_BLOCK_CHANGE);
    protocol.registerClientbound(ClientboundPackets1_15.CHUNK_DATA, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Chunk chunk = wrapper.read(new Chunk1_15Type());
                    wrapper.write(new Chunk1_14Type(), chunk);
                    if (chunk.isFullChunk()) {
                        int[] biomeData = chunk.getBiomeData();
                        int[] newBiomeData = new int[256];
                        for (int i = 0; i < 4; ++i) {
                            for (int j = 0; j < 4; ++j) {
                                int x = j << 2;
                                int z = i << 2;
                                int newIndex = z << 4 | x;
                                int oldIndex = i << 2 | j;
                                int biome = biomeData[oldIndex];
                                for (int k = 0; k < 4; k++) {
                                    int offX = newIndex + (k << 4);
                                    for (int l = 0; l < 4; l++) {
                                        newBiomeData[offX + l] = biome;
                                    }
                                }
                            }
                        }
                        chunk.setBiomeData(newBiomeData);
                    }
                    for (int i = 0; i < chunk.getSections().length; i++) {
                        ChunkSection section = chunk.getSections()[i];
                        if (section == null)
                            continue;
                        for (int j = 0; j < section.getPaletteSize(); j++) {
                            int old = section.getPaletteEntry(j);
                            int newId = protocol.getMappingData().getNewBlockStateId(old);
                            section.setPaletteEntry(j, newId);
                        }
                    }
                }
            });
        }
    });
    blockRewriter.registerEffect(ClientboundPackets1_15.EFFECT, 1010, 2001);
    protocol.registerClientbound(ClientboundPackets1_15.SPAWN_PARTICLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Particle ID
            map(Type.INT);
            // 1 - Long Distance
            map(Type.BOOLEAN);
            // 2 - X
            map(Type.DOUBLE, Type.FLOAT);
            // 3 - Y
            map(Type.DOUBLE, Type.FLOAT);
            // 4 - Z
            map(Type.DOUBLE, Type.FLOAT);
            // 5 - Offset X
            map(Type.FLOAT);
            // 6 - Offset Y
            map(Type.FLOAT);
            // 7 - Offset Z
            map(Type.FLOAT);
            // 8 - Particle Data
            map(Type.FLOAT);
            // 9 - Particle Count
            map(Type.INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int id = wrapper.get(Type.INT, 0);
                    if (id == 3 || id == 23) {
                        int data = wrapper.passthrough(Type.VAR_INT);
                        wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data));
                    } else if (id == 32) {
                        Item item = handleItemToClient(wrapper.read(Type.FLAT_VAR_INT_ITEM));
                        wrapper.write(Type.FLAT_VAR_INT_ITEM, item);
                    }
                    int mappedId = protocol.getMappingData().getNewParticleId(id);
                    if (id != mappedId) {
                        wrapper.set(Type.INT, 0, mappedId);
                    }
                }
            });
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) RecipeRewriter1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Chunk1_14Type(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type) Item(com.viaversion.viaversion.api.minecraft.item.Item) ServerboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) Protocol1_14_4To1_15(com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.Protocol1_14_4To1_15) Chunk1_15Type(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.types.Chunk1_15Type) ClientboundPackets1_15(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) RecipeRewriter1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Chunk1_15Type(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.types.Chunk1_15Type) Chunk1_14Type(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) Item(com.viaversion.viaversion.api.minecraft.item.Item) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)

Example 27 with Item

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

the class EntityPackets1_16 method registerRewrites.

@Override
protected void registerRewrites() {
    filter().handler((event, meta) -> {
        meta.setMetaType(Types1_14.META_TYPES.byId(meta.metaType().typeId()));
        MetaType type = meta.metaType();
        if (type == Types1_14.META_TYPES.itemType) {
            meta.setValue(protocol.getItemRewriter().handleItemToClient((Item) meta.getValue()));
        } else if (type == Types1_14.META_TYPES.blockStateType) {
            meta.setValue(protocol.getMappingData().getNewBlockStateId((int) meta.getValue()));
        } else if (type == Types1_14.META_TYPES.particleType) {
            rewriteParticle((Particle) meta.getValue());
        } else if (type == Types1_14.META_TYPES.optionalComponentType) {
            JsonElement text = meta.value();
            if (text != null) {
                protocol.getTranslatableRewriter().processText(text);
            }
        }
    });
    mapEntityType(Entity1_16Types.ZOMBIFIED_PIGLIN, Entity1_15Types.ZOMBIE_PIGMAN);
    mapTypes(Entity1_16Types.values(), Entity1_15Types.class);
    mapEntityTypeWithData(Entity1_16Types.HOGLIN, Entity1_16Types.COW).jsonName();
    mapEntityTypeWithData(Entity1_16Types.ZOGLIN, Entity1_16Types.COW).jsonName();
    mapEntityTypeWithData(Entity1_16Types.PIGLIN, Entity1_16Types.ZOMBIFIED_PIGLIN).jsonName();
    mapEntityTypeWithData(Entity1_16Types.STRIDER, Entity1_16Types.MAGMA_CUBE).jsonName();
    filter().type(Entity1_16Types.ZOGLIN).cancel(16);
    filter().type(Entity1_16Types.HOGLIN).cancel(15);
    filter().type(Entity1_16Types.PIGLIN).cancel(16);
    filter().type(Entity1_16Types.PIGLIN).cancel(17);
    filter().type(Entity1_16Types.PIGLIN).cancel(18);
    filter().type(Entity1_16Types.STRIDER).index(15).handler((event, meta) -> {
        boolean baby = meta.value();
        meta.setTypeAndValue(Types1_14.META_TYPES.varIntType, baby ? 1 : 3);
    });
    filter().type(Entity1_16Types.STRIDER).cancel(16);
    filter().type(Entity1_16Types.STRIDER).cancel(17);
    filter().type(Entity1_16Types.STRIDER).cancel(18);
    filter().type(Entity1_16Types.FISHING_BOBBER).cancel(8);
    filter().filterFamily(Entity1_16Types.ABSTRACT_ARROW).cancel(8);
    filter().filterFamily(Entity1_16Types.ABSTRACT_ARROW).handler((event, meta) -> {
        if (event.index() >= 8) {
            event.setIndex(event.index() + 1);
        }
    });
    filter().type(Entity1_16Types.WOLF).index(16).handler((event, meta) -> {
        byte mask = meta.value();
        StoredEntityData data = tracker(event.user()).entityData(event.entityId());
        data.put(new WolfDataMaskStorage(mask));
    });
    filter().type(Entity1_16Types.WOLF).index(20).handler((event, meta) -> {
        StoredEntityData data = tracker(event.user()).entityDataIfPresent(event.entityId());
        byte previousMask = 0;
        if (data != null) {
            WolfDataMaskStorage wolfData = data.get(WolfDataMaskStorage.class);
            if (wolfData != null) {
                previousMask = wolfData.tameableMask();
            }
        }
        int angerTime = meta.value();
        byte tameableMask = (byte) (angerTime > 0 ? previousMask | 2 : previousMask & -3);
        event.createExtraMeta(new Metadata(16, Types1_14.META_TYPES.byteType, tameableMask));
        event.cancel();
    });
}
Also used : Item(com.viaversion.viaversion.api.minecraft.item.Item) WolfDataMaskStorage(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.storage.WolfDataMaskStorage) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData) MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType)

Example 28 with Item

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

the class RecipeRewriter1_16 method handleSmithing.

public void handleSmithing(PacketWrapper wrapper) throws Exception {
    Item[] baseIngredients = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT);
    for (Item item : baseIngredients) {
        rewrite(item);
    }
    Item[] ingredients = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT);
    for (Item item : ingredients) {
        rewrite(item);
    }
    // Result
    rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
}
Also used : Item(com.viaversion.viaversion.api.minecraft.item.Item)

Example 29 with Item

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

the class ComponentRewriter1_13 method handleHoverEvent.

@Override
protected void handleHoverEvent(JsonObject hoverEvent) {
    super.handleHoverEvent(hoverEvent);
    String action = hoverEvent.getAsJsonPrimitive("action").getAsString();
    if (!action.equals("show_item"))
        return;
    JsonElement value = hoverEvent.get("value");
    if (value == null)
        return;
    String text = findItemNBT(value);
    if (text == null)
        return;
    CompoundTag tag;
    try {
        tag = BinaryTagIO.readString(text);
    } catch (Exception e) {
        if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
            Via.getPlatform().getLogger().warning("Error reading NBT in show_item:" + text);
            e.printStackTrace();
        }
        return;
    }
    CompoundTag itemTag = tag.get("tag");
    ShortTag damageTag = tag.get("Damage");
    // Call item converter
    short damage = damageTag != null ? damageTag.asShort() : 0;
    Item item = new DataItem();
    item.setData(damage);
    item.setTag(itemTag);
    protocol.getItemRewriter().handleItemToClient(item);
    // Serialize again
    if (damage != item.data()) {
        tag.put("Damage", new ShortTag(item.data()));
    }
    if (itemTag != null) {
        tag.put("tag", itemTag);
    }
    JsonArray array = new JsonArray();
    JsonObject object = new JsonObject();
    array.add(object);
    String serializedNBT;
    try {
        serializedNBT = BinaryTagIO.writeString(tag);
        object.addProperty("text", serializedNBT);
        hoverEvent.add("value", array);
    } catch (IOException e) {
        Via.getPlatform().getLogger().warning("Error writing NBT in show_item:" + text);
        e.printStackTrace();
    }
}
Also used : JsonArray(com.google.gson.JsonArray) Item(com.viaversion.viaversion.api.minecraft.item.Item) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) IOException(java.io.IOException) ShortTag(com.github.steveice10.opennbt.tag.builtin.ShortTag)

Example 30 with Item

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

the class RecipeRewriter1_14 method handleStonecutting.

public void handleStonecutting(PacketWrapper wrapper) throws Exception {
    wrapper.passthrough(Type.STRING);
    // Ingredients
    Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT);
    for (Item item : items) {
        rewrite(item);
    }
    // Result
    rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
}
Also used : Item(com.viaversion.viaversion.api.minecraft.item.Item)

Aggregations

Item (com.viaversion.viaversion.api.minecraft.item.Item)43 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)28 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)27 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)23 Type (com.viaversion.viaversion.api.type.Type)15 DataItem (com.viaversion.viaversion.api.minecraft.item.DataItem)13 ClientWorld (com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)9 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)8 ArrayList (java.util.ArrayList)8 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)6 Position (com.viaversion.viaversion.api.minecraft.Position)6 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)6 ClientboundPackets1_13 (com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13)6 List (java.util.List)6 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)5 Via (com.viaversion.viaversion.api.Via)5 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)5 UUID (java.util.UUID)5 ListTag (com.github.steveice10.opennbt.tag.builtin.ListTag)4 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)4