1212public class ChiselInventory implements IInventory {
1313 private final DefaultedList <ItemStack > inventory ;
1414
15- public ChiselInventory ()
16- {
15+ public ChiselInventory () {
1716 this .inventory = DefaultedList .ofSize (61 , ItemStack .EMPTY );
1817 }
1918
@@ -24,46 +23,81 @@ public DefaultedList<ItemStack> getItems() {
2423
2524 @ Override
2625 public void setStack (int slot , ItemStack stack ) {
27- if (stack .getCount () <= 0 || stack .getCount () > 99 ) {
28- stack = ItemStack .EMPTY ; // Ensure invalid stacks are cleared
26+ validateAndSetStack (slot , stack );
27+ }
28+
29+ private void validateAndSetStack (int slot , ItemStack stack ) {
30+ // Ensure stack is either empty or has a valid count
31+ if (stack == null ) {
32+ getItems ().set (slot , ItemStack .EMPTY );
33+ return ;
2934 }
30- getItems ().set (slot , stack );
35+
36+ // If stack is empty or count is invalid, set to empty
37+ if (stack .isEmpty () || stack .getCount () <= 0 ) {
38+ getItems ().set (slot , ItemStack .EMPTY );
39+ } else {
40+ // Clamp count between 1 and 99
41+ ItemStack sanitizedStack = stack .copyWithCount (Math .max (1 , Math .min (stack .getCount (), 99 )));
42+ getItems ().set (slot , sanitizedStack );
43+ }
44+
45+ // Refresh if it's the input slot
3146 if (slot == 0 ) {
32- refresh (stack .getItem ());
47+ refresh (getStack ( slot ) .getItem ());
3348 }
3449 }
3550
36-
3751 @ Override
3852 public ItemStack removeStack (int slot , int count ) {
3953 ItemStack result = Inventories .splitStack (getItems (), slot , count );
54+
55+ // Ensure the result has a valid count
4056 if (!result .isEmpty ()) {
41- markDirty ();
57+ // If the input slot, respect the current input stack count
4258 if (slot != 0 && !inventory .get (0 ).isEmpty ()) {
43- int c = inventory .get (0 ).getCount ();
44- result .setCount (Math .max (1 , Math .min (c , 99 ))); // Clamp between 1 and 99
59+ int inputCount = inventory .get (0 ).getCount ();
60+ result .setCount (Math .max (1 , Math .min (result . getCount (), inputCount )));
4561 }
4662
63+ // Ensure the result always has at least 1 item
64+ if (result .getCount () <= 0 ) {
65+ result = ItemStack .EMPTY ;
66+ }
67+
68+ markDirty ();
4769 }
70+
4871 return result ;
4972 }
5073
51- public void refresh (Item item )
52- {
53- if (!(item instanceof BlockItem ))
54- {
74+ @ Override
75+ public ItemStack removeStack (int slot ) {
76+ ItemStack stack = getItems ().get (slot );
77+
78+ // If the stack is empty or invalid, return empty stack
79+ if (stack .isEmpty () || stack .getCount () <= 0 ) {
80+ return ItemStack .EMPTY ;
81+ }
82+
83+ getItems ().set (slot , ItemStack .EMPTY );
84+ return stack ;
85+ }
86+
87+ public void refresh (Item item ) {
88+ if (!(item instanceof BlockItem )) {
5589 clearInv ();
5690 return ;
5791 }
5892 List <Item > chiselBlocks = ChiselGroupLookup .getBlocksInGroup (item );
5993 populate (chiselBlocks );
6094 }
6195
62- public void populate (List <Item > chiselBlocks )
63- {
96+ public void populate (List <Item > chiselBlocks ) {
6497 clearInv ();
6598 int baseCount = getStack (0 ).getCount ();
6699 baseCount = Math .max (1 , Math .min (baseCount , 99 )); // Clamp stack size
100+
67101 for (int i = 0 ; i < 60 && i < chiselBlocks .size (); i ++) {
68102 this .setStack (i + 1 , new ItemStack (chiselBlocks .get (i ), baseCount ));
69103 }
@@ -74,4 +108,12 @@ public void clearInv() {
74108 this .setStack (i , ItemStack .EMPTY );
75109 }
76110 }
111+
112+ // Additional method to ensure all stacks are valid before saving
113+ public void sanitizeInventory () {
114+ for (int i = 0 ; i < inventory .size (); i ++) {
115+ ItemStack stack = inventory .get (i );
116+ validateAndSetStack (i , stack );
117+ }
118+ }
77119}
0 commit comments