Handle jukebox song level event

This commit is contained in:
Nassim Jahnke 2024-05-23 14:09:03 +02:00
parent 3749a5d5da
commit ab1467dba5
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
4 changed files with 40 additions and 9 deletions

View File

@ -66,7 +66,6 @@ public final class BlockItemPacketRewriter1_21 extends BackwardsStructuredItemRe
blockRewriter.registerBlockEvent(ClientboundPackets1_21.BLOCK_EVENT);
blockRewriter.registerBlockUpdate(ClientboundPackets1_21.BLOCK_UPDATE);
blockRewriter.registerSectionBlocksUpdate1_20(ClientboundPackets1_21.SECTION_BLOCKS_UPDATE);
blockRewriter.registerLevelEvent(ClientboundPackets1_21.LEVEL_EVENT, 1010, 2001);
blockRewriter.registerLevelChunk1_19(ClientboundPackets1_21.LEVEL_CHUNK_WITH_LIGHT, ChunkType1_20_2::new);
blockRewriter.registerBlockEntityData(ClientboundPackets1_21.BLOCK_ENTITY_DATA);
@ -82,6 +81,26 @@ public final class BlockItemPacketRewriter1_21 extends BackwardsStructuredItemRe
registerLevelParticles1_20_5(ClientboundPackets1_21.LEVEL_PARTICLES, Types1_21.PARTICLE, Types1_20_5.PARTICLE);
registerExplosion(ClientboundPackets1_21.EXPLODE, Types1_21.PARTICLE, Types1_20_5.PARTICLE);
protocol.registerClientbound(ClientboundPackets1_21.LEVEL_EVENT, wrapper -> {
final int event = wrapper.passthrough(Types.INT);
wrapper.passthrough(Types.BLOCK_POSITION1_14);
final int data = wrapper.read(Types.INT);
if (event == 1010) {
final int itemId = wrapper.user().get(EnchantmentsPaintingsStorage.class).jubeboxSongToItem(data);
if (itemId == -1) {
wrapper.cancel();
return;
}
wrapper.write(Types.INT, itemId);
} else if (event == 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

View File

@ -63,14 +63,10 @@ public final class EntityPacketRewriter1_21 extends EntityRewriter<ClientboundPa
protocol.registerClientbound(ClientboundConfigurationPackets1_21.REGISTRY_DATA, wrapper -> {
final String key = Key.stripMinecraftNamespace(wrapper.passthrough(Types.STRING));
if (key.equals("jukebox_song")) {
wrapper.cancel();
return;
}
final RegistryEntry[] entries = wrapper.passthrough(Types.REGISTRY_ENTRY_ARRAY);
final boolean paintingVariant = key.equals("painting_variant");
if (paintingVariant || key.equals("enchantment")) {
final boolean enchantment = key.equals("enchantment");
if (paintingVariant || enchantment || key.equals("jukebox_song")) {
// Track custom registries and cancel the packet
final String[] keys = new String[entries.length];
for (int i = 0; i < entries.length; i++) {
@ -80,7 +76,7 @@ public final class EntityPacketRewriter1_21 extends EntityRewriter<ClientboundPa
final EnchantmentsPaintingsStorage storage = wrapper.user().get(EnchantmentsPaintingsStorage.class);
if (paintingVariant) {
storage.setPaintings(new KeyMappings(keys), paintingMappingsForEntries(entries));
} else {
} else if (enchantment) {
final Tag[] descriptions = new Tag[entries.length];
for (int i = 0; i < entries.length; i++) {
final RegistryEntry entry = entries[i];
@ -89,6 +85,13 @@ public final class EntityPacketRewriter1_21 extends EntityRewriter<ClientboundPa
}
}
storage.setEnchantments(new KeyMappings(keys), descriptions);
} else {
final int[] jukeboxSongMappings = new int[keys.length];
for (int i = 0; i < keys.length; i++) {
final int itemId = protocol.getMappingData().getFullItemMappings().mappedId("music_disc_" + keys[i]);
jukeboxSongMappings[i] = itemId;
}
storage.setJubeboxSongsToItems(jukeboxSongMappings);
}
wrapper.cancel();

View File

@ -27,6 +27,7 @@ public final class EnchantmentsPaintingsStorage implements StorableObject {
private KeyMappings paintings;
private int[] paintingMappings;
private Tag[] enchantmentDescriptions;
private int[] jubeboxSongsToItems;
public KeyMappings enchantments() {
return enchantments;
@ -46,6 +47,14 @@ public final class EnchantmentsPaintingsStorage implements StorableObject {
this.paintingMappings = paintingMappings;
}
public void setJubeboxSongsToItems(final int[] jubeboxSongsToItems) {
this.jubeboxSongsToItems = jubeboxSongsToItems;
}
public int jubeboxSongToItem(final int id) {
return id >= 0 && id < jubeboxSongsToItems.length ? jubeboxSongsToItems[id] : -1;
}
@Override
public boolean clearOnServerSwitch() {
return false;

View File

@ -43,7 +43,7 @@ public final class BlockItemPacketRewriter1_99 extends BackwardsStructuredItemRe
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);