Skip to content

Commit 2b2339e

Browse files
committed
Fix StaticPane#addItem allowing duplicate locations
StaticPane#addItem would not overwrite an already occupied location, but simply add the item to that location. This has now been fixed.
1 parent 99eeadb commit 2b2339e

File tree

1 file changed

+20
-22
lines changed
  • src/main/java/com/github/stefvanschie/inventoryframework/pane

1 file changed

+20
-22
lines changed

src/main/java/com/github/stefvanschie/inventoryframework/pane/StaticPane.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717

1818
import java.util.*;
1919
import java.util.function.Consumer;
20-
import java.util.stream.Collectors;
2120

2221
/**
2322
* A pane for static items and stuff. All items will have to be specified a slot, or will be added in the next position.
2423
*/
2524
public class StaticPane extends Pane implements Flippable, Rotatable {
2625

2726
/**
28-
* A set of items inside this pane and their locations
27+
* A map of locations inside this pane and their item. The locations are stored in a way where the x coordinate is
28+
* the key and the y coordinate is the value.
2929
*/
3030
@NotNull
31-
private final Set<Map.Entry<GuiItem, Map.Entry<Integer, Integer>>> items;
31+
private final Map<Map.Entry<Integer, Integer>, GuiItem> items;
3232

3333
/**
3434
* The clockwise rotation of this pane in degrees
@@ -46,7 +46,7 @@ public class StaticPane extends Pane implements Flippable, Rotatable {
4646
public StaticPane(int x, int y, int length, int height, @NotNull Priority priority) {
4747
super(x, y, length, height, priority);
4848

49-
this.items = new HashSet<>(length * height);
49+
this.items = new HashMap<>(length * height);
5050
}
5151

5252
/**
@@ -60,9 +60,7 @@ public StaticPane(int x, int y, int length, int height) {
6060
* {@inheritDoc}
6161
*/
6262
public StaticPane(int length, int height) {
63-
super(length, height);
64-
65-
this.items = new HashSet<>(length * height);
63+
this(0, 0, length, height);
6664
}
6765

6866
/**
@@ -74,13 +72,13 @@ public void display(@NotNull Gui gui, @NotNull Inventory inventory, @NotNull Pla
7472
int length = Math.min(this.length, maxLength);
7573
int height = Math.min(this.height, maxHeight);
7674

77-
items.stream().filter(entry -> {
78-
GuiItem key = entry.getKey();
79-
Map.Entry<Integer, Integer> value = entry.getValue();
75+
items.entrySet().stream().filter(entry -> {
76+
GuiItem item = entry.getValue();
77+
Map.Entry<Integer, Integer> location = entry.getKey();
8078

81-
return key.isVisible() && value.getKey() + paneOffsetX <= 9 && value.getValue() + paneOffsetY <= 6;
79+
return item.isVisible() && location.getKey() + paneOffsetX <= 9 && location.getValue() + paneOffsetY <= 6;
8280
}).forEach(entry -> {
83-
Map.Entry<Integer, Integer> location = entry.getValue();
81+
Map.Entry<Integer, Integer> location = entry.getKey();
8482

8583
int x = location.getKey(), y = location.getValue();
8684

@@ -93,7 +91,7 @@ public void display(@NotNull Gui gui, @NotNull Inventory inventory, @NotNull Pla
9391
Map.Entry<Integer, Integer> coordinates = GeometryUtil.processClockwiseRotation(x, y, length, height,
9492
rotation);
9593

96-
ItemStack item = entry.getKey().getItem();
94+
ItemStack item = entry.getValue().getItem();
9795

9896
int finalRow = getY() + coordinates.getValue() + paneOffsetY;
9997
int finalColumn = getX() + coordinates.getKey() + paneOffsetX;
@@ -113,14 +111,17 @@ public void display(@NotNull Gui gui, @NotNull Inventory inventory, @NotNull Pla
113111
}
114112

115113
/**
116-
* Adds a gui item at the specific spot in the pane
114+
* Adds a gui item at the specific spot in the pane. If the coordinates as specified by the x and y parameters is
115+
* already occupied, that item will be replaced by the item parameter.
117116
*
118117
* @param item the item to set
119118
* @param x the x coordinate of the position of the item
120119
* @param y the y coordinate of the position of the item
121120
*/
122121
public void addItem(@NotNull GuiItem item, int x, int y) {
123-
items.add(new AbstractMap.SimpleEntry<>(item, new AbstractMap.SimpleEntry<>(x, y)));
122+
items.keySet().removeIf(entry -> entry.getKey() == x && entry.getValue() == y);
123+
124+
items.put(new AbstractMap.SimpleEntry<>(x, y), item);
124125
}
125126

126127
/**
@@ -130,7 +131,7 @@ public void addItem(@NotNull GuiItem item, int x, int y) {
130131
* @since 0.5.8
131132
*/
132133
public void removeItem(@NotNull GuiItem item) {
133-
items.removeIf(entry -> entry.getKey().equals(item));
134+
items.values().removeIf(guiItem -> guiItem.equals(item));
134135
}
135136

136137
/**
@@ -172,8 +173,7 @@ public boolean click(@NotNull Gui gui, @NotNull InventoryClickEvent event, int p
172173
return false;
173174
}
174175

175-
Set<GuiItem> items = this.items.stream().map(Map.Entry::getKey).collect(Collectors.toSet());
176-
GuiItem clickedItem = findMatchingItem(items, itemStack);
176+
GuiItem clickedItem = findMatchingItem(items.values(), itemStack);
177177

178178
if (clickedItem == null) {
179179
return false;
@@ -209,9 +209,7 @@ public void setRotation(int rotation) {
209209
@Contract("null, _ -> fail")
210210
public void fillWith(@NotNull ItemStack itemStack, @Nullable Consumer<InventoryClickEvent> action) {
211211
//The non empty spots
212-
List<Map.Entry<Integer, Integer>> locations = this.items.stream()
213-
.map(Map.Entry::getValue)
214-
.collect(Collectors.toList());
212+
Set<Map.Entry<Integer, Integer>> locations = this.items.keySet();
215213

216214
for (int y = 0; y < this.getHeight(); y++) {
217215
for (int x = 0; x < this.getLength(); x++) {
@@ -248,7 +246,7 @@ public void fillWith(@NotNull ItemStack itemStack) {
248246
@NotNull
249247
@Override
250248
public Collection<GuiItem> getItems() {
251-
return items.stream().map(Map.Entry::getKey).collect(Collectors.toList());
249+
return items.values();
252250
}
253251

254252
/**

0 commit comments

Comments
 (0)