-
Notifications
You must be signed in to change notification settings - Fork 78
/
StaticPane.java
315 lines (249 loc) · 9.01 KB
/
StaticPane.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package com.github.stefvanschie.inventoryframework.pane;
import com.github.stefvanschie.inventoryframework.gui.InventoryComponent;
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.exception.XMLLoadException;
import com.github.stefvanschie.inventoryframework.util.GeometryUtil;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.util.*;
import java.util.function.Consumer;
/**
* 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 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 Map<Map.Entry<Integer, Integer>, GuiItem> items;
/**
* The clockwise rotation of this pane in degrees
*/
private int rotation;
/**
* Whether the items should be flipped horizontally and/or vertically
*/
private boolean flipHorizontally, flipVertically;
public StaticPane(int x, int y, int length, int height, @NotNull Priority priority) {
super(x, y, length, height, priority);
this.items = new HashMap<>(length * height);
}
public StaticPane(int x, int y, int length, int height) {
this(x, y, length, height, Priority.NORMAL);
}
public StaticPane(int length, int height) {
this(0, 0, length, height);
}
@Override
public void display(@NotNull InventoryComponent inventoryComponent, int paneOffsetX, int paneOffsetY, int maxLength,
int maxHeight) {
int length = Math.min(this.length, maxLength);
int height = Math.min(this.height, maxHeight);
items.entrySet().stream().filter(entry -> entry.getValue().isVisible()).forEach(entry -> {
Map.Entry<Integer, Integer> location = entry.getKey();
int x = location.getKey(), y = location.getValue();
if (flipHorizontally)
x = length - x - 1;
if (flipVertically)
y = height - y - 1;
Map.Entry<Integer, Integer> coordinates = GeometryUtil.processClockwiseRotation(x, y, length, height,
rotation);
x = coordinates.getKey();
y = coordinates.getValue();
if (x < 0 || x >= length || y < 0 || y >= height) {
return;
}
GuiItem item = entry.getValue();
int finalRow = getY() + y + paneOffsetY;
int finalColumn = getX() + x + paneOffsetX;
inventoryComponent.setItem(item.getItem(), finalColumn, finalRow);
});
}
/**
* 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.keySet().removeIf(entry -> entry.getKey() == x && entry.getValue() == y);
items.put(new AbstractMap.SimpleEntry<>(x, y), item);
}
/**
* Removes the specified item from the pane
*
* @param item the item to remove
* @since 0.5.8
*/
public void removeItem(@NotNull GuiItem item) {
items.values().removeIf(guiItem -> guiItem.equals(item));
}
@Override
public boolean click(@NotNull Gui gui, @NotNull InventoryComponent inventoryComponent,
@NotNull InventoryClickEvent event, int slot, int paneOffsetX, int paneOffsetY, int maxLength,
int maxHeight) {
int length = Math.min(this.length, maxLength);
int height = Math.min(this.height, maxHeight);
int adjustedSlot = slot - (getX() + paneOffsetX) - inventoryComponent.getLength() * (getY() + paneOffsetY);
int x = adjustedSlot % length;
int y = adjustedSlot / length;
//this isn't our item
if (x < 0 || x >= length || y < 0 || y >= height) {
return false;
}
callOnClick(event);
ItemStack itemStack = event.getCurrentItem();
if (itemStack == null) {
return false;
}
GuiItem clickedItem = findMatchingItem(items.values(), itemStack);
if (clickedItem == null) {
return false;
}
clickedItem.callAction(event);
return true;
}
@NotNull
@Contract(pure = true)
@Override
public StaticPane copy() {
StaticPane staticPane = new StaticPane(x, y, length, height, getPriority());
for (Map.Entry<Map.Entry<Integer, Integer>, GuiItem> entry : items.entrySet()) {
Map.Entry<Integer, Integer> coordinates = entry.getKey();
staticPane.addItem(entry.getValue().copy(), coordinates.getKey(), coordinates.getValue());
}
staticPane.setVisible(isVisible());
staticPane.onClick = onClick;
staticPane.uuid = uuid;
staticPane.rotation = rotation;
staticPane.flipHorizontally = flipHorizontally;
staticPane.flipVertically = flipVertically;
return staticPane;
}
@Override
public void setRotation(int rotation) {
if (length != height) {
throw new UnsupportedOperationException("length and height are different");
}
if (rotation % 90 != 0) {
throw new IllegalArgumentException("rotation isn't divisible by 90");
}
this.rotation = rotation % 360;
}
/**
* Fills all empty space in the pane with the given {@code itemStack} and adds the given action
*
* @param itemStack The {@link ItemStack} to fill the empty space with
* @param action The action called whenever an interaction with the item happens
* @since 0.5.9
*/
public void fillWith(@NotNull ItemStack itemStack, @Nullable Consumer<InventoryClickEvent> action) {
//The non empty spots
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++) {
boolean found = false;
for (Map.Entry<Integer, Integer> location : locations) {
if (location.getKey() == x && location.getValue() == y) {
found = true;
break;
}
}
if (!found) {
this.addItem(new GuiItem(itemStack, action), x, y);
}
}
}
}
/**
* Fills all empty space in the pane with the given {@code itemStack}
*
* @param itemStack The {@link ItemStack} to fill the empty space with
* @since 0.2.4
*/
@Contract("null -> fail")
public void fillWith(@NotNull ItemStack itemStack) {
this.fillWith(itemStack, null);
}
@NotNull
@Override
public Collection<GuiItem> getItems() {
return items.values();
}
@Override
public void clear() {
items.clear();
}
@NotNull
@Contract(pure = true)
@Override
public Collection<Pane> getPanes() {
return new HashSet<>();
}
@Override
public void flipHorizontally(boolean flipHorizontally) {
this.flipHorizontally = flipHorizontally;
}
@Override
public void flipVertically(boolean flipVertically) {
this.flipVertically = flipVertically;
}
@Contract(pure = true)
@Override
public int getRotation() {
return rotation;
}
@Contract(pure = true)
@Override
public boolean isFlippedHorizontally() {
return flipHorizontally;
}
@Contract(pure = true)
@Override
public boolean isFlippedVertically() {
return flipVertically;
}
/**
* Loads an outline pane from a given element
*
* @param instance the instance class
* @param element the element
* @return the outline pane
*/
@NotNull
public static StaticPane load(@NotNull Object instance, @NotNull Element element) {
try {
StaticPane staticPane = new StaticPane(
Integer.parseInt(element.getAttribute("length")),
Integer.parseInt(element.getAttribute("height"))
);
Pane.load(staticPane, instance, element);
Flippable.load(staticPane, element);
Rotatable.load(staticPane, element);
if (element.hasAttribute("populate"))
return staticPane;
NodeList childNodes = element.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node item = childNodes.item(i);
if (item.getNodeType() != Node.ELEMENT_NODE)
continue;
Element child = (Element) item;
staticPane.addItem(Pane.loadItem(instance, child), Integer.parseInt(child.getAttribute("x")),
Integer.parseInt(child.getAttribute("y")));
}
return staticPane;
} catch (NumberFormatException exception) {
throw new XMLLoadException(exception);
}
}
}