@@ -57,7 +57,7 @@ public boolean canInsert(ItemStack stack){
5757 return super .canInsert (stack ) && stack .getItem () instanceof ModuleItem ;
5858 }
5959
60- public int getMaxItemCount () {
60+ public int getMaxItemCount (){
6161 return 1 ;
6262 }
6363 });
@@ -81,28 +81,28 @@ public boolean canInsert(ItemStack stack){
8181 }
8282
8383 public ItemStack quickMove (PlayerEntity player , int index ){
84- ItemStack itemStack = ItemStack .EMPTY ;
84+ ItemStack remaining = ItemStack .EMPTY ;
8585 Slot slot = slots .get (index );
8686 if (slot .hasStack ()){
87- ItemStack itemStack2 = slot .getStack ();
88- itemStack = itemStack2 .copy ();
87+ ItemStack inserting = slot .getStack ();
88+ remaining = inserting .copy ();
8989 int num = automoton .moduleNum () + 2 ;
9090 if (index < num ){
91- if (!this . insertItem (itemStack2 , num , num + 36 , true ))
91+ if (!insertItem (inserting , num , num + 36 , true ))
9292 return ItemStack .EMPTY ;
93- }else if (!this . insertItem (itemStack2 , 0 , num , false ))
93+ }else if (!insertItem (inserting , 0 , num , false ))
9494 return ItemStack .EMPTY ;
95- if (itemStack2 .isEmpty ())
95+ if (inserting .isEmpty ())
9696 slot .setStack (ItemStack .EMPTY );
9797 else
9898 slot .markDirty ();
99- if (itemStack2 .getCount () == itemStack .getCount ())
99+ if (inserting .getCount () == remaining .getCount ())
100100 return ItemStack .EMPTY ;
101101
102- slot .onTakeItem (player , itemStack2 );
102+ slot .onTakeItem (player , inserting );
103103 }
104104
105- return itemStack ;
105+ return remaining ;
106106 }
107107
108108 public void onContentChanged (Inventory inventory ){
@@ -121,4 +121,68 @@ public void switchAutomoton(AutomotonBlockEntity automoton){
121121 for (int i = 0 ; i < 14 ; i ++)
122122 getSlot (i ).inventory = automoton ;
123123 }
124+
125+ // variant that respects max slot counts
126+ protected boolean insertItem (ItemStack inserting , int startIdx , int endIdx , boolean fromLast ){
127+ boolean done = false ;
128+ int i = startIdx ;
129+ if (fromLast )
130+ i = endIdx - 1 ;
131+
132+ if (inserting .isStackable ()){
133+ while (!inserting .isEmpty () && (fromLast ? i >= startIdx : i < endIdx )){
134+ Slot slot = slots .get (i );
135+ int thisMaxCount = Math .min (slot .getMaxItemCount (), inserting .getMaxCount ());
136+ ItemStack existing = slot .getStack ();
137+ if (!existing .isEmpty () && ItemStack .canCombine (inserting , existing )){
138+ int newCount = existing .getCount () + inserting .getCount ();
139+ if (newCount <= thisMaxCount ){
140+ inserting .setCount (0 );
141+ existing .setCount (newCount );
142+ slot .markDirty ();
143+ done = true ;
144+ }else if (existing .getCount () < thisMaxCount ){
145+ inserting .decrement (thisMaxCount - existing .getCount ());
146+ existing .setCount (thisMaxCount );
147+ slot .markDirty ();
148+ done = true ;
149+ }
150+ }
151+
152+ if (fromLast )
153+ i --;
154+ else
155+ i ++;
156+ }
157+ }
158+
159+ if (!inserting .isEmpty ()){
160+ if (fromLast )
161+ i = endIdx - 1 ;
162+ else
163+ i = startIdx ;
164+
165+ while (fromLast ? i >= startIdx : i < endIdx ){
166+ Slot slot = slots .get (i );
167+ ItemStack existing = slot .getStack ();
168+ if (existing .isEmpty () && slot .canInsert (inserting )){
169+ if (inserting .getCount () > slot .getMaxItemCount ())
170+ slot .setStack (inserting .split (slot .getMaxItemCount ()));
171+ else
172+ slot .setStack (inserting .split (inserting .getCount ()));
173+
174+ slot .markDirty ();
175+ done = true ;
176+ break ;
177+ }
178+
179+ if (fromLast )
180+ i --;
181+ else
182+ i ++;
183+ }
184+ }
185+
186+ return done ;
187+ }
124188}
0 commit comments