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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
package de.hysky.skyblocker.skyblock.searchoverlay;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.config.configs.UIAndVisualsConfig;
import de.hysky.skyblocker.skyblock.item.tooltip.TooltipInfoType;
import de.hysky.skyblocker.utils.NEURepoManager;
import de.hysky.skyblocker.utils.scheduler.MessageScheduler;
import io.github.moulberry.repo.data.NEUItem;
import it.unimi.dsi.fastutil.Pair;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.block.entity.SignBlockEntity;
import net.minecraft.client.MinecraftClient;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.network.packet.c2s.play.UpdateSignC2SPacket;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal;
public class SearchOverManager {
private static final MinecraftClient CLIENT = MinecraftClient.getInstance();
private static final Logger LOGGER = LoggerFactory.getLogger("Skyblocker Search Overlay");
private static final Pattern BAZAAR_ENCHANTMENT_PATTERN = Pattern.compile("ENCHANTMENT_(\\D*)_(\\d+)");
private static final Pattern AUCTION_PET_AND_RUNE_PATTERN = Pattern.compile("([A-Z0-9_]+);(\\d+)");
/**
* converts index (in array) +1 to a roman numeral
*/
private static final String[] ROMAN_NUMERALS = new String[]{
"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI",
"XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX"
};
private static @Nullable SignBlockEntity sign = null;
private static boolean signFront = true;
private static boolean isAuction;
private static boolean isCommand;
protected static String search = "";
// Use non-final variables and swap them to prevent concurrent modification
private static HashSet<String> bazaarItems;
private static HashSet<String> auctionItems;
private static HashMap<String, String> namesToId;
public static String[] suggestionsArray = {};
/**
* uses the skyblock api and Moulberry auction to load a list of items in bazaar and auction house
*/
public static void init() {
ClientCommandRegistrationCallback.EVENT.register(SearchOverManager::registerSearchCommands);
NEURepoManager.runAsyncAfterLoad(SearchOverManager::loadItems);
}
private static void registerSearchCommands(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) {
if (SkyblockerConfigManager.get().uiAndVisuals.searchOverlay.enableCommands) {
dispatcher.register(literal("ahs").executes(context -> startCommand(true)));
dispatcher.register(literal("bzs").executes(context -> startCommand(false)));
}
}
private static int startCommand(boolean isAuction) {
isCommand = true;
SearchOverManager.isAuction = isAuction;
search = "";
suggestionsArray = new String[]{};
CLIENT.send(() -> CLIENT.setScreen(new OverlayScreen(Text.of(""))));
return Command.SINGLE_SUCCESS;
}
private static void loadItems() {
HashSet<String> bazaarItems = new HashSet<>();
HashSet<String> auctionItems = new HashSet<>();
HashMap<String, String> namesToId = new HashMap<>();
//get bazaar items
try {
if (TooltipInfoType.BAZAAR.getData() == null) TooltipInfoType.BAZAAR.run();
JsonObject products = TooltipInfoType.BAZAAR.getData();
for (Map.Entry<String, JsonElement> entry : products.entrySet()) {
if (entry.getValue().isJsonObject()) {
JsonObject product = entry.getValue().getAsJsonObject();
String id = product.get("id").getAsString();
int sellVolume = product.get("sellVolume").getAsInt();
if (sellVolume == 0)
continue; //do not add items that do not sell e.g. they are not actual in the bazaar
Matcher matcher = BAZAAR_ENCHANTMENT_PATTERN.matcher(id);
if (matcher.matches()) {//format enchantments
//remove ultimate if in name
String name = matcher.group(1);
if (!name.contains("WISE")) { //only way found to remove ultimate from everything but ultimate wise
name = name.replace("ULTIMATE_", "");
}
name = name.replace("_", " ");
name = capitalizeFully(name);
int enchantLevel = Integer.parseInt(matcher.group(2));
String level = "";
if (enchantLevel > 0) {
level = ROMAN_NUMERALS[enchantLevel - 1];
}
bazaarItems.add(name + " " + level);
namesToId.put(name + " " + level, matcher.group(1) + ";" + matcher.group(2));
continue;
}
//look up id for name
NEUItem neuItem = NEURepoManager.NEU_REPO.getItems().getItemBySkyblockId(id);
if (neuItem != null) {
String name = Formatting.strip(neuItem.getDisplayName());
bazaarItems.add(name);
namesToId.put(name, id);
continue;
}
}
}
} catch (Exception e) {
LOGGER.error("[Skyblocker] Failed to load bazaar item list! ", e);
}
//get auction items
try {
if (TooltipInfoType.THREE_DAY_AVERAGE.getData() == null) {
TooltipInfoType.THREE_DAY_AVERAGE.run();
}
for (Map.Entry<String, JsonElement> entry : TooltipInfoType.THREE_DAY_AVERAGE.getData().entrySet()) {
String id = entry.getKey();
Matcher matcher = AUCTION_PET_AND_RUNE_PATTERN.matcher(id);
if (matcher.find()) {//is a pet or rune convert id to name
String name = matcher.group(1).replace("_", " ");
name = capitalizeFully(name);
auctionItems.add(name);
namesToId.put(name, id);
continue;
}
//something else look up in NEU repo.
id = id.split("[+-]")[0];
NEUItem neuItem = NEURepoManager.NEU_REPO.getItems().getItemBySkyblockId(id);
if (neuItem != null) {
String name = Formatting.strip(neuItem.getDisplayName());
auctionItems.add(name);
namesToId.put(name, id);
continue;
}
}
} catch (Exception e) {
LOGGER.error("[Skyblocker] Failed to load auction house item list! ", e);
}
SearchOverManager.bazaarItems = bazaarItems;
SearchOverManager.auctionItems = auctionItems;
SearchOverManager.namesToId = namesToId;
}
/**
* Capitalizes the first letter off every word in a string
* @param str string to capitalize
*/
private static String capitalizeFully(String str) {
if (str == null || str.isEmpty()) {
return str;
}
return Arrays.stream(str.split("\\s+"))
.map(t -> t.substring(0, 1).toUpperCase() + t.substring(1).toLowerCase())
.collect(Collectors.joining(" "));
}
/**
* Receives data when a search is started and resets values
* @param sign the sign that is being edited
* @param front if it's the front of the sign
* @param isAuction if the sign is loaded from the auction house menu or bazaar
*/
public static void updateSign(@NotNull SignBlockEntity sign, boolean front, boolean isAuction) {
signFront = front;
SearchOverManager.sign = sign;
isCommand = false;
SearchOverManager.isAuction = isAuction;
if (SkyblockerConfigManager.get().uiAndVisuals.searchOverlay.keepPreviousSearches) {
Text[] messages = SearchOverManager.sign.getText(signFront).getMessages(CLIENT.shouldFilterText());
search = messages[0].getString();
if (!messages[1].getString().isEmpty()) {
if (!search.endsWith(" ")) {
search += " ";
}
search += messages[1].getString();
}
} else {
search = "";
}
suggestionsArray = new String[]{};
}
/**
* Updates the search value and the suggestions based on that value.
* @param newValue new search value
*/
protected static void updateSearch(String newValue) {
search = newValue;
//update the suggestion values
int totalSuggestions = SkyblockerConfigManager.get().uiAndVisuals.searchOverlay.maxSuggestions;
if (newValue.isBlank() || totalSuggestions == 0) return; //do not search for empty value
suggestionsArray = (isAuction ? auctionItems : bazaarItems).stream().filter(item -> item.toLowerCase().contains(search.toLowerCase())).limit(totalSuggestions).toArray(String[]::new);
}
/**
* Gets the suggestion in the suggestion array at the index
* @param index index of suggestion
*/
protected static String getSuggestion(int index) {
if (suggestionsArray.length > index && suggestionsArray[index] != null) {
return suggestionsArray[index];
} else {//there are no suggestions yet
return "";
}
}
protected static String getSuggestionId(int index) {
return namesToId.get(getSuggestion(index));
}
/**
* Gets the item name in the history array at the index
* @param index index of suggestion
*/
protected static String getHistory(int index) {
if (isAuction) {
if (SkyblockerConfigManager.get().uiAndVisuals.searchOverlay.auctionHistory.size() > index) {
return SkyblockerConfigManager.get().uiAndVisuals.searchOverlay.auctionHistory.get(index);
}
} else {
if (SkyblockerConfigManager.get().uiAndVisuals.searchOverlay.bazaarHistory.size() > index) {
return SkyblockerConfigManager.get().uiAndVisuals.searchOverlay.bazaarHistory.get(index);
}
}
return null;
}
protected static String getHistoryId(int index) {
return namesToId.get(getHistory(index));
}
/**
* Add the current search value to the start of the history list and truncate to the max history value and save this to the config
*/
private static void saveHistory() {
//save to history
UIAndVisualsConfig.SearchOverlay config = SkyblockerConfigManager.get().uiAndVisuals.searchOverlay;
if (isAuction) {
if (config.auctionHistory.isEmpty() || !config.auctionHistory.getFirst().equals(search)) {
config.auctionHistory.addFirst(search);
if (config.auctionHistory.size() > config.historyLength) {
config.auctionHistory = config.auctionHistory.subList(0, config.historyLength);
}
}
} else {
if (config.bazaarHistory.isEmpty() || !config.bazaarHistory.getFirst().equals(search)) {
config.bazaarHistory.addFirst(search);
if (config.bazaarHistory.size() > config.historyLength) {
config.bazaarHistory = config.bazaarHistory.subList(0, config.historyLength);
}
}
}
SkyblockerConfigManager.save();
}
/**
*Saves the current value of ({@link SearchOverManager#search}) then pushes it to a command or sign depending on how the gui was opened
*/
protected static void pushSearch() {
//save to history
if (!search.isEmpty()) {
saveHistory();
}
if (isCommand) {
pushCommand();
} else {
pushSign();
}
}
/**
* runs the command to search for the value in ({@link SearchOverManager#search})
*/
private static void pushCommand() {
if (search.isEmpty()) return;
String command;
if (isAuction) {
command = "/ahSearch " + search;
} else {
command = "/bz " + search;
}
MessageScheduler.INSTANCE.sendMessageAfterCooldown(command);
}
/**
* pushes the ({@link SearchOverManager#search}) to the sign. It needs to split it over two lines without splitting a word
*/
private static void pushSign() {
//splits text into 2 lines max = 30 chars
Pair<String, String> split = splitString(search);
// send packet to update sign
if (CLIENT.player != null && sign != null) {
Text[] messages = sign.getText(signFront).getMessages(CLIENT.shouldFilterText());
CLIENT.player.networkHandler.sendPacket(new UpdateSignC2SPacket(sign.getPos(), signFront,
split.left(),
split.right(),
messages[2].getString(),
messages[3].getString()
));
}
}
public static Pair<String, String> splitString(String s) {
if (s.length() <= 15) {
return Pair.of(s, "");
}
int index = s.lastIndexOf(' ', 15);
if (index == -1) {
return Pair.of(s.substring(0, 15), "");
}
return Pair.of(s.substring(0, index), s.substring(index + 1, Math.min(index + 16, s.length())).trim());
}
}
|