From 5edb6ff411366c59c56b5143ebaba286eccc5580 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Thu, 23 May 2024 12:16:49 +0200 Subject: [PATCH] Handle jukebox songs --- .../data/StructuredDataContainer.java | 1 - .../minecraft/item/data/JukeboxPlayable.java | 8 +++++ .../minecraft/item/data/PotDecorations.java | 1 - .../com/viaversion/viaversion/util/Key.java | 2 +- .../v1_20_5to1_21/Protocol1_20_5To1_21.java | 15 +++++--- ...{MappingData.java => MappingData1_21.java} | 19 ++++++++-- .../rewriter/BlockItemPacketRewriter1_21.java | 34 +++++++++++++++++- .../rewriter/EntityPacketRewriter1_21.java | 5 +++ .../viaversion/rewriter/BlockRewriter.java | 32 +++++++++-------- .../viaversion/data/jukebox-songs-1.21.nbt | Bin 0 -> 2473 bytes .../rewriter/BlockItemPacketRewriter1_99.java | 2 +- 11 files changed, 93 insertions(+), 26 deletions(-) rename common/src/main/java/com/viaversion/viaversion/protocols/v1_20_5to1_21/data/{MappingData.java => MappingData1_21.java} (67%) create mode 100644 common/src/main/resources/assets/viaversion/data/jukebox-songs-1.21.nbt diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredDataContainer.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredDataContainer.java index 898f40ab2..f3d2acac4 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredDataContainer.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredDataContainer.java @@ -165,7 +165,6 @@ public final class StructuredDataContainer { for (final StructuredData data : data.values()) { final int mappedId = rewriter.applyAsInt(data.id()); if (mappedId == -1) { - protocol.getLogger().warning("Could not find item data serializer for id " + data.id()); continue; } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/JukeboxPlayable.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/JukeboxPlayable.java index 8c47ba650..e3a848a95 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/JukeboxPlayable.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/JukeboxPlayable.java @@ -33,6 +33,14 @@ import io.netty.buffer.ByteBuf; public record JukeboxPlayable(Either, String> song, boolean showInTooltip) { + public JukeboxPlayable(final Holder song, final boolean showInTooltip) { + this(Either.left(song), showInTooltip); + } + + public JukeboxPlayable(final String resourceKey, final boolean showInTooltip) { + this(Either.right(resourceKey), showInTooltip); + } + public static final Type TYPE = new Type<>(JukeboxPlayable.class) { @Override public JukeboxPlayable read(final ByteBuf buffer) { diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotDecorations.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotDecorations.java index 3ee279c1a..8b610e978 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotDecorations.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotDecorations.java @@ -22,7 +22,6 @@ */ package com.viaversion.viaversion.api.minecraft.item.data; -import com.viaversion.viaversion.api.data.MappingData; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Types; import io.netty.buffer.ByteBuf; diff --git a/api/src/main/java/com/viaversion/viaversion/util/Key.java b/api/src/main/java/com/viaversion/viaversion/util/Key.java index 82af5b62c..0434f853c 100644 --- a/api/src/main/java/com/viaversion/viaversion/util/Key.java +++ b/api/src/main/java/com/viaversion/viaversion/util/Key.java @@ -47,7 +47,7 @@ public final class Key { } public static boolean equals(final String firstIdentifier, final String secondIdentifier) { - return stripNamespace(firstIdentifier).equals(stripNamespace(secondIdentifier)); + return stripMinecraftNamespace(firstIdentifier).equals(stripMinecraftNamespace(secondIdentifier)); } public static String namespaced(final String identifier) { diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_5to1_21/Protocol1_20_5To1_21.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_5to1_21/Protocol1_20_5To1_21.java index 20d4a73d6..c9cfeff96 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_5to1_21/Protocol1_20_5To1_21.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_5to1_21/Protocol1_20_5To1_21.java @@ -34,7 +34,7 @@ import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.packet.ServerboundCon import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.packet.ServerboundPacket1_20_5; import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.packet.ServerboundPackets1_20_5; import com.viaversion.viaversion.protocols.v1_20_5to1_21.data.AttributeModifierMappings1_21; -import com.viaversion.viaversion.protocols.v1_20_5to1_21.data.MappingData; +import com.viaversion.viaversion.protocols.v1_20_5to1_21.data.MappingData1_21; import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundConfigurationPackets1_21; import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundPacket1_21; import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundPackets1_21; @@ -43,6 +43,8 @@ import com.viaversion.viaversion.protocols.v1_20_5to1_21.rewriter.EntityPacketRe import com.viaversion.viaversion.rewriter.SoundRewriter; import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.TagRewriter; +import com.viaversion.viaversion.util.Key; +import com.viaversion.viaversion.util.UUIDUtil; import java.util.Locale; import java.util.UUID; @@ -50,7 +52,7 @@ import static com.viaversion.viaversion.util.ProtocolUtil.packetTypeMap; public final class Protocol1_20_5To1_21 extends AbstractProtocol { - public static final MappingData MAPPINGS = new MappingData(); + public static final MappingData1_21 MAPPINGS = new MappingData1_21(); private final EntityPacketRewriter1_21 entityRewriter = new EntityPacketRewriter1_21(this); private final BlockItemPacketRewriter1_21 itemRewriter = new BlockItemPacketRewriter1_21(this); private final TagRewriter tagRewriter = new TagRewriter<>(this); @@ -112,7 +114,12 @@ public final class Protocol1_20_5To1_21 extends AbstractProtocol enchantments; + private CompoundTag jukeboxSongs; - public MappingData() { + public MappingData1_21() { super("1.20.5", "1.21"); } @@ -34,9 +38,20 @@ public final class MappingData extends MappingDataBase { protected void loadExtras(final CompoundTag data) { final CompoundTag extraMappings = MappingDataLoader.INSTANCE.loadNBT("enchantments-1.21.nbt"); enchantments = extraMappings.getListTag("entries", CompoundTag.class); + jukeboxSongs = MappingDataLoader.INSTANCE.loadNBT("jukebox-songs-1.21.nbt"); } public CompoundTag enchantment(final int id) { return enchantments.get(id).copy(); } + + public RegistryEntry[] jukeboxSongs() { + final RegistryEntry[] entries = new RegistryEntry[jukeboxSongs.size()]; + int i = 0; + for (final Map.Entry entry : jukeboxSongs.entrySet()) { + final CompoundTag tag = (CompoundTag) entry.getValue(); + entries[i++] = new RegistryEntry(entry.getKey(), tag.copy()); + } + return entries; + } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_5to1_21/rewriter/BlockItemPacketRewriter1_21.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_5to1_21/rewriter/BlockItemPacketRewriter1_21.java index 6667e0ccd..53c50dcc6 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_5to1_21/rewriter/BlockItemPacketRewriter1_21.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_5to1_21/rewriter/BlockItemPacketRewriter1_21.java @@ -37,10 +37,13 @@ import com.viaversion.viaversion.protocols.v1_20_5to1_21.data.AttributeModifierM import com.viaversion.viaversion.rewriter.BlockRewriter; import com.viaversion.viaversion.rewriter.StructuredItemRewriter; import java.util.Arrays; +import java.util.List; import org.checkerframework.checker.nullness.qual.Nullable; public final class BlockItemPacketRewriter1_21 extends StructuredItemRewriter { + private static final List DISCS = List.of("11", "13", "5", "blocks", "cat", "chirp", "far", "mall", "mellohi", "otherside", "pigstep", "relic", "stal", "strad", "wait", "ward"); + public BlockItemPacketRewriter1_21(final Protocol1_20_5To1_21 protocol) { super(protocol, Types1_20_5.ITEM, Types1_20_5.ITEM_ARRAY, Types1_21.ITEM, Types1_21.ITEM_ARRAY); } @@ -51,7 +54,6 @@ public final class BlockItemPacketRewriter1_21 extends StructuredItemRewriter { + final int id = wrapper.passthrough(Types.INT); + wrapper.passthrough(Types.BLOCK_POSITION1_14); + + final int data = wrapper.read(Types.INT); + if (id == 1010) { + final int jukeboxSong = itemToJubeboxSong(data); + if (jukeboxSong == -1) { + wrapper.cancel(); + return; + } + + wrapper.write(Types.INT, jukeboxSong); + } else if (id == 2001) { + wrapper.write(Types.INT, protocol.getMappingData().getNewBlockStateId(data)); + } else { + wrapper.write(Types.INT, data); + } + }); + protocol.registerServerbound(ServerboundPackets1_20_5.USE_ITEM, wrapper -> { wrapper.passthrough(Types.VAR_INT); // Hand wrapper.passthrough(Types.VAR_INT); // Sequence @@ -131,4 +153,14 @@ public final class BlockItemPacketRewriter1_21 extends StructuredItemRewriter { } public void registerLevelEvent(C packetType, int playRecordId, int blockBreakId) { - protocol.registerClientbound(packetType, new PacketHandlers() { - @Override - public void register() { - map(Types.INT); // Effect Id - map(positionType); // Location - map(Types.INT); // Data - handler(wrapper -> { - int id = wrapper.get(Types.INT, 0); - int data = wrapper.get(Types.INT, 1); - if (id == playRecordId && protocol.getMappingData().getItemMappings() != null) { - wrapper.set(Types.INT, 1, protocol.getMappingData().getNewItemId(data)); - } else if (id == blockBreakId && protocol.getMappingData().getBlockStateMappings() != null) { - wrapper.set(Types.INT, 1, protocol.getMappingData().getNewBlockStateId(data)); - } - }); + protocol.registerClientbound(packetType, wrapper -> { + final int id = wrapper.passthrough(Types.INT); + wrapper.passthrough(positionType); + + final int data = wrapper.read(Types.INT); + final MappingData mappingData = protocol.getMappingData(); + if (playRecordId != -1 && id == playRecordId && mappingData.getItemMappings() != null) { + wrapper.write(Types.INT, mappingData.getNewItemId(data)); + } else if (id == blockBreakId && mappingData.getBlockStateMappings() != null) { + wrapper.write(Types.INT, mappingData.getNewBlockStateId(data)); + } else { + wrapper.write(Types.INT, data); } }); } + public void registerLevelEvent1_21(C packetType, int blockBreakId) { + registerLevelEvent(packetType, -1, blockBreakId); + } + public void registerLevelChunk(C packetType, Type chunkType, Type newChunkType) { registerLevelChunk(packetType, chunkType, newChunkType, null); } diff --git a/common/src/main/resources/assets/viaversion/data/jukebox-songs-1.21.nbt b/common/src/main/resources/assets/viaversion/data/jukebox-songs-1.21.nbt new file mode 100644 index 0000000000000000000000000000000000000000..004cfaece4910ce2904217d711ee8a7a73a4aca3 GIT binary patch literal 2473 zcmbW&OHRWu6a`R2Tj+<67*PI*6;vu!2OvgNY=TMTI;J%wc4Rw%#Do>F5*ESIuoF}v z>piQ={OaoDMlb0T(kGkaahD8?DrK7RS1v6%%7x@i(KMJtWdH`dR9|uGHN+Fc`jf0T)iho$;D{Z>&i%r959s^R8y;T zqDotomJl+iNR#zQ6NogsZbq75qzy;fmS6wmG;IJnMmCyao_*)pnrDi=m;iMK zqUtCp`~Hl*y$eKE7h=4Yt86i91KTlZb2$)p6McuL2+Xx%s!gZru%*yN;5vm}KG{Ha zj>&ukLP9|UE? zs0=$0Ux&fEel+9$!s%M0RY{O`7o}mNRehMP=SO>^BK$qTUlkt&Wil$mM)P$TY^R3l Ta5D_1QxNvi5T;WM)+gi_V@o&I literal 0 HcmV?d00001 diff --git a/template/src/main/java/com/viaversion/viaversion/template/protocols/rewriter/BlockItemPacketRewriter1_99.java b/template/src/main/java/com/viaversion/viaversion/template/protocols/rewriter/BlockItemPacketRewriter1_99.java index 15c643316..b07a3e2eb 100644 --- a/template/src/main/java/com/viaversion/viaversion/template/protocols/rewriter/BlockItemPacketRewriter1_99.java +++ b/template/src/main/java/com/viaversion/viaversion/template/protocols/rewriter/BlockItemPacketRewriter1_99.java @@ -46,7 +46,7 @@ public final class BlockItemPacketRewriter1_99 extends StructuredItemRewriter