-
Notifications
You must be signed in to change notification settings - Fork 0
/
InventoryUtils.java
285 lines (254 loc) · 10.1 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
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
package net.taunahi_v3.ezskyblock.util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiChest;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.inventory.ContainerChest;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.Integer.parseInt;
public class InventoryUtils {
private static final Minecraft mc = Minecraft.getMinecraft();
public static boolean holdItem(String item) {
int slot = getSlotIdOfItemInHotbar(item);
if (slot == -1) return false;
mc.thePlayer.inventory.currentItem = slot;
return true;
}
public static int getSlotIdOfItemInContainer(String item) {
return getSlotIdOfItemInContainer(item, false);
}
public static int getSlotIdOfItemInContainer(String item, boolean equals) {
for (Slot slot : mc.thePlayer.openContainer.inventorySlots) {
if (!slot.getHasStack()) continue;
String itemName = StringUtils.stripControlCodes(slot.getStack().getDisplayName());
if (equals) {
if (itemName.equalsIgnoreCase(item)) {
return slot.slotNumber;
}
} else {
if (itemName.contains(item)) {
return slot.slotNumber;
}
}
}
return -1;
}
public static Slot getSlotOfItemInContainer(String item) {
return getSlotOfItemInContainer(item, false);
}
public static Slot getSlotOfItemInContainer(String item, boolean equals) {
for (Slot slot : mc.thePlayer.openContainer.inventorySlots) {
if (slot.getHasStack()) {
String itemName = StringUtils.stripControlCodes(slot.getStack().getDisplayName());
if (equals) {
if (itemName.equalsIgnoreCase(item)) {
return slot;
}
} else {
if (itemName.contains(item)) {
return slot;
}
}
}
}
return null;
}
public static int getSlotIdOfItemInHotbar(String... items) {
for (int i = 0; i < 9; i++) {
ItemStack slot = mc.thePlayer.inventory.getStackInSlot(i);
if (slot != null && slot.getItem() != null) {
String itemName = StringUtils.stripControlCodes(slot.getDisplayName());
if (Arrays.stream(items).anyMatch(itemName::contains)) {
return i;
}
}
}
return -1;
}
public static Slot getSlotOfItemInHotbar(String item) {
for (int i = 0; i < 9; i++) {
ItemStack slot = mc.thePlayer.inventory.getStackInSlot(i);
if (slot != null && slot.getItem() != null) {
String itemName = StringUtils.stripControlCodes(slot.getDisplayName());
if (itemName.contains(item)) {
return mc.thePlayer.inventoryContainer.getSlot(i);
}
}
}
return null;
}
public static int getSlotIdOfItemInInventory(String item) {
for (Slot slot : mc.thePlayer.inventoryContainer.inventorySlots) {
if (slot.getHasStack()) {
String itemName = StringUtils.stripControlCodes(slot.getStack().getDisplayName());
if (itemName.contains(item)) {
return slot.slotNumber;
}
}
}
return -1;
}
public static Slot getSlotOfItemInInventory(String item) {
for (Slot slot : mc.thePlayer.inventoryContainer.inventorySlots) {
if (slot.getHasStack()) {
String itemName = StringUtils.stripControlCodes(slot.getStack().getDisplayName());
if (itemName.contains(item)) {
return slot;
}
}
}
return null;
}
public static String getInventoryName() {
try {
if (mc.currentScreen instanceof GuiChest) {
final ContainerChest chest = (ContainerChest) mc.thePlayer.openContainer;
if (chest == null) return null;
final IInventory inv = chest.getLowerChestInventory();
return inv.hasCustomName() ? inv.getName() : null;
}
return null;
} catch (Exception e) {
return null;
}
}
public static boolean hasItemInInventory(String item) {
for (Slot slot : mc.thePlayer.inventoryContainer.inventorySlots) {
if (slot.getHasStack()) {
String itemName = StringUtils.stripControlCodes(slot.getStack().getDisplayName());
if (itemName.contains(item)) {
return true;
}
}
}
return false;
}
public static boolean hasItemInHotbar(String... item) {
// return getSlotIdOfItemInHotbar(item) != -1;
for (int i = 0; i < 9; i++) {
ItemStack slot = mc.thePlayer.inventory.getStackInSlot(i);
if (slot != null && slot.getItem() != null) {
String itemName = StringUtils.stripControlCodes(slot.getDisplayName());
if (Arrays.stream(item).anyMatch(itemName::contains)) {
return true;
}
}
}
return false;
}
public static ArrayList<Slot> getIndexesOfItemsFromInventory(Predicate<Slot> predicate) {
ArrayList<Slot> indexes = new ArrayList<>();
for (int i = 0; i < 36; i++) {
Slot slot = mc.thePlayer.inventoryContainer.getSlot(i);
if (slot != null && slot.getHasStack()) {
if (predicate.test(slot)) {
indexes.add(slot);
}
}
}
return indexes;
}
public static ArrayList<Slot> getIndexesOfItemsFromContainer(Predicate<Slot> predicate) {
ArrayList<Slot> indexes = new ArrayList<>();
for (int i = 0; i < mc.thePlayer.openContainer.inventorySlots.size(); i++) {
Slot slot = mc.thePlayer.openContainer.getSlot(i);
if (slot != null && slot.getHasStack()) {
if (predicate.test(slot)) {
indexes.add(slot);
}
}
}
return indexes;
}
public static void clickSlotWithId(int id, ClickType mouseButton, ClickMode mode, int windowId) {
mc.playerController.windowClick(windowId, id, mouseButton.ordinal(), mode.ordinal(), mc.thePlayer);
}
public static void clickContainerSlot(int slot, ClickType mouseButton, ClickMode mode) {
mc.playerController.windowClick(mc.thePlayer.openContainer.windowId, slot, mouseButton.ordinal(), mode.ordinal(), mc.thePlayer);
}
public static void clickSlot(int slot, ClickType mouseButton, ClickMode mode) {
mc.playerController.windowClick(mc.thePlayer.inventoryContainer.windowId, slot, mouseButton.ordinal(), mode.ordinal(), mc.thePlayer);
}
public static void swapSlots(int slot, int hotbarSlot) {
mc.playerController.windowClick(mc.thePlayer.inventoryContainer.windowId, slot, hotbarSlot, 2, mc.thePlayer);
}
public static void openInventory() {
KeyBinding.onTick(mc.gameSettings.keyBindInventory.getKeyCode());
}
public static Slot getSlotOfId(int id) {
for (Slot slot : mc.thePlayer.inventoryContainer.inventorySlots) {
if (slot.slotNumber == id) {
return slot;
}
}
return null;
}
public static Slot getSlotOfIdInContainer(int id) {
for (Slot slot : mc.thePlayer.openContainer.inventorySlots) {
if (slot.slotNumber == id) {
return slot;
}
}
return null;
}
public static ArrayList<String> getItemLore(ItemStack itemStack) {
NBTTagList loreTag = itemStack.getTagCompound().getCompoundTag("display").getTagList("Lore", 8);
ArrayList<String> loreList = new ArrayList<>();
for (int i = 0; i < loreTag.tagCount(); i++) {
loreList.add(StringUtils.stripControlCodes(loreTag.getStringTagAt(i)));
}
return loreList;
}
public static List<String> getLoreOfItemInContainer(int slot) {
if (slot == -1) return new ArrayList<>();
ItemStack itemStack = mc.thePlayer.openContainer.getSlot(slot).getStack();
if (itemStack == null) return new ArrayList<>();
return getItemLore(itemStack);
}
public static int getAmountOfItemInInventory(String item) {
int amount = 0;
for (Slot slot : mc.thePlayer.inventoryContainer.inventorySlots) {
if (slot.getHasStack()) {
String itemName = StringUtils.stripControlCodes(slot.getStack().getDisplayName());
if (itemName.equals(item)) {
amount += slot.getStack().stackSize;
}
}
}
return amount;
}
public static int getRancherBootSpeed() {
final ItemStack stack = mc.thePlayer.inventoryContainer.getSlot(8).getStack();
int speed = -1;
if (stack != null && stack.hasTagCompound()) {
final NBTTagCompound tag = stack.getTagCompound();
final Pattern pattern = Pattern.compile("Current Speed Cap: (\\d+)", Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(StringUtils.stripControlCodes(tag.toString()));
while (matcher.find()) {
if (matcher.group(1) != null) {
speed = parseInt(matcher.group(1));
}
}
}
return speed;
}
public static enum ClickType {
LEFT,
RIGHT
}
public static enum ClickMode {
PICKUP,
QUICK_MOVE,
SWAP
}
}