From 1fd0bc5813f928729da004b42fa0cf1780f994e7 Mon Sep 17 00:00:00 2001 From: Patbox <39821509+Patbox@users.noreply.github.com> Date: Sun, 20 Aug 2023 15:05:00 +0200 Subject: [PATCH] Fix compatibility with ItemFavourites (and likely other mods with bad packet format changing mixins) --- gradle.properties | 2 +- .../core/api/block/PolymerBlockUtils.java | 6 +++-- .../mixin/item/packet/PacketByteBufMixin.java | 27 +++++++++---------- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/gradle.properties b/gradle.properties index ec6127d0..f8fab32e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,7 +12,7 @@ fabric_version=0.83.0+1.20.1 maven_group = eu.pb4 -mod_version = 0.5.11 +mod_version = 0.5.12 minecraft_version_supported = ">=1.20-" diff --git a/polymer-core/src/main/java/eu/pb4/polymer/core/api/block/PolymerBlockUtils.java b/polymer-core/src/main/java/eu/pb4/polymer/core/api/block/PolymerBlockUtils.java index 9fe1927f..0ee699d6 100644 --- a/polymer-core/src/main/java/eu/pb4/polymer/core/api/block/PolymerBlockUtils.java +++ b/polymer-core/src/main/java/eu/pb4/polymer/core/api/block/PolymerBlockUtils.java @@ -26,6 +26,8 @@ import java.util.function.Predicate; public final class PolymerBlockUtils { + private static final NbtCompound STATIC_COMPOUND = new NbtCompound(); + private PolymerBlockUtils() { } @@ -246,8 +248,8 @@ public static Block getBlockSafely(PolymerBlock block, BlockState state) { return getBlockSafely(block, state, NESTED_DEFAULT_DISTANCE); } - public static BlockEntityUpdateS2CPacket createBlockEntityPacket(BlockPos pos, BlockEntityType type, NbtCompound nbtCompound) { - return BlockEntityUpdateS2CPacketAccessor.createBlockEntityUpdateS2CPacket(pos.toImmutable(), type, nbtCompound); + public static BlockEntityUpdateS2CPacket createBlockEntityPacket(BlockPos pos, BlockEntityType type, @Nullable NbtCompound nbtCompound) { + return BlockEntityUpdateS2CPacketAccessor.createBlockEntityUpdateS2CPacket(pos.toImmutable(), type, nbtCompound != null ? nbtCompound : STATIC_COMPOUND); } @ApiStatus.Experimental diff --git a/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/item/packet/PacketByteBufMixin.java b/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/item/packet/PacketByteBufMixin.java index b8df8d50..d329bf1b 100644 --- a/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/item/packet/PacketByteBufMixin.java +++ b/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/item/packet/PacketByteBufMixin.java @@ -1,6 +1,7 @@ package eu.pb4.polymer.core.mixin.item.packet; -import eu.pb4.polymer.common.impl.client.ClientUtils; +import com.llamalad7.mixinextras.injector.ModifyReturnValue; +import eu.pb4.polymer.common.api.PolymerCommonUtils; import eu.pb4.polymer.core.api.item.PolymerItemUtils; import eu.pb4.polymer.core.api.utils.PolymerUtils; import eu.pb4.polymer.core.impl.client.InternalClientRegistry; @@ -38,9 +39,9 @@ public abstract class PacketByteBufMixin { } @Environment(EnvType.SERVER) - @Inject(method = "readItemStack", at = @At("RETURN"), cancellable = true) - private void polymer$decodeItemStackServer(CallbackInfoReturnable cir) { - cir.setReturnValue(PolymerItemUtils.getRealItemStack(cir.getReturnValue())); + @ModifyReturnValue(method = "readItemStack", at = @At(value = "RETURN", ordinal = 1)) + private ItemStack polymerCore$decodeItemStackServer(ItemStack stack) { + return (PolymerItemUtils.getRealItemStack(stack)); } @Environment(EnvType.CLIENT) @@ -50,9 +51,8 @@ public abstract class PacketByteBufMixin { } @Environment(EnvType.CLIENT) - @Inject(method = "readItemStack", at = @At("TAIL"), cancellable = true, locals = LocalCapture.CAPTURE_FAILHARD) - private void polymer$decodeItemStackClient(CallbackInfoReturnable cir, Item decodedItem, int i, ItemStack itemStack) { - ItemStack stack = cir.getReturnValue(); + @ModifyReturnValue(method = "readItemStack", at = @At(value = "RETURN", ordinal = 1)) + private ItemStack polymerCore$decodeItemStackClient(ItemStack stack) { var currentIndex = this.readerIndex(); this.readerIndex(this.polymer$readerIndex); var rawId = this.readVarInt(); @@ -62,15 +62,12 @@ public abstract class PacketByteBufMixin { var item = InternalClientRegistry.ITEMS.get(rawId); if (item != null) { - var count = stack.getCount(); - - stack = item.visualStack().copy(); - stack.setCount(count); - - cir.setReturnValue(stack); + return item.visualStack().copyWithCount(stack.getCount()); } - } else if (PolymerUtils.isOnPlayerNetworking() && !ClientUtils.isClientThread()) { - cir.setReturnValue(PolymerItemUtils.getRealItemStack(stack)); + } else if (PolymerCommonUtils.isNetworkingThread()) { + return PolymerItemUtils.getRealItemStack(stack); } + + return stack; } } \ No newline at end of file