-
-
Notifications
You must be signed in to change notification settings - Fork 247
/
InventoryUtils.java
252 lines (222 loc) · 9.86 KB
/
InventoryUtils.java
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
package codes.biscuit.skyblockaddons.utils;
import codes.biscuit.skyblockaddons.SkyblockAddons;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.inventory.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;
import java.util.*;
/**
* Utility methods related to player inventories
*/
public class InventoryUtils {
private static final String SUMMONING_EYE_DISPLAY_NAME = "\u00A75Summoning Eye";
/**
* Display name of the Quiver Arrow item
*/
private static final String QUIVER_ARROW_DISPLAY_NAME = "\u00A78Quiver Arrow";
/**
* Display name of the Skeleton Helmet
*/
private static final String SKELETON_HELMET_DISPLAY_NAME = "Skeleton's Helmet";
private List<ItemStack> previousInventory;
private Multimap<String, ItemDiff> itemPickupLog = ArrayListMultimap.create();
private boolean inventoryIsFull;
private boolean wearingSkeletonHelmet;
private SkyblockAddons main;
public InventoryUtils(SkyblockAddons main) {
this.main = main;
}
/**
* Copies an inventory into a List of copied ItemStacks
*
* @param inventory Inventory to copy
* @return List of copied ItemStacks
*/
private List<ItemStack> copyInventory(ItemStack[] inventory) {
List<ItemStack> copy = new ArrayList<>(inventory.length);
for (ItemStack item : inventory) {
if (item != null) {
copy.add(ItemStack.copyItemStack(item));
} else {
copy.add(null);
}
}
return copy;
}
/**
* Compares previously recorded Inventory state with current Inventory state to determine changes and
* stores them in {@link #itemPickupLog}
*
* @param currentInventory Current Inventory state
*/
public void getInventoryDifference(ItemStack[] currentInventory) {
List<ItemStack> newInventory = copyInventory(currentInventory);
Map<String, Integer> previousInventoryMap = new HashMap<>();
Map<String, Integer> newInventoryMap = new HashMap<>();
if (previousInventory != null) {
for(int i = 0; i < newInventory.size(); i++) {
ItemStack previousItem = previousInventory.get(i);
ItemStack newItem = newInventory.get(i);
if(previousItem != null) {
int amount = previousInventoryMap.getOrDefault(previousItem.getDisplayName(), 0) + previousItem.stackSize;
previousInventoryMap.put(previousItem.getDisplayName(), amount);
}
if(newItem != null) {
if(newItem.getDisplayName().equals(QUIVER_ARROW_DISPLAY_NAME)) {
newInventory.set(i, previousItem);
if(previousItem != null) {
newItem = previousItem;
} else {
continue;
}
}
if (newItem.getDisplayName().contains(" "+ EnumChatFormatting.DARK_GRAY+"x")) {
String newName = newItem.getDisplayName().substring(0, newItem.getDisplayName().lastIndexOf(" "));
newItem.setStackDisplayName(newName); // This is a workaround for merchants, it adds x64 or whatever to the end of the name.
}
int amount = newInventoryMap.getOrDefault(newItem.getDisplayName(), 0) + newItem.stackSize;
newInventoryMap.put(newItem.getDisplayName(), amount);
}
}
List<ItemDiff> inventoryDifference = new LinkedList<>();
Set<String> keySet = new HashSet<>(previousInventoryMap.keySet());
keySet.addAll(newInventoryMap.keySet());
keySet.forEach(key -> {
int previousAmount = previousInventoryMap.getOrDefault(key, 0);
int newAmount = newInventoryMap.getOrDefault(key, 0);
int diff = newAmount - previousAmount;
if (diff != 0) {
inventoryDifference.add(new ItemDiff(key, diff));
}
});
// Add changes to already logged changes of the same item, so it will increase/decrease the amount
// instead of displaying the same item twice
for (ItemDiff diff : inventoryDifference) {
Collection<ItemDiff> itemDiffs = itemPickupLog.get(diff.getDisplayName());
if (itemDiffs.size() <= 0) {
itemPickupLog.put(diff.getDisplayName(), diff);
} else {
boolean added = false;
for (ItemDiff loopDiff : itemDiffs) {
if ((diff.getAmount() < 0 && loopDiff.getAmount() < 0) ||
(diff.getAmount() > 0 && loopDiff.getAmount() > 0)) {
loopDiff.add(diff.getAmount());
added = true;
}
}
if (!added) itemPickupLog.put(diff.getDisplayName(), diff);
}
if (main.getConfigValues().isEnabled(Feature.SUMMONING_EYE_ALERT)
&& diff.getAmount() == 1 && diff.getDisplayName().equals(SUMMONING_EYE_DISPLAY_NAME)
&& (main.getUtils().getLocation() == EnumUtils.Location.THE_END || main.getUtils().getLocation() == EnumUtils.Location.DRAGONS_NEST)
&& main.getPlayerListener().didntRecentlyCloseScreen()){
main.getUtils().playSound("random.orb", 0.5);
main.getRenderListener().setTitleFeature(Feature.SUMMONING_EYE_ALERT);
main.getScheduler().schedule(Scheduler.CommandType.RESET_TITLE_FEATURE, main.getConfigValues().getWarningSeconds());
}
}
}
previousInventory = newInventory;
}
/**
* Resets the previously stored Inventory state
*/
public void resetPreviousInventory() {
previousInventory = null;
}
/**
* Removes items in the pickup log that have been there for longer than {@link ItemDiff#LIFESPAN}
*/
public void cleanUpPickupLog() {
itemPickupLog.entries().removeIf(entry -> entry.getValue().getLifetime() > ItemDiff.LIFESPAN);
}
/**
* Checks if the players inventory is full and displays an alarm if so.
*
* @param mc Minecraft instance
* @param p Player to check
*/
public void checkIfInventoryIsFull(Minecraft mc, EntityPlayerSP p) {
if (main.getUtils().isOnSkyblock() && main.getConfigValues().isEnabled(Feature.FULL_INVENTORY_WARNING)) {
for (ItemStack item : p.inventory.mainInventory) {
if (item == null) {
inventoryIsFull = false;
return;
}
}
if (!inventoryIsFull) {
inventoryIsFull = true;
if (mc.currentScreen == null && main.getPlayerListener().didntRecentlyJoinWorld()) {
main.getUtils().playSound("random.orb", 0.5);
main.getRenderListener().setTitleFeature(Feature.FULL_INVENTORY_WARNING);
main.getScheduler().schedule(Scheduler.CommandType.RESET_TITLE_FEATURE, main.getConfigValues().getWarningSeconds());
}
}
}
}
/**
* Checks if the player is wearing a Skeleton Helmet and updates {@link #wearingSkeletonHelmet} accordingly
*
* @param p Player to check
*/
public void checkIfWearingSkeletonHelmet(EntityPlayerSP p) {
ItemStack item = p.getEquipmentInSlot(4);
if (item != null && item.hasDisplayName() && item.getDisplayName().contains(SKELETON_HELMET_DISPLAY_NAME)) {
wearingSkeletonHelmet = true;
return;
}
wearingSkeletonHelmet = false;
}
/**
* @return Whether the player is wearing a Skeleton Helmet
*/
public boolean isWearingSkeletonHelmet() {
return wearingSkeletonHelmet;
}
/**
* @return Log of recent Inventory changes
*/
public Collection<ItemDiff> getItemPickupLog() {
return itemPickupLog.values();
}
private Item lastItem = null;
private long lastDrop = System.currentTimeMillis();
private int dropCount = 1;
public boolean shouldCancelDrop(Slot slot) {
if (slot != null && slot.getHasStack()) {
ItemStack stack = slot.getStack();
return shouldCancelDrop(stack);
}
return false;
}
public boolean shouldCancelDrop(ItemStack stack) {
if (main.getUtils().cantDropItem(stack, EnumUtils.Rarity.getRarity(stack), false)) {
Item item = stack.getItem();
if (lastItem != null && lastItem == item && System.currentTimeMillis() - lastDrop < 3000 && dropCount >= 2) {
lastDrop = System.currentTimeMillis();
} else {
if (lastItem == item) {
if (System.currentTimeMillis() - lastDrop > 3000) {
dropCount = 1;
} else {
dropCount++;
}
} else {
dropCount = 1;
}
SkyblockAddons.getInstance().getUtils().sendMessage(main.getConfigValues().getColor(
Feature.STOP_DROPPING_SELLING_RARE_ITEMS).getChatFormatting() +
Message.MESSAGE_CLICK_MORE_TIMES.getMessage(String.valueOf(3-dropCount)));
lastItem = item;
lastDrop = System.currentTimeMillis();
main.getUtils().playSound("note.bass", 0.5);
return true;
}
}
return false;
}
}