3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-20 06:50:08 +01:00

Handle jukebox songs

Dieser Commit ist enthalten in:
Nassim Jahnke 2024-05-23 12:16:49 +02:00
Ursprung 251ab42c14
Commit 5edb6ff411
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: EF6771C01F6EF02F
11 geänderte Dateien mit 93 neuen und 26 gelöschten Zeilen

Datei anzeigen

@ -165,7 +165,6 @@ public final class StructuredDataContainer {
for (final StructuredData<?> data : data.values()) { for (final StructuredData<?> data : data.values()) {
final int mappedId = rewriter.applyAsInt(data.id()); final int mappedId = rewriter.applyAsInt(data.id());
if (mappedId == -1) { if (mappedId == -1) {
protocol.getLogger().warning("Could not find item data serializer for id " + data.id());
continue; continue;
} }

Datei anzeigen

@ -33,6 +33,14 @@ import io.netty.buffer.ByteBuf;
public record JukeboxPlayable(Either<Holder<JukeboxSong>, String> song, boolean showInTooltip) { public record JukeboxPlayable(Either<Holder<JukeboxSong>, String> song, boolean showInTooltip) {
public JukeboxPlayable(final Holder<JukeboxSong> 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<JukeboxPlayable> TYPE = new Type<>(JukeboxPlayable.class) { public static final Type<JukeboxPlayable> TYPE = new Type<>(JukeboxPlayable.class) {
@Override @Override
public JukeboxPlayable read(final ByteBuf buffer) { public JukeboxPlayable read(final ByteBuf buffer) {

Datei anzeigen

@ -22,7 +22,6 @@
*/ */
package com.viaversion.viaversion.api.minecraft.item.data; 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.Type;
import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.Types;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;

Datei anzeigen

@ -47,7 +47,7 @@ public final class Key {
} }
public static boolean equals(final String firstIdentifier, final String secondIdentifier) { 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) { public static String namespaced(final String identifier) {

Datei anzeigen

@ -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.ServerboundPacket1_20_5;
import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.packet.ServerboundPackets1_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.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.ClientboundConfigurationPackets1_21;
import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundPacket1_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; 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.SoundRewriter;
import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.StatisticsRewriter;
import com.viaversion.viaversion.rewriter.TagRewriter; 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.Locale;
import java.util.UUID; 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<ClientboundPacket1_20_5, ClientboundPacket1_21, ServerboundPacket1_20_5, ServerboundPacket1_20_5> { public final class Protocol1_20_5To1_21 extends AbstractProtocol<ClientboundPacket1_20_5, ClientboundPacket1_21, ServerboundPacket1_20_5, ServerboundPacket1_20_5> {
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 EntityPacketRewriter1_21 entityRewriter = new EntityPacketRewriter1_21(this);
private final BlockItemPacketRewriter1_21 itemRewriter = new BlockItemPacketRewriter1_21(this); private final BlockItemPacketRewriter1_21 itemRewriter = new BlockItemPacketRewriter1_21(this);
private final TagRewriter<ClientboundPacket1_20_5> tagRewriter = new TagRewriter<>(this); private final TagRewriter<ClientboundPacket1_20_5> tagRewriter = new TagRewriter<>(this);
@ -112,7 +114,12 @@ public final class Protocol1_20_5To1_21 extends AbstractProtocol<ClientboundPack
} }
public static UUID mapAttributeId(final String id) { public static UUID mapAttributeId(final String id) {
final UUID uuid = AttributeModifierMappings1_21.idToUuid(id); UUID uuid = AttributeModifierMappings1_21.idToUuid(id);
if (uuid != null) {
return uuid;
}
uuid = UUIDUtil.parseUUID(Key.stripNamespace(id).toUpperCase(Locale.ROOT));
return uuid != null ? uuid : UUID.randomUUID(); return uuid != null ? uuid : UUID.randomUUID();
} }
@ -161,7 +168,7 @@ public final class Protocol1_20_5To1_21 extends AbstractProtocol<ClientboundPack
} }
@Override @Override
public MappingData getMappingData() { public MappingData1_21 getMappingData() {
return MAPPINGS; return MAPPINGS;
} }

Datei anzeigen

@ -19,14 +19,18 @@ package com.viaversion.viaversion.protocols.v1_20_5to1_21.data;
import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.nbt.tag.ListTag; import com.viaversion.nbt.tag.ListTag;
import com.viaversion.nbt.tag.Tag;
import com.viaversion.viaversion.api.data.MappingDataBase; import com.viaversion.viaversion.api.data.MappingDataBase;
import com.viaversion.viaversion.api.data.MappingDataLoader; import com.viaversion.viaversion.api.data.MappingDataLoader;
import com.viaversion.viaversion.api.minecraft.RegistryEntry;
import java.util.Map;
public final class MappingData extends MappingDataBase { public final class MappingData1_21 extends MappingDataBase {
private ListTag<CompoundTag> enchantments; private ListTag<CompoundTag> enchantments;
private CompoundTag jukeboxSongs;
public MappingData() { public MappingData1_21() {
super("1.20.5", "1.21"); super("1.20.5", "1.21");
} }
@ -34,9 +38,20 @@ public final class MappingData extends MappingDataBase {
protected void loadExtras(final CompoundTag data) { protected void loadExtras(final CompoundTag data) {
final CompoundTag extraMappings = MappingDataLoader.INSTANCE.loadNBT("enchantments-1.21.nbt"); final CompoundTag extraMappings = MappingDataLoader.INSTANCE.loadNBT("enchantments-1.21.nbt");
enchantments = extraMappings.getListTag("entries", CompoundTag.class); enchantments = extraMappings.getListTag("entries", CompoundTag.class);
jukeboxSongs = MappingDataLoader.INSTANCE.loadNBT("jukebox-songs-1.21.nbt");
} }
public CompoundTag enchantment(final int id) { public CompoundTag enchantment(final int id) {
return enchantments.get(id).copy(); return enchantments.get(id).copy();
} }
public RegistryEntry[] jukeboxSongs() {
final RegistryEntry[] entries = new RegistryEntry[jukeboxSongs.size()];
int i = 0;
for (final Map.Entry<String, Tag> entry : jukeboxSongs.entrySet()) {
final CompoundTag tag = (CompoundTag) entry.getValue();
entries[i++] = new RegistryEntry(entry.getKey(), tag.copy());
}
return entries;
}
} }

Datei anzeigen

@ -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.BlockRewriter;
import com.viaversion.viaversion.rewriter.StructuredItemRewriter; import com.viaversion.viaversion.rewriter.StructuredItemRewriter;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
public final class BlockItemPacketRewriter1_21 extends StructuredItemRewriter<ClientboundPacket1_20_5, ServerboundPacket1_20_5, Protocol1_20_5To1_21> { public final class BlockItemPacketRewriter1_21 extends StructuredItemRewriter<ClientboundPacket1_20_5, ServerboundPacket1_20_5, Protocol1_20_5To1_21> {
private static final List<String> 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) { 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); 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<Cl
blockRewriter.registerBlockEvent(ClientboundPackets1_20_5.BLOCK_EVENT); blockRewriter.registerBlockEvent(ClientboundPackets1_20_5.BLOCK_EVENT);
blockRewriter.registerBlockUpdate(ClientboundPackets1_20_5.BLOCK_UPDATE); blockRewriter.registerBlockUpdate(ClientboundPackets1_20_5.BLOCK_UPDATE);
blockRewriter.registerSectionBlocksUpdate1_20(ClientboundPackets1_20_5.SECTION_BLOCKS_UPDATE); blockRewriter.registerSectionBlocksUpdate1_20(ClientboundPackets1_20_5.SECTION_BLOCKS_UPDATE);
blockRewriter.registerLevelEvent(ClientboundPackets1_20_5.LEVEL_EVENT, 1010, 2001);
blockRewriter.registerLevelChunk1_19(ClientboundPackets1_20_5.LEVEL_CHUNK_WITH_LIGHT, ChunkType1_20_2::new); blockRewriter.registerLevelChunk1_19(ClientboundPackets1_20_5.LEVEL_CHUNK_WITH_LIGHT, ChunkType1_20_2::new);
blockRewriter.registerBlockEntityData(ClientboundPackets1_20_5.BLOCK_ENTITY_DATA); blockRewriter.registerBlockEntityData(ClientboundPackets1_20_5.BLOCK_ENTITY_DATA);
@ -67,6 +69,26 @@ public final class BlockItemPacketRewriter1_21 extends StructuredItemRewriter<Cl
registerLevelParticles1_20_5(ClientboundPackets1_20_5.LEVEL_PARTICLES, Types1_20_5.PARTICLE, Types1_21.PARTICLE); registerLevelParticles1_20_5(ClientboundPackets1_20_5.LEVEL_PARTICLES, Types1_20_5.PARTICLE, Types1_21.PARTICLE);
registerExplosion(ClientboundPackets1_20_5.EXPLODE, Types1_20_5.PARTICLE, Types1_21.PARTICLE); // Rewrites the included sound and particles registerExplosion(ClientboundPackets1_20_5.EXPLODE, Types1_20_5.PARTICLE, Types1_21.PARTICLE); // Rewrites the included sound and particles
protocol.registerClientbound(ClientboundPackets1_20_5.LEVEL_EVENT, wrapper -> {
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 -> { protocol.registerServerbound(ServerboundPackets1_20_5.USE_ITEM, wrapper -> {
wrapper.passthrough(Types.VAR_INT); // Hand wrapper.passthrough(Types.VAR_INT); // Hand
wrapper.passthrough(Types.VAR_INT); // Sequence wrapper.passthrough(Types.VAR_INT); // Sequence
@ -131,4 +153,14 @@ public final class BlockItemPacketRewriter1_21 extends StructuredItemRewriter<Cl
return new AttributeModifiers1_20_5(modifiers, attributeModifiers.showInTooltip()); return new AttributeModifiers1_20_5(modifiers, attributeModifiers.showInTooltip());
}); });
} }
private int itemToJubeboxSong(final int id) {
String identifier = Protocol1_20_5To1_21.MAPPINGS.getFullItemMappings().identifier(id);
if (!identifier.contains("music_disc_")) {
return -1;
}
identifier = identifier.substring("minecraft:music_disc_".length());
return DISCS.indexOf(identifier);
}
} }

Datei anzeigen

@ -91,6 +91,11 @@ public final class EntityPacketRewriter1_21 extends EntityRewriter<ClientboundPa
} }
enchantmentRegistryPacket.write(Types.REGISTRY_ENTRY_ARRAY, enchantmentRegistry); enchantmentRegistryPacket.write(Types.REGISTRY_ENTRY_ARRAY, enchantmentRegistry);
enchantmentRegistryPacket.send(Protocol1_20_5To1_21.class); enchantmentRegistryPacket.send(Protocol1_20_5To1_21.class);
final PacketWrapper jukeboxSongsPacket = wrapper.create(ClientboundConfigurationPackets1_20_5.REGISTRY_DATA);
jukeboxSongsPacket.write(Types.STRING, "minecraft:jukebox_song");
jukeboxSongsPacket.write(Types.REGISTRY_ENTRY_ARRAY, protocol.getMappingData().jukeboxSongs());
jukeboxSongsPacket.send(Protocol1_20_5To1_21.class);
}); });
protocol.registerClientbound(ClientboundPackets1_20_5.LOGIN, new PacketHandlers() { protocol.registerClientbound(ClientboundPackets1_20_5.LOGIN, new PacketHandlers() {

Datei anzeigen

@ -20,6 +20,7 @@ package com.viaversion.viaversion.rewriter;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.data.MappingData;
import com.viaversion.viaversion.api.data.Mappings; import com.viaversion.viaversion.api.data.Mappings;
import com.viaversion.viaversion.api.data.entity.EntityTracker; import com.viaversion.viaversion.api.data.entity.EntityTracker;
import com.viaversion.viaversion.api.minecraft.BlockChangeRecord; import com.viaversion.viaversion.api.minecraft.BlockChangeRecord;
@ -154,23 +155,24 @@ public class BlockRewriter<C extends ClientboundPacketType> {
} }
public void registerLevelEvent(C packetType, int playRecordId, int blockBreakId) { public void registerLevelEvent(C packetType, int playRecordId, int blockBreakId) {
protocol.registerClientbound(packetType, new PacketHandlers() { protocol.registerClientbound(packetType, wrapper -> {
@Override final int id = wrapper.passthrough(Types.INT);
public void register() { wrapper.passthrough(positionType);
map(Types.INT); // Effect Id
map(positionType); // Location final int data = wrapper.read(Types.INT);
map(Types.INT); // Data final MappingData mappingData = protocol.getMappingData();
handler(wrapper -> { if (playRecordId != -1 && id == playRecordId && mappingData.getItemMappings() != null) {
int id = wrapper.get(Types.INT, 0); wrapper.write(Types.INT, mappingData.getNewItemId(data));
int data = wrapper.get(Types.INT, 1); } else if (id == blockBreakId && mappingData.getBlockStateMappings() != null) {
if (id == playRecordId && protocol.getMappingData().getItemMappings() != null) { wrapper.write(Types.INT, mappingData.getNewBlockStateId(data));
wrapper.set(Types.INT, 1, protocol.getMappingData().getNewItemId(data)); } else {
} else if (id == blockBreakId && protocol.getMappingData().getBlockStateMappings() != null) { wrapper.write(Types.INT, data);
wrapper.set(Types.INT, 1, protocol.getMappingData().getNewBlockStateId(data));
} }
}); });
} }
});
public void registerLevelEvent1_21(C packetType, int blockBreakId) {
registerLevelEvent(packetType, -1, blockBreakId);
} }
public void registerLevelChunk(C packetType, Type<Chunk> chunkType, Type<Chunk> newChunkType) { public void registerLevelChunk(C packetType, Type<Chunk> chunkType, Type<Chunk> newChunkType) {

Datei anzeigen

@ -46,7 +46,7 @@ public final class BlockItemPacketRewriter1_99 extends StructuredItemRewriter<Cl
blockRewriter.registerBlockEvent(ClientboundPackets1_20_5.BLOCK_EVENT); blockRewriter.registerBlockEvent(ClientboundPackets1_20_5.BLOCK_EVENT);
blockRewriter.registerBlockUpdate(ClientboundPackets1_20_5.BLOCK_UPDATE); blockRewriter.registerBlockUpdate(ClientboundPackets1_20_5.BLOCK_UPDATE);
blockRewriter.registerSectionBlocksUpdate1_20(ClientboundPackets1_20_5.SECTION_BLOCKS_UPDATE); blockRewriter.registerSectionBlocksUpdate1_20(ClientboundPackets1_20_5.SECTION_BLOCKS_UPDATE);
blockRewriter.registerLevelEvent(ClientboundPackets1_20_5.LEVEL_EVENT, 1010, 2001); blockRewriter.registerLevelEvent1_21(ClientboundPackets1_20_5.LEVEL_EVENT, 2001);
blockRewriter.registerLevelChunk1_19(ClientboundPackets1_20_5.LEVEL_CHUNK_WITH_LIGHT, ChunkType1_20_2::new); blockRewriter.registerLevelChunk1_19(ClientboundPackets1_20_5.LEVEL_CHUNK_WITH_LIGHT, ChunkType1_20_2::new);
blockRewriter.registerBlockEntityData(ClientboundPackets1_20_5.BLOCK_ENTITY_DATA); blockRewriter.registerBlockEntityData(ClientboundPackets1_20_5.BLOCK_ENTITY_DATA);