BlueGit

timfa copper-key

Files

copper-key / src / main / java / nl / timfalken / copperkeys / KeyItems.java

KeyItems.java

KeyItems.java 10.8 KiB Download Raw
1package nl.timfalken.copperkeys;
2
3import net.kyori.adventure.text.Component;
4import net.kyori.adventure.text.format.NamedTextColor;
5import net.kyori.adventure.text.format.TextDecoration;
6import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
7import org.bukkit.Material;
8import org.bukkit.NamespacedKey;
9import org.bukkit.block.ShulkerBox;
10import org.bukkit.inventory.Inventory;
11import org.bukkit.inventory.ItemStack;
12import org.bukkit.inventory.meta.BlockStateMeta;
13import org.bukkit.inventory.meta.BundleMeta;
14import org.bukkit.inventory.meta.ItemMeta;
15import org.bukkit.persistence.PersistentDataContainer;
16import org.bukkit.persistence.PersistentDataType;
17
18import java.util.ArrayList;
19import java.util.List;
20import java.util.UUID;
21
22final class KeyItems {
23 private static final int MAX_NESTING_DEPTH = 16;
24 static final int MAX_KEY_NAME_LENGTH = 50;
25 /** Bundle occupancy is 1/maxStackSize; keys with the same binding share identical meta and can stack. */
26 private static final int KEY_MAX_STACK_SIZE = 64;
27
28 private final CopperKeysPlugin plugin;
29 private final NamespacedKey markerKey;
30 private final NamespacedKey bindingKey;
31 private final NamespacedKey itemModel;
32
33 KeyItems(CopperKeysPlugin plugin) {
34 this.plugin = plugin;
35 this.markerKey = new NamespacedKey(plugin, "key");
36 this.bindingKey = new NamespacedKey(plugin, "binding");
37 this.itemModel = new NamespacedKey("copperkeys", "key");
38 }
39
40 ItemStack createBlankKey() {
41 ItemStack item = new ItemStack(Material.CLOCK);
42 makeBlank(item);
43 return item;
44 }
45
46 ItemStack createBoundKey(LockRecord lock) {
47 ItemStack item = new ItemStack(Material.CLOCK);
48 bind(item, lock);
49 return item;
50 }
51
52 boolean isKey(ItemStack item) {
53 if (item == null || item.getType() != Material.CLOCK || !item.hasItemMeta()) {
54 return false;
55 }
56 Byte marker = item.getItemMeta().getPersistentDataContainer().get(markerKey, PersistentDataType.BYTE);
57 return marker != null && marker == (byte) 1;
58 }
59
60 UUID getBinding(ItemStack item) {
61 if (!isKey(item)) {
62 return null;
63 }
64 String raw = item.getItemMeta().getPersistentDataContainer().get(bindingKey, PersistentDataType.STRING);
65 if (raw == null) {
66 return null;
67 }
68 try {
69 return UUID.fromString(raw);
70 } catch (IllegalArgumentException ignored) {
71 return null;
72 }
73 }
74
75 boolean isBlank(ItemStack item) {
76 return isKey(item) && getBinding(item) == null;
77 }
78
79 void bind(ItemStack item, LockRecord lock) {
80 applyBoundAppearance(item, lock);
81 }
82
83 void applyBoundAppearance(ItemStack item, LockRecord lock) {
84 ItemMeta meta = item.getItemMeta();
85 applyCommon(meta);
86 meta.getPersistentDataContainer().set(bindingKey, PersistentDataType.STRING, lock.bindingId().toString());
87 meta.itemName(Component.text(lock.keyName(), NamedTextColor.WHITE));
88 meta.customName(null);
89 meta.lore(List.of(
90 Component.text("Unlocks " + lock.displayName(), NamedTextColor.GRAY)
91 .decoration(TextDecoration.ITALIC, false)
92 ));
93 item.setItemMeta(meta);
94 }
95
96 void makeBlank(ItemStack item) {
97 ItemMeta meta = item.getItemMeta();
98 applyCommon(meta);
99 meta.getPersistentDataContainer().remove(bindingKey);
100 meta.itemName(Component.text("Blank Key", NamedTextColor.WHITE));
101 meta.customName(null);
102 meta.lore(null);
103 item.setItemMeta(meta);
104 }
105
106 private void applyCommon(ItemMeta meta) {
107 PersistentDataContainer pdc = meta.getPersistentDataContainer();
108 pdc.set(markerKey, PersistentDataType.BYTE, (byte) 1);
109 meta.setMaxStackSize(KEY_MAX_STACK_SIZE);
110 meta.setItemModel(itemModel);
111 }
112
113 private boolean syncKey(ItemStack key) {
114 boolean updated = migrateKeyFormat(key);
115 UUID binding = getBinding(key);
116 if (binding == null) {
117 // Blank keys must never keep a custom name.
118 ItemMeta meta = key.getItemMeta();
119 if (meta != null && meta.hasCustomName()) {
120 meta.customName(null);
121 meta.itemName(Component.text("Blank Key", NamedTextColor.WHITE));
122 key.setItemMeta(meta);
123 return true;
124 }
125 return updated;
126 }
127 LockRecord lock = plugin.lockStore().get(binding);
128 if (lock == null) {
129 makeBlank(key);
130 return true;
131 }
132 if (!matchesBoundAppearance(key, lock)) {
133 applyBoundAppearance(key, lock);
134 return true;
135 }
136 return updated;
137 }
138
139 private boolean matchesBoundAppearance(ItemStack key, LockRecord lock) {
140 ItemMeta meta = key.getItemMeta();
141 if (meta == null || meta.hasCustomName()) {
142 return false;
143 }
144 Component itemName = meta.itemName();
145 if (itemName == null) {
146 return false;
147 }
148 String plain = PlainTextComponentSerializer.plainText().serialize(itemName);
149 return lock.keyName().equals(plain);
150 }
151
152 private boolean migrateKeyFormat(ItemStack key) {
153 ItemMeta meta = key.getItemMeta();
154 if (meta == null) {
155 return false;
156 }
157 if (meta.hasMaxStackSize() && meta.getMaxStackSize() == KEY_MAX_STACK_SIZE) {
158 return false;
159 }
160 applyCommon(meta);
161 key.setItemMeta(meta);
162 return true;
163 }
164
165 void applyNameInInventory(Inventory inventory, UUID bindingId) {
166 LockRecord lock = plugin.lockStore().get(bindingId);
167 if (lock == null) {
168 return;
169 }
170 for (int slot = 0; slot < inventory.getSize(); slot++) {
171 ItemStack item = inventory.getItem(slot);
172 if (item == null) {
173 continue;
174 }
175 boolean changed = visit(item, 0, key -> {
176 if (bindingId.equals(getBinding(key))) {
177 applyBoundAppearance(key, lock);
178 return true;
179 }
180 return false;
181 });
182 if (changed) {
183 inventory.setItem(slot, item);
184 }
185 }
186 }
187
188 boolean inventoryHasBinding(Inventory inventory, UUID bindingId) {
189 for (int slot = 0; slot < inventory.getSize(); slot++) {
190 ItemStack item = inventory.getItem(slot);
191 if (item == null) {
192 continue;
193 }
194 if (containsBinding(item, 0, bindingId)) {
195 return true;
196 }
197 }
198 return false;
199 }
200
201 private boolean containsBinding(ItemStack item, int depth, UUID bindingId) {
202 if (item == null || item.getType().isAir() || depth > MAX_NESTING_DEPTH) {
203 return false;
204 }
205 if (isKey(item) && bindingId.equals(getBinding(item))) {
206 return true;
207 }
208
209 ItemMeta meta = item.getItemMeta();
210 if (meta instanceof BundleMeta bundleMeta) {
211 for (ItemStack nested : bundleMeta.getItems()) {
212 if (containsBinding(nested, depth + 1, bindingId)) {
213 return true;
214 }
215 }
216 } else if (meta instanceof BlockStateMeta blockStateMeta
217 && blockStateMeta.getBlockState() instanceof ShulkerBox shulker) {
218 Inventory contents = shulker.getInventory();
219 for (int slot = 0; slot < contents.getSize(); slot++) {
220 if (containsBinding(contents.getItem(slot), depth + 1, bindingId)) {
221 return true;
222 }
223 }
224 }
225 return false;
226 }
227
228 void normalizeInventory(Inventory inventory) {
229 for (int slot = 0; slot < inventory.getSize(); slot++) {
230 ItemStack item = inventory.getItem(slot);
231 if (item == null) {
232 continue;
233 }
234 boolean changed = visit(item, 0, this::syncKey);
235 if (changed) {
236 inventory.setItem(slot, item);
237 }
238 }
239 }
240
241 void blankBindingInInventory(Inventory inventory, UUID bindingId) {
242 for (int slot = 0; slot < inventory.getSize(); slot++) {
243 ItemStack item = inventory.getItem(slot);
244 if (item == null) {
245 continue;
246 }
247 boolean changed = visit(item, 0, key -> {
248 boolean updated = migrateKeyFormat(key);
249 if (bindingId.equals(getBinding(key))) {
250 makeBlank(key);
251 return true;
252 }
253 return updated;
254 });
255 if (changed) {
256 inventory.setItem(slot, item);
257 }
258 }
259 }
260
261 boolean normalizeItem(ItemStack item) {
262 if (item == null) {
263 return false;
264 }
265 return visit(item, 0, this::syncKey);
266 }
267
268 private boolean visit(ItemStack item, int depth, KeyVisitor visitor) {
269 if (item == null || item.getType().isAir() || depth > MAX_NESTING_DEPTH) {
270 return false;
271 }
272
273 boolean changed = false;
274 if (isKey(item)) {
275 changed |= visitor.visit(item);
276 }
277
278 ItemMeta meta = item.getItemMeta();
279 if (meta instanceof BundleMeta bundleMeta) {
280 List<ItemStack> oldItems = bundleMeta.getItems();
281 List<ItemStack> newItems = new ArrayList<>(oldItems.size());
282 boolean nestedChanged = false;
283 for (ItemStack nested : oldItems) {
284 ItemStack copy = nested.clone();
285 nestedChanged |= visit(copy, depth + 1, visitor);
286 newItems.add(copy);
287 }
288 if (nestedChanged) {
289 bundleMeta.setItems(newItems);
290 item.setItemMeta(bundleMeta);
291 changed = true;
292 }
293 } else if (meta instanceof BlockStateMeta blockStateMeta
294 && blockStateMeta.getBlockState() instanceof ShulkerBox shulker) {
295 Inventory contents = shulker.getInventory();
296 boolean nestedChanged = false;
297 for (int slot = 0; slot < contents.getSize(); slot++) {
298 ItemStack nested = contents.getItem(slot);
299 if (nested == null) {
300 continue;
301 }
302 ItemStack copy = nested.clone();
303 if (visit(copy, depth + 1, visitor)) {
304 contents.setItem(slot, copy);
305 nestedChanged = true;
306 }
307 }
308 if (nestedChanged) {
309 blockStateMeta.setBlockState(shulker);
310 item.setItemMeta(blockStateMeta);
311 changed = true;
312 }
313 }
314 return changed;
315 }
316
317 @FunctionalInterface
318 private interface KeyVisitor {
319 boolean visit(ItemStack key);
320 }
321}