aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/skyblock/searchOverlay/SearchOverManager.java
blob: c076edc7671881f10fc26f5adfdb068d659f4dc8 (plain)
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
package de.hysky.skyblocker.skyblock.searchOverlay;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import de.hysky.skyblocker.SkyblockerMod;
import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.utils.Http;
import net.minecraft.block.entity.SignBlockEntity;
import net.minecraft.client.MinecraftClient;
import net.minecraft.network.packet.c2s.play.UpdateSignC2SPacket;
import net.minecraft.text.Text;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.jetbrains.annotations.Nullable;

import java.net.http.HttpHeaders;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class SearchOverManager {

    private static final MinecraftClient CLIENT = MinecraftClient.getInstance();

    private static final String THREE_DAY_AVERAGE = "https://moulberry.codes/auction_averages_lbin/3day.json";
    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+)");
    private static final Pattern AUCTION_PET_SKIN_PATTERN = Pattern.compile("PET_SKIN_(\\D*)");
    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"
    };

    public static boolean visible = false;
    public static String search = "";
    private static @Nullable SignBlockEntity Sign = null;
    private static boolean SignFront = true;

    private static boolean IsAuction;


    public static Map<String,String> itemNameLookup = new HashMap<>();
    public static HashSet<String> bazaarItems =new HashSet<>();
    public static HashSet<String> auctionItems =new HashSet<>();

    public static String[] suggestionsArray = {};
    public static void init() {
        //get bazaar items
        try {
            String response = Http.sendGetRequest("https://api.hypixel.net/v2/resources/skyblock/items");
            JsonArray items = JsonParser.parseString(response).getAsJsonObject().getAsJsonArray("items");
            for (JsonElement entry : items) {
                if (entry.isJsonObject()) {
                    JsonObject item = entry.getAsJsonObject();
                    String itemId = item.get("id").getAsString();
                    String itemName = item.get("name").getAsString();
                    itemNameLookup.put(itemId,itemName);
                }

            }
        } catch (Exception e) {
            //can not get items skyblock items
        }
        try (Http.ApiResponse response = Http.sendHypixelRequest("skyblock/bazaar", "")) {
            JsonObject products = JsonParser.parseString(response.content()).getAsJsonObject().get("products").getAsJsonObject();
            for (Map.Entry<String, JsonElement> entry : products.entrySet()) {
                if (entry.getValue().isJsonObject()) {
                    JsonObject product = entry.getValue().getAsJsonObject();
                    String id = product.get("product_id").getAsString();
                    int sellVolume = product.get("quick_status").getAsJsonObject().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).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);
                    }else{//look up id for name
                        String name = itemNameLookup.get(product.get("product_id").getAsString());
                        if (name != null){
                            name = trimItemColor(name);
                            bazaarItems.add(name);
                        }
                    }
                }
            }


        } catch (Exception e) {
           //can not get items for bazaar search //todo log
        }
        //get auction items
        try {
            JsonObject AuctionData = SkyblockerMod.GSON.fromJson(Http.sendGetRequest(THREE_DAY_AVERAGE), JsonObject.class);
            for (Map.Entry<String, JsonElement> entry : AuctionData.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);
                    continue;
                }
                 matcher = AUCTION_PET_SKIN_PATTERN.matcher(id);
                if (matcher.find()){//is a pet skin
                    String name = matcher.group(1).replace("_", " ");
                    name = capitalizeFully(name);
                    //put name of pet first however does not work with multi-word pets e.g. black cat.
                    if (name.contains(" ")){
                        String[] splitName = name.split(" ",2);
                        name = splitName[1] + " " + splitName[0];
                    }
                    name += " Skin";
                    auctionItems.add(name);
                    continue;
                }
                //something else just loop up id.
                id = id.split("[+;-]")[0];
                String name = itemNameLookup.get(id);
                if (name != null){
                    name = trimItemColor(name);
                    auctionItems.add(name);
                }
            }


        } catch (Exception e) {
           //can not find ah todo logger
            System.out.println(e);
        }

    }

    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(" "));
    }

    private static String trimItemColor(String string){
        if (string.isEmpty()) return string;
        if (string.startsWith("§") ){
            return string.substring(2);
        }else {
            return string;
        }
    }

    public static void updateSign(SignBlockEntity sign, boolean front, boolean isAuction) {
        visible= true;
        SignFront = front;
        Sign = sign;
        IsAuction = isAuction;
        if (SkyblockerConfigManager.get().general.searchOverlay.keepPreviousSearches){
            Text[] messages = 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[]{};

    }
    protected static void updateSearch(String newValue) {
        search = newValue;
        //update the suggestion values
        int totalSuggestions = SkyblockerConfigManager.get().general.searchOverlay.maxSuggestions;
        suggestionsArray = new String[totalSuggestions];
        if (newValue.isBlank() || totalSuggestions == 0) return; //do not search for empty value
        if (IsAuction){
            suggestionsArray = auctionItems.stream().filter(item -> item.toLowerCase().contains(search.toLowerCase())).limit(totalSuggestions).toList().toArray(suggestionsArray);
        }else {
            suggestionsArray = bazaarItems.stream().filter(item -> item.toLowerCase().contains(search.toLowerCase())).limit(totalSuggestions).toList().toArray(suggestionsArray);
        }
    }
    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 getHistory(int index){
        if (IsAuction){
            if (SkyblockerConfigManager.get().general.searchOverlay.auctionHistory.size() >index){
                return  SkyblockerConfigManager.get().general.searchOverlay.auctionHistory.get(index);
            }

        }else{
            if (SkyblockerConfigManager.get().general.searchOverlay.bazaarHistory.size() >index){
                return  SkyblockerConfigManager.get().general.searchOverlay.bazaarHistory.get(index);
            }
        }
        return  null;
    }
    private static void saveHistory(){
        //save to history
        int historyLength = SkyblockerConfigManager.get().general.searchOverlay.historyLength;
        if (IsAuction){
            SkyblockerConfigManager.get().general.searchOverlay.auctionHistory.add(0,search);
            if (SkyblockerConfigManager.get().general.searchOverlay.auctionHistory.size() >historyLength) {
                SkyblockerConfigManager.get().general.searchOverlay.auctionHistory = SkyblockerConfigManager.get().general.searchOverlay.auctionHistory.subList(0, historyLength);
            }
        }else{
            SkyblockerConfigManager.get().general.searchOverlay.bazaarHistory.add(0,search);
            if (SkyblockerConfigManager.get().general.searchOverlay.bazaarHistory.size() >historyLength) {
                SkyblockerConfigManager.get().general.searchOverlay.bazaarHistory = SkyblockerConfigManager.get().general.searchOverlay.bazaarHistory.subList(0, historyLength);
            }
        }
        SkyblockerConfigManager.save();
    }

    protected static void pushSearch() {
        //save to history
        if (!search.isEmpty()){
            saveHistory();
        }
        //splits text into 2 lines max = 30 chars
        StringBuilder line0 = new StringBuilder();
        String line1;
        if (search.length() <= 15){
            line0 = new StringBuilder(search);
            line1 = "";
        }else {
            String[] words = search.split(" ");
            for (String word : words){
                if (line0.isEmpty()) {
                    line0 = new StringBuilder(word);
                    continue;
                }
                if (line0.length() + word.length() < 14 ){ //max 15 but including space is 14
                    line0.append(" ").append(word);
                }
                else {
                    break;
                }
            }
            line1 = search.substring(line0.length(),Math.min(search.length(),30));
        }

        // 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,
                    line0.toString(),
                    line1,
                    messages[2].getString(),
                    messages[3].getString()
            ));
        }
    }

}