@@ -31,12 +31,12 @@ public class BulkCellInventory implements StorageCell {
31
31
private final AEItemKey filterItem ;
32
32
33
33
private final boolean compressionEnabled ;
34
- private final CompressionChain compressionChain ;
34
+ private CompressionChain compressionChain ;
35
35
private BigInteger unitCount ;
36
- private final BigInteger unitFactor ;
37
- private final int compressionCutoff ;
36
+ private BigInteger unitFactor ;
37
+ private int compressionCutoff ;
38
38
39
- private final Map <Item , Long > availableStacks ;
39
+ private Map <Item , Long > availableStacks ;
40
40
private List <IPatternDetails > decompressionPatterns ;
41
41
42
42
private boolean isPersisted = true ;
@@ -47,17 +47,16 @@ public class BulkCellInventory implements StorageCell {
47
47
48
48
var cell = (BulkCellItem ) stack .getItem ();
49
49
filterItem = (AEItemKey ) cell .getConfigInventory (stack ).getKey (0 );
50
+ compressionEnabled = cell .getUpgrades (stack ).isInstalled (MEGAItems .COMPRESSION_CARD );
50
51
51
52
storedItem = (AEItemKey ) stack .get (MEGAComponents .BULK_CELL_ITEM );
52
53
unitCount = stack .getOrDefault (MEGAComponents .BULK_CELL_UNIT_COUNT , BigInteger .ZERO );
53
54
54
55
var determiningKey = storedItem != null ? storedItem : filterItem ;
55
56
var determiningItem = determiningKey != null ? determiningKey .getItem () : null ;
56
57
compressionChain = CompressionService .getChain (determiningItem );
57
- unitFactor = compressionChain .unitFactor (determiningItem );
58
58
59
- // Check newly-calculated factor against what's already recorded in order to adjust for a compression chain that
60
- // has changed from the left of the item filtered to or stored
59
+ unitFactor = compressionChain .unitFactor (determiningItem );
61
60
var recordedFactor = stack .getOrDefault (MEGAComponents .BULK_CELL_UNIT_FACTOR , unitFactor );
62
61
63
62
if (!unitFactor .equals (recordedFactor )) {
@@ -70,12 +69,7 @@ public class BulkCellInventory implements StorageCell {
70
69
int recordedCutoff = stack .getOrDefault (MEGAComponents .BULK_CELL_COMPRESSION_CUTOFF , maxCutoff );
71
70
compressionCutoff = recordedCutoff < 0 ? maxCutoff : Math .min (recordedCutoff , maxCutoff );
72
71
73
- if (hasCompressionChain ()) {
74
- stack .set (MEGAComponents .BULK_CELL_COMPRESSION_CUTOFF , compressionCutoff );
75
- }
76
-
77
72
availableStacks = compressionChain .initStacks (unitCount , compressionCutoff , determiningItem );
78
- compressionEnabled = cell .getUpgrades (stack ).isInstalled (MEGAItems .COMPRESSION_CARD );
79
73
}
80
74
81
75
@ Override
@@ -91,11 +85,11 @@ public CellState getStatus() {
91
85
return CellState .NOT_EMPTY ;
92
86
}
93
87
94
- public AEItemKey getStoredItem () {
88
+ AEItemKey getStoredItem () {
95
89
return storedItem ;
96
90
}
97
91
98
- public long getStoredQuantity () {
92
+ long getStoredQuantity () {
99
93
return CompressionChain .clamp (unitCount .divide (unitFactor ), Long .MAX_VALUE );
100
94
}
101
95
@@ -104,7 +98,26 @@ AEItemKey getFilterItem() {
104
98
}
105
99
106
100
private boolean isFilterMismatched () {
107
- return storedItem != null && !storedItem .equals (filterItem );
101
+ if (storedItem == null ) {
102
+ return false ;
103
+ }
104
+
105
+ if (storedItem .equals (filterItem )) {
106
+ return false ;
107
+ }
108
+
109
+ if (filterItem == null ) {
110
+ return true ;
111
+ }
112
+
113
+ if (compressionChain .containsVariant (filterItem .getItem ())) {
114
+ storedItem = filterItem ;
115
+ unitFactor = compressionChain .unitFactor (storedItem .getItem ());
116
+ saveChanges ();
117
+ return false ;
118
+ }
119
+
120
+ return true ;
108
121
}
109
122
110
123
public boolean isCompressionEnabled () {
@@ -138,7 +151,7 @@ public long insert(AEKey what, long amount, Actionable mode, IActionSource sourc
138
151
return 0 ;
139
152
}
140
153
141
- if (filterItem == null || isFilterMismatched ()) {
154
+ if (isFilterMismatched ()) {
142
155
return 0 ;
143
156
}
144
157
@@ -154,7 +167,7 @@ public long insert(AEKey what, long amount, Actionable mode, IActionSource sourc
154
167
storedItem = filterItem ;
155
168
}
156
169
157
- saveChanges (units );
170
+ updateContents (units );
158
171
}
159
172
160
173
return amount ;
@@ -187,30 +200,39 @@ public long extract(AEKey what, long amount, Actionable mode, IActionSource sour
187
200
if (currentUnitCount .compareTo (units ) <= 0 ) {
188
201
if (mode == Actionable .MODULATE ) {
189
202
storedItem = null ;
190
- saveChanges (unitCount .negate ());
203
+ unitCount = BigInteger .ZERO ;
204
+
205
+ var filter = filterItem != null ? filterItem .getItem () : null ;
206
+ compressionChain = CompressionService .getChain (filter );
207
+ availableStacks = compressionChain .initStacks (unitCount , compressionCutoff , filter );
208
+
209
+ saveChanges ();
191
210
}
192
211
193
212
return CompressionChain .clamp (currentUnitCount .divide (factor ), Long .MAX_VALUE );
194
213
} else {
195
214
if (mode == Actionable .MODULATE ) {
196
- saveChanges (units .negate ());
215
+ updateContents (units .negate ());
197
216
}
198
217
199
218
return CompressionChain .clamp (units .divide (factor ), Long .MAX_VALUE );
200
219
}
201
220
}
202
221
203
- private void saveChanges (BigInteger unitsToAdd ) {
222
+ private void updateContents (BigInteger unitsToAdd ) {
204
223
unitCount = unitCount .add (unitsToAdd );
224
+ saveChanges ();
225
+ compressionChain .updateStacks (availableStacks , unitsToAdd , compressionCutoff );
226
+ }
227
+
228
+ private void saveChanges () {
205
229
isPersisted = false ;
206
230
207
231
if (container != null ) {
208
232
container .saveChanges ();
209
233
} else {
210
234
persist ();
211
235
}
212
-
213
- compressionChain .updateStacks (availableStacks , unitsToAdd , compressionCutoff );
214
236
}
215
237
216
238
@ Override
@@ -229,6 +251,12 @@ public void persist() {
229
251
stack .set (MEGAComponents .BULK_CELL_UNIT_FACTOR , unitFactor );
230
252
}
231
253
254
+ if (hasCompressionChain ()) {
255
+ stack .set (MEGAComponents .BULK_CELL_COMPRESSION_CUTOFF , compressionCutoff );
256
+ } else {
257
+ stack .remove (MEGAComponents .BULK_CELL_COMPRESSION_CUTOFF );
258
+ }
259
+
232
260
isPersisted = true ;
233
261
}
234
262
@@ -240,7 +268,8 @@ public void switchCompressionCutoff(boolean backwards) {
240
268
var newCutoff = compressionCutoff ;
241
269
newCutoff += compressionChain .size () + (backwards ? 1 : -1 );
242
270
newCutoff %= compressionChain .size ();
243
- stack .set (MEGAComponents .BULK_CELL_COMPRESSION_CUTOFF , newCutoff );
271
+ compressionCutoff = newCutoff ;
272
+ stack .set (MEGAComponents .BULK_CELL_COMPRESSION_CUTOFF , compressionCutoff );
244
273
}
245
274
246
275
public Item getCutoffItem () {
@@ -253,7 +282,7 @@ Item getHighestVariant() {
253
282
254
283
@ Override
255
284
public void getAvailableStacks (KeyCounter out ) {
256
- if (!compressionEnabled && storedItem != null && ! isFilterMismatched () ) {
285
+ if (!compressionEnabled && ! isFilterMismatched () && storedItem != null ) {
257
286
out .add (storedItem , availableStacks .get (storedItem .getItem ()));
258
287
} else {
259
288
availableStacks .forEach ((item , amount ) -> out .add (AEItemKey .of (item ), amount ));
0 commit comments