Handle jukebox songs

This commit is contained in:
Nassim Jahnke 2024-05-23 12:16:49 +02:00
parent 251ab42c14
commit 5edb6ff411
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
11 changed files with 93 additions and 26 deletions

View File

@ -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;
}

View File

@ -33,6 +33,14 @@ import io.netty.buffer.ByteBuf;
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) {
@Override
public JukeboxPlayable read(final ByteBuf buffer) {

View File

@ -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;

View File

@ -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) {

View File

@ -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<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 BlockItemPacketRewriter1_21 itemRewriter = new BlockItemPacketRewriter1_21(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) {
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();
}
@ -161,7 +168,7 @@ public final class Protocol1_20_5To1_21 extends AbstractProtocol<ClientboundPack
}
@Override
public MappingData getMappingData() {
public MappingData1_21 getMappingData() {
return MAPPINGS;
}

View File

@ -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.ListTag;
import com.viaversion.nbt.tag.Tag;
import com.viaversion.viaversion.api.data.MappingDataBase;
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 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<String, Tag> entry : jukeboxSongs.entrySet()) {
final CompoundTag tag = (CompoundTag) entry.getValue();
entries[i++] = new RegistryEntry(entry.getKey(), tag.copy());
}
return entries;
}
}

View File

@ -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<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) {
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.registerBlockUpdate(ClientboundPackets1_20_5.BLOCK_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.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);
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 -> {
wrapper.passthrough(Types.VAR_INT); // Hand
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());
});
}
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);
}
}

View File

@ -91,6 +91,11 @@ public final class EntityPacketRewriter1_21 extends EntityRewriter<ClientboundPa
}
enchantmentRegistryPacket.write(Types.REGISTRY_ENTRY_ARRAY, enchantmentRegistry);
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() {

View File

@ -20,6 +20,7 @@ package com.viaversion.viaversion.rewriter;
import com.google.common.base.Preconditions;
import com.viaversion.nbt.tag.CompoundTag;
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.entity.EntityTracker;
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) {
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<Chunk> chunkType, Type<Chunk> newChunkType) {

View File

@ -46,7 +46,7 @@ public final class BlockItemPacketRewriter1_99 extends StructuredItemRewriter<Cl
blockRewriter.registerBlockEvent(ClientboundPackets1_20_5.BLOCK_EVENT);
blockRewriter.registerBlockUpdate(ClientboundPackets1_20_5.BLOCK_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.registerBlockEntityData(ClientboundPackets1_20_5.BLOCK_ENTITY_DATA);