Skip to content

Commit f7aa4c9

Browse files
authored
Fix CellInventory stack size data losses (#363)
Migrates the total item count and serialized stack sizes to `long`
1 parent 0709d2d commit f7aa4c9

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/main/java/appeng/me/storage/AbstractCellInventory.java

+16-16
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public abstract class AbstractCellInventory<T extends IAEStack<T>> implements IC
5151
private final NBTTagCompound tagCompound;
5252
protected final ISaveProvider container;
5353
private int maxItemTypes = MAX_ITEM_TYPES;
54-
private short storedItems = 0;
55-
private int storedItemCount = 0;
54+
private short storedItemTypes = 0;
55+
private long storedItemCount = 0;
5656
protected IItemList<T> cellItems;
5757
private final ItemStack i;
5858
protected final IStorageCell<T> cellType;
@@ -81,8 +81,8 @@ protected AbstractCellInventory(final IStorageCell<T> cellType, final ItemStack
8181

8282
this.container = container;
8383
this.tagCompound = Platform.openNbtData(o);
84-
this.storedItems = this.tagCompound.getShort(ITEM_TYPE_TAG);
85-
this.storedItemCount = this.tagCompound.getInteger(ITEM_COUNT_TAG);
84+
this.storedItemTypes = this.tagCompound.getShort(ITEM_TYPE_TAG);
85+
this.storedItemCount = this.tagCompound.getLong(ITEM_COUNT_TAG);
8686
this.cellItems = null;
8787
}
8888

@@ -101,7 +101,7 @@ public void persist() {
101101
return;
102102
}
103103

104-
int itemCount = 0;
104+
long itemCount = 0;
105105

106106
// add new pretty stuff...
107107
int x = 0;
@@ -111,25 +111,25 @@ public void persist() {
111111
final NBTTagCompound g = new NBTTagCompound();
112112
v.writeToNBT(g);
113113
this.tagCompound.setTag(ITEM_SLOT_KEYS[x], g);
114-
this.tagCompound.setInteger(ITEM_SLOT_COUNT_KEYS[x], (int) v.getStackSize());
114+
this.tagCompound.setLong(ITEM_SLOT_COUNT_KEYS[x], v.getStackSize());
115115

116116
x++;
117117
}
118118

119-
final short oldStoredItems = this.storedItems;
119+
final short oldStoredItems = this.storedItemTypes;
120120

121-
this.storedItems = (short) this.cellItems.size();
121+
this.storedItemTypes = (short) this.cellItems.size();
122122
if (this.cellItems.isEmpty()) {
123123
this.tagCompound.removeTag(ITEM_TYPE_TAG);
124124
} else {
125-
this.tagCompound.setShort(ITEM_TYPE_TAG, this.storedItems);
125+
this.tagCompound.setShort(ITEM_TYPE_TAG, this.storedItemTypes);
126126
}
127127

128128
this.storedItemCount = itemCount;
129129
if (itemCount == 0) {
130130
this.tagCompound.removeTag(ITEM_COUNT_TAG);
131131
} else {
132-
this.tagCompound.setInteger(ITEM_COUNT_TAG, itemCount);
132+
this.tagCompound.setLong(ITEM_COUNT_TAG, itemCount);
133133
}
134134

135135
// clean any old crusty stuff...
@@ -143,7 +143,7 @@ public void persist() {
143143

144144
protected void saveChanges() {
145145
// recalculate values
146-
this.storedItems = (short) this.cellItems.size();
146+
this.storedItemTypes = (short) this.cellItems.size();
147147
this.storedItemCount = 0;
148148
for (final T v : this.cellItems) {
149149
this.storedItemCount += v.getStackSize();
@@ -165,12 +165,12 @@ private void loadCellItems() {
165165

166166
this.cellItems.resetStatus(); // clears totals and stuff.
167167

168-
final int types = (int) this.getStoredItemTypes();
168+
final long types = this.getStoredItemTypes();
169169
boolean needsUpdate = false;
170170

171171
for (int slot = 0; slot < types; slot++) {
172172
NBTTagCompound compoundTag = this.tagCompound.getCompoundTag(ITEM_SLOT_KEYS[slot]);
173-
int stackSize = this.tagCompound.getInteger(ITEM_SLOT_COUNT_KEYS[slot]);
173+
long stackSize = this.tagCompound.getLong(ITEM_SLOT_COUNT_KEYS[slot]);
174174
needsUpdate |= !this.loadCellItem(compoundTag, stackSize);
175175
}
176176

@@ -186,7 +186,7 @@ private void loadCellItems() {
186186
* @param stackSize
187187
* @return true when successfully loaded
188188
*/
189-
protected abstract boolean loadCellItem(NBTTagCompound compoundTag, int stackSize);
189+
protected abstract boolean loadCellItem(NBTTagCompound compoundTag, long stackSize);
190190

191191
@Override
192192
public IItemList<T> getAvailableItems(final IItemList<T> out) {
@@ -256,14 +256,14 @@ public long getStoredItemCount() {
256256

257257
@Override
258258
public long getStoredItemTypes() {
259-
return this.storedItems;
259+
return this.storedItemTypes;
260260
}
261261

262262
@Override
263263
public long getRemainingItemTypes() {
264264
final long basedOnStorage = this.getFreeBytes() / this.getBytesPerType();
265265
final long baseOnTotal = this.getTotalItemTypes() - this.getStoredItemTypes();
266-
return basedOnStorage > baseOnTotal ? baseOnTotal : basedOnStorage;
266+
return Math.min(basedOnStorage, baseOnTotal);
267267
}
268268

269269
@Override

src/main/java/appeng/me/storage/BasicCellInventory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public IStorageChannel<T> getChannel() {
205205
}
206206

207207
@Override
208-
protected boolean loadCellItem(NBTTagCompound compoundTag, int stackSize) {
208+
protected boolean loadCellItem(NBTTagCompound compoundTag, long stackSize) {
209209
// Now load the item stack
210210
final T t;
211211
try {

0 commit comments

Comments
 (0)