-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalInventoryBuilder.java
More file actions
105 lines (95 loc) · 3.27 KB
/
GlobalInventoryBuilder.java
File metadata and controls
105 lines (95 loc) · 3.27 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
package net.theevilreaper.aves.inventory;
import net.kyori.adventure.text.Component;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryType;
import net.minestom.server.item.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.Locale;
/**
* The {@link GlobalInventoryBuilder} builds an inventory which can be used in a global context.
* That means that the inventory is related to all players on the server and not bound to a single player.
*
* @author Patrick Zdarsky / Rxcki
* @version 1.0.0
* @since 1.0.0
*/
@SuppressWarnings("java:S3252")
public class GlobalInventoryBuilder extends BaseInventoryBuilderImpl {
private Component titleComponent;
private CustomInventory inventory;
/**
* Creates a new instance from the builder with the given parameter values.
*
* @param title the title as {@link Component }for the inventory
* @param type the {@link InventoryType} for the inventory
*/
public GlobalInventoryBuilder(@NotNull Component title, @NotNull InventoryType type) {
super(type);
this.titleComponent = title;
}
/**
* Updates the title for the inventory
*
* @param titleComponent the new title as {@link Component} to set
*/
public void setTitleComponent(@NotNull Component titleComponent) {
this.titleComponent = titleComponent;
this.updateTitle(inventory, titleComponent);
}
/**
* Returns the inventory with the current state of the items.
*
* @param ignored can be ignored
* @return the underlying inventory
*/
@Override
public Inventory getInventory(Locale ignored) {
if (dataLayoutValid && inventoryLayoutValid && inventory != null) {
return inventory;
}
updateInventory();
return inventory;
}
/**
* Indicates if an inventory is currently open by a player.
*
* @return True if a player has the inventory open otherwise false
*/
@Override
protected boolean isOpen() {
return inventory != null && !inventory.getViewers().isEmpty();
}
/**
* Updates the inventory content if something received a change.
*/
@Override
protected void updateInventory() {
boolean applyLayout = !inventoryLayoutValid;
if (inventory == null) {
this.inventory = new CustomInventory(this.holder, type, titleComponent);
applyLayout = true;
}
updateInventory(inventory, null, applyLayout);
updateViewer(inventory);
}
/**
* Applies the data layout to the inventory if the specific layout is set.
*/
@Override
protected void applyDataLayout() {
if (getDataLayout() == null) return;
synchronized (this) {
LOGGER.debug("Applying data layouts");
ItemStack[] contents = inventory.getItemStacks();
getDataLayout().applyLayout(contents, null);
for (int i = 0; i < contents.length; i++) {
ItemStack stack = contents[i];
if (stack != null && !stack.isAir()) {
this.inventory.setItemStack(i, stack);
}
}
this.dataLayoutValid = true;
updateViewer(inventory);
}
}
}