From d1059a454270b433d04eeb000695b02ec16e0921 Mon Sep 17 00:00:00 2001 From: FlorianMichael <60033407+FlorianMichael@users.noreply.github.com> Date: Fri, 21 Apr 2023 20:37:42 +0200 Subject: [PATCH] Fixed InventoryTracker16 in ViaVersion --- .../access/IInventoryTracker1_16.java | 25 ++++++++ .../fixes/minecraft/item/MixinItemStack.java | 2 +- .../MixinEntityPackets.java | 44 +++++++++++++ .../MixinInventoryPackets.java | 61 +++++++++++++++++++ .../MixinInventoryTracker1_16.java | 49 +++++++++++++++ src/main/resources/viafabricplus.mixins.json | 5 +- 6 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 src/main/java/de/florianmichael/viafabricplus/injection/access/IInventoryTracker1_16.java create mode 100644 src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/viaversion/protocol1_16to1_15_2/MixinEntityPackets.java create mode 100644 src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/viaversion/protocol1_16to1_15_2/MixinInventoryPackets.java create mode 100644 src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/viaversion/protocol1_16to1_15_2/MixinInventoryTracker1_16.java diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/access/IInventoryTracker1_16.java b/src/main/java/de/florianmichael/viafabricplus/injection/access/IInventoryTracker1_16.java new file mode 100644 index 00000000..1ebafffb --- /dev/null +++ b/src/main/java/de/florianmichael/viafabricplus/injection/access/IInventoryTracker1_16.java @@ -0,0 +1,25 @@ +/* + * This file is part of ViaFabricPlus - https://github.com/FlorianMichael/ViaFabricPlus + * Copyright (C) 2021-2023 FlorianMichael/EnZaXD 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 de.florianmichael.viafabricplus.injection.access; + +public interface IInventoryTracker1_16 { + + boolean viafabricplus_isInventoryOpen(); + + void viafabricplus_setInventoryOpen(final boolean inventoryOpen); +} diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinItemStack.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinItemStack.java index 3e1f78cc..9ac85f57 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinItemStack.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinItemStack.java @@ -53,7 +53,7 @@ public abstract class MixinItemStack { if (ProtocolHack.getTargetVersion().isOlderThanOrEqualTo(ProtocolVersion.v1_10)) { this.empty = false; final ItemStack self = (ItemStack) (Object) this; - this.empty = self == EMPTY || this.getItem() == null || self.isOf(Items.AIR) && count != 0; + this.empty = self == EMPTY || this.getItem() == null || self.isOf(Items.AIR) || count == 0; ci.cancel(); } } diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/viaversion/protocol1_16to1_15_2/MixinEntityPackets.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/viaversion/protocol1_16to1_15_2/MixinEntityPackets.java new file mode 100644 index 00000000..4a335a55 --- /dev/null +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/viaversion/protocol1_16to1_15_2/MixinEntityPackets.java @@ -0,0 +1,44 @@ +/* + * This file is part of ViaFabricPlus - https://github.com/FlorianMichael/ViaFabricPlus + * Copyright (C) 2021-2023 FlorianMichael/EnZaXD 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 de.florianmichael.viafabricplus.injection.mixin.fixes.viaversion.protocol1_16to1_15_2; + +import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14; +import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2; +import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16; +import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.EntityPackets; +import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16; +import de.florianmichael.viafabricplus.injection.access.IInventoryTracker1_16; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(value = EntityPackets.class, remap = false) +public class MixinEntityPackets { + + @Inject(method = "register", at = @At("RETURN")) + private static void rewriteCheck(Protocol1_16To1_15_2 protocol, CallbackInfo ci) { + protocol.registerServerbound(ServerboundPackets1_16.ANIMATION, ServerboundPackets1_14.ANIMATION, wrapper -> { + final InventoryTracker1_16 inventoryTracker = wrapper.user().get(InventoryTracker1_16.class); + // Don't send an arm swing if the player has an inventory opened. + if (((IInventoryTracker1_16) inventoryTracker).viafabricplus_isInventoryOpen()) { + wrapper.cancel(); + } + }, true); + } +} diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/viaversion/protocol1_16to1_15_2/MixinInventoryPackets.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/viaversion/protocol1_16to1_15_2/MixinInventoryPackets.java new file mode 100644 index 00000000..b05c75eb --- /dev/null +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/viaversion/protocol1_16to1_15_2/MixinInventoryPackets.java @@ -0,0 +1,61 @@ +/* + * This file is part of ViaFabricPlus - https://github.com/FlorianMichael/ViaFabricPlus + * Copyright (C) 2021-2023 FlorianMichael/EnZaXD 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 de.florianmichael.viafabricplus.injection.mixin.fixes.viaversion.protocol1_16to1_15_2; + +import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; +import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14; +import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15; +import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16; +import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2; +import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16; +import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.InventoryPackets; +import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16; +import com.viaversion.viaversion.rewriter.ItemRewriter; +import de.florianmichael.viafabricplus.injection.access.IInventoryTracker1_16; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; + +@Mixin(value = InventoryPackets.class, remap = false) +public class MixinInventoryPackets extends ItemRewriter { + + protected MixinInventoryPackets(Protocol1_16To1_15_2 protocol) { + super(protocol); + } + + @Inject(method = "registerPackets", at = @At("RETURN"), locals = LocalCapture.CAPTURE_FAILHARD) + public void fixInventoryIssue(CallbackInfo ci, PacketHandler cursorRemapper) { + protocol.registerClientbound(ClientboundPackets1_15.CLOSE_WINDOW, ClientboundPackets1_16.CLOSE_WINDOW, new PacketHandlers() { + @Override + public void register() { + handler(cursorRemapper); + handler(wrapper -> { + InventoryTracker1_16 inventoryTracker = wrapper.user().get(InventoryTracker1_16.class); + ((IInventoryTracker1_16) inventoryTracker).viafabricplus_setInventoryOpen(false); + }); + } + }, true); + protocol.registerServerbound(ServerboundPackets1_16.CLOSE_WINDOW, ServerboundPackets1_14.CLOSE_WINDOW, wrapper -> { + InventoryTracker1_16 inventoryTracker = wrapper.user().get(InventoryTracker1_16.class); + ((IInventoryTracker1_16) inventoryTracker).viafabricplus_setInventoryOpen(false); + }); + } +} diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/viaversion/protocol1_16to1_15_2/MixinInventoryTracker1_16.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/viaversion/protocol1_16to1_15_2/MixinInventoryTracker1_16.java new file mode 100644 index 00000000..7eaeb2c5 --- /dev/null +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/viaversion/protocol1_16to1_15_2/MixinInventoryTracker1_16.java @@ -0,0 +1,49 @@ +/* + * This file is part of ViaFabricPlus - https://github.com/FlorianMichael/ViaFabricPlus + * Copyright (C) 2021-2023 FlorianMichael/EnZaXD 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 de.florianmichael.viafabricplus.injection.mixin.fixes.viaversion.protocol1_16to1_15_2; + +import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16; +import de.florianmichael.viafabricplus.injection.access.IInventoryTracker1_16; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +// If the server uses -1 as Window ID, it can break ViaVersion +@Mixin(value = InventoryTracker1_16.class, remap = false) +public class MixinInventoryTracker1_16 implements IInventoryTracker1_16 { + + @Unique + private boolean viafabricplus_inventoryOpen = false; + + @Inject(method = "setInventory", at = @At("RETURN")) + public void setInventoryOpen(short inventory, CallbackInfo ci) { + this.viafabricplus_inventoryOpen = true; + } + + @Override + public boolean viafabricplus_isInventoryOpen() { + return this.viafabricplus_inventoryOpen; + } + + @Override + public void viafabricplus_setInventoryOpen(boolean inventoryOpen) { + this.viafabricplus_inventoryOpen = inventoryOpen; + } +} diff --git a/src/main/resources/viafabricplus.mixins.json b/src/main/resources/viafabricplus.mixins.json index 16b38c0d..8412e588 100644 --- a/src/main/resources/viafabricplus.mixins.json +++ b/src/main/resources/viafabricplus.mixins.json @@ -157,7 +157,10 @@ "fixes.viaversion.protocol1_9to1_8.MixinViaIdleThread", "jsonwebtoken.MixinClasses", "jsonwebtoken.MixinDefaultCompressionCodecResolver", - "jsonwebtoken.MixinDefaultJwtParserBuilder" + "jsonwebtoken.MixinDefaultJwtParserBuilder", + "fixes.viaversion.protocol1_16to1_15_2.MixinEntityPackets", + "fixes.viaversion.protocol1_16to1_15_2.MixinInventoryPackets", + "fixes.viaversion.protocol1_16to1_15_2.MixinInventoryTracker1_16" ], "injectors": { "defaultRequire": 1