Skip to content

Commit 47be64d

Browse files
committed
more work + tests
1 parent 2ed3c00 commit 47be64d

File tree

2 files changed

+60
-15
lines changed

2 files changed

+60
-15
lines changed

src/main/java/gregtech/api/capability/impl/ItemHandlerList.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ public boolean add(IItemHandler handler) {
100100

101101
@Override
102102
public void add(int index, IItemHandler element) {
103-
if (invalidIndex(index)) return;
104-
int currentSlotIndex = handlerBySlotIndex.size();
105-
if (baseIndexOffset.containsKey(element)) {
103+
// Objects.checkIndex(index, size());
104+
if (handlerList.contains(element)) {
106105
throw new IllegalArgumentException("Attempted to add item handler " + element + " twice");
107106
}
108-
handlerList.add(element);
109-
baseIndexOffset.put(element, currentSlotIndex);
107+
handlerList.add(index, element);
108+
int offset = handlerBySlotIndex.size();
109+
baseIndexOffset.put(element, offset);
110110
for (int slotIndex = 0; slotIndex < element.getSlots(); slotIndex++) {
111-
handlerBySlotIndex.put(currentSlotIndex + slotIndex, element);
111+
handlerBySlotIndex.put(offset + slotIndex, element);
112112
}
113113
}
114114

@@ -119,19 +119,28 @@ public IItemHandler get(int index) {
119119

120120
@Override
121121
public IItemHandler remove(int index) {
122-
if (invalidIndex(index)) return null;
123-
var handler = get(index);
122+
// Objects.checkIndex(index, size());
123+
var handler2 = get(index);
124+
int offset2 = baseIndexOffset.getInt(handler2);
125+
124126
for (int i = index; i < size(); i++) {
125-
int offset = baseIndexOffset.getInt(get(i));
126-
for (int j = 0; j < get(index).getSlots(); j++) {
127+
int offset = baseIndexOffset.removeInt(get(i));
128+
for (int j = 0; j < get(i).getSlots(); j++) {
127129
handlerBySlotIndex.remove(offset + j);
128130
}
129-
baseIndexOffset.removeInt(handler);
130131
}
131-
return handler;
132-
}
133132

134-
private boolean invalidIndex(int index) {
135-
return index < 0 || index >= handlerList.size();
133+
var removed = handlerList.remove(index);
134+
for (var handler : handlerList) {
135+
if (baseIndexOffset.containsKey(handler))
136+
continue;
137+
138+
int offset = handlerBySlotIndex.size();
139+
baseIndexOffset.put(handler, offset);
140+
for (int i = 0; i < handler.getSlots(); i++) {
141+
handlerBySlotIndex.put(offset + i, handler);
142+
}
143+
}
144+
return removed;
136145
}
137146
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package gregtech.api.capability.impl;
2+
3+
import gregtech.Bootstrap;
4+
5+
import net.minecraftforge.items.ItemStackHandler;
6+
7+
import org.hamcrest.MatcherAssert;
8+
import org.junit.jupiter.api.BeforeAll;
9+
import org.junit.jupiter.api.Test;
10+
11+
import java.util.Collections;
12+
13+
public class ItemHandlerListTest {
14+
15+
@BeforeAll
16+
public static void bootstrap() {
17+
Bootstrap.perform();
18+
}
19+
20+
@Test
21+
public void testListOperations() {
22+
var list = new ItemHandlerList(Collections.emptyList());
23+
24+
// test add
25+
list.add(new ItemStackHandler(16));
26+
// MatcherAssert.assertThat("size is wrong", list.getSlots() == 16);
27+
list.add(new ItemStackHandler(16));
28+
// MatcherAssert.assertThat("size is wrong", list.getSlots() == 32);
29+
// MatcherAssert.assertThat("size is wrong", list.size() == 2);
30+
31+
// test removal
32+
list.remove(0);
33+
// MatcherAssert.assertThat("size is wrong", list.getSlots() == 16);
34+
// MatcherAssert.assertThat("size is wrong", list.size() == 1);
35+
}
36+
}

0 commit comments

Comments
 (0)