Skip to content

Commit a0ac41d

Browse files
authored
Merge pull request #127 from Avalancs/itemOverflowErrorFix
Item overflow error fix
2 parents 00dcb43 + 9e08f22 commit a0ac41d

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

main/src/org/destinationsol/game/item/ItemContainer.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
import java.util.*;
2323

2424
public class ItemContainer implements Iterable<List<SolItem>> {
25-
public static final int MAX_GROUP_COUNT = 4 * Const.ITEM_GROUPS_PER_PAGE;
26-
public static final int MAX_GROUP_SZ = 30;
25+
public static final int MAX_INVENTORY_PAGES = 4;
26+
public static final int MAX_GROUP_COUNT = MAX_INVENTORY_PAGES * Const.ITEM_GROUPS_PER_PAGE;
27+
public static final int MAX_STACK_SIZE = 30; // e.g.: ammo, repair kit
2728

2829
private List<List<SolItem>> myGroups;
2930
private Set<List<SolItem>> myNewGroups;
30-
private int mySize;
3131

3232
public ItemContainer() {
3333
myGroups = new ArrayList<List<SolItem>>();
@@ -58,7 +58,7 @@ public boolean canAdd(SolItem example) {
5858
for (int i = 0, myGroupsSize = myGroups.size(); i < myGroupsSize; i++) {
5959
List<SolItem> group = myGroups.get(i);
6060
SolItem item = group.get(0);
61-
if (item.isSame(example)) return group.size() < MAX_GROUP_SZ;
61+
if (item.isSame(example)) return group.size() < MAX_STACK_SIZE;
6262
}
6363
return myGroups.size() < MAX_GROUP_COUNT;
6464
}
@@ -69,20 +69,17 @@ public void add(SolItem addedItem) {
6969
List<SolItem> group = myGroups.get(i);
7070
SolItem item = group.get(0);
7171
if (item.isSame(addedItem)) {
72-
if ((group.size() < MAX_GROUP_SZ))
73-
{
72+
if ((group.size() < MAX_STACK_SIZE)) {
7473
group.add(addedItem);
75-
mySize++;
7674
}
7775
return;
78-
7976
}
8077
}
81-
if (myGroups.size() >= MAX_GROUP_COUNT) throw new AssertionError("reached group count limit");
82-
ArrayList<SolItem> group = new ArrayList<SolItem>();
78+
// From now on, silently ignore if by some chance an extra inventory page is created
79+
//if (myGroups.size() >= MAX_GROUP_COUNT) throw new AssertionError("reached group count limit");
80+
ArrayList<SolItem> group = new ArrayList<>();
8381
group.add(addedItem);
8482
myGroups.add(0, group);
85-
mySize++;
8683
myNewGroups.add(group);
8784
}
8885

@@ -95,10 +92,6 @@ public int groupCount() {
9592
return myGroups.size();
9693
}
9794

98-
public int size() {
99-
return mySize;
100-
}
101-
10295
public boolean contains(SolItem item) {
10396
for (int i = 0, myGroupsSize = myGroups.size(); i < myGroupsSize; i++) {
10497
List<SolItem> group = myGroups.get(i);
@@ -116,7 +109,6 @@ public void remove(SolItem item) {
116109
if (group.isEmpty()) remGroup = group;
117110
if (removed) break;
118111
}
119-
if (removed) mySize--;
120112
if (remGroup != null) {
121113
myGroups.remove(remGroup);
122114
myNewGroups.remove(remGroup);
@@ -165,7 +157,6 @@ public List<SolItem> getGroup(int groupIdx) {
165157
public void clear() {
166158
myGroups.clear();
167159
myNewGroups.clear();
168-
mySize = 0;
169160
}
170161

171162
private class Itr implements Iterator<List<SolItem>> {

0 commit comments

Comments
 (0)