Skip to content

Commit

Permalink
Fix StaticPane#addItem allowing duplicate locations
Browse files Browse the repository at this point in the history
StaticPane#addItem would not overwrite an already occupied location, but simply add the item to that location. This has now been fixed.
  • Loading branch information
stefvanschie committed Jan 11, 2020
1 parent 99eeadb commit 2b2339e
Showing 1 changed file with 20 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@

import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

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

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

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

this.items = new HashSet<>(length * height);
this.items = new HashMap<>(length * height);
}

/**
Expand All @@ -60,9 +60,7 @@ public StaticPane(int x, int y, int length, int height) {
* {@inheritDoc}
*/
public StaticPane(int length, int height) {
super(length, height);

this.items = new HashSet<>(length * height);
this(0, 0, length, height);
}

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

items.stream().filter(entry -> {
GuiItem key = entry.getKey();
Map.Entry<Integer, Integer> value = entry.getValue();
items.entrySet().stream().filter(entry -> {
GuiItem item = entry.getValue();
Map.Entry<Integer, Integer> location = entry.getKey();

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

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

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

ItemStack item = entry.getKey().getItem();
ItemStack item = entry.getValue().getItem();

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

/**
* Adds a gui item at the specific spot in the pane
* Adds a gui item at the specific spot in the pane. If the coordinates as specified by the x and y parameters is
* already occupied, that item will be replaced by the item parameter.
*
* @param item the item to set
* @param x the x coordinate of the position of the item
* @param y the y coordinate of the position of the item
*/
public void addItem(@NotNull GuiItem item, int x, int y) {
items.add(new AbstractMap.SimpleEntry<>(item, new AbstractMap.SimpleEntry<>(x, y)));
items.keySet().removeIf(entry -> entry.getKey() == x && entry.getValue() == y);

items.put(new AbstractMap.SimpleEntry<>(x, y), item);
}

/**
Expand All @@ -130,7 +131,7 @@ public void addItem(@NotNull GuiItem item, int x, int y) {
* @since 0.5.8
*/
public void removeItem(@NotNull GuiItem item) {
items.removeIf(entry -> entry.getKey().equals(item));
items.values().removeIf(guiItem -> guiItem.equals(item));
}

/**
Expand Down Expand Up @@ -172,8 +173,7 @@ public boolean click(@NotNull Gui gui, @NotNull InventoryClickEvent event, int p
return false;
}

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

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

for (int y = 0; y < this.getHeight(); y++) {
for (int x = 0; x < this.getLength(); x++) {
Expand Down Expand Up @@ -248,7 +246,7 @@ public void fillWith(@NotNull ItemStack itemStack) {
@NotNull
@Override
public Collection<GuiItem> getItems() {
return items.stream().map(Map.Entry::getKey).collect(Collectors.toList());
return items.values();
}

/**
Expand Down

0 comments on commit 2b2339e

Please sign in to comment.