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
|
package de.cowtipper.cowlection.chesttracker;
import de.cowtipper.cowlection.Cowlection;
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 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 java.util.*;
public class ChestTracker {
public static long lastBazaarUpdate;
public static long lastLowestBinsUpdate;
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 Map<String, ItemData> analysisResult = new HashMap<>();
private ChestInteractionListener chestInteractionListener;
private HyBazaarData bazaarCache;
private LowestBinsCache lowestBinsCache;
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(ChestOverviewGui.Column orderBy, boolean orderDesc, boolean useInstantSellPrices) {
List<ItemData> orderedAnalysisResult = new ArrayList<>();
// sort by bazaar value (most value first)
for (Map.Entry<String, ItemData> itemEntry : analysisResult.entrySet()) {
boolean foundPriceForItem = false;
if (bazaarCache != null && bazaarCache.isSuccess()) {
String productKey = itemEntry.getKey();
HyBazaarData.Product product = bazaarCache.getProduct(productKey);
if (product != null) {
// item is sold on bazaar!
itemEntry.getValue().setBazaarInstantSellPrice(product.getInstantSellPrice());
itemEntry.getValue().setBazaarSellOfferPrice(product.getSellOfferPrice());
foundPriceForItem = true;
}
}
if (!foundPriceForItem && lowestBinsCache != null && lowestBinsCache.size() > 0) {
String productKey = itemEntry.getKey().replace(':', '-');
Integer lowestBin = lowestBinsCache.get(productKey);
if (lowestBin != null) {
// item is sold via BIN
itemEntry.getValue().setLowestBin(lowestBin);
}
}
orderedAnalysisResult.add(itemEntry.getValue());
}
Comparator<ItemData> comparator;
switch (orderBy) {
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();
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<Updating> refreshPriceCache() {
EnumSet<Updating> updating = EnumSet.of(Updating.UNDEFINED);
if (allowUpdateBazaar()) {
updating.add(Updating.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(Updating.LOWEST_BINS);
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();
});
}
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;
}
public void markChestsWithWantedItem(String sbKey, int amount, String itemName) {
// clear old search results
chestsWithWantedItem.clear();
if (sbKey.endsWith("_ambiguous")) {
sbKey = sbKey.substring(0, sbKey.length() - 10);
}
int relevantChests = 0;
for (Map.Entry<BlockPos, List<ItemStack>> chestCache : chestCache.entrySet()) {
boolean hasItemBeenFoundInChest = false;
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)) {
if (!hasItemBeenFoundInChest) {
chestsWithWantedItem.add(chestCache.getKey());
hasItemBeenFoundInChest = true;
++relevantChests;
}
amount -= item.stackSize;
}
}
if (amount <= 0) {
// already found all relevant chests
break;
}
}
main.getChatHelper().sendMessage(EnumChatFormatting.GREEN, "Chest Tracker & Analyzer is now highlighting " + EnumChatFormatting.LIGHT_PURPLE + relevantChests + EnumChatFormatting.GREEN + " chest" + (relevantChests > 1 ? "s" : "") + " with " + itemName
+ EnumChatFormatting.GREEN + ". Re-opening the chest analysis results with " + EnumChatFormatting.GRAY + "/moo analyzeChests " + EnumChatFormatting.GREEN + "clears the current search.");
}
public enum Updating {
UNDEFINED, BAZAAR, LOWEST_BINS
}
}
|