Skip to content

Commit 093b74c

Browse files
committed
simpler uuid switching handling
1 parent e463d9a commit 093b74c

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
@@ -116,6 +116,10 @@ public class CoflModClient implements ClientModInitializer {
116116
// Staggered refresh tracking: inventory name -> last request time
117117
private static final Map<String, Long> lastRefreshTimePerInventory = new HashMap<>();
118118
private static final long REFRESH_THROTTLE_MS = 500; // 0.5 seconds minimum between requests
119+
120+
// Maps new UUIDs to original UUID when items update with new UUIDs but same title
121+
// This allows finding descriptions loaded for the original UUID when hovering an item with updated UUID
122+
public static final Map<String, String> uuidToOriginalUuid = new HashMap<>();
119123

120124
public class TooltipMessage implements Message{
121125
private final String text;
@@ -285,7 +289,11 @@ public void onInitializeClient() {
285289

286290
ItemTooltipCallback.EVENT.register((stack, tooltipContext, tooltipType, lines) -> {
287291
String stackId = getIdFromStack(stack);
288-
if (!knownIds.contains(stackId)
292+
293+
// Check if this UUID maps to an original UUID that has descriptions
294+
String lookupId = uuidToOriginalUuid.getOrDefault(stackId, stackId);
295+
296+
if (!knownIds.contains(stackId) && !knownIds.contains(lookupId)
289297
&& MinecraftClient.getInstance().currentScreen instanceof HandledScreen<?> hs) {
290298

291299
if(!stack.isEmpty() && !stackId.equals("Go Back;1"))
@@ -295,7 +303,10 @@ public void onInitializeClient() {
295303
return;
296304
}
297305

298-
DescriptionHandler.DescModification[] tooltips = DescriptionHandler.getTooltipData(stackId);
306+
// Try to get descriptions using the original UUID if mapped
307+
DescriptionHandler.DescModification[] tooltips = DescriptionHandler.getTooltipData(lookupId);
308+
if(tooltips == null && !lookupId.equals(stackId))
309+
tooltips = DescriptionHandler.getTooltipData(stackId); // fallback to current UUID
299310
if(tooltips == null)
300311
return;
301312

@@ -882,12 +893,11 @@ public static void loadDescriptionsForItems(String title, DefaultedList<ItemStac
882893
try {
883894
Thread.sleep(delayMs);
884895
// Request with current inventory state (in case it updated)
885-
DefaultedList<ItemStack> currentItems = new DefaultedList<>();
896+
DefaultedList<ItemStack> currentItems = DefaultedList.of();
886897
HandledScreen currentScreen = MinecraftClient.getInstance().currentScreen instanceof HandledScreen
887898
? (HandledScreen) MinecraftClient.getInstance().currentScreen
888899
: null;
889900
if (currentScreen != null && currentScreen.getTitle().getString().equals(title)) {
890-
currentItems = DefaultedList.of();
891901
currentItems.addAll(currentScreen.getScreenHandler().getStacks());
892902
} else {
893903
// 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)