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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
package de.hysky.skyblocker.skyblock;
import de.hysky.skyblocker.annotations.RegisterWidget;
import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.events.SkyblockEvents;
import de.hysky.skyblocker.skyblock.itemlist.ItemRepository;
import de.hysky.skyblocker.skyblock.tabhud.config.WidgetsConfigurationScreen;
import de.hysky.skyblocker.skyblock.tabhud.util.Ico;
import de.hysky.skyblocker.skyblock.tabhud.widget.ComponentBasedWidget;
import de.hysky.skyblocker.skyblock.tabhud.widget.component.SeparatorComponent;
import de.hysky.skyblocker.utils.Formatters;
import de.hysky.skyblocker.utils.Location;
import de.hysky.skyblocker.utils.NEURepoManager;
import de.hysky.skyblocker.utils.scheduler.Scheduler;
import io.github.moulberry.repo.data.NEUItem;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.text.HoverEvent;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@RegisterWidget
public class ItemPickupWidget extends ComponentBasedWidget {
private static final MinecraftClient CLIENT = MinecraftClient.getInstance();
private static final int LOBBY_CHANGE_DELAY = 60;
private static final String SACKS_MESSAGE_START = "[Sacks]";
private static final Pattern CHANGE_REGEX = Pattern.compile("([+-])([\\d,]+) (.+) \\((.+)\\)");
private static ItemPickupWidget instance;
private boolean changingLobby;
private final Object2ObjectOpenHashMap<String, ChangeData> addedCount = new Object2ObjectOpenHashMap<>();
private final Object2ObjectOpenHashMap<String, ChangeData> removedCount = new Object2ObjectOpenHashMap<>();
private final Object2ObjectOpenHashMap<String, ChangeData> addedSackCount = new Object2ObjectOpenHashMap<>();
private final Object2ObjectOpenHashMap<String, ChangeData> removedSackCount = new Object2ObjectOpenHashMap<>();
public ItemPickupWidget() {
super(Text.literal("Items"), Formatting.AQUA.getColorValue(), "Item Pickup");
instance = this;
ClientReceiveMessageEvents.ALLOW_GAME.register(instance::onChatMessage);
ClientPlayConnectionEvents.JOIN.register((_handler, _sender, _client) -> changingLobby = true);
// Make changingLobby true for a short period while the player loads into a new lobby and their items are loading
SkyblockEvents.LOCATION_CHANGE.register(location -> Scheduler.INSTANCE.schedule(() -> changingLobby = false, LOBBY_CHANGE_DELAY));
}
public static ItemPickupWidget getInstance() {
return instance;
}
/**
* Searches the NEU REPO for the item linked to the name
*/
private static ItemStack getItem(String itemName) {
if (NEURepoManager.isLoading() || !ItemRepository.filesImported()) return new ItemStack(Items.BARRIER);
return NEURepoManager.getItemByName(itemName)
.stream()
.findFirst()
.map(NEUItem::getSkyblockItemId)
.map(ItemRepository::getItemStack)
.orElseGet(() -> new ItemStack(Items.BARRIER));
}
/**
* Checks chat messages for a stack update message, then finds the items linked to it
*/
private boolean onChatMessage(Text message, boolean overlay) {
if (!Formatting.strip(message.getString()).startsWith(SACKS_MESSAGE_START)) return true;
if (!SkyblockerConfigManager.get().uiAndVisuals.itemPickup.sackNotifications) return true;
HoverEvent hoverEvent = message.getSiblings().getFirst().getStyle().getHoverEvent();
if (hoverEvent == null || hoverEvent.getAction() != HoverEvent.Action.SHOW_TEXT) return true;
String hoverMessage = ((HoverEvent.ShowText) hoverEvent).value().getString();
Matcher matcher = CHANGE_REGEX.matcher(hoverMessage);
while (matcher.find()) {
ItemStack item = getItem(matcher.group(3));
//positive
int existingCount = 0;
if (matcher.group(1).equals("+")) {
if (addedSackCount.containsKey(item.getNeuName())) {
existingCount = addedSackCount.get(item.getNeuName()).amount;
}
addedSackCount.put(item.getNeuName(), new ChangeData(item, existingCount + Formatters.parseNumber(matcher.group(2)).intValue(), System.currentTimeMillis()));
}
//negative
else if (matcher.group(1).equals("-")) {
if (removedSackCount.containsKey(item.getNeuName())) {
existingCount = removedSackCount.get(item.getNeuName()).amount;
}
removedSackCount.put(item.getNeuName(), new ChangeData(item, existingCount - Formatters.parseNumber(matcher.group(2)).intValue(), System.currentTimeMillis()));
}
}
return true;
}
@Override
public boolean shouldUpdateBeforeRendering() {
return true;
}
@Override
public void updateContent() {
if (MinecraftClient.getInstance().currentScreen instanceof WidgetsConfigurationScreen) {
addSimpleIcoText(Ico.BONE, "Bone ", Formatting.GREEN, "+64");
return;
}
// If the notifications should not be split, merge the counts.
boolean split = SkyblockerConfigManager.get().uiAndVisuals.itemPickup.splitNotifications;
if (!split) {
for (String item : addedSackCount.keySet()) {
ChangeData sackEntry = addedSackCount.get(item);
if (addedCount.containsKey(item)) {
ChangeData generalEntry = addedCount.get(item);
addedCount.put(item, new ChangeData(generalEntry.item, generalEntry.amount + sackEntry.amount, System.currentTimeMillis()));
} else {
addedCount.put(item, new ChangeData(sackEntry.item, sackEntry.amount, System.currentTimeMillis()));
}
}
for (String item : removedSackCount.keySet()) {
ChangeData sackEntry = removedSackCount.get(item);
if (removedCount.containsKey(item)) {
ChangeData generalEntry = removedCount.get(item);
removedCount.put(item, new ChangeData(generalEntry.item, generalEntry.amount + sackEntry.amount, System.currentTimeMillis()));
} else {
removedCount.put(item, new ChangeData(sackEntry.item, sackEntry.amount, System.currentTimeMillis()));
}
}
addedSackCount.clear();
removedSackCount.clear();
}
//add each diff item to the widget
//add positive changes
for (String item : addedCount.keySet()) {
ChangeData entry = addedCount.get(item);
String itemName = checkNextItem(entry);
if (itemName == null) {
addedCount.remove(item);
continue;
}
addSimpleIcoText(entry.item, itemName, Formatting.GREEN, Formatters.DIFF_NUMBERS.format(entry.amount));
}
//add negative changes
for (String item : removedCount.keySet()) {
ChangeData entry = removedCount.get(item);
String itemName = checkNextItem(entry);
if (itemName == null) {
removedCount.remove(item);
continue;
}
addSimpleIcoText(entry.item, itemName, Formatting.RED, Formatters.DIFF_NUMBERS.format(entry.amount));
}
if (split && !(this.addedSackCount.isEmpty() && this.removedSackCount.isEmpty())) {
// Remove the borders and some random 8 value I do not know where that comes from from the width of the widget to make it fit.
this.addComponent(new SeparatorComponent(Text.of("Sacks")));
for (String item : addedSackCount.keySet()) {
ChangeData entry = addedSackCount.get(item);
String itemName = checkNextItem(entry);
if (itemName == null) {
addedSackCount.remove(item);
continue;
}
addSimpleIcoText(entry.item, itemName, Formatting.GREEN, Formatters.DIFF_NUMBERS.format(entry.amount));
}
for (String item : removedSackCount.keySet()) {
ChangeData entry = removedSackCount.get(item);
String itemName = checkNextItem(entry);
if (itemName == null) {
removedSackCount.remove(item);
continue;
}
addSimpleIcoText(entry.item, itemName, Formatting.RED, Formatters.DIFF_NUMBERS.format(entry.amount));
}
}
}
/**
* Checks if the ChangeData has expired and if not, returns the item name for the entry
*
* @param entry ChangeData to check
* @return formatted name from ChangeData
*/
private String checkNextItem(ChangeData entry) {
//check the item has not expired
|