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
|
package de.torui.coflsky.handlers;
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.Base64;
import java.util.HashMap;
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[]> tooltipItemUuidMap = 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();
public static String ExtractStackableIdFromItemStack(ItemStack stack) {
if (stack != null) {
try {
String uuid = stack.serializeNBT().getCompoundTag("tag").getCompoundTag("ExtraAttributes")
.getString("id") + ":" + stack.stackSize;
if (uuid.length() == 0) {
throw new Exception();
}
return uuid;
} catch (Exception e) {
}
}
return "";
}
public static String ExtractUuidFromItemStack(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 "";
}
private static DescModification[] getTooltipData(ItemStack itemStack) {
if (tooltipItemMap.containsKey(itemStack)) {
return tooltipItemMap.getOrDefault(itemStack, EMPTY_ARRAY);
}
if(!itemStack.isStackable()){
String id = ExtractUuidFromItemStack(itemStack);
if (tooltipItemUuidMap.containsKey(id)) {
return tooltipItemUuidMap.getOrDefault(id, EMPTY_ARRAY);
}
} else {
String itemId = ExtractStackableIdFromItemStack(itemStack);
if(tooltipItemIdMap.containsKey(itemId)){
return tooltipItemIdMap.getOrDefault(itemId, EMPTY_ARRAY);
}
}
return EMPTY_ARRAY;
}
public static void getTooltipDataFromBackend(GuiOpenEvent event){
InventoryWrapper wrapper = new InventoryWrapper();
GuiContainer gc = (GuiContainer) event.gui;
if (event.gui instanceof GuiChest) {
ContainerChest chest = (ContainerChest) ((GuiChest) event.gui).inventorySlots;
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();
}
}
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());
String data = WSClient.gson.toJson(wrapper);
String info = QueryServerCommands.PostRequest("https://sky.coflnet.com/api/mod/description/modifications", data);
DescModification[][] arr = WSClient.gson.fromJson(info, DescModification[][].class);
int i = 0;
for (Slot obj : gc.inventorySlots.inventorySlots) {
ItemStack stack = obj.getStack();
tooltipItemMap.put(stack, arr[i]);
String uuid = ExtractUuidFromItemStack(stack);
if(uuid.length()>0) tooltipItemUuidMap.put(uuid, arr[i]);
String id = ExtractStackableIdFromItemStack(stack);
if(id.length()>0) tooltipItemIdMap.put(id, arr[i]);
i++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static 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;
}
}
}
public static void emptyTooltipData(){
tooltipItemMap.clear();
tooltipItemIdMap.clear();
tooltipItemUuidMap.clear();
}
}
|