1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
package de.torui.coflsky.handlers;
import de.torui.coflsky.Config;
import de.torui.coflsky.network.QueryServerCommands;
import de.torui.coflsky.network.WSClient;
import net.minecraft.client.gui.inventory.GuiChest;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.inventory.ContainerChest;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
public class DescriptionHandler {
private static class InventoryWrapper {
public String chestName;
public String fullInventoryNbt;
}
private static class DescModification {
public String type;
public String value;
public int line;
}
public static HashMap<ItemStack, DescModification[]> tooltipItemMap = new HashMap<>();
public static HashMap<String, DescModification[]> tooltipItemIdMap = new HashMap<>();
public static final DescModification[] EMPTY_ARRAY = new DescModification[0];
public static final NBTTagCompound EMPTY_COMPOUND = new NBTTagCompound();
private boolean IsOpen = true;
private boolean shouldUpdate = false;
public void Close() {
IsOpen = false;
}
public static String ExtractStackableIdFromItemStack(ItemStack stack) {
if (stack != null) {
try {
NBTTagCompound serialized = stack.serializeNBT();
String itemTag = serialized.getCompoundTag("tag").getCompoundTag("ExtraAttributes")
.getString("id");
if (itemTag != null && itemTag.length() > 1)
return itemTag + ":" + stack.stackSize;
return serialized.getCompoundTag("tag").getCompoundTag("display")
.getString("Name");
} catch (Exception e) {
}
}
return "";
}
public static String ExtractIdFromItemStack(ItemStack stack) {
if (stack != null) {
try {
String uuid = stack.serializeNBT().getCompoundTag("tag").getCompoundTag("ExtraAttributes")
.getString("uuid");
if (uuid.length() == 0) {
throw new Exception();
}
return uuid;
} catch (Exception e) {
}
}
return ExtractStackableIdFromItemStack(stack);
}
private DescModification[] getTooltipData(ItemStack itemStack) {
if (tooltipItemMap.containsKey(itemStack)) {
return tooltipItemMap.getOrDefault(itemStack, EMPTY_ARRAY);
}
String id = ExtractIdFromItemStack(itemStack);
if (tooltipItemIdMap.containsKey(id)) {
return tooltipItemIdMap.getOrDefault(id, EMPTY_ARRAY);
}
shouldUpdate = true;
return EMPTY_ARRAY;
}
/**
* Called when the inventory is opened
* checks for changes every once in a while and updates the description if
* there was a change found
*
* @param event
*/
public void loadDescriptionAndListenForChanges(GuiOpenEvent event) {
GuiContainer gc = (GuiContainer) event.gui;
loadDescriptionForInventory(event, gc, false);
int iteration = 1;
while (IsOpen) {
try {
Thread.sleep(300 + iteration++);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (shouldUpdate || hasAnyStackChanged(gc)) {
shouldUpdate = false;
loadDescriptionForInventory(event, gc, true);
// reduce update time since its more likely that more changes occure after one
iteration = 5;
}
if (iteration >= 30)
iteration = 29; // cap at 9 second update interval
}
}
private static boolean hasAnyStackChanged(GuiContainer gc) {
for (Slot obj : gc.inventorySlots.inventorySlots) {
ItemStack stack = obj.getStack();
if (stack != null && !tooltipItemMap.containsKey(stack)) {
return true;
}
}
return false;
}
private static void loadDescriptionForInventory(GuiOpenEvent event, GuiContainer gc, boolean skipLoadCheck) {
InventoryWrapper wrapper = new InventoryWrapper();
if (event.gui instanceof GuiChest) {
if (!skipLoadCheck)
waitForChestContentLoad(event, gc);
ContainerChest chest = (ContainerChest) ((GuiChest) event.gui).inventorySlots;
IInventory inv = chest.getLowerChestInventory();
if (inv.hasCustomName()) {
String chestName = inv.getName();
wrapper.chestName = chestName;
}
}
NBTTagCompound compound = new NBTTagCompound();
NBTTagList tl = new NBTTagList();
for (Slot obj : gc.inventorySlots.inventorySlots) {
ItemStack stack = obj.getStack();
if (stack != null) {
tl.appendTag(stack.serializeNBT());
} else {
tl.appendTag(EMPTY_COMPOUND);
}
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
compound.setTag("i", tl);
CompressedStreamTools.writeCompressed(compound, baos);
wrapper.fullInventoryNbt = Base64.getEncoder().encodeToString(baos.toByteArray());
List<ItemStack> stacks = new ArrayList<>();
for (Slot obj : gc.inventorySlots.inventorySlots) {
stacks.add(obj.getStack());
}
String data = WSClient.gson.toJson(wrapper);
String info = QueryServerCommands.PostRequest(Config.BaseUrl + "/api/mod/description/modifications", data);
DescModification[][] arr = WSClient.gson.fromJson(info, DescModification[][].class);
int i = 0;
for (ItemStack stack : stacks) {
tooltipItemMap.put(stack, arr[i]);
String id = ExtractIdFromItemStack(stack);
if (id.length() > 0)
tooltipItemIdMap.put(id, arr[i]);
i++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void waitForChestContentLoad(GuiOpenEvent event, GuiContainer gc) {
for (int i = 1; i < 10; i++) {
if (gc.inventorySlots.inventorySlots.get(gc.inventorySlots.inventorySlots.size() - 37).getStack() != null)
break;
try {
// incremental backoff to wait for all inventory packages to arrive
// (each slot is sent individually)
Thread.sleep(20 * i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void setTooltips(ItemTooltipEvent event) {
DescModification[] data = getTooltipData(event.itemStack);
if (data == null || data.length == 0)
return;
for (DescModification datum : data) {
if (event.toolTip.size() <= datum.line) {
System.out.println(
"Skipped line modification " + datum.line + " for " + event.itemStack.getDisplayName());
continue;
}
switch (datum.type) {
case "APPEND":
event.toolTip.add(datum.value);
break;
case "REPLACE":
event.toolTip.set(datum.line, datum.value);
break;
case "INSERT":
event.toolTip.add(datum.line, datum.value);
break;
case "DELETE":
event.toolTip.remove(datum.line);
break;
}
}
}
/**
* Called when the inventory is closed
*/
public static void emptyTooltipData() {
tooltipItemMap.clear();
tooltipItemIdMap.clear();
tooltipItemIdMap.clear();
}
}
|