17
17
18
18
import java .util .*;
19
19
import java .util .function .Consumer ;
20
- import java .util .stream .Collectors ;
21
20
22
21
/**
23
22
* A pane for static items and stuff. All items will have to be specified a slot, or will be added in the next position.
24
23
*/
25
24
public class StaticPane extends Pane implements Flippable , Rotatable {
26
25
27
26
/**
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.
29
29
*/
30
30
@ NotNull
31
- private final Set < Map . Entry < GuiItem , Map .Entry <Integer , Integer >> > items ;
31
+ private final Map < Map .Entry <Integer , Integer >, GuiItem > items ;
32
32
33
33
/**
34
34
* The clockwise rotation of this pane in degrees
@@ -46,7 +46,7 @@ public class StaticPane extends Pane implements Flippable, Rotatable {
46
46
public StaticPane (int x , int y , int length , int height , @ NotNull Priority priority ) {
47
47
super (x , y , length , height , priority );
48
48
49
- this .items = new HashSet <>(length * height );
49
+ this .items = new HashMap <>(length * height );
50
50
}
51
51
52
52
/**
@@ -60,9 +60,7 @@ public StaticPane(int x, int y, int length, int height) {
60
60
* {@inheritDoc}
61
61
*/
62
62
public StaticPane (int length , int height ) {
63
- super (length , height );
64
-
65
- this .items = new HashSet <>(length * height );
63
+ this (0 , 0 , length , height );
66
64
}
67
65
68
66
/**
@@ -74,13 +72,13 @@ public void display(@NotNull Gui gui, @NotNull Inventory inventory, @NotNull Pla
74
72
int length = Math .min (this .length , maxLength );
75
73
int height = Math .min (this .height , maxHeight );
76
74
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 ();
80
78
81
- return key .isVisible () && value .getKey () + paneOffsetX <= 9 && value .getValue () + paneOffsetY <= 6 ;
79
+ return item .isVisible () && location .getKey () + paneOffsetX <= 9 && location .getValue () + paneOffsetY <= 6 ;
82
80
}).forEach (entry -> {
83
- Map .Entry <Integer , Integer > location = entry .getValue ();
81
+ Map .Entry <Integer , Integer > location = entry .getKey ();
84
82
85
83
int x = location .getKey (), y = location .getValue ();
86
84
@@ -93,7 +91,7 @@ public void display(@NotNull Gui gui, @NotNull Inventory inventory, @NotNull Pla
93
91
Map .Entry <Integer , Integer > coordinates = GeometryUtil .processClockwiseRotation (x , y , length , height ,
94
92
rotation );
95
93
96
- ItemStack item = entry .getKey ().getItem ();
94
+ ItemStack item = entry .getValue ().getItem ();
97
95
98
96
int finalRow = getY () + coordinates .getValue () + paneOffsetY ;
99
97
int finalColumn = getX () + coordinates .getKey () + paneOffsetX ;
@@ -113,14 +111,17 @@ public void display(@NotNull Gui gui, @NotNull Inventory inventory, @NotNull Pla
113
111
}
114
112
115
113
/**
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.
117
116
*
118
117
* @param item the item to set
119
118
* @param x the x coordinate of the position of the item
120
119
* @param y the y coordinate of the position of the item
121
120
*/
122
121
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 );
124
125
}
125
126
126
127
/**
@@ -130,7 +131,7 @@ public void addItem(@NotNull GuiItem item, int x, int y) {
130
131
* @since 0.5.8
131
132
*/
132
133
public void removeItem (@ NotNull GuiItem item ) {
133
- items .removeIf (entry -> entry . getKey () .equals (item ));
134
+ items .values (). removeIf (guiItem -> guiItem .equals (item ));
134
135
}
135
136
136
137
/**
@@ -172,8 +173,7 @@ public boolean click(@NotNull Gui gui, @NotNull InventoryClickEvent event, int p
172
173
return false ;
173
174
}
174
175
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 );
177
177
178
178
if (clickedItem == null ) {
179
179
return false ;
@@ -209,9 +209,7 @@ public void setRotation(int rotation) {
209
209
@ Contract ("null, _ -> fail" )
210
210
public void fillWith (@ NotNull ItemStack itemStack , @ Nullable Consumer <InventoryClickEvent > action ) {
211
211
//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 ();
215
213
216
214
for (int y = 0 ; y < this .getHeight (); y ++) {
217
215
for (int x = 0 ; x < this .getLength (); x ++) {
@@ -248,7 +246,7 @@ public void fillWith(@NotNull ItemStack itemStack) {
248
246
@ NotNull
249
247
@ Override
250
248
public Collection <GuiItem > getItems () {
251
- return items .stream (). map ( Map . Entry :: getKey ). collect ( Collectors . toList () );
249
+ return items .values ( );
252
250
}
253
251
254
252
/**
0 commit comments