aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/cowtipper/cowlection/chesttracker/ChestTracker.java
blob: a823401b2b571bd6e3fe591afbab002eb611e67d (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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package de.cowtipper.cowlection.chesttracker;

import de.cowtipper.cowlection.Cowlection;
import de.cowtipper.cowlection.chesttracker.data.HyBazaarData;
import de.cowtipper.cowlection.chesttracker.data.HyItemsData;
import de.cowtipper.cowlection.chesttracker.data.ItemData;
import de.cowtipper.cowlection.chesttracker.data.LowestBinsCache;
import de.cowtipper.cowlection.data.DataHelper;
import de.cowtipper.cowlection.data.HySkyBlockStats;
import de.cowtipper.cowlection.util.ApiUtils;
import de.cowtipper.cowlection.util.GsonUtils;
import de.cowtipper.cowlection.util.MooChatComponent;
import de.cowtipper.cowlection.util.Utils;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.Constants;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

public class ChestTracker {
    public static long lastBazaarUpdate;
    public static long lastLowestBinsUpdate;
    public static long lastNpcSellUpdate;
    private final Map<BlockPos, List<ItemStack>> chestCache = new HashMap<>();
    private final Map<BlockPos, EnumFacing> doubleChestCache = new HashMap<>();
    private final Set<BlockPos> chestsWithWantedItem = new HashSet<>();
    private final Set<String> hiddenItems = new HashSet<>();
    private Map<String, ItemData> analysisResult = new HashMap<>();
    private ChestInteractionListener chestInteractionListener;
    private HyBazaarData bazaarCache;
    private LowestBinsCache lowestBinsCache;
    private Map<String, Double> npcSellCache;
    private final Cowlection main;

    public ChestTracker(Cowlection main) {
        this.main = main;
        refreshPriceCache();
        chestInteractionListener = new ChestInteractionListener(main);
        MinecraftForge.EVENT_BUS.register(chestInteractionListener);
    }

    public void analyzeResults() {
        Map<String, ItemData> itemCounts = new HashMap<>();
        for (List<ItemStack> chestContents : chestCache.values()) {
            for (ItemStack item : chestContents) {
                String key = item.hasDisplayName() ? item.getDisplayName() : item.getUnlocalizedName();

                boolean isAmbiguousItem = false;
                if (item.hasTagCompound()) {
                    key = item.getTagCompound().getCompoundTag("ExtraAttributes").getString("id");
                }
                if ("PET".equals(key)) {
                    HySkyBlockStats.Profile.Pet petInfo = GsonUtils.fromJson(item.getTagCompound().getCompoundTag("ExtraAttributes").getString("petInfo"), HySkyBlockStats.Profile.Pet.class);
                    key = petInfo.getType() + ";" + petInfo.getRarity().ordinal();
                    // remove pet lvl from name, as lowest BINs also disregard it
                    String petName = item.getDisplayName();
                    int endOfPetLevel = petName.indexOf(']');
                    if (petName.startsWith(EnumChatFormatting.GRAY + "[Lvl ") && endOfPetLevel > 0) {
                        item.setStackDisplayName(EnumChatFormatting.GRAY + "[Lvl " + EnumChatFormatting.DARK_GRAY + "?" + EnumChatFormatting.GRAY + petName.substring(endOfPetLevel));
                    }
                } else if (DataHelper.AMBIGUOUS_ITEM_IDS.contains(key)) {
                    isAmbiguousItem = true;
                    key += "_ambiguous";
                }

                ItemData itemData = itemCounts.get(key);
                if (itemData == null) {
                    // item hasn't been cached yet
                    if (isAmbiguousItem) {
                        convertToDummyItem(item, key);
                    }
                    itemData = new ItemData(key, item.copy());
                }
                itemCounts.put(key, itemData.addAmount(item.stackSize));
            }
        }
        this.analysisResult = itemCounts;
    }

    private void convertToDummyItem(ItemStack item, String key) {
        NBTTagCompound itemNbtDisplay = item.getSubCompound("display", true);
        NBTTagList loreList = new NBTTagList();
        loreList.appendTag(new NBTTagString("" + EnumChatFormatting.RED + EnumChatFormatting.ITALIC + "This ambiguous item type"));
        loreList.appendTag(new NBTTagString("" + EnumChatFormatting.RED + EnumChatFormatting.ITALIC + "is not listed separately."));
        itemNbtDisplay.setTag("Lore", loreList);
        String itemName = null;
        switch (key) {
            case "ENCHANTED_BOOK_ambiguous":
                itemName = "Enchanted Book";
                break;
            case "POTION_ambiguous":
                itemName = "Potion";
                break;
            case "RUNE_ambiguous":
                itemName = "Rune";
                NBTTagCompound skullNbtTextureData = item.getSubCompound("SkullOwner", false);
                if (skullNbtTextureData != null) {
                    skullNbtTextureData.setString("Id", UUID.randomUUID().toString());
                    NBTTagCompound nbtSkin = new NBTTagCompound();
                    // set texture to Empty Rune
                    nbtSkin.setString("Value", "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvODJiODIwN2E1ZmUxOTJjZDQ3N2U5MjE0NjYxOTdjOGFmNzQ5YWYxOGRkMWVmMzg5ZTI3MzNhMmY3NGQwOTI4YiJ9fX0=");
                    skullNbtTextureData.getCompoundTag("Properties").getTagList("textures", Constants.NBT.TAG_COMPOUND).set(0, nbtSkin);
                }
                break;
            case "NEW_YEAR_CAKE_ambiguous":
                itemName = "New Year Cake";
                break;
            case "SPOOKY_PIE_ambiguous":
                itemName = "Spooky Pie";
                break;
            case "CAKE_SOUL_ambiguous":
                itemName = "Cake Soul";
                break;
        }
        if (itemName != null) {
            item.setStackDisplayName(EnumChatFormatting.GRAY + itemName);
        }
    }

    /**
     * Returns ordered analysis result with prices
     */
    public List<ItemData> getAnalysisResult(String searchQuery, ChestOverviewGui.Column orderBy, boolean orderDesc, EnumSet<ItemData.PriceType> visiblePriceTypes, boolean useInstantSellPrices) {
        List<ItemData> orderedAnalysisResult = new ArrayList<>();

        boolean checkBazaarPrices = bazaarCache != null && bazaarCache.isSuccess() && visiblePriceTypes.contains(ItemData.PriceType.BAZAAR);
        boolean checkLowestBinPrices = lowestBinsCache != null && lowestBinsCache.size() > 0 && visiblePriceTypes.contains(ItemData.PriceType.LOWEST_BIN);
        boolean checkNpcSellPrices = npcSellCache != null && npcSellCache.size() > 0 && visiblePriceTypes.contains(ItemData.PriceType.NPC_SELL);

        boolean hasSearchQuery = StringUtils.isNotEmpty(searchQuery);
        for (Map.Entry<String, ItemData> itemEntry : analysisResult.entrySet()) {
            ItemData itemData = itemEntry.getValue();

            if (hasSearchQuery
                    && !StringUtils.containsIgnoreCase(itemData.getKey(), searchQuery)
                    && !StringUtils.containsIgnoreCase(EnumChatFormatting.getTextWithoutFormattingCodes(itemData.getName()), searchQuery)) {
                // item doesn't match search query
                continue;
            }
            boolean foundPriceForItem = false;

            if (checkBazaarPrices) {
                String productKey = itemEntry.getKey();
                HyBazaarData.Product product = bazaarCache.getProduct(productKey);
                if (product != null) {
                    // item is sold on bazaar!
                    itemData.setBazaarInstantSellPrice(product.getInstantSellPrice());
                    itemData.setBazaarSellOfferPrice(product.getSellOfferPrice());
                    foundPriceForItem = true;
                }
            }
            if (!foundPriceForItem && checkLowestBinPrices) {
                String productKey = itemEntry.getKey().replace(':', '-');
                Integer lowestBin = lowestBinsCache.get(productKey);
                if (lowestBin != null) {
                    // item is sold via BIN
                    itemData.setLowestBin(lowestBin);
                    foundPriceForItem = true;
                }
            }
            if (!foundPriceForItem && checkNpcSellPrices) {
                String productKey = itemEntry.getKey();
                Double npcSellPrice = npcSellCache.get(productKey);
                if (npcSellPrice != null) {
                    // item can be sold to NPC
                    itemData.setNpcPrice(npcSellPrice);
                    // foundPriceForItem = true;
                }
            }
            itemData.setHidden(hiddenItems.contains(itemData.getKey()));
            orderedAnalysisResult.add(itemData);
        }
        Comparator<ItemData> comparator;
        switch (orderBy) {
            case PRICE_TYPE:
                comparator = Comparator.comparing(ItemData::getPriceType).reversed();
                break;
            case ITEM_NAME:
                comparator = Comparator.comparing(ItemData::getName);
                break;
            case ITEM_AMOUNT:
                comparator = Comparator.comparing(ItemData::getAmount);
                break;
            case PRICE_EACH:
                comparator = Comparator.comparing(itemData -> itemData.getPrice(useInstantSellPrices));
                break;
            default: // case PRICE_SUM:
                comparator = Comparator.comparing(itemData -> itemData.getPriceSum(useInstantSellPrices));
                break;
        }
        orderedAnalysisResult.sort((orderDesc ? comparator.reversed() : comparator).thenComparing(ItemData::getName));
        return orderedAnalysisResult;
    }

    public Set<BlockPos> getCachedPositions() {
        return chestCache.keySet();
    }

    public Set<BlockPos> getChestsWithWantedItem() {
        return chestsWithWantedItem;
    }

    public void clear() {
        MinecraftForge.EVENT_BUS.unregister(chestInteractionListener);
        chestInteractionListener = null;
        bazaarCache = null;
        lowestBinsCache = null;
        chestCache.clear();
        doubleChestCache.clear();
        chestsWithWantedItem.clear();
        hiddenItems.clear();
        analysisResult.clear();
    }

    public void addChest(BlockPos chestPos, List<ItemStack> chestContents, EnumFacing otherChestFacing) {
        if (chestContents.size() > 0) { // check if the chest is a chest we want to cache/analyze
            ItemStack firstItem = chestContents.get(0);
            if (firstItem != null && firstItem.hasDisplayName() && firstItem.getDisplayName().equals(" ") && firstItem.getItem() == Item.getItemFromBlock(Blocks.stained_glass_pane)) {
                // item in first slot of chest is a glass pane with the display name " ", indicating e.g. a minion chest which we don't want to track
                return;
            }
        }
        BlockPos mainChestPos = chestPos;

        if (otherChestFacing != EnumFacing.UP) { // we have a double chest!
            if (isOtherChestCached(chestPos, otherChestFacing)) { // other chest is cached already, update that one instead
                mainChestPos = chestPos.offset(otherChestFacing);
            }

            if (chestPos.equals(mainChestPos)) {
                doubleChestCache.put(chestPos, otherChestFacing);
            } else {
                doubleChestCache.put(mainChestPos, otherChestFacing.getOpposite());
            }
        }
        chestCache.put(mainChestPos, chestContents);
    }

    public void removeChest(BlockPos chestPos, EnumFacing otherChestFacing) {
        BlockPos mainChestPos = chestPos;

        if (otherChestFacing != EnumFacing.UP) { // we have a double chest!
            if (isOtherChestCached(chestPos, otherChestFacing)) { // other chest is cached already, update that one instead
                mainChestPos = chestPos.offset(otherChestFacing);
            }

            if (chestPos.equals(mainChestPos)) {
                doubleChestCache.remove(chestPos);
            } else {
                doubleChestCache.remove(mainChestPos);
            }
        }
        chestCache.remove(mainChestPos);
        chestsWithWantedItem.remove(mainChestPos);
    }

    private boolean isOtherChestCached(BlockPos chestPos, EnumFacing otherChestFacing) {
        BlockPos otherChestPos = chestPos.offset(otherChestFacing);
        return chestCache.containsKey(otherChestPos);
    }

    public EnumFacing getOtherChestFacing(BlockPos pos) {
        return doubleChestCache.getOrDefault(pos, EnumFacing.UP);
    }

    public EnumSet<ItemData.PriceType> refreshPriceCache() {
        EnumSet<ItemData.PriceType> updating = EnumSet.noneOf(ItemData.PriceType.class);
        if (allowUpdateBazaar()) {
            updating.add(ItemData.PriceType.BAZAAR);
            ApiUtils.fetchBazaarData(bazaarData -> {
                if (bazaarData == null || !bazaarData.isSuccess()) {
                    main.getChatHelper().sendMessage(new MooChatComponent("Error: Couldn't get Bazaar data from Hypixel API! API might be down: check status.hypixel.net").red().setUrl("https://status.hypixel.net/"));
                }
                this.bazaarCache = bazaarData;
                lastBazaarUpdate = System.currentTimeMillis();
            });
        }
        if (allowUpdateLowestBins()) {
            updating.add(ItemData.PriceType.LOWEST_BIN);
            ApiUtils.fetchLowestBins(lowestBins -> {
                if (!lowestBins.hasData()) {
                    main.getChatHelper().sendMessage(new MooChatComponent("Error: Couldn't get lowest BINs from Moulberry's API! API might be down: check if " + ApiUtils.LOWEST_BINS + " is reachable.").red().setUrl(ApiUtils.LOWEST_BINS));
                }
                this.lowestBinsCache = lowestBins;
                lastLowestBinsUpdate = System.currentTimeMillis();
            });
        }
        if (allowUpdateNpcSell()) {
            updating.add(ItemData.PriceType.NPC_SELL);
            ApiUtils.fetchItemsData(itemsData -> {
                this.npcSellCache = new HashMap<>();
                if (itemsData == null || !itemsData.isSuccess()) {
                    main.getChatHelper().sendMessage(new MooChatComponent("Error: Couldn't get Items data from Hypixel API! API might be down: check status.hypixel.net").red().setUrl("https://status.hypixel.net/"));
                } else {
                    for (HyItemsData.Item item : itemsData.getItems()) {
                        if (item.getNpcSellPrice() > 0) {
                            // item has a NPC sell price
                            this.npcSellCache.put(item.getId(), item.getNpcSellPrice());
                        }
                    }
                }
                lastNpcSellUpdate = System.currentTimeMillis();
            });
        }
        return updating;
    }

    /**
     * Allow bazaar update once per minute
     */
    public boolean allowUpdateBazaar() {
        return bazaarCache == null || bazaarCache.allowRefreshData();
    }

    /**
     * Allow lowest bins update once every 5 minutes
     */
    public boolean allowUpdateLowestBins() {
        return lowestBinsCache == null || (System.currentTimeMillis() - lastLowestBinsUpdate) > 300000;
    }

    /**
     * Allow NPC sell prices update once every 15 minutes
     */
    public boolean allowUpdateNpcSell() {
        return npcSellCache == null || (System.currentTimeMillis() - lastNpcSellUpdate) > 900000;
    }

    public boolean allowAnyPriceUpdate() {
        return allowUpdateBazaar() || allowUpdateLowestBins() || allowUpdateNpcSell();
    }

    public void markChestsWithWantedItem(String sbKey, int amount, String itemName) {
        // clear old search results
        chestsWithWantedItem.clear();

        Map<BlockPos, Integer> chestsWithWantedItemsAndCount = new TreeMap<>();

        if (sbKey.endsWith("_ambiguous")) {
            sbKey = sbKey.substring(0, sbKey.length() - 10);
        }
        int mostWantedItemsInOneChest = 0;
        for (Map.Entry<BlockPos, List<ItemStack>> chestCache : chestCache.entrySet()) {
            int foundWantedItemsInChest = 0;
            for (ItemStack item : chestCache.getValue()) {
                String key = item.hasDisplayName() ? item.getDisplayName() : item.getUnlocalizedName();
                if (item.hasTagCompound()) {
                    key = item.getTagCompound().getCompoundTag("ExtraAttributes").getString("id");
                }
                if (sbKey.equals(key)) {
                    foundWantedItemsInChest += item.stackSize;
                }
            }
            if (foundWantedItemsInChest > 0) {
                // chest was a match!
                chestsWithWantedItemsAndCount.put(chestCache.getKey(), foundWantedItemsInChest);
                if (foundWantedItemsInChest > mostWantedItemsInOneChest) {
                    mostWantedItemsInOneChest = foundWantedItemsInChest;
                }
            }
            amount -= foundWantedItemsInChest;
            if (amount <= 0) {
                // already found all relevant chests
                break;
            }
        }

        int relevantChestCount = chestsWithWantedItemsAndCount.size();
        int relevantChestsCoordsLimit = 30;
        int maxItemCountLength = Utils.formatNumber(mostWantedItemsInOneChest).length();
        final MooChatComponent relevantChestCoordsHover = new MooChatComponent("Chests with ").gold().bold().appendSibling(new MooChatComponent(itemName).reset())
                .appendFreshSibling(new MooChatComponent(StringUtils.repeat(' ', maxItemCountLength) + "        (x | y | z)").gray());

        chestsWithWantedItemsAndCount.entrySet().stream()
                .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                .limit(relevantChestsCoordsLimit)
                .forEach(wantedChest -> {
                    BlockPos chestPos = wantedChest.getKey();
                    String itemCountInChest = StringUtils.leftPad(Utils.formatNumber(wantedChest.getValue()), maxItemCountLength, ' ');

                    relevantChestCoordsHover.appendFreshSibling(new MooChatComponent(" " + EnumChatFormatting.YELLOW + itemCountInChest + EnumChatFormatting.DARK_GRAY + "x ➜ "
                            + EnumChatFormatting.WHITE + chestPos.getX() + EnumChatFormatting.DARK_GRAY
                            + " | " + EnumChatFormatting.WHITE + chestPos.getY() + EnumChatFormatting.DARK_GRAY
                            + " | " + EnumChatFormatting.WHITE + chestPos.getZ()).white());
                });
        if (relevantChestCount > relevantChestsCoordsLimit) {
            relevantChestCoordsHover.appendFreshSibling(new MooChatComponent("      + " + (relevantChestCount - relevantChestsCoordsLimit) + " more chests").gray());
        }
        main.getChatHelper().sendMessage(new MooChatComponent("Chest Tracker & Analyzer is now highlighting " + EnumChatFormatting.LIGHT_PURPLE + relevantChestCount + EnumChatFormatting.GREEN + " chest" + (relevantChestCount > 1 ? "s" : "") + " with " + itemName
                + EnumChatFormatting.DARK_GREEN + " [hover for coords, click to re-open GUI]").green()
                .setHover(relevantChestCoordsHover)
                .setSuggestCommand("/moo analyzeChests", false));
        chestsWithWantedItem.addAll(chestsWithWantedItemsAndCount.keySet());
    }

    public void toggleHiddenStateForItem(String key) {
        boolean removed = hiddenItems.remove(key);
        if (!removed) {
            hiddenItems.add(key);
        }
    }
}