Skip to content

Commit 86c24ae

Browse files
committed
simpler uuid switching handling
1 parent 6ec009b commit 86c24ae

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

src/client/java/com/coflnet/CoflModClient.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ public class CoflModClient implements ClientModInitializer {
113113
// Staggered refresh tracking: inventory name -> last request time
114114
private static final Map<String, Long> lastRefreshTimePerInventory = new HashMap<>();
115115
private static final long REFRESH_THROTTLE_MS = 500; // 0.5 seconds minimum between requests
116+
117+
// Maps new UUIDs to original UUID when items update with new UUIDs but same title
118+
// This allows finding descriptions loaded for the original UUID when hovering an item with updated UUID
119+
public static final Map<String, String> uuidToOriginalUuid = new HashMap<>();
116120

117121
public class TooltipMessage implements Message{
118122
private final String text;
@@ -282,7 +286,11 @@ public void onInitializeClient() {
282286

283287
ItemTooltipCallback.EVENT.register((stack, tooltipContext, tooltipType, lines) -> {
284288
String stackId = getIdFromStack(stack);
285-
if (!knownIds.contains(stackId)
289+
290+
// Check if this UUID maps to an original UUID that has descriptions
291+
String lookupId = uuidToOriginalUuid.getOrDefault(stackId, stackId);
292+
293+
if (!knownIds.contains(stackId) && !knownIds.contains(lookupId)
286294
&& MinecraftClient.getInstance().currentScreen instanceof HandledScreen<?> hs) {
287295

288296
if(!stack.isEmpty() && !stackId.equals("Go Back;1"))
@@ -292,7 +300,10 @@ public void onInitializeClient() {
292300
return;
293301
}
294302

295-
DescriptionHandler.DescModification[] tooltips = DescriptionHandler.getTooltipData(stackId);
303+
// Try to get descriptions using the original UUID if mapped
304+
DescriptionHandler.DescModification[] tooltips = DescriptionHandler.getTooltipData(lookupId);
305+
if(tooltips == null && !lookupId.equals(stackId))
306+
tooltips = DescriptionHandler.getTooltipData(stackId); // fallback to current UUID
296307
if(tooltips == null)
297308
return;
298309

@@ -864,12 +875,11 @@ public static void loadDescriptionsForItems(String title, DefaultedList<ItemStac
864875
try {
865876
Thread.sleep(delayMs);
866877
// Request with current inventory state (in case it updated)
867-
DefaultedList<ItemStack> currentItems = new DefaultedList<>();
878+
DefaultedList<ItemStack> currentItems = DefaultedList.of();
868879
HandledScreen currentScreen = MinecraftClient.getInstance().currentScreen instanceof HandledScreen
869880
? (HandledScreen) MinecraftClient.getInstance().currentScreen
870881
: null;
871882
if (currentScreen != null && currentScreen.getTitle().getString().equals(title)) {
872-
currentItems = DefaultedList.of();
873883
currentItems.addAll(currentScreen.getScreenHandler().getStacks());
874884
} else {
875885
// Inventory changed, use the items we have

src/client/java/com/coflnet/mixin/NewItemInChestMixin.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.coflnet.mixin;
22

33
import com.coflnet.CoflModClient;
4+
import com.google.gson.Gson;
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonObject;
47
import net.minecraft.client.MinecraftClient;
58
import net.minecraft.client.gui.screen.ingame.HandledScreen;
9+
import net.minecraft.component.ComponentType;
610
import net.minecraft.component.DataComponentTypes;
711
import net.minecraft.item.ItemStack;
8-
import net.minecraft.network.packet.Packet;
912
import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket;
1013
import net.minecraft.text.Text;
1114
import org.spongepowered.asm.mixin.Mixin;
@@ -16,6 +19,44 @@
1619

1720
@Mixin(ClientPlayNetworkHandler.class)
1821
public 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

Comments
 (0)