From 719bb11938ce028fee5669b82864560797856815 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Wed, 18 Oct 2023 14:56:44 +1000 Subject: [PATCH] Update TranslatableRewriter --- .../api/rewriters/ItemRewriter.java | 44 ++++++ .../api/rewriters/TranslatableRewriter.java | 95 +++---------- .../packets/ChatPackets1_12.java | 2 +- .../Protocol1_12_2To1_13.java | 7 +- .../Protocol1_13_2To1_14.java | 5 +- .../Protocol1_13To1_13_1.java | 5 +- .../Protocol1_14_4To1_15.java | 5 +- .../Protocol1_15_2To1_16.java | 3 +- .../chat/TranslatableRewriter1_16.java | 2 +- .../Protocol1_16_1To1_16_2.java | 5 +- .../Protocol1_16_4To1_17.java | 5 +- .../Protocol1_17_1To1_18.java | 5 +- .../Protocol1_18_2To1_19.java | 5 +- .../Protocol1_19_1To1_19_3.java | 5 +- .../Protocol1_19_3To1_19_4.java | 5 +- .../Protocol1_19_4To1_20.java | 5 +- .../Protocol1_19To1_19_1.java | 5 +- gradle/libs.versions.toml | 2 +- settings.gradle.kts | 1 + template/build.gradle.kts | 3 + .../template/protocol/Protocol1_98To_99.java | 124 +++++++++++++++++ .../rewriter/BlockItemPacketRewriter1_99.java | 61 +++++++++ .../rewriter/EntityPacketRewriter1_99.java | 126 ++++++++++++++++++ 23 files changed, 418 insertions(+), 107 deletions(-) create mode 100644 template/build.gradle.kts create mode 100644 template/src/main/java/com/viaversion/viabackwards/template/protocol/Protocol1_98To_99.java create mode 100644 template/src/main/java/com/viaversion/viabackwards/template/protocol/rewriter/BlockItemPacketRewriter1_99.java create mode 100644 template/src/main/java/com/viaversion/viabackwards/template/protocol/rewriter/EntityPacketRewriter1_99.java diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/ItemRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/ItemRewriter.java index 4c6500a5..902a9892 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/ItemRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/ItemRewriter.java @@ -177,4 +177,48 @@ public class ItemRewriter { + wrapper.passthrough(Type.BOOLEAN); // Reset/clear + final int size = wrapper.passthrough(Type.VAR_INT); // Mapping size + for (int i = 0; i < size; i++) { + wrapper.passthrough(Type.STRING); // Identifier + + // Parent + if (wrapper.passthrough(Type.BOOLEAN)) { + wrapper.passthrough(Type.STRING); + } + + // Display data + if (wrapper.passthrough(Type.BOOLEAN)) { + final Tag title = wrapper.passthrough(Type.TAG); + final Tag description = wrapper.passthrough(Type.TAG); + final TranslatableRewriter translatableRewriter = protocol.getTranslatableRewriter(); + if (translatableRewriter != null) { + translatableRewriter.processTag(title); + translatableRewriter.processTag(description); + } + + handleItemToClient(wrapper.passthrough(Type.ITEM1_20_2)); // Icon + wrapper.passthrough(Type.VAR_INT); // Frame type + final int flags = wrapper.passthrough(Type.INT); + if ((flags & 1) != 0) { + wrapper.passthrough(Type.STRING); // Background texture + } + wrapper.passthrough(Type.FLOAT); // X + wrapper.passthrough(Type.FLOAT); // Y + } + + final int requirements = wrapper.passthrough(Type.VAR_INT); + for (int array = 0; array < requirements; array++) { + wrapper.passthrough(Type.STRING_ARRAY); + } + + wrapper.passthrough(Type.BOOLEAN); // Send telemetry + } + }); + } } diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/TranslatableRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/TranslatableRewriter.java index d7820748..503bd943 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/TranslatableRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/TranslatableRewriter.java @@ -21,9 +21,6 @@ import com.viaversion.viabackwards.ViaBackwards; import com.viaversion.viabackwards.api.BackwardsProtocol; import com.viaversion.viabackwards.api.data.VBMappingDataLoader; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; -import com.viaversion.viaversion.api.protocol.packet.State; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; -import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.libs.gson.JsonObject; import com.viaversion.viaversion.rewriter.ComponentRewriter; @@ -34,101 +31,43 @@ import org.checkerframework.checker.nullness.qual.Nullable; public class TranslatableRewriter extends ComponentRewriter { private static final Map> TRANSLATABLES = new HashMap<>(); - private final Map newTranslatables; + private final Map translatables; public static void loadTranslatables() { - JsonObject jsonObject = VBMappingDataLoader.loadFromDataDir("translation-mappings.json"); - for (Map.Entry entry : jsonObject.entrySet()) { - Map versionMappings = new HashMap<>(); + final JsonObject jsonObject = VBMappingDataLoader.loadFromDataDir("translation-mappings.json"); + for (final Map.Entry entry : jsonObject.entrySet()) { + final Map versionMappings = new HashMap<>(); TRANSLATABLES.put(entry.getKey(), versionMappings); - for (Map.Entry translationEntry : entry.getValue().getAsJsonObject().entrySet()) { + for (final Map.Entry translationEntry : entry.getValue().getAsJsonObject().entrySet()) { versionMappings.put(translationEntry.getKey(), translationEntry.getValue().getAsString()); } } } - public TranslatableRewriter(BackwardsProtocol protocol) { - this(protocol, protocol.getClass().getSimpleName().split("To")[1].replace("_", ".")); + public TranslatableRewriter(final BackwardsProtocol protocol, final ReadType type) { + this(protocol, type, protocol.getClass().getSimpleName().split("To")[1].replace("_", ".")); } - public TranslatableRewriter(BackwardsProtocol protocol, String sectionIdentifier) { - super(protocol); - final Map newTranslatables = TRANSLATABLES.get(sectionIdentifier); - if (newTranslatables == null) { - ViaBackwards.getPlatform().getLogger().warning("Error loading " + sectionIdentifier + " translatables!"); - this.newTranslatables = new HashMap<>(); + public TranslatableRewriter(final BackwardsProtocol protocol, final ReadType type, final String sectionIdentifier) { + super(protocol, type); + final Map translatableMappings = TRANSLATABLES.get(sectionIdentifier); + if (translatableMappings == null) { + ViaBackwards.getPlatform().getLogger().warning("Missing " + sectionIdentifier + " translatables!"); + this.translatables = new HashMap<>(); } else { - this.newTranslatables = newTranslatables; + this.translatables = translatableMappings; } } - public void registerPing() { - protocol.registerClientbound(State.LOGIN, 0x00, 0x00, wrapper -> processText(wrapper.passthrough(Type.COMPONENT))); - } - - public void registerDisconnect(C packetType) { - protocol.registerClientbound(packetType, wrapper -> processText(wrapper.passthrough(Type.COMPONENT))); - } - - public void registerLegacyOpenWindow(C packetType) { - protocol.registerClientbound(packetType, new PacketHandlers() { - @Override - public void register() { - map(Type.UNSIGNED_BYTE); // Id - map(Type.STRING); // Window Type - handler(wrapper -> processText(wrapper.passthrough(Type.COMPONENT))); - } - }); - } - - public void registerOpenWindow(C packetType) { - protocol.registerClientbound(packetType, new PacketHandlers() { - @Override - public void register() { - map(Type.VAR_INT); // Id - map(Type.VAR_INT); // Window Type - handler(wrapper -> processText(wrapper.passthrough(Type.COMPONENT))); - } - }); - } - - public void registerTabList(C packetType) { - protocol.registerClientbound(packetType, wrapper -> { - processText(wrapper.passthrough(Type.COMPONENT)); - processText(wrapper.passthrough(Type.COMPONENT)); - }); - } - - public void registerCombatKill(C packetType) { - protocol.registerClientbound(packetType, new PacketHandlers() { - @Override - public void register() { - map(Type.VAR_INT); - map(Type.INT); - handler(wrapper -> processText(wrapper.passthrough(Type.COMPONENT))); - } - }); - } - - public void registerCombatKill1_20(C packetType) { - protocol.registerClientbound(packetType, new PacketHandlers() { - @Override - public void register() { - map(Type.VAR_INT); // Duration - handler(wrapper -> processText(wrapper.passthrough(Type.COMPONENT))); - } - }); - } - @Override - protected void handleTranslate(JsonObject root, String translate) { - String newTranslate = mappedTranslationKey(translate); + protected void handleTranslate(final JsonObject root, final String translate) { + final String newTranslate = mappedTranslationKey(translate); if (newTranslate != null) { root.addProperty("translate", newTranslate); } } public @Nullable String mappedTranslationKey(final String translationKey) { - return newTranslatables.get(translationKey); + return translatables.get(translationKey); } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/ChatPackets1_12.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/ChatPackets1_12.java index 2e2f1af6..d23f513f 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/ChatPackets1_12.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/ChatPackets1_12.java @@ -29,7 +29,7 @@ import com.viaversion.viaversion.rewriter.ComponentRewriter; public class ChatPackets1_12 extends RewriterBase { - public static final ComponentRewriter COMPONENT_REWRITER = new ComponentRewriter() { + public static final ComponentRewriter COMPONENT_REWRITER = new ComponentRewriter(null, ComponentRewriter.ReadType.JSON) { @Override public void processText(JsonElement element) { super.processText(element); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java index 07937e4b..820f53a2 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java @@ -47,6 +47,7 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPacke import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; +import com.viaversion.viaversion.rewriter.ComponentRewriter; import org.checkerframework.checker.nullness.qual.Nullable; public class Protocol1_12_2To1_13 extends BackwardsProtocol { @@ -54,7 +55,7 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol translatableRewriter = new TranslatableRewriter(this) { + private final TranslatableRewriter translatableRewriter = new TranslatableRewriter(this, ComponentRewriter.ReadType.JSON) { @Override protected void handleTranslate(JsonObject root, String translate) { String mappedKey = mappedTranslationKey(translate); @@ -63,7 +64,7 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol translatableToLegacyRewriter = new TranslatableRewriter(this) { + private final TranslatableRewriter translatableToLegacyRewriter = new TranslatableRewriter(this, ComponentRewriter.ReadType.JSON) { @Override protected void handleTranslate(JsonObject root, String translate) { String mappedKey = mappedTranslationKey(translate); @@ -89,7 +90,7 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol { @@ -44,7 +45,7 @@ public class Protocol1_13_2To1_14 extends BackwardsProtocol translatableRewriter = new TranslatableRewriter<>(this); + private final TranslatableRewriter translatableRewriter = new TranslatableRewriter<>(this, ComponentRewriter.ReadType.JSON); public Protocol1_13_2To1_14() { super(ClientboundPackets1_14.class, ClientboundPackets1_13.class, ServerboundPackets1_14.class, ServerboundPackets1_13.class); @@ -57,7 +58,7 @@ public class Protocol1_13_2To1_14 extends BackwardsProtocol translatableRewriter = new TranslatableRewriter<>(this); + private final TranslatableRewriter translatableRewriter = new TranslatableRewriter<>(this, ComponentRewriter.ReadType.JSON); public Protocol1_13To1_13_1() { super(ClientboundPackets1_13.class, ClientboundPackets1_13.class, ServerboundPackets1_13.class, ServerboundPackets1_13.class); @@ -63,7 +64,7 @@ public class Protocol1_13To1_13_1 extends BackwardsProtocol translatableRewriter = new TranslatableRewriter<>(this); + private final TranslatableRewriter translatableRewriter = new TranslatableRewriter<>(this, ComponentRewriter.ReadType.JSON); public Protocol1_14_4To1_15() { super(ClientboundPackets1_15.class, ClientboundPackets1_14_4.class, ServerboundPackets1_14.class, ServerboundPackets1_14.class); @@ -56,7 +57,7 @@ public class Protocol1_14_4To1_15 extends BackwardsProtocol translatableRewriter = new TranslatableRewriter<>(this); + private final TranslatableRewriter translatableRewriter = new TranslatableRewriter<>(this, ComponentRewriter.ReadType.JSON); public Protocol1_16_1To1_16_2() { super(ClientboundPackets1_16_2.class, ClientboundPackets1_16.class, ServerboundPackets1_16_2.class, ServerboundPackets1_16.class); @@ -57,7 +58,7 @@ public class Protocol1_16_1To1_16_2 extends BackwardsProtocol translatableRewriter = new TranslatableRewriter<>(this); + private final TranslatableRewriter translatableRewriter = new TranslatableRewriter<>(this, ComponentRewriter.ReadType.JSON); public Protocol1_16_4To1_17() { super(ClientboundPackets1_17.class, ClientboundPackets1_16_2.class, ServerboundPackets1_17.class, ServerboundPackets1_16_2.class); @@ -69,7 +70,7 @@ public final class Protocol1_16_4To1_17 extends BackwardsProtocol { @@ -40,7 +41,7 @@ public final class Protocol1_17_1To1_18 extends BackwardsProtocol translatableRewriter = new TranslatableRewriter<>(this); + private final TranslatableRewriter translatableRewriter = new TranslatableRewriter<>(this, ComponentRewriter.ReadType.JSON); public Protocol1_17_1To1_18() { super(ClientboundPackets1_18.class, ClientboundPackets1_17_1.class, ServerboundPackets1_17.class, ServerboundPackets1_17.class); @@ -55,7 +56,7 @@ public final class Protocol1_17_1To1_18 extends BackwardsProtocol translatableRewriter = new TranslatableRewriter<>(this); + private final TranslatableRewriter translatableRewriter = new TranslatableRewriter<>(this, ComponentRewriter.ReadType.JSON); public Protocol1_18_2To1_19() { super(ClientboundPackets1_19.class, ClientboundPackets1_18.class, ServerboundPackets1_19.class, ServerboundPackets1_17.class); @@ -71,7 +72,7 @@ public final class Protocol1_18_2To1_19 extends BackwardsProtocol translatableRewriter = new TranslatableRewriter<>(this); + private final TranslatableRewriter translatableRewriter = new TranslatableRewriter<>(this, ComponentRewriter.ReadType.JSON); public Protocol1_19_1To1_19_3() { super(ClientboundPackets1_19_3.class, ClientboundPackets1_19_1.class, ServerboundPackets1_19_3.class, ServerboundPackets1_19_1.class); @@ -75,7 +76,7 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol translatableRewriter = new TranslatableRewriter<>(this); + private final TranslatableRewriter translatableRewriter = new TranslatableRewriter<>(this, ComponentRewriter.ReadType.JSON); public Protocol1_19_3To1_19_4() { super(ClientboundPackets1_19_4.class, ClientboundPackets1_19_3.class, ServerboundPackets1_19_4.class, ServerboundPackets1_19_3.class); @@ -65,7 +66,7 @@ public final class Protocol1_19_3To1_19_4 extends BackwardsProtocol { public static final BackwardsMappings MAPPINGS = new BackwardsMappings(); - private final TranslatableRewriter translatableRewriter = new TranslatableRewriter<>(this); + private final TranslatableRewriter translatableRewriter = new TranslatableRewriter<>(this, ComponentRewriter.ReadType.JSON); private final EntityPackets1_20 entityRewriter = new EntityPackets1_20(this); private final BlockItemPackets1_20 itemRewriter = new BlockItemPackets1_20(this); @@ -65,7 +66,7 @@ public final class Protocol1_19_4To1_20 extends BackwardsProtocol translatableRewriter = new TranslatableRewriter<>(this); + private final TranslatableRewriter translatableRewriter = new TranslatableRewriter<>(this, ComponentRewriter.ReadType.JSON); public Protocol1_19To1_19_1() { super(ClientboundPackets1_19_1.class, ClientboundPackets1_19.class, ServerboundPackets1_19_1.class, ServerboundPackets1_19.class); @@ -80,7 +81,7 @@ public final class Protocol1_19To1_19_1 extends BackwardsProtocol. + */ +package com.viaversion.viabackwards.template.protocol; + +import com.viaversion.viabackwards.api.BackwardsProtocol; +import com.viaversion.viabackwards.api.data.BackwardsMappings; +import com.viaversion.viabackwards.api.rewriters.SoundRewriter; +import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter; +import com.viaversion.viabackwards.template.protocol.rewriter.BlockItemPacketRewriter1_99; +import com.viaversion.viabackwards.template.protocol.rewriter.EntityPacketRewriter1_99; +import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.entities.Entity1_19_4Types; +import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; +import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; +import com.viaversion.viaversion.data.entity.EntityTrackerBase; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundConfigurationPackets1_20_2; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundPackets1_20_2; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ServerboundConfigurationPackets1_20_2; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ServerboundPackets1_20_2; +import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.Protocol1_20_3To1_20_2; +import com.viaversion.viaversion.rewriter.ComponentRewriter.ReadType; +import com.viaversion.viaversion.rewriter.StatisticsRewriter; +import com.viaversion.viaversion.rewriter.TagRewriter; +import org.checkerframework.checker.nullness.qual.Nullable; + +// Placeholders to replace (in the entire package): +// Protocol1_98To_99, EntityPacketRewriter1_99, BlockItemPacketRewriter1_99 +// Protocol1_20_3To1_20_2 (the ViaVersion protocol class the mappings depend on) +// ClientboundPackets1_20_2 +// ServerboundPackets1_20_2 +// ClientboundConfigurationPackets1_20_2 +// ServerboundConfigurationPackets1_20_2 +// Entity1_19_4Types (UNMAPPED type) +// 1.99, 1.98 +public final class Protocol1_98To_99 extends BackwardsProtocol { + + // ViaBackwards uses its own mappings and also needs a translatablerewriter for translation mappings + public static final BackwardsMappings MAPPINGS = new BackwardsMappings("1.99", "1.98", Protocol1_20_3To1_20_2.class); + private final EntityPacketRewriter1_99 entityRewriter = new EntityPacketRewriter1_99(this); + private final BlockItemPacketRewriter1_99 itemRewriter = new BlockItemPacketRewriter1_99(this); + private final TranslatableRewriter translatableRewriter = new TranslatableRewriter<>(this, ReadType.NBT); + + public Protocol1_98To_99() { + super(ClientboundPackets1_20_2.class, ClientboundPackets1_20_2.class, ServerboundPackets1_20_2.class, ServerboundPackets1_20_2.class); + } + + @Override + protected void registerPackets() { + super.registerPackets(); + + final TagRewriter tagRewriter = new TagRewriter<>(this); + tagRewriter.registerGeneric(ClientboundPackets1_20_2.TAGS); + + final SoundRewriter soundRewriter = new SoundRewriter<>(this); + soundRewriter.register1_19_3Sound(ClientboundPackets1_20_2.SOUND); + soundRewriter.registerSound(ClientboundPackets1_20_2.ENTITY_SOUND); + soundRewriter.registerStopSound(ClientboundPackets1_20_2.STOP_SOUND); + + new StatisticsRewriter<>(this).register(ClientboundPackets1_20_2.STATISTICS); + + // Registers translatable mappings (missing a whole bunch still) + //translatableRewriter.registerOpenWindow(ClientboundPackets1_20_2.OPEN_WINDOW); // Handled by registerOpenWindow in item rewriters + translatableRewriter.registerComponentPacket(ClientboundPackets1_20_2.ACTIONBAR); + translatableRewriter.registerComponentPacket(ClientboundPackets1_20_2.TITLE_TEXT); + translatableRewriter.registerComponentPacket(ClientboundPackets1_20_2.TITLE_SUBTITLE); + translatableRewriter.registerBossBar(ClientboundPackets1_20_2.BOSSBAR); + translatableRewriter.registerComponentPacket(ClientboundPackets1_20_2.DISCONNECT); + translatableRewriter.registerTabList(ClientboundPackets1_20_2.TAB_LIST); + translatableRewriter.registerCombatKill1_20(ClientboundPackets1_20_2.COMBAT_KILL); + translatableRewriter.registerComponentPacket(ClientboundPackets1_20_2.SYSTEM_CHAT); + translatableRewriter.registerComponentPacket(ClientboundPackets1_20_2.DISGUISED_CHAT); + translatableRewriter.registerPing(); + } + + @Override + public void init(final UserConnection user) { + addEntityTracker(user, new EntityTrackerBase(user, Entity1_19_4Types.PLAYER)); + } + + @Override + public BackwardsMappings getMappingData() { + return MAPPINGS; + } + + @Override + public EntityPacketRewriter1_99 getEntityRewriter() { + return entityRewriter; + } + + @Override + public BlockItemPacketRewriter1_99 getItemRewriter() { + return itemRewriter; + } + + @Nullable + public TranslatableRewriter getTranslatableRewriter() { + return translatableRewriter; + } + + @Override + protected ClientboundPacketType clientboundFinishConfigurationPacket() { + return ClientboundConfigurationPackets1_20_2.FINISH_CONFIGURATION; + } + + @Override + protected ServerboundPacketType serverboundFinishConfigurationPacket() { + return ServerboundConfigurationPackets1_20_2.FINISH_CONFIGURATION; + } +} \ No newline at end of file diff --git a/template/src/main/java/com/viaversion/viabackwards/template/protocol/rewriter/BlockItemPacketRewriter1_99.java b/template/src/main/java/com/viaversion/viabackwards/template/protocol/rewriter/BlockItemPacketRewriter1_99.java new file mode 100644 index 00000000..f4cc7328 --- /dev/null +++ b/template/src/main/java/com/viaversion/viabackwards/template/protocol/rewriter/BlockItemPacketRewriter1_99.java @@ -0,0 +1,61 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2023 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viabackwards.template.protocol.rewriter; + +import com.viaversion.viabackwards.api.rewriters.ItemRewriter; +import com.viaversion.viabackwards.template.protocol.Protocol1_98To_99; +import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.types.Chunk1_18Type; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundPackets1_20_2; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ServerboundPackets1_20_2; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.rewriter.RecipeRewriter1_20_2; +import com.viaversion.viaversion.rewriter.BlockRewriter; + +// To replace if needed: +// Chunk1_18Type +// RecipeRewriter1_20_2 +public final class BlockItemPacketRewriter1_99 extends ItemRewriter { + + public BlockItemPacketRewriter1_99(final Protocol1_98To_99 protocol) { + super(protocol, Type.ITEM1_20_2, Type.ITEM1_20_2_VAR_INT_ARRAY); + } + + @Override + public void registerPackets() { + final BlockRewriter blockRewriter = new BlockRewriter<>(protocol, Type.POSITION1_14); + blockRewriter.registerBlockAction(ClientboundPackets1_20_2.BLOCK_ACTION); + blockRewriter.registerBlockChange(ClientboundPackets1_20_2.BLOCK_CHANGE); + blockRewriter.registerVarLongMultiBlockChange1_20(ClientboundPackets1_20_2.MULTI_BLOCK_CHANGE); + blockRewriter.registerEffect(ClientboundPackets1_20_2.EFFECT, 1010, 2001); + blockRewriter.registerChunkData1_19(ClientboundPackets1_20_2.CHUNK_DATA, Chunk1_18Type::new); + blockRewriter.registerBlockEntityData(ClientboundPackets1_20_2.BLOCK_ENTITY_DATA); + + registerSetCooldown(ClientboundPackets1_20_2.COOLDOWN); + registerWindowItems1_17_1(ClientboundPackets1_20_2.WINDOW_ITEMS); + registerSetSlot1_17_1(ClientboundPackets1_20_2.SET_SLOT); + registerAdvancements1_20_3(ClientboundPackets1_20_2.ADVANCEMENTS); + registerEntityEquipmentArray(ClientboundPackets1_20_2.ENTITY_EQUIPMENT); + registerClickWindow1_17_1(ServerboundPackets1_20_2.CLICK_WINDOW); + registerTradeList1_19(ClientboundPackets1_20_2.TRADE_LIST); + registerCreativeInvAction(ServerboundPackets1_20_2.CREATIVE_INVENTORY_ACTION, Type.ITEM1_20_2); + registerWindowPropertyEnchantmentHandler(ClientboundPackets1_20_2.WINDOW_PROPERTY); + registerSpawnParticle1_19(ClientboundPackets1_20_2.SPAWN_PARTICLE); + + new RecipeRewriter1_20_2<>(protocol).register(ClientboundPackets1_20_2.DECLARE_RECIPES); + } +} \ No newline at end of file diff --git a/template/src/main/java/com/viaversion/viabackwards/template/protocol/rewriter/EntityPacketRewriter1_99.java b/template/src/main/java/com/viaversion/viabackwards/template/protocol/rewriter/EntityPacketRewriter1_99.java new file mode 100644 index 00000000..de4d96db --- /dev/null +++ b/template/src/main/java/com/viaversion/viabackwards/template/protocol/rewriter/EntityPacketRewriter1_99.java @@ -0,0 +1,126 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2023 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viabackwards.template.protocol.rewriter; + +import com.viaversion.viabackwards.api.rewriters.EntityRewriter; +import com.viaversion.viabackwards.template.protocol.Protocol1_98To_99; +import com.viaversion.viaversion.api.minecraft.entities.Entity1_19_4Types; +import com.viaversion.viaversion.api.minecraft.entities.EntityType; +import com.viaversion.viaversion.api.protocol.packet.State; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; +import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.api.type.types.version.Types1_20_3; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundConfigurationPackets1_20_2; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundPackets1_20_2; + +// Replace if needed +// Types1_OLD +// Types1_20_3 +public final class EntityPacketRewriter1_99 extends EntityRewriter { + + public EntityPacketRewriter1_99(final Protocol1_98To_99 protocol) { + super(protocol); + } + + @Override + public void registerPackets() { + registerTrackerWithData1_19(ClientboundPackets1_20_2.SPAWN_ENTITY, Entity1_19_4Types.FALLING_BLOCK); + registerMetadataRewriter(ClientboundPackets1_20_2.ENTITY_METADATA, /*Types1_OLD.METADATA_LIST, */Types1_20_3.METADATA_LIST); // Specify old and new metadata list if changed + registerRemoveEntities(ClientboundPackets1_20_2.REMOVE_ENTITIES); + + protocol.registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_2.REGISTRY_DATA, new PacketHandlers() { + @Override + protected void register() { + map(Type.NAMED_COMPOUND_TAG); // Registry data + handler(dimensionDataHandler()); // Caches dimensions to access data like height later + handler(biomeSizeTracker()); // Tracks the amount of biomes sent for chunk data + } + }); + + protocol.registerClientbound(ClientboundPackets1_20_2.JOIN_GAME, new PacketHandlers() { + @Override + public void register() { + map(Type.INT); // Entity id + map(Type.BOOLEAN); // Hardcore + map(Type.STRING_ARRAY); // World List + map(Type.VAR_INT); // Max players + map(Type.VAR_INT); // View distance + map(Type.VAR_INT); // Simulation distance + map(Type.BOOLEAN); // Reduced debug info + map(Type.BOOLEAN); // Show death screen + map(Type.BOOLEAN); // Limited crafting + map(Type.STRING); // Dimension key + map(Type.STRING); // World + handler(worldDataTrackerHandlerByKey()); // Tracks world height and name for chunk data and entity (un)tracking + } + }); + + protocol.registerClientbound(ClientboundPackets1_20_2.RESPAWN, new PacketHandlers() { + @Override + public void register() { + map(Type.STRING); // Dimension + map(Type.STRING); // World + handler(worldDataTrackerHandlerByKey()); // Tracks world height and name for chunk data and entity (un)tracking + } + }); + } + + @Override + protected void registerRewrites() { + /*filter().handler((event, meta) -> { + int id = meta.metaType().typeId(); + if (id >= ac) { + return; + } else if (id >= ab) { + id--; + } + + meta.setMetaType(Types1_20_3.META_TYPES.byId(id)); + });*/ + + registerMetaTypeHandler( + Types1_20_3.META_TYPES.itemType, + Types1_20_3.META_TYPES.blockStateType, + Types1_20_3.META_TYPES.optionalBlockStateType, + Types1_20_3.META_TYPES.particleType, + Types1_20_3.META_TYPES.componentType, + Types1_20_3.META_TYPES.optionalComponentType + ); + + filter().filterFamily(Entity1_19_4Types.MINECART_ABSTRACT).index(11).handler((event, meta) -> { + final int blockState = meta.value(); + meta.setValue(protocol.getMappingData().getNewBlockStateId(blockState)); + }); + + // Remove metadata of new entity type + // filter().type(Entity1_xTypes.SNIFFER).removeIndex(newIndex); + } + + @Override + public void onMappingDataLoaded() { + // If types changed, uncomment to map them + // mapTypes(); + + // mapEntityTypeWithData(Entity1_19_4Types.SNIFFER, Entity1_19_4Types.RAVAGER).jsonName(); + } + + @Override + public EntityType typeFromId(final int type) { + return Entity1_19_4Types.getTypeFromId(type); + } +} \ No newline at end of file