-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalTranslatedInventoryBuilder.java
More file actions
127 lines (110 loc) · 4.22 KB
/
GlobalTranslatedInventoryBuilder.java
File metadata and controls
127 lines (110 loc) · 4.22 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
package net.theevilreaper.aves.inventory;
import net.kyori.adventure.text.Component;
import net.minestom.server.item.ItemStack;
import net.theevilreaper.aves.i18n.TextData;
import net.theevilreaper.aves.inventory.holder.InventoryHolderImpl;
import net.kyori.adventure.translation.GlobalTranslator;
import net.minestom.server.entity.Player;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryType;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/**
* The {@link GlobalTranslatedInventoryBuilder} is an implementation of the {@link BaseInventoryBuilderImpl} for a global translation context.
*
* @author Patrick Zdarsky / Rxcki
* @version 1.0.0
* @since 1.0.12
*/
@SuppressWarnings("java:S3252")
public class GlobalTranslatedInventoryBuilder extends BaseInventoryBuilderImpl {
private final Map<Locale, CustomInventory> inventoryTranslatedObjectCache = new HashMap<>();
private TextData titleData;
/**
* Creates a new instance from the class with the given {@link InventoryType}.
*
* @param type the type to define the size of the inventory
*/
public GlobalTranslatedInventoryBuilder(@NotNull InventoryType type) {
super(type);
}
@Contract(value = "_ -> new", pure = true)
private @NotNull CustomInventory create(Locale locale) {
Component title = GlobalTranslator.render(titleData.createComponent(), locale);
CustomInventory inventory = new CustomInventory(new InventoryHolderImpl(this), type, title);
updateInventory(inventory, locale, true);
return inventory;
}
@Override
public void unregister() {
this.unregister(this.getInventory().eventNode(), openListener, closeListener, clickListener);
this.holder = null;
if (this.inventoryTranslatedObjectCache.isEmpty()) return;
for (Map.Entry<Locale, CustomInventory> entry : this.inventoryTranslatedObjectCache.entrySet()) {
if (!entry.getValue().hasViewers()) continue;
for (Player viewer : entry.getValue().getViewers()) {
viewer.closeInventory();
}
}
}
@Override
public Inventory getInventory(@Nullable Locale locale) {
if (!dataLayoutValid || !inventoryLayoutValid) {
updateInventory();
}
return inventoryTranslatedObjectCache.computeIfAbsent(locale, this::create);
}
@Override
protected boolean isOpen() {
if (inventoryTranslatedObjectCache.isEmpty()) return false;
for (Inventory inventory : inventoryTranslatedObjectCache.values())
if (!inventory.getViewers().isEmpty())
return true;
return false;
}
@Override
protected void updateInventory() {
for (Map.Entry<Locale, CustomInventory> entry : inventoryTranslatedObjectCache.entrySet()) {
Locale locale = entry.getKey();
updateInventory(entry.getValue(), locale, true);
updateViewer(entry.getValue());
}
}
@Override
protected void applyDataLayout() {
if (getDataLayout() == null) return;
LOGGER.debug("Applying data layout");
synchronized (this) {
for (var entry : inventoryTranslatedObjectCache.entrySet()) {
var contents = entry.getValue().getItemStacks();
getDataLayout().applyLayout(contents, entry.getKey());
for (int i = 0; i < contents.length; i++) {
ItemStack stack = contents[i];
if (stack.isAir()) continue;
entry.getValue().setItemStack(i, stack);
}
entry.getValue().update();
}
}
}
/**
* Overwrites the current {@link TextData} with a new one.
*
* @param titleData The {@link TextData} to set.
*/
public void setTitleData(@NotNull TextData titleData) {
this.titleData = titleData;
}
/**
* Returns the {@link TextData} from the builder.
*
* @return The underlying value
*/
public TextData getTitleData() {
return titleData;
}
}