11package com .coflnet .mixin ;
22
33import com .coflnet .CoflModClient ;
4+ import com .google .gson .Gson ;
5+ import com .google .gson .JsonElement ;
6+ import com .google .gson .JsonObject ;
47import net .minecraft .client .MinecraftClient ;
58import net .minecraft .client .gui .screen .ingame .HandledScreen ;
9+ import net .minecraft .component .ComponentType ;
610import net .minecraft .component .DataComponentTypes ;
711import net .minecraft .item .ItemStack ;
8- import net .minecraft .network .packet .Packet ;
912import net .minecraft .network .packet .s2c .play .ScreenHandlerSlotUpdateS2CPacket ;
1013import net .minecraft .text .Text ;
1114import org .spongepowered .asm .mixin .Mixin ;
1619
1720@ Mixin (ClientPlayNetworkHandler .class )
1821public class NewItemInChestMixin {
22+
23+ private static final Gson gson = new Gson ();
24+
25+ @ Inject (method = "onScreenHandlerSlotUpdate" , at = @ At ("HEAD" ))
26+ private void onSlotUpdateHead (ScreenHandlerSlotUpdateS2CPacket packet , CallbackInfo ci ) {
27+ // Track UUID changes before the slot is updated
28+ try {
29+ if (MinecraftClient .getInstance ().player == null || MinecraftClient .getInstance ().player .currentScreenHandler == null )
30+ return ;
31+
32+ int slot = packet .getSlot ();
33+ if (slot < 0 || slot >= MinecraftClient .getInstance ().player .currentScreenHandler .slots .size ())
34+ return ;
35+
36+ ItemStack previousStack = MinecraftClient .getInstance ().player .currentScreenHandler .getSlot (slot ).getStack ();
37+ ItemStack newStack = packet .getStack ();
38+
39+ if (previousStack .isEmpty () || newStack .isEmpty ())
40+ return ;
41+
42+ String prevTitle = previousStack .getCustomName () != null ? previousStack .getCustomName ().getString () : "" ;
43+ String newTitle = newStack .getCustomName () != null ? newStack .getCustomName ().getString () : "" ;
44+
45+ // If item title is the same but UUIDs differ, map new UUID to original
46+ if (!prevTitle .isEmpty () && prevTitle .equals (newTitle )) {
47+ String prevUuid = extractUuid (previousStack );
48+ String newUuid = extractUuid (newStack );
49+
50+ if (prevUuid != null && newUuid != null && !prevUuid .equals (newUuid )) {
51+ // Find the original UUID (follow chain if exists)
52+ String originalUuid = CoflModClient .uuidToOriginalUuid .getOrDefault (prevUuid , prevUuid );
53+ CoflModClient .uuidToOriginalUuid .put (newUuid , originalUuid );
54+ }
55+ }
56+ } catch (Exception e ) {
57+ // Silently ignore errors in UUID tracking
58+ }
59+ }
1960
2061 @ Inject (method = "onScreenHandlerSlotUpdate" , at = @ At ("TAIL" ))
2162 private void onPacketReceive (ScreenHandlerSlotUpdateS2CPacket packet , CallbackInfo ci ) {
@@ -53,4 +94,19 @@ private void onPacketReceive(ScreenHandlerSlotUpdateS2CPacket packet, CallbackIn
5394 System .out .println ("[NewItemInChestMixin] Failed to process packet: " + e .getMessage ());
5495 }
5596 }
97+
98+ private String extractUuid (ItemStack stack ) {
99+ for (ComponentType <?> type : stack .getComponents ().getTypes ()) {
100+ if (type .toString ().contains ("minecraft:custom_data" )) {
101+ JsonObject stackJson = gson .fromJson (stack .get (type ).toString (), JsonObject .class );
102+ if (stackJson != null ) {
103+ JsonElement uuid = stackJson .get ("uuid" );
104+ if (uuid != null ) {
105+ return uuid .getAsString ();
106+ }
107+ }
108+ }
109+ }
110+ return null ;
111+ }
56112}
0 commit comments