From dca8ecf1db8ec904208d6566804082afc8dbee87 Mon Sep 17 00:00:00 2001 From: EnZaXD <60033407+FlorianMichael@users.noreply.github.com> Date: Thu, 1 Feb 2024 16:28:40 +0100 Subject: [PATCH] Fix notes color from note block being wrong in 1.13->1.12 (#680) Closes https://github.com/ViaVersion/ViaBackwards/issues/679 --- .../Protocol1_12_2To1_13.java | 3 ++ .../packets/BlockItemPackets1_13.java | 19 +++++++ .../packets/EntityPackets1_13.java | 6 ++- .../storage/NoteBlockStorage.java | 50 +++++++++++++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/storage/NoteBlockStorage.java diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java index e3eec36c..fc34c976 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java @@ -29,6 +29,7 @@ import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.packets.PlayerP import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.packets.SoundPackets1_13; import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.providers.BackwardsBlockEntityProvider; import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.BackwardsBlockStorage; +import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.NoteBlockStorage; import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.PlayerPositionStorage1_13; import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.TabCompleteStorage; import com.viaversion.viaversion.api.Via; @@ -124,6 +125,8 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol= 483 && blockId <= 498) blockId = blockId - 483 + 219; + if (blockId == 25) { // Note block + final NoteBlockStorage noteBlockStorage = wrapper.user().get(NoteBlockStorage.class); + + final Position position = wrapper.get(Type.POSITION1_8, 0); + final Pair update = noteBlockStorage.getNoteBlockUpdate(position); + if (update != null) { // Use values from block state update + wrapper.set(Type.UNSIGNED_BYTE, 0, update.key().shortValue()); + wrapper.set(Type.UNSIGNED_BYTE, 1, update.value().shortValue()); + } + } + wrapper.set(Type.VAR_INT, 0, blockId); }); } @@ -189,6 +203,11 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit int blockState = wrapper.read(Type.VAR_INT); Position position = wrapper.get(Type.POSITION1_8, 0); + // Note block special treatment + if (blockState >= 249 && blockState <= 748) { // Note block states id range + wrapper.user().get(NoteBlockStorage.class).storeNoteBlockUpdate(position, blockState); + } + // Store blocks BackwardsBlockStorage storage = wrapper.user().get(BackwardsBlockStorage.class); storage.checkAndStore(position, blockState); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java index c12b4590..b07c224d 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java @@ -25,6 +25,7 @@ import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.data.EntityType import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.data.PaintingMapping; import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.data.ParticleMapping; import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.BackwardsBlockStorage; +import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.NoteBlockStorage; import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.PlayerPositionStorage1_13; import com.viaversion.viaversion.api.minecraft.Particle; import com.viaversion.viaversion.api.minecraft.entities.EntityType; @@ -197,7 +198,10 @@ public class EntityPackets1_13 extends LegacyEntityRewriter wrapper.user().get(BackwardsBlockStorage.class).clear()); + handler(wrapper -> { + wrapper.user().get(BackwardsBlockStorage.class).clear(); + wrapper.user().get(NoteBlockStorage.class).clear(); + }); } }); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/storage/NoteBlockStorage.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/storage/NoteBlockStorage.java new file mode 100644 index 00000000..a31725c6 --- /dev/null +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/storage/NoteBlockStorage.java @@ -0,0 +1,50 @@ +/* + * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage; + +import com.viaversion.viaversion.api.connection.StorableObject; +import com.viaversion.viaversion.api.minecraft.Position; +import com.viaversion.viaversion.libs.fastutil.objects.Object2IntMap; +import com.viaversion.viaversion.libs.fastutil.objects.Object2IntOpenHashMap; +import com.viaversion.viaversion.util.Pair; + +public class NoteBlockStorage implements StorableObject { + + private static final int MAX_NOTE_ID = 24; + + private final Object2IntMap noteBlockUpdates = new Object2IntOpenHashMap<>(); + + public void storeNoteBlockUpdate(final Position position, final int blockStateId) { + noteBlockUpdates.put(position, blockStateId); + } + + public Pair getNoteBlockUpdate(final Position position) { + if (!noteBlockUpdates.containsKey(position)) { + return null; + } + int relativeBlockState = noteBlockUpdates.removeInt(position) - 249; + relativeBlockState = relativeBlockState / 2; // Get rid of powered state + + return new Pair<>(relativeBlockState / MAX_NOTE_ID + 1, relativeBlockState % MAX_NOTE_ID + 1); + } + + public void clear() { + noteBlockUpdates.clear(); + } + +}