99import meteordevelopment .meteorclient .mixin .HorseScreenHandlerAccessor ;
1010import meteordevelopment .meteorclient .mixin .ItemGroupsAccessor ;
1111import net .minecraft .client .gui .screen .ingame .CreativeInventoryScreen ;
12+ import net .minecraft .client .network .ClientPlayerInteractionManager ;
1213import net .minecraft .entity .mob .SkeletonHorseEntity ;
1314import 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 ;
1817import net .minecraft .registry .Registries ;
1918import net .minecraft .screen .*;
19+ import net .minecraft .screen .slot .Slot ;
20+ import net .minecraft .screen .slot .SlotActionType ;
2021
2122import static meteordevelopment .meteorclient .MeteorClient .mc ;
2223
2324public 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