1616import com .lishid .openinv .internal .common .container .slot .SlotViewOnly ;
1717import com .lishid .openinv .internal .common .container .slot .placeholder .Placeholders ;
1818import com .lishid .openinv .internal .common .player .PlayerManager ;
19+ import it .unimi .dsi .fastutil .ints .Int2ObjectMap ;
1920import net .minecraft .ChatFormatting ;
2021import net .minecraft .core .NonNullList ;
2122import net .minecraft .network .chat .Component ;
3839import org .jetbrains .annotations .Nullable ;
3940
4041import java .util .ArrayList ;
42+ import java .util .Comparator ;
4143import java .util .List ;
44+ import java .util .Map ;
4245
4346public class OpenInventory implements Container , InternalOwned <ServerPlayer >, ISpecialPlayerInventory {
4447
45- private final List <Content > slots ;
48+ protected final List <Content > slots ;
4649 private final int size ;
47- private ServerPlayer owner ;
50+ protected ServerPlayer owner ;
4851 private int maxStackSize = 99 ;
4952 private CraftInventory bukkitEntity ;
5053 public List <HumanEntity > transaction = new ArrayList <>();
@@ -60,15 +63,14 @@ public OpenInventory(@NotNull org.bukkit.entity.Player bukkitPlayer) {
6063 setupSlots ();
6164 }
6265
63- private void setupSlots () {
66+ protected void setupSlots () {
6467 // Top of inventory: Regular contents.
6568 int nextIndex = addMainInventory ();
6669
6770 // If inventory is expected size, we can arrange slots to be pretty.
6871 Inventory ownerInv = owner .getInventory ();
69- if (ownerInv .items .size () == 36
70- && ownerInv .armor .size () == 4
71- && ownerInv .offhand .size () == 1
72+ if (ownerInv .getNonEquipmentItems ().size () == 36
73+ && Inventory .EQUIPMENT_SLOT_MAPPING .size () == 5
7274 && owner .inventoryMenu .getCraftSlots ().getContainerSize () == 4 ) {
7375 // Armor slots: Bottom left.
7476 addArmor (36 );
@@ -95,7 +97,7 @@ private void setupSlots() {
9597 }
9698
9799 private int addMainInventory () {
98- int listSize = owner .getInventory ().items .size ();
100+ int listSize = owner .getInventory ().getNonEquipmentItems () .size ();
99101 // Hotbar slots are 0-8. We want those to appear on the bottom of the inventory like a normal player inventory,
100102 // so everything else needs to move up a row.
101103 int hotbarDiff = listSize - 9 ;
@@ -113,58 +115,44 @@ private int addMainInventory() {
113115 slots .set (localIndex , new ContentList (owner , invIndex , type ) {
114116 @ Override
115117 public void setHolder (@ NotNull ServerPlayer holder ) {
116- items = holder .getInventory ().items ;
118+ items = holder .getInventory ().getNonEquipmentItems () ;
117119 }
118120 });
119121 }
120122 return listSize ;
121123 }
122124
123125 private int addArmor (int startIndex ) {
124- int listSize = owner .getInventory ().armor .size ();
125-
126- for (int i = 0 ; i < listSize ; ++i ) {
127- // Armor slots go bottom to top; boots are slot 0, helmet is slot 3.
128- // Since we have to display horizontally due to space restrictions,
129- // making the left side the "top" is more user-friendly.
130- int armorIndex ;
131- EquipmentSlot slot ;
132- switch (i ) {
133- case 3 -> {
134- armorIndex = 0 ;
135- slot = EquipmentSlot .FEET ;
136- }
137- case 2 -> {
138- armorIndex = 1 ;
139- slot = EquipmentSlot .LEGS ;
140- }
141- case 1 -> {
142- armorIndex = 2 ;
143- slot = EquipmentSlot .CHEST ;
144- }
145- case 0 -> {
146- armorIndex = 3 ;
147- slot = EquipmentSlot .HEAD ;
148- }
149- default -> {
150- // In the event that new armor slots are added, they can be placed at the end.
151- armorIndex = i ;
152- slot = EquipmentSlot .MAINHAND ;
153- }
126+ // Armor slots go bottom to top; boots are first and helmet is last.
127+ // Since we have to display horizontally due to space restrictions,
128+ // making the left side the "top" is more user-friendly.
129+ EquipmentSlot [] sorted = Inventory .EQUIPMENT_SLOT_MAPPING .int2ObjectEntrySet ()
130+ .stream ()
131+ .sorted (Comparator .comparingInt (Int2ObjectMap .Entry ::getIntKey ))
132+ .map (Map .Entry ::getValue )
133+ .toArray (EquipmentSlot []::new );
134+ int localIndex = 0 ;
135+ for (int i = sorted .length - 1 ; i >= 0 ; --i ) {
136+ // Skip off-hand, handled separately.
137+ if (sorted [i ] == EquipmentSlot .OFFHAND ) {
138+ continue ;
154139 }
155140
156- slots .set (startIndex + i , new ContentEquipment (owner , armorIndex , slot ));
141+ slots .set (startIndex + localIndex , new ContentEquipment (owner , sorted [i ]));
142+ ++localIndex ;
157143 }
158144
159- return startIndex + listSize ;
145+ return startIndex + localIndex ;
160146 }
161147
162148 private int addOffHand (int startIndex ) {
163- int listSize = owner . getInventory (). offhand . size ();
164- for ( int localIndex = 0 ; localIndex < listSize ; ++ localIndex ) {
165- slots . set ( startIndex + localIndex , new ContentOffHand ( owner , localIndex )) ;
149+ // No off-hand?
150+ if (! Inventory . EQUIPMENT_SLOT_MAPPING . containsValue ( EquipmentSlot . OFFHAND ) ) {
151+ return startIndex ;
166152 }
167- return startIndex + listSize ;
153+
154+ slots .set (startIndex , new ContentOffHand (owner ));
155+ return startIndex + 1 ;
168156 }
169157
170158 private int addCrafting (int startIndex , boolean pretty ) {
@@ -280,22 +268,22 @@ public boolean isEmpty() {
280268 }
281269
282270 @ Override
283- public ItemStack getItem (int index ) {
271+ public @ NotNull ItemStack getItem (int index ) {
284272 return slots .get (index ).get ();
285273 }
286274
287275 @ Override
288- public ItemStack removeItem (int index , int amount ) {
276+ public @ NotNull ItemStack removeItem (int index , int amount ) {
289277 return slots .get (index ).removePartial (amount );
290278 }
291279
292280 @ Override
293- public ItemStack removeItemNoUpdate (int index ) {
281+ public @ NotNull ItemStack removeItemNoUpdate (int index ) {
294282 return slots .get (index ).remove ();
295283 }
296284
297285 @ Override
298- public void setItem (int index , ItemStack itemStack ) {
286+ public void setItem (int index , @ NotNull ItemStack itemStack ) {
299287 slots .get (index ).set (itemStack );
300288 }
301289
@@ -327,17 +315,17 @@ public boolean stillValid(@NotNull Player player) {
327315 }
328316
329317 @ Override
330- public void onOpen (CraftHumanEntity viewer ) {
318+ public void onOpen (@ NotNull CraftHumanEntity viewer ) {
331319 transaction .add (viewer );
332320 }
333321
334322 @ Override
335- public void onClose (CraftHumanEntity viewer ) {
323+ public void onClose (@ NotNull CraftHumanEntity viewer ) {
336324 transaction .remove (viewer );
337325 }
338326
339327 @ Override
340- public List <HumanEntity > getViewers () {
328+ public @ NotNull List <HumanEntity > getViewers () {
341329 return transaction ;
342330 }
343331
0 commit comments