From f4df276f32879f8a200aebbf5998e626e499bec2 Mon Sep 17 00:00:00 2001 From: RaphiMC <50594595+RaphiMC@users.noreply.github.com> Date: Wed, 18 Jan 2023 13:05:31 +0100 Subject: [PATCH] Added API to remap non-existing items to stone --- gradle.properties | 2 +- ...mRewriter.java => LegacyItemRewriter.java} | 126 ++++++++++----- .../Protocol1_7_2_5to1_6_4.java | 42 ++--- .../rewriter/ItemRewriter.java | 70 ++++---- .../Protocol1_8to1_7_6_10.java | 50 ++++-- .../metadata/MetadataRewriter.java | 10 +- .../model/HologramPartEntity.java | 3 +- .../rewriter/ChatItemRewriter.java | 150 +++++++++--------- .../rewriter/ItemRewriter.java | 57 ++++--- 9 files changed, 294 insertions(+), 216 deletions(-) rename src/main/java/net/raphimc/vialegacy/api/remapper/{AbstractItemRewriter.java => LegacyItemRewriter.java} (54%) diff --git a/gradle.properties b/gradle.properties index b6bc913..70401e1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,4 +6,4 @@ org.gradle.configureondemand=true # Project properties maven_name=ViaLegacy maven_group=net.raphimc -maven_version=2.2.2 +maven_version=2.2.3 diff --git a/src/main/java/net/raphimc/vialegacy/api/remapper/AbstractItemRewriter.java b/src/main/java/net/raphimc/vialegacy/api/remapper/LegacyItemRewriter.java similarity index 54% rename from src/main/java/net/raphimc/vialegacy/api/remapper/AbstractItemRewriter.java rename to src/main/java/net/raphimc/vialegacy/api/remapper/LegacyItemRewriter.java index 7651af3..28e608e 100644 --- a/src/main/java/net/raphimc/vialegacy/api/remapper/AbstractItemRewriter.java +++ b/src/main/java/net/raphimc/vialegacy/api/remapper/LegacyItemRewriter.java @@ -18,63 +18,115 @@ package net.raphimc.vialegacy.api.remapper; import com.viaversion.viaversion.api.minecraft.item.Item; +import com.viaversion.viaversion.api.protocol.Protocol; +import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; +import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.rewriter.ItemRewriter; +import com.viaversion.viaversion.api.rewriter.RewriterBase; +import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.libs.fastutil.ints.IntArrayList; +import com.viaversion.viaversion.libs.fastutil.ints.IntList; import com.viaversion.viaversion.libs.fastutil.objects.ObjectArrayList; import com.viaversion.viaversion.libs.fastutil.objects.ObjectList; -import com.viaversion.viaversion.libs.gson.JsonObject; import com.viaversion.viaversion.libs.opennbt.tag.builtin.*; import java.util.List; -public abstract class AbstractItemRewriter { +public abstract class LegacyItemRewriter

extends RewriterBase

implements ItemRewriter

{ - private final ObjectList REWRITE_ENTRIES = new ObjectArrayList<>(); - protected final String HACK_TAG_NAME; + private final ObjectList rewriteEntries = new ObjectArrayList<>(); + private final IntList nonExistentItems = new IntArrayList(); + protected final String tagName; protected final String protocolName; - private final boolean jsonName; - public AbstractItemRewriter(final String protocolName, final boolean jsonName) { - this.HACK_TAG_NAME = protocolName.replace(".", "_") + "_ProtocolHack_" + System.currentTimeMillis(); + public LegacyItemRewriter(final P protocol, final String protocolName) { + super(protocol); + this.tagName = protocolName.replace(".", "_") + "_ProtocolHack_" + System.currentTimeMillis(); this.protocolName = protocolName; - this.jsonName = jsonName; } - protected void registerRemappedItem(final int oldItemId, final int newItemId, final String newItemName) { - registerRemappedItem(oldItemId, newItemId, -1, newItemName); + protected void addRemappedItem(final int oldItemId, final int newItemId, final String newItemName) { + this.addRemappedItem(oldItemId, newItemId, -1, newItemName); } - protected void registerRemappedItem(final int oldItemId, final int newItemId, final int newItemMeta, final String newItemName) { - registerRemappedItem(oldItemId, -1, newItemId, newItemMeta, newItemName); + protected void addRemappedItem(final int oldItemId, final int newItemId, final int newItemMeta, final String newItemName) { + this.addRemappedItem(oldItemId, -1, newItemId, newItemMeta, newItemName); } - protected void registerRemappedItem(final int oldItemId, final int oldItemMeta, final int newItemId, final int newItemMeta, final String newItemName) { - REWRITE_ENTRIES.add(new RewriteEntry(oldItemId, (short) oldItemMeta, newItemId, (short) newItemMeta, newItemName)); + protected void addRemappedItem(final int oldItemId, final int oldItemMeta, final int newItemId, final int newItemMeta, final String newItemName) { + this.rewriteEntries.add(new RewriteEntry(oldItemId, (short) oldItemMeta, newItemId, (short) newItemMeta, newItemName)); } - public void rewriteRead(final Item item) { - if (item == null) return; + protected void addNonExistentItems(final int... itemIds) { + for (int itemId : itemIds) { + this.nonExistentItems.add(itemId); + } + } - for (RewriteEntry rewriteEntry : REWRITE_ENTRIES) { + protected void addNonExistentItemRange(final int startItemId, final int endItemId) { + for (int i = startItemId; i <= endItemId; i++) { + this.nonExistentItems.add(i); + } + } + + + public void registerCreativeInventoryAction(final ServerboundPacketType packetType, final Type type) { + this.protocol.registerServerbound(packetType, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.SHORT); // slot + map(type); // item + handler(itemToServerHandler(type)); + } + }); + } + + @Override + public Item handleItemToClient(final Item item) { + if (item == null) return null; + + for (RewriteEntry rewriteEntry : this.rewriteEntries) { if (rewriteEntry.rewrites(item)) { - setRemappedNameRead(item, rewriteEntry.newItemName); + this.setRemappedNameRead(item, rewriteEntry.newItemName); if (rewriteEntry.newItemMeta != -1) { item.setData(rewriteEntry.newItemMeta); } item.setIdentifier(rewriteEntry.newItemID); } } + + return item; } - public void rewriteWrite(final Item item) { - if (item == null) return; + @Override + public Item handleItemToServer(final Item item) { + if (item == null) return null; - setRemappedTagWrite(item); + if (this.nonExistentItems.contains(item.identifier())) { + item.setIdentifier(1); + item.setData((short) 0); + return item; + } + + this.setRemappedTagWrite(item); + + return item; } + private PacketHandler itemToClientHandler(Type type) { + return wrapper -> handleItemToClient(wrapper.get(type, 0)); + } + + private PacketHandler itemToServerHandler(Type type) { + return wrapper -> handleItemToServer(wrapper.get(type, 0)); + } + private void setRemappedNameRead(final Item item, final String name) { //Set protocol hack tag for later remapping - CompoundTag protocolHackTag = (item.tag() != null && item.tag().contains(HACK_TAG_NAME) ? item.tag().get(HACK_TAG_NAME) : new CompoundTag()); - if (item.tag() == null || !item.tag().contains(HACK_TAG_NAME)) { + final CompoundTag protocolHackTag = (item.tag() != null && item.tag().contains(tagName) ? item.tag().get(tagName) : new CompoundTag()); + if (item.tag() == null || !item.tag().contains(tagName)) { protocolHackTag.put("Id", new IntTag(item.identifier())); protocolHackTag.put("Meta", new ShortTag(item.data())); } @@ -86,7 +138,7 @@ public abstract class AbstractItemRewriter { item.setTag(tag); protocolHackTag.put("RemoveTag", new IntTag(0)); } - tag.put(HACK_TAG_NAME, protocolHackTag); + tag.put(tagName, protocolHackTag); //Set name/lore of item CompoundTag display = tag.get("display"); @@ -102,29 +154,21 @@ public abstract class AbstractItemRewriter { display.put("Lore", lore); protocolHackTag.put("RemoveLore", new IntTag(0)); } - if (this.jsonName) { - lore.add(new StringTag(this.messageToJson("§r " + this.protocolName + " Item ID: " + item.identifier() + " (" + name + ")").toString())); - } else { - lore.add(new StringTag("§r " + this.protocolName + " Item ID: " + item.identifier() + " (" + name + ")")); - } + lore.add(new StringTag("§r " + this.protocolName + " Item ID: " + item.identifier() + " (" + name + ")")); protocolHackTag.put("RemoveLastLore", new IntTag(0)); } else { - if (this.jsonName) { - display.put("Name", new StringTag(this.messageToJson("§r" + this.protocolName + " " + name).toString())); - } else { - display.put("Name", new StringTag("§r" + this.protocolName + " " + name)); - } + display.put("Name", new StringTag("§r" + this.protocolName + " " + name)); protocolHackTag.put("RemoveDisplayName", new IntTag(0)); } } private void setRemappedTagWrite(final Item item) { if (item.tag() == null) return; - if (!item.tag().contains(HACK_TAG_NAME)) return; + if (!item.tag().contains(tagName)) return; - CompoundTag tag = item.tag(); - CompoundTag protocolHackTag = tag.get(HACK_TAG_NAME); - tag.remove(HACK_TAG_NAME); + final CompoundTag tag = item.tag(); + final CompoundTag protocolHackTag = tag.get(tagName); + tag.remove(tagName); item.setIdentifier(((IntTag) protocolHackTag.get("Id")).asInt()); item.setData(((ShortTag) protocolHackTag.get("Meta")).asShort()); @@ -148,12 +192,6 @@ public abstract class AbstractItemRewriter { } } - private JsonObject messageToJson(final String s) { - JsonObject ob = new JsonObject(); - ob.addProperty("text", s); - return ob; - } - public static class RewriteEntry { diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_7_2_5to1_6_4/Protocol1_7_2_5to1_6_4.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_7_2_5to1_6_4/Protocol1_7_2_5to1_6_4.java index cd22962..e03d58a 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_7_2_5to1_6_4/Protocol1_7_2_5to1_6_4.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_7_2_5to1_6_4/Protocol1_7_2_5to1_6_4.java @@ -48,6 +48,7 @@ import io.netty.channel.ChannelOutboundHandlerAdapter; import io.netty.channel.ChannelPromise; import net.raphimc.vialegacy.ViaLegacy; import net.raphimc.vialegacy.api.model.IdAndData; +import net.raphimc.vialegacy.api.remapper.LegacyItemRewriter; import net.raphimc.vialegacy.api.splitter.PreNettySplitter; import net.raphimc.vialegacy.protocols.release.protocol1_7_2_5to1_6_4.providers.EncryptionProvider; import net.raphimc.vialegacy.protocols.release.protocol1_7_2_5to1_6_4.rewriter.*; @@ -62,13 +63,14 @@ import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Chunk import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkBulk1_7_6Type; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.MetaType1_7_6; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Types1_7_6; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.List; import java.util.logging.Level; public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol { - public static ItemRewriter ITEM_REWRITER = new ItemRewriter(); + private final LegacyItemRewriter itemRewriter = new ItemRewriter(this); public Protocol1_7_2_5to1_6_4() { super(ClientboundPackets1_6_4.class, ClientboundPackets1_7_2.class, ServerboundPackets1_6_4.class, ServerboundPackets1_7_2.class); @@ -76,6 +78,8 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol ITEM_REWRITER.rewriteRead(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0))); + handler(wrapper -> itemRewriter.handleItemToClient(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0))); } }); this.registerClientbound(ClientboundPackets1_6_4.RESPAWN, new PacketRemapper() { @@ -392,7 +396,7 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol { final Item currentItem = new DataItem(wrapper.read(Type.UNSIGNED_SHORT), (byte) 1, (short) 0, null); // item - ITEM_REWRITER.rewriteRead(currentItem); + itemRewriter.handleItemToClient(currentItem); wrapper.write(Type.SHORT, (short) currentItem.identifier()); }); map(Types1_6_4.METADATA_LIST, Types1_7_6.METADATA_LIST); // metadata @@ -726,7 +730,7 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol ITEM_REWRITER.rewriteRead(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0))); + handler(wrapper -> itemRewriter.handleItemToClient(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0))); } }); this.registerClientbound(ClientboundPackets1_6_4.WINDOW_ITEMS, new PacketRemapper() { @@ -736,7 +740,7 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol { final Item[] items = wrapper.passthrough(Types1_7_6.COMPRESSED_ITEM_ARRAY); // items for (Item item : items) { - ITEM_REWRITER.rewriteRead(item); + itemRewriter.handleItemToClient(item); } }); } @@ -867,10 +871,10 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol ITEM_REWRITER.rewriteWrite(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0))); + handler(wrapper -> itemRewriter.handleItemToServer(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0))); map(Type.UNSIGNED_BYTE); // offset x map(Type.UNSIGNED_BYTE); // offset y map(Type.UNSIGNED_BYTE); // offset z @@ -1027,15 +1031,7 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol ITEM_REWRITER.rewriteWrite(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0))); - } - }); - this.registerServerbound(ServerboundPackets1_7_2.CREATIVE_INVENTORY_ACTION, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.SHORT); // slot - map(Types1_7_6.COMPRESSED_ITEM); // item - handler(wrapper -> ITEM_REWRITER.rewriteWrite(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0))); + handler(wrapper -> itemRewriter.handleItemToServer(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0))); } }); this.registerServerbound(ServerboundPackets1_7_2.UPDATE_SIGN, new PacketRemapper() { @@ -1127,7 +1123,7 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol metadataList) { for (Metadata metadata : metadataList) { if (metadata.metaType().equals(MetaType1_6_4.Slot)) { - ITEM_REWRITER.rewriteRead(metadata.value()); + itemRewriter.handleItemToClient(metadata.value()); } metadata.setMetaType(MetaType1_7_6.byId(metadata.metaType().typeId())); } @@ -1208,4 +1204,10 @@ public class Protocol1_7_2_5to1_6_4 extends AbstractProtocol getItemRewriter() { + return this.itemRewriter; + } + } diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_7_2_5to1_6_4/rewriter/ItemRewriter.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_7_2_5to1_6_4/rewriter/ItemRewriter.java index f1e89bc..27c71fc 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_7_2_5to1_6_4/rewriter/ItemRewriter.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_7_2_5to1_6_4/rewriter/ItemRewriter.java @@ -17,39 +17,47 @@ */ package net.raphimc.vialegacy.protocols.release.protocol1_7_2_5to1_6_4.rewriter; -import net.raphimc.vialegacy.api.remapper.AbstractItemRewriter; +import net.raphimc.vialegacy.api.remapper.LegacyItemRewriter; +import net.raphimc.vialegacy.protocols.release.protocol1_7_2_5to1_6_4.Protocol1_7_2_5to1_6_4; +import net.raphimc.vialegacy.protocols.release.protocol1_7_6_10to1_7_2_5.ServerboundPackets1_7_2; +import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Types1_7_6; -public class ItemRewriter extends AbstractItemRewriter { +public class ItemRewriter extends LegacyItemRewriter { - public ItemRewriter() { - super("1.6.4", false); - registerRemappedItem(26, 355, "Bed Block"); - registerRemappedItem(34, 33, "Piston Head"); - registerRemappedItem(36, 33, "Piston Moving"); - registerRemappedItem(55, 331, "Redstone Wire"); - registerRemappedItem(59, 295, "Wheat Crops"); - registerRemappedItem(63, 323, "Standing Sign"); - registerRemappedItem(64, 324, "Oak Door Block"); - registerRemappedItem(68, 323, "Wall Sign"); - registerRemappedItem(71, 330, "Iron Door Block"); - registerRemappedItem(74, 73, "Lit Redstone Ore"); - registerRemappedItem(75, 76, "Unlit Redstone Torch"); - registerRemappedItem(83, 338, "Sugar Cane Block"); - registerRemappedItem(92, 354, "Cake Block"); - registerRemappedItem(93, 356, "Unlit Redstone Repeater"); - registerRemappedItem(94, 356, "Lit Redstone Repeater"); - registerRemappedItem(95, 146, "Locked Chest"); - registerRemappedItem(104, 361, "Pumpkin Stem"); - registerRemappedItem(105, 362, "Melon Stem"); - registerRemappedItem(115, 372, "Nether Wart Block"); - registerRemappedItem(117, 379, "Brewing Stand Block"); - registerRemappedItem(118, 380, "Cauldron Block"); - registerRemappedItem(124, 123, "Lit Redstone Lamp"); - registerRemappedItem(132, 287, "Tripwire"); - registerRemappedItem(140, 390, "Flower Pot"); - registerRemappedItem(144, 397, "Undefined Mob Head"); - registerRemappedItem(149, 404, "Unlit Redstone Comparator"); - registerRemappedItem(150, 404, "Lit Redstone Comparator"); + public ItemRewriter(final Protocol1_7_2_5to1_6_4 protocol) { + super(protocol, "1.6.4"); + addRemappedItem(26, 355, "Bed Block"); + addRemappedItem(34, 33, "Piston Head"); + addRemappedItem(36, 33, "Piston Moving"); + addRemappedItem(55, 331, "Redstone Wire"); + addRemappedItem(59, 295, "Wheat Crops"); + addRemappedItem(63, 323, "Standing Sign"); + addRemappedItem(64, 324, "Oak Door Block"); + addRemappedItem(68, 323, "Wall Sign"); + addRemappedItem(71, 330, "Iron Door Block"); + addRemappedItem(74, 73, "Lit Redstone Ore"); + addRemappedItem(75, 76, "Unlit Redstone Torch"); + addRemappedItem(83, 338, "Sugar Cane Block"); + addRemappedItem(92, 354, "Cake Block"); + addRemappedItem(93, 356, "Unlit Redstone Repeater"); + addRemappedItem(94, 356, "Lit Redstone Repeater"); + addRemappedItem(95, 146, "Locked Chest"); + addRemappedItem(104, 361, "Pumpkin Stem"); + addRemappedItem(105, 362, "Melon Stem"); + addRemappedItem(115, 372, "Nether Wart Block"); + addRemappedItem(117, 379, "Brewing Stand Block"); + addRemappedItem(118, 380, "Cauldron Block"); + addRemappedItem(124, 123, "Lit Redstone Lamp"); + addRemappedItem(132, 287, "Tripwire"); + addRemappedItem(140, 390, "Flower Pot"); + addRemappedItem(144, 397, "Undefined Mob Head"); + addRemappedItem(149, 404, "Unlit Redstone Comparator"); + addRemappedItem(150, 404, "Lit Redstone Comparator"); + } + + @Override + protected void registerPackets() { + this.registerCreativeInventoryAction(ServerboundPackets1_7_2.CREATIVE_INVENTORY_ACTION, Types1_7_6.COMPRESSED_ITEM); } } diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/Protocol1_8to1_7_6_10.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/Protocol1_8to1_7_6_10.java index aafda64..72086b5 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/Protocol1_8to1_7_6_10.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/Protocol1_8to1_7_6_10.java @@ -49,6 +49,7 @@ import io.netty.buffer.Unpooled; import net.raphimc.vialegacy.ViaLegacy; import net.raphimc.vialegacy.api.data.ItemList1_6; import net.raphimc.vialegacy.api.model.IdAndData; +import net.raphimc.vialegacy.api.remapper.LegacyItemRewriter; import net.raphimc.vialegacy.protocols.release.protocol1_7_6_10to1_7_2_5.ClientboundPackets1_7_2; import net.raphimc.vialegacy.protocols.release.protocol1_7_6_10to1_7_2_5.ServerboundPackets1_7_2; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.data.Particle; @@ -65,13 +66,16 @@ import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.storage.*; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Chunk1_7_6Type; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkBulk1_7_6Type; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Types1_7_6; +import org.checkerframework.checker.nullness.qual.Nullable; import java.nio.charset.StandardCharsets; import java.util.*; public class Protocol1_8to1_7_6_10 extends AbstractProtocol { - public static final ItemRewriter ITEM_REWRITER = new ItemRewriter(); + private final LegacyItemRewriter itemRewriter = new ItemRewriter(this); + private final ChatItemRewriter chatItemRewriter = new ChatItemRewriter(this); + private final MetadataRewriter metadataRewriter = new MetadataRewriter(this); public Protocol1_8to1_7_6_10() { super(ClientboundPackets1_7_2.class, ClientboundPackets1_8.class, ServerboundPackets1_7_2.class, ServerboundPackets1_8.class); @@ -97,6 +101,8 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol TranslationRewriter.toClient(ChatItemRewriter.toClient(msg))); // message + map(Type.STRING, Type.STRING, msg -> TranslationRewriter.toClient(chatItemRewriter.remapShowItem(msg))); // message create(Type.BYTE, (byte) 0); // position } }); @@ -150,7 +156,7 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol ITEM_REWRITER.rewriteRead(wrapper.get(Type.ITEM, 0))); + handler(wrapper -> itemRewriter.handleItemToClient(wrapper.get(Type.ITEM, 0))); } }); this.registerClientbound(ClientboundPackets1_7_2.SPAWN_POSITION, new PacketRemapper() { @@ -233,11 +239,11 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol metadata = wrapper.read(Types1_7_6.METADATA_LIST); // metadata - MetadataRewriter.transform(Entity1_10Types.EntityType.PLAYER, metadata); + metadataRewriter.transform(Entity1_10Types.EntityType.PLAYER, metadata); wrapper.write(Types1_8.METADATA_LIST, metadata); tablistStorage.sendTempEntry(tempTabEntry); @@ -352,7 +358,7 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol 2) damage = Integer.parseInt(parts[2]); final DataItem item = new DataItem(id, (byte) 1, (short) damage, null); - ITEM_REWRITER.rewriteRead(item); + itemRewriter.handleItemToClient(item); wrapper.write(Type.VAR_INT, item.identifier()); // particle data if (item.data() != 0) wrapper.write(Type.VAR_INT, (int) item.data()); // particle data @@ -922,7 +928,7 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol ITEM_REWRITER.rewriteRead(wrapper.get(Type.ITEM, 0))); + handler(wrapper -> itemRewriter.handleItemToClient(wrapper.get(Type.ITEM, 0))); } }); this.registerClientbound(ClientboundPackets1_7_2.WINDOW_ITEMS, new PacketRemapper() { @@ -940,7 +946,7 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol { final short direction = wrapper.get(Type.UNSIGNED_BYTE, 0); final Item item = wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0); - ITEM_REWRITER.rewriteWrite(item); + itemRewriter.handleItemToServer(item); if (item != null && item.identifier() == ItemList1_6.writtenBook.itemID && direction == 255) { // If placed item is a book then cancel it and send a MC|BOpen to the client final PacketWrapper openBook = PacketWrapper.create(ClientboundPackets1_8.PLUGIN_MESSAGE, wrapper.user()); @@ -1349,7 +1355,7 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol ITEM_REWRITER.rewriteWrite(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0))); + handler(wrapper -> itemRewriter.handleItemToServer(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0))); } }); this.registerServerbound(ServerboundPackets1_8.CREATIVE_INVENTORY_ACTION, new PacketRemapper() { @@ -1357,7 +1363,7 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol ITEM_REWRITER.rewriteWrite(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0))); + handler(wrapper -> itemRewriter.handleItemToServer(wrapper.get(Types1_7_6.COMPRESSED_ITEM, 0))); } }); this.registerServerbound(ServerboundPackets1_8.UPDATE_SIGN, new PacketRemapper() { @@ -1419,7 +1425,7 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol getItemRewriter() { + return this.itemRewriter; + } + + public MetadataRewriter getMetadataRewriter() { + return this.metadataRewriter; + } + } diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/metadata/MetadataRewriter.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/metadata/MetadataRewriter.java index 0310906..6a273ee 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/metadata/MetadataRewriter.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/metadata/MetadataRewriter.java @@ -29,7 +29,13 @@ import java.util.logging.Level; public class MetadataRewriter { - public static void transform(Entity1_10Types.EntityType type, List list) { + private final Protocol1_8to1_7_6_10 protocol; + + public MetadataRewriter(final Protocol1_8to1_7_6_10 protocol) { + this.protocol = protocol; + } + + public void transform(Entity1_10Types.EntityType type, List list) { for (Metadata entry : new ArrayList<>(list)) { final MetaIndex1_8to1_7_6 metaIndex = MetaIndex1_8to1_7_6.searchIndex(type, entry.id()); try { @@ -85,7 +91,7 @@ public class MetadataRewriter { entry.setValue(((Number) value).floatValue()); break; case Slot: - Protocol1_8to1_7_6_10.ITEM_REWRITER.rewriteRead((Item) value); + this.protocol.getItemRewriter().handleItemToClient((Item) value); break; case String: case Position: diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/model/HologramPartEntity.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/model/HologramPartEntity.java index 232f997..1fc4718 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/model/HologramPartEntity.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/model/HologramPartEntity.java @@ -27,7 +27,6 @@ import com.viaversion.viaversion.protocols.protocol1_8.ClientboundPackets1_8; import net.raphimc.vialegacy.api.model.Location; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.Protocol1_8to1_7_6_10; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.metadata.MetaIndex1_8to1_7_6; -import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.metadata.MetadataRewriter; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.storage.EntityTracker; import java.util.*; @@ -306,7 +305,7 @@ public class HologramPartEntity { for (final Map.Entry entry : this.metadata.entrySet()) { metadataList.add(new Metadata(entry.getKey().getOldIndex(), entry.getKey().getOldType(), entry.getValue())); } - MetadataRewriter.transform(this.entityType, metadataList); + this.user.getProtocolInfo().getPipeline().getProtocol(Protocol1_8to1_7_6_10.class).getMetadataRewriter().transform(this.entityType, metadataList); return metadataList; } diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/rewriter/ChatItemRewriter.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/rewriter/ChatItemRewriter.java index e007bac..c67cd82 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/rewriter/ChatItemRewriter.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/rewriter/ChatItemRewriter.java @@ -35,7 +35,7 @@ import net.raphimc.vialegacy.util.ViaStringTagReader1_11_2; import java.io.IOException; -public class ChatItemRewriter extends ComponentRewriter { +public class ChatItemRewriter { private static final Int2ObjectOpenHashMap ID_TO_NAME = new Int2ObjectOpenHashMap<>(315, 0.99F); @@ -357,89 +357,93 @@ public class ChatItemRewriter extends ComponentRewriter { ID_TO_NAME.put(2267, "record_wait"); } - private static final ComponentRewriter REMAP_SHOW_ITEM = new ComponentRewriter() { - @Override - protected void handleHoverEvent(JsonObject hoverEvent) { - super.handleHoverEvent(hoverEvent); - final String action = hoverEvent.getAsJsonPrimitive("action").getAsString(); - if (!action.equals("show_item")) return; + private final ComponentRewriter SHOW_ITEM; - final JsonElement value = hoverEvent.get("value"); - if (value == null) return; + public ChatItemRewriter(final Protocol1_8to1_7_6_10 protocol) { + this.SHOW_ITEM = new ComponentRewriter(protocol) { + @Override + protected void handleHoverEvent(JsonObject hoverEvent) { + super.handleHoverEvent(hoverEvent); + final String action = hoverEvent.getAsJsonPrimitive("action").getAsString(); + if (!action.equals("show_item")) return; - final String text = findItemNBT(value); - if (text == null) return; + final JsonElement value = hoverEvent.get("value"); + if (value == null) return; - final CompoundTag tag; - try { - tag = ViaStringTagReader1_11_2.getTagFromJson(text); - } catch (Exception e) { - ViaLegacy.getPlatform().getLogger().warning("Error reading NBT in show_item:" + text); - throw new RuntimeException(e); + final String text = findItemNBT(value); + if (text == null) return; + + final CompoundTag tag; + try { + tag = ViaStringTagReader1_11_2.getTagFromJson(text); + } catch (Exception e) { + ViaLegacy.getPlatform().getLogger().warning("Error reading NBT in show_item:" + text); + throw new RuntimeException(e); + } + + final CompoundTag itemTag = tag.get("tag"); + final ShortTag idTag = tag.get("id"); + final ShortTag damageTag = tag.get("Damage"); + + // Call item converter + final short damage = damageTag != null ? damageTag.asShort() : 0; + final short id = idTag != null ? idTag.asShort() : 1; + final Item item = new DataItem(); + item.setIdentifier(id); + item.setData(damage); + item.setTag(itemTag); + handleItem(item); + + // Serialize again + if (damage != item.data()) { + tag.put("Damage", new ShortTag(item.data())); + } + tag.put("id", new StringTag("minecraft:" + ID_TO_NAME.getOrDefault(item.identifier(), "stone"))); + if (item.tag() != null) { + tag.put("tag", new CompoundTag(item.tag().getValue())); + } + + final JsonArray array = new JsonArray(); + final JsonObject object = new JsonObject(); + array.add(object); + final String serializedNBT; + try { + serializedNBT = BinaryTagIO.writeString(tag); + object.addProperty("text", serializedNBT); + hoverEvent.add("value", array); + } catch (IOException e) { + ViaLegacy.getPlatform().getLogger().warning("Error writing NBT in show_item:" + text); + e.printStackTrace(); + } } - final CompoundTag itemTag = tag.get("tag"); - final ShortTag idTag = tag.get("id"); - final ShortTag damageTag = tag.get("Damage"); - - // Call item converter - final short damage = damageTag != null ? damageTag.asShort() : 0; - final short id = idTag != null ? idTag.asShort() : 1; - final Item item = new DataItem(); - item.setIdentifier(id); - item.setData(damage); - item.setTag(itemTag); - handleItem(item); - - // Serialize again - if (damage != item.data()) { - tag.put("Damage", new ShortTag(item.data())); - } - tag.put("id", new StringTag("minecraft:" + ID_TO_NAME.getOrDefault(item.identifier(), "stone"))); - if (item.tag() != null) { - tag.put("tag", new CompoundTag(item.tag().getValue())); + private void handleItem(Item item) { + this.protocol.getItemRewriter().handleItemToClient(item); } - final JsonArray array = new JsonArray(); - final JsonObject object = new JsonObject(); - array.add(object); - final String serializedNBT; - try { - serializedNBT = BinaryTagIO.writeString(tag); - object.addProperty("text", serializedNBT); - hoverEvent.add("value", array); - } catch (IOException e) { - ViaLegacy.getPlatform().getLogger().warning("Error writing NBT in show_item:" + text); - e.printStackTrace(); - } - } - - private void handleItem(Item item) { - Protocol1_8to1_7_6_10.ITEM_REWRITER.rewriteRead(item); - } - - private String findItemNBT(JsonElement element) { - if (element.isJsonArray()) { - for (JsonElement jsonElement : element.getAsJsonArray()) { - String value = findItemNBT(jsonElement); - if (value != null) { - return value; + private String findItemNBT(JsonElement element) { + if (element.isJsonArray()) { + for (JsonElement jsonElement : element.getAsJsonArray()) { + String value = findItemNBT(jsonElement); + if (value != null) { + return value; + } } + } else if (element.isJsonObject()) { + final JsonPrimitive text = element.getAsJsonObject().getAsJsonPrimitive("text"); + if (text != null) { + return text.getAsString(); + } + } else if (element.isJsonPrimitive()) { + return element.getAsJsonPrimitive().getAsString(); } - } else if (element.isJsonObject()) { - final JsonPrimitive text = element.getAsJsonObject().getAsJsonPrimitive("text"); - if (text != null) { - return text.getAsString(); - } - } else if (element.isJsonPrimitive()) { - return element.getAsJsonPrimitive().getAsString(); + return null; } - return null; - } - }; + }; + } - public static String toClient(final String text) { - return REMAP_SHOW_ITEM.processText(text).toString(); + public String remapShowItem(final String text) { + return SHOW_ITEM.processText(text).toString(); } } diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/rewriter/ItemRewriter.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/rewriter/ItemRewriter.java index 887f710..7133de8 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/rewriter/ItemRewriter.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/rewriter/ItemRewriter.java @@ -23,38 +23,41 @@ import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag; import net.raphimc.vialegacy.ViaLegacy; import net.raphimc.vialegacy.api.data.ItemList1_6; -import net.raphimc.vialegacy.api.remapper.AbstractItemRewriter; +import net.raphimc.vialegacy.api.remapper.LegacyItemRewriter; import net.raphimc.vialegacy.protocols.release.protocol1_7_6_10to1_7_2_5.Protocol1_7_6_10to1_7_2_5; +import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.Protocol1_8to1_7_6_10; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.model.GameProfile; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.providers.GameProfileFetcher; import java.util.UUID; -public class ItemRewriter extends AbstractItemRewriter { +public class ItemRewriter extends LegacyItemRewriter { - public ItemRewriter() { - super("1.7", false); - registerRemappedItem(8, 326, "Water Block"); - registerRemappedItem(9, 326, "Stationary Water Block"); - registerRemappedItem(10, 327, "Lava Block"); - registerRemappedItem(11, 327, "Stationary Lava Block"); - registerRemappedItem(51, 385, "Fire"); - registerRemappedItem(90, 399, "Nether portal"); - registerRemappedItem(119, 381, "End portal"); - registerRemappedItem(127, 351, 3, "Cocoa Block"); - registerRemappedItem(141, 391, "Carrot Crops"); - registerRemappedItem(142, 392, "Potato Crops"); - registerRemappedItem(43, 44, "Double Stone Slab"); - registerRemappedItem(125, 126, "Double Wood Slab"); + public ItemRewriter(final Protocol1_8to1_7_6_10 protocol) { + super(protocol, "1.7"); + addRemappedItem(8, 326, "Water Block"); + addRemappedItem(9, 326, "Stationary Water Block"); + addRemappedItem(10, 327, "Lava Block"); + addRemappedItem(11, 327, "Stationary Lava Block"); + addRemappedItem(51, 385, "Fire"); + addRemappedItem(90, 399, "Nether portal"); + addRemappedItem(119, 381, "End portal"); + addRemappedItem(127, 351, 3, "Cocoa Block"); + addRemappedItem(141, 391, "Carrot Crops"); + addRemappedItem(142, 392, "Potato Crops"); + addRemappedItem(43, 44, "Double Stone Slab"); + addRemappedItem(125, 126, "Double Wood Slab"); + + addNonExistentItems(2); } @Override - public void rewriteRead(Item item) { - super.rewriteRead(item); - if (item == null) return; + public Item handleItemToClient(Item item) { + super.handleItemToClient(item); + if (item == null) return null; if (item.identifier() == ItemList1_6.skull.itemID && item.data() == 3 && item.tag() != null) { // player_skull - if (!item.tag().contains("SkullOwner")) return; + if (!item.tag().contains("SkullOwner")) return item; String skullOwnerName = null; if (item.tag().get("SkullOwner") instanceof StringTag) { @@ -71,26 +74,28 @@ public class ItemRewriter extends AbstractItemRewriter { if (skullOwnerName != null) { final GameProfileFetcher gameProfileFetcher = Via.getManager().getProviders().get(GameProfileFetcher.class); - if (!ViaLegacy.getConfig().isLegacySkullLoading()) return; + if (!ViaLegacy.getConfig().isLegacySkullLoading()) return item; if (gameProfileFetcher.isUUIDLoaded(skullOwnerName)) { final UUID uuid = gameProfileFetcher.getMojangUUID(skullOwnerName); if (gameProfileFetcher.isGameProfileLoaded(uuid)) { final GameProfile skullProfile = gameProfileFetcher.getGameProfile(uuid); - if (skullProfile == null || skullProfile.isOffline()) return; + if (skullProfile == null || skullProfile.isOffline()) return item; item.tag().put("SkullOwner", Protocol1_7_6_10to1_7_2_5.writeGameProfileToTag(skullProfile)); - return; + return item; } } gameProfileFetcher.getMojangUUIDAsync(skullOwnerName).thenAccept(gameProfileFetcher::getGameProfile); } } + + return item; } @Override - public void rewriteWrite(Item item) { - if (item == null) return; + public Item handleItemToServer(Item item) { + if (item == null) return null; NOT_VALID: if (item.identifier() == ItemList1_6.skull.itemID && item.data() == 3 && item.tag() != null) { // player_skull @@ -100,7 +105,7 @@ public class ItemRewriter extends AbstractItemRewriter { } } - super.rewriteWrite(item); + return super.handleItemToServer(item); } }