-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventoryBuilder.java
More file actions
348 lines (311 loc) · 11.3 KB
/
InventoryBuilder.java
File metadata and controls
348 lines (311 loc) · 11.3 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package net.theevilreaper.aves.inventory;
import net.kyori.adventure.text.Component;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Player;
import net.minestom.server.event.inventory.InventoryCloseEvent;
import net.minestom.server.event.inventory.InventoryOpenEvent;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryType;
import net.minestom.server.inventory.click.Click;
import net.minestom.server.inventory.click.ClickType;
import net.minestom.server.item.ItemStack;
import net.theevilreaper.aves.inventory.click.ClickHolder;
import net.theevilreaper.aves.inventory.function.CloseFunction;
import net.theevilreaper.aves.inventory.function.InventoryClick;
import net.theevilreaper.aves.inventory.function.OpenFunction;
import net.theevilreaper.aves.inventory.layout.InventoryLayout;
import net.theevilreaper.aves.inventory.slot.EmptySlot;
import net.theevilreaper.aves.inventory.slot.ISlot;
import net.theevilreaper.aves.inventory.util.InventoryConstants;
import net.theevilreaper.aves.util.functional.ThrowingFunction;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Locale;
import java.util.function.Consumer;
/**
* The {@link InventoryBuilder} is the base class which contains the necessary methods to create different context implementations for an inventory.
*
* @author Patrick Zdarsky / Rxcki
* @version 1.3.1
* @since 1.0.12
*/
@SuppressWarnings("java:S3252")
public abstract class InventoryBuilder {
protected static final Logger LOGGER = LoggerFactory.getLogger(InventoryBuilder.class);
protected final InventoryType type;
protected volatile boolean inventoryLayoutValid = true;
protected volatile boolean dataLayoutValid = false;
protected volatile boolean dataLayoutPending = false;
protected OpenFunction openFunction;
protected CloseFunction closeFunction;
protected ThrowingFunction<InventoryLayout, InventoryLayout> dataLayoutFunction;
protected InventoryClick inventoryClick;
private InventoryLayout inventoryLayout;
private InventoryLayout dataLayout;
/**
* Creates a new instance from the inventory builder with the given size.
*
* @param type The type from the inventory to get the size from it
*/
protected InventoryBuilder(@NotNull InventoryType type) {
this.type = type;
this.inventoryClick = (player, slot, clickType, stack, result) -> {
if (slot == InventoryConstants.INVALID_SLOT_ID) {
result.accept(ClickHolder.noClick());
return;
}
ISlot clickedSlot = null;
if (this.dataLayout != null) {
clickedSlot = this.dataLayout.getSlot(slot);
}
if ((clickedSlot == null || clickedSlot instanceof EmptySlot) && this.inventoryLayout != null) {
clickedSlot = this.inventoryLayout.getSlot(slot);
}
acceptClick(clickedSlot, player, clickType, slot, stack, result);
};
}
/**
* Handles the click request on a given slot.
*
* @param slot the {@link ISlot} which is clicked
* @param player the {@link Player} who is involved
* @param click the given {@link ClickType}
* @param slotID the slot ID which is clicked
* @param stack the {@link ItemStack} which is clicked
* @param result the consumer to accept the {@link ClickHolder} result
*/
private void acceptClick(
@Nullable ISlot slot,
@NotNull Player player,
@NotNull Click click,
int slotID,
@NotNull ItemStack stack,
@NotNull Consumer<ClickHolder> result
) {
if (slot == null || slot instanceof EmptySlot) {
result.accept(ClickHolder.noClick());
return;
}
slot.getClick().onClick(player, slotID, click, stack, result);
}
/**
* Set a new reference to the data layout
*
* @param dataLayout The {@link InventoryLayout} to set
*/
public void setDataLayoutFunction(ThrowingFunction<InventoryLayout, InventoryLayout> dataLayout) {
this.dataLayoutFunction = dataLayout;
}
/**
* Registers the underlying listeners into the event graph.
*/
public abstract void register();
/**
* Removes the underlying listeners from the event graph.
*/
public abstract void unregister();
/**
* Returns the inventory for the given locale.
* If the locale is null, it indicates that the context is not translated and the non translated inventory will be returned.
*
* @param locale the locale to use
* @return the inventory for the given locale
*/
public abstract Inventory getInventory(@Nullable Locale locale);
/**
* Returns if the inventory is currently opened by a player.
*
* @return true if a player does the inventory currently opened otherwise false
*/
protected abstract boolean isOpen();
/**
* Updates the underlying inventory.
*/
protected abstract void updateInventory();
/**
* Apply the data layout to the inventory
*/
protected abstract void applyDataLayout();
/**
* Marks the inventory layout as not valid an updates the inventory.
*/
public void invalidateLayout() {
if (!inventoryLayoutValid) return;
inventoryLayoutValid = false;
if (isOpen()) {
updateInventory();
}
}
/**
* Invalidates the boolean for the data layout.
*/
public void invalidateDataLayout() {
if (isOpen()) {
retrieveDataLayout();
LOGGER.debug("DataLayout invalidated on open inv, requested data...");
} else {
LOGGER.debug("DataLayout invalidated on closed inv");
dataLayoutValid = false;
}
}
/**
* Handles the open function when an inventory will be opened.
*
* @param event The instance from the inventory event
*/
protected void handleOpen(@NotNull InventoryOpenEvent event) {
if (this.openFunction == null) return;
openFunction.onOpen(event);
}
/**
* Handles the close function if a close event is called from the server
*
* @param event The instance from the close event
*/
protected void handleClose(@NotNull InventoryCloseEvent event) {
if (this.closeFunction == null) return;
closeFunction.onClose(event);
}
/**
* Updates the given inventory with the content.
*
* @param inventory the inventory that should receive the update
* @param locale the locale for the inventory
* @param applyLayout if the layout should be applied
*/
protected void updateInventory(@NotNull Inventory inventory, Locale locale, boolean applyLayout){
if (!applyLayout) return;
if (this.inventoryLayout == null) {
throw new IllegalStateException("Can't update content because the layout is null");
}
// Design
ItemStack[] contents = inventory.getItemStacks();
inventory.clear();
this.inventoryLayout.applyLayout(contents, locale);
this.setItemsInternal(inventory, contents);
LOGGER.debug("UpdateInventory applied the InventoryLayout!");
this.inventoryLayoutValid = true;
synchronized (this) {
if (!dataLayoutValid) {
retrieveDataLayout();
}
}
}
/**
* Set's the given array with the {@link ItemStack}'s into an inventory.
*
* @param inventory the inventory for the items
* @param contents the array itself that contains all items
*/
private void setItemsInternal(@NotNull Inventory inventory, @NotNull ItemStack[] contents) {
for (int i = 0; i < contents.length; i++) {
var contentSlot = contents[i];
if (contentSlot == null || contentSlot.isAir()) continue;
inventory.setItemStack(i, contentSlot);
}
}
/**
* Executes the logic to retrieve the {@link InventoryLayout} which comes from the {@link ThrowingFunction}.
*/
protected void retrieveDataLayout() {
if (this.dataLayoutFunction == null) return;
if (dataLayoutPending) return;
synchronized (this) {
this.dataLayoutPending = true;
MinecraftServer.getSchedulerManager().scheduleNextTick(() -> {
try {
this.dataLayout = this.dataLayoutFunction.acceptThrows(this.dataLayout);
applyDataLayout();
} catch (Exception exception) {
MinecraftServer.getExceptionManager().handleException(exception);
} finally {
this.dataLayoutPending = false;
}
});
}
}
/**
* Updates the title of an inventory.
* Note that this method must be called from the developer
*
* @param inventory the {@link Inventory} to update the title
* @param newTitle the new title as {@link Component} to set
*/
protected void updateTitle(@NotNull Inventory inventory, @NotNull Component newTitle) {
inventory.setTitle(newTitle);
}
/**
* Updates the inventory for all current viewers.
*
* @param inventory The inventory to update
*/
protected void updateViewer(@NotNull Inventory inventory) {
if (inventory.getViewers().isEmpty()) return;
inventory.update();
}
/**
* Returns the underlying inventory.
* Please note this method ignores the translation context
*
* @return the given inventory
*/
public Inventory getInventory() {
return getInventory(null);
}
/**
* Returns the given {@link InventoryType} for the inventory.
*
* @return the given type
*/
public @NotNull InventoryType getType() {
return type;
}
/**
* Set the open function to the builder
*
* @param openFunction The function to set
* @return the current instance of the builder
*/
public InventoryBuilder setOpenFunction(OpenFunction openFunction) {
this.openFunction = openFunction;
return this;
}
/**
* Set the close function to the builder.
*
* @param closeFunction the function to set
* @return the current instance of the builder
*/
public InventoryBuilder setCloseFunction(CloseFunction closeFunction) {
this.closeFunction = closeFunction;
return this;
}
/**
* Set a new instance of the {@link InventoryLayout} to the builder
*
* @param inventoryLayoutImpl The layout to set
* @return the current instance of the builder
*/
public InventoryBuilder setLayout(@NotNull InventoryLayout inventoryLayoutImpl) {
this.inventoryLayout = inventoryLayoutImpl;
return this;
}
/**
* Returns the underlying {@link InventoryLayout}.
*
* @return the given layout
*/
public @Nullable InventoryLayout getLayout() {
return inventoryLayout;
}
/**
* Get underlying data {@link InventoryLayout}.
*
* @return the given layout
*/
public @Nullable InventoryLayout getDataLayout() {
return dataLayout;
}
}