Skip to content

Commit 5cd1b05

Browse files
committed
Update inventory utils
- fix inconsistencies with the offhand - add index to id translations for missing screens - add some documentation closes #5598 closes #3762
1 parent 4da2b5c commit 5cd1b05

5 files changed

Lines changed: 85 additions & 25 deletions

File tree

src/main/java/meteordevelopment/meteorclient/commands/commands/DropCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public void build(LiteralArgumentBuilder<CommandSource> builder) {
4444

4545
// Main Inv
4646
builder.then(literal("inventory").executes(context -> drop(player -> {
47-
for (int i = 9; i < player.getInventory().getMainStacks().size(); i++) {
48-
InvUtils.drop().slotMain(i - 9);
47+
for (int i = 0; i < 27; i++) {
48+
InvUtils.drop().slotMain(i);
4949
}
5050
})));
5151

src/main/java/meteordevelopment/meteorclient/systems/modules/player/AutoEat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ private void eat() {
193193
}
194194

195195
private void stopEating() {
196-
changeSlot(prevSlot);
196+
if (prevSlot != SlotUtils.OFFHAND) changeSlot(prevSlot);
197197
setPressed(false);
198198

199199
eating = false;

src/main/java/meteordevelopment/meteorclient/utils/player/FindItemResult.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
import static meteordevelopment.meteorclient.MeteorClient.mc;
1111

12+
/**
13+
* @param slot The slot index
14+
* @param count The number of items in the slot
15+
*/
1216
public record FindItemResult(int slot, int count) {
1317
public boolean found() {
1418
return slot != -1;

src/main/java/meteordevelopment/meteorclient/utils/player/InvUtils.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@
77

88
import meteordevelopment.meteorclient.mixininterface.IClientPlayerInteractionManager;
99
import net.minecraft.block.BlockState;
10+
import net.minecraft.entity.EquipmentSlot;
1011
import net.minecraft.item.Item;
1112
import net.minecraft.item.ItemStack;
1213
import net.minecraft.screen.ScreenHandler;
1314
import net.minecraft.screen.slot.SlotActionType;
15+
import org.jetbrains.annotations.Range;
1416

1517
import java.util.function.Predicate;
1618

1719
import static meteordevelopment.meteorclient.MeteorClient.mc;
1820

21+
/**
22+
* Util class for dealing inventories and the items in them.
23+
* See {@link SlotUtils} for details on how slots are referenced.
24+
*/
1925
public class InvUtils {
2026
private static final Action ACTION = new Action();
2127
public static int previousSlot = -1;
@@ -224,22 +230,28 @@ public Action fromId(int id) {
224230
return this;
225231
}
226232

233+
/**
234+
* @param index The index of one of the slots within the inventory
235+
*/
227236
public Action from(int index) {
228237
return fromId(SlotUtils.indexToId(index));
229238
}
230239

231-
public Action fromHotbar(int i) {
240+
public Action fromHotbar(@Range(from = 0, to = 8) int i) {
232241
return from(SlotUtils.HOTBAR_START + i);
233242
}
234243

235244
public Action fromOffhand() {
236245
return from(SlotUtils.OFFHAND);
237246
}
238247

239-
public Action fromMain(int i) {
248+
public Action fromMain(@Range(from = 0, to = 26) int i) {
240249
return from(SlotUtils.MAIN_START + i);
241250
}
242251

252+
/**
253+
* @param i The entity slot id of one of the four humanoid armor pieces, as defined in {@link EquipmentSlot}
254+
*/
243255
public Action fromArmor(int i) {
244256
return from(SlotUtils.ARMOR_START + (3 - i));
245257
}
@@ -251,22 +263,28 @@ public void toId(int id) {
251263
run();
252264
}
253265

266+
/**
267+
* @param index The index of one of the slots within the inventory
268+
*/
254269
public void to(int index) {
255270
toId(SlotUtils.indexToId(index));
256271
}
257272

258-
public void toHotbar(int i) {
273+
public void toHotbar(@Range(from = 0, to = 8) int i) {
259274
to(SlotUtils.HOTBAR_START + i);
260275
}
261276

262277
public void toOffhand() {
263278
to(SlotUtils.OFFHAND);
264279
}
265280

266-
public void toMain(int i) {
281+
public void toMain(@Range(from = 0, to = 26) int i) {
267282
to(SlotUtils.MAIN_START + i);
268283
}
269284

285+
/**
286+
* @param i The entity slot id of one of the four humanoid armor pieces, as defined in {@link EquipmentSlot}
287+
*/
270288
public void toArmor(int i) {
271289
to(SlotUtils.ARMOR_START + (3 - i));
272290
}
@@ -278,22 +296,28 @@ public void slotId(int id) {
278296
run();
279297
}
280298

299+
/**
300+
* @param index The index of one of the slots within the inventory
301+
*/
281302
public void slot(int index) {
282303
slotId(SlotUtils.indexToId(index));
283304
}
284305

285-
public void slotHotbar(int i) {
306+
public void slotHotbar(@Range(from = 0, to = 8) int i) {
286307
slot(SlotUtils.HOTBAR_START + i);
287308
}
288309

289310
public void slotOffhand() {
290311
slot(SlotUtils.OFFHAND);
291312
}
292313

293-
public void slotMain(int i) {
314+
public void slotMain(@Range(from = 0, to = 26) int i) {
294315
slot(SlotUtils.MAIN_START + i);
295316
}
296317

318+
/**
319+
* @param i The entity slot id of one of the four humanoid armor pieces, as defined in {@link EquipmentSlot}
320+
*/
297321
public void slotArmor(int i) {
298322
slot(SlotUtils.ARMOR_START + (3 - i));
299323
}

src/main/java/meteordevelopment/meteorclient/utils/player/SlotUtils.java

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,48 @@
99
import meteordevelopment.meteorclient.mixin.HorseScreenHandlerAccessor;
1010
import meteordevelopment.meteorclient.mixin.ItemGroupsAccessor;
1111
import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
12+
import net.minecraft.client.network.ClientPlayerInteractionManager;
1213
import net.minecraft.entity.mob.SkeletonHorseEntity;
1314
import net.minecraft.entity.mob.ZombieHorseEntity;
14-
import net.minecraft.entity.passive.AbstractDonkeyEntity;
15-
import net.minecraft.entity.passive.AbstractHorseEntity;
16-
import net.minecraft.entity.passive.HorseEntity;
17-
import net.minecraft.entity.passive.LlamaEntity;
15+
import net.minecraft.entity.passive.*;
16+
import net.minecraft.entity.player.PlayerEntity;
1817
import net.minecraft.registry.Registries;
1918
import net.minecraft.screen.*;
19+
import net.minecraft.screen.slot.Slot;
20+
import net.minecraft.screen.slot.SlotActionType;
2021

2122
import static meteordevelopment.meteorclient.MeteorClient.mc;
2223

2324
public class SlotUtils {
25+
/**
26+
* These constants refer to the slot index of relevant player slots. They are used when dealing directly with the
27+
* player inventory - e.g. {@code mc.player.getInventory().getSelectedSlot()} returns the slot index of your
28+
* selected slot (i.e. main hand).
29+
*
30+
* @see net.minecraft.entity.player.PlayerInventory
31+
* @see Slot#index
32+
*/
2433
public static final int HOTBAR_START = 0;
2534
public static final int HOTBAR_END = 8;
26-
27-
public static final int OFFHAND = 45;
28-
2935
public static final int MAIN_START = 9;
3036
public static final int MAIN_END = 35;
31-
3237
public static final int ARMOR_START = 36;
3338
public static final int ARMOR_END = 39;
39+
public static final int OFFHAND = 40;
3440

3541
private SlotUtils() {
3642
}
3743

44+
/**
45+
* Slot ids are used when inventory interactions have to be communicated to the server - you'll only find references
46+
* to slot ids when dealing with screen handlers or slot/inventory packets. All the methods in this class are used
47+
* to translate slot indices to the ids for each handled screen.
48+
*
49+
* @see <a href="https://minecraft.wiki/w/Java_Edition_protocol/Inventory">the minecraft.wiki page</a> for every slot id
50+
* @see ClientPlayerInteractionManager#clickSlot(int, int, int, SlotActionType, PlayerEntity)
51+
* @see ScreenHandler#internalOnSlotClick(int, int, SlotActionType, PlayerEntity)
52+
* @see Slot#id
53+
*/
3854
public static int indexToId(int i) {
3955
if (mc.player == null) return -1;
4056
ScreenHandler handler = mc.player.currentScreenHandler;
@@ -60,18 +76,21 @@ public static int indexToId(int i) {
6076
if (handler instanceof LecternScreenHandler) return lectern();
6177
if (handler instanceof LoomScreenHandler) return loom(i);
6278
if (handler instanceof StonecutterScreenHandler) return stonecutter(i);
79+
if (handler instanceof CrafterScreenHandler) return crafter(i);
80+
if (handler instanceof SmithingScreenHandler) return smithingTable(i);
6381

6482
return -1;
6583
}
6684

6785
private static int survivalInventory(int i) {
6886
if (isHotbar(i)) return 36 + i;
6987
if (isArmor(i)) return 5 + (i - 36);
88+
if (i == OFFHAND) return 45;
7089
return i;
7190
}
7291

7392
private static int creativeInventory(int i) {
74-
if (!(mc.currentScreen instanceof CreativeInventoryScreen) || CreativeInventoryScreenAccessor.meteor$getSelectedTab() != Registries.ITEM_GROUP.get(ItemGroupsAccessor.meteor$getInventory()))
93+
if (CreativeInventoryScreenAccessor.meteor$getSelectedTab() != Registries.ITEM_GROUP.get(ItemGroupsAccessor.meteor$getInventory()))
7594
return -1;
7695
return survivalInventory(i);
7796
}
@@ -143,7 +162,8 @@ private static int horse(ScreenHandler handler, int i) {
143162
int strength = llamaEntity.getStrength();
144163
if (isHotbar(i)) return (2 + 3 * strength) + 28 + i;
145164
if (isMain(i)) return (2 + 3 * strength) + 1 + (i - 9);
146-
} else if (entity instanceof HorseEntity || entity instanceof SkeletonHorseEntity || entity instanceof ZombieHorseEntity) {
165+
} else if (entity instanceof HorseEntity || entity instanceof SkeletonHorseEntity
166+
|| entity instanceof ZombieHorseEntity || entity instanceof CamelEntity) {
147167
if (isHotbar(i)) return 29 + i;
148168
if (isMain(i)) return 2 + (i - 9);
149169
} else if (entity instanceof AbstractDonkeyEntity abstractDonkeyEntity) {
@@ -183,17 +203,29 @@ private static int stonecutter(int i) {
183203
return -1;
184204
}
185205

206+
private static int crafter(int i) {
207+
if (isHotbar(i)) return 36 + i;
208+
if (isMain(i)) return i;
209+
return -1;
210+
}
211+
212+
private static int smithingTable(int i) {
213+
if (isHotbar(i)) return 31 + i;
214+
if (isMain(i)) return 4 + (i - 9);
215+
return -1;
216+
}
217+
186218
// Utils
187219

188-
public static boolean isHotbar(int i) {
189-
return i >= HOTBAR_START && i <= HOTBAR_END;
220+
public static boolean isHotbar(int slotIndex) {
221+
return slotIndex >= HOTBAR_START && slotIndex <= HOTBAR_END;
190222
}
191223

192-
public static boolean isMain(int i) {
193-
return i >= MAIN_START && i <= MAIN_END;
224+
public static boolean isMain(int slotIndex) {
225+
return slotIndex >= MAIN_START && slotIndex <= MAIN_END;
194226
}
195227

196-
public static boolean isArmor(int i) {
197-
return i >= ARMOR_START && i <= ARMOR_END;
228+
public static boolean isArmor(int slotIndex) {
229+
return slotIndex >= ARMOR_START && slotIndex <= ARMOR_END;
198230
}
199231
}

0 commit comments

Comments
 (0)