BlueGit

timfa copper-key

Files

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

KeyItems.java

KeyItems.java 7.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 org.bukkit.Material;
7import org.bukkit.NamespacedKey;
8import org.bukkit.block.ShulkerBox;
9import org.bukkit.inventory.Inventory;
10import org.bukkit.inventory.ItemStack;
11import org.bukkit.inventory.meta.BlockStateMeta;
12import org.bukkit.inventory.meta.BundleMeta;
13import org.bukkit.inventory.meta.ItemMeta;
14import org.bukkit.persistence.PersistentDataContainer;
15import org.bukkit.persistence.PersistentDataType;
16
17import java.util.ArrayList;
18import java.util.List;
19import java.util.UUID;
20
21final class KeyItems {
22 private static final int MAX_NESTING_DEPTH = 16;
23
24 private final CopperKeysPlugin plugin;
25 private final NamespacedKey markerKey;
26 private final NamespacedKey bindingKey;
27 private final NamespacedKey itemModel;
28
29 KeyItems(CopperKeysPlugin plugin) {
30 this.plugin = plugin;
31 this.markerKey = new NamespacedKey(plugin, "key");
32 this.bindingKey = new NamespacedKey(plugin, "binding");
33 this.itemModel = new NamespacedKey("copperkeys", "key");
34 }
35
36 ItemStack createBlankKey() {
37 ItemStack item = new ItemStack(Material.CLOCK);
38 makeBlank(item);
39 return item;
40 }
41
42 ItemStack createBoundKey(LockRecord lock) {
43 ItemStack item = new ItemStack(Material.CLOCK);
44 bind(item, lock);
45 return item;
46 }
47
48 boolean isKey(ItemStack item) {
49 if (item == null || item.getType() != Material.CLOCK || !item.hasItemMeta()) {
50 return false;
51 }
52 Byte marker = item.getItemMeta().getPersistentDataContainer().get(markerKey, PersistentDataType.BYTE);
53 return marker != null && marker == (byte) 1;
54 }
55
56 UUID getBinding(ItemStack item) {
57 if (!isKey(item)) {
58 return null;
59 }
60 String raw = item.getItemMeta().getPersistentDataContainer().get(bindingKey, PersistentDataType.STRING);
61 if (raw == null) {
62 return null;
63 }
64 try {
65 return UUID.fromString(raw);
66 } catch (IllegalArgumentException ignored) {
67 return null;
68 }
69 }
70
71 boolean isBlank(ItemStack item) {
72 return isKey(item) && getBinding(item) == null;
73 }
74
75 void bind(ItemStack item, LockRecord lock) {
76 ItemMeta meta = item.getItemMeta();
77 applyCommon(meta);
78 meta.getPersistentDataContainer().set(bindingKey, PersistentDataType.STRING, lock.bindingId().toString());
79 meta.itemName(Component.text("Key", NamedTextColor.WHITE));
80 meta.lore(List.of(
81 Component.text("Unlocks " + lock.displayName(), NamedTextColor.GRAY)
82 .decoration(TextDecoration.ITALIC, false)
83 ));
84 item.setItemMeta(meta);
85 }
86
87 void makeBlank(ItemStack item) {
88 ItemMeta meta = item.getItemMeta();
89 applyCommon(meta);
90 meta.getPersistentDataContainer().remove(bindingKey);
91 meta.itemName(Component.text("Blank Key", NamedTextColor.WHITE));
92 meta.lore(null);
93 item.setItemMeta(meta);
94 }
95
96 private void applyCommon(ItemMeta meta) {
97 PersistentDataContainer pdc = meta.getPersistentDataContainer();
98 pdc.set(markerKey, PersistentDataType.BYTE, (byte) 1);
99 meta.setMaxStackSize(1);
100 meta.setItemModel(itemModel);
101 }
102
103 boolean inventoryHasBinding(Inventory inventory, UUID bindingId) {
104 boolean[] found = {false};
105 for (int slot = 0; slot < inventory.getSize(); slot++) {
106 ItemStack item = inventory.getItem(slot);
107 if (item == null) {
108 continue;
109 }
110 boolean changed = visit(item, 0, key -> {
111 UUID keyBinding = getBinding(key);
112 if (keyBinding != null && !plugin.lockStore().exists(keyBinding)) {
113 makeBlank(key);
114 return true;
115 }
116 if (bindingId.equals(keyBinding)) {
117 found[0] = true;
118 }
119 return false;
120 });
121 if (changed) {
122 inventory.setItem(slot, item);
123 }
124 if (found[0]) {
125 return true;
126 }
127 }
128 return false;
129 }
130
131 void normalizeInventory(Inventory inventory) {
132 for (int slot = 0; slot < inventory.getSize(); slot++) {
133 ItemStack item = inventory.getItem(slot);
134 if (item == null) {
135 continue;
136 }
137 boolean changed = visit(item, 0, key -> {
138 UUID binding = getBinding(key);
139 if (binding != null && !plugin.lockStore().exists(binding)) {
140 makeBlank(key);
141 return true;
142 }
143 return false;
144 });
145 if (changed) {
146 inventory.setItem(slot, item);
147 }
148 }
149 }
150
151 void blankBindingInInventory(Inventory inventory, UUID bindingId) {
152 for (int slot = 0; slot < inventory.getSize(); slot++) {
153 ItemStack item = inventory.getItem(slot);
154 if (item == null) {
155 continue;
156 }
157 boolean changed = visit(item, 0, key -> {
158 if (bindingId.equals(getBinding(key))) {
159 makeBlank(key);
160 return true;
161 }
162 return false;
163 });
164 if (changed) {
165 inventory.setItem(slot, item);
166 }
167 }
168 }
169
170 boolean normalizeItem(ItemStack item) {
171 if (item == null) {
172 return false;
173 }
174 return visit(item, 0, key -> {
175 UUID binding = getBinding(key);
176 if (binding != null && !plugin.lockStore().exists(binding)) {
177 makeBlank(key);
178 return true;
179 }
180 return false;
181 });
182 }
183
184 private boolean visit(ItemStack item, int depth, KeyVisitor visitor) {
185 if (item == null || item.getType().isAir() || depth > MAX_NESTING_DEPTH) {
186 return false;
187 }
188
189 boolean changed = false;
190 if (isKey(item)) {
191 changed |= visitor.visit(item);
192 }
193
194 ItemMeta meta = item.getItemMeta();
195 if (meta instanceof BundleMeta bundleMeta) {
196 List<ItemStack> oldItems = bundleMeta.getItems();
197 List<ItemStack> newItems = new ArrayList<>(oldItems.size());
198 boolean nestedChanged = false;
199 for (ItemStack nested : oldItems) {
200 ItemStack copy = nested.clone();
201 nestedChanged |= visit(copy, depth + 1, visitor);
202 newItems.add(copy);
203 }
204 if (nestedChanged) {
205 bundleMeta.setItems(newItems);
206 item.setItemMeta(bundleMeta);
207 changed = true;
208 }
209 } else if (meta instanceof BlockStateMeta blockStateMeta && blockStateMeta.getBlockState() instanceof ShulkerBox shulker) {
210 Inventory contents = shulker.getInventory();
211 boolean nestedChanged = false;
212 for (int slot = 0; slot < contents.getSize(); slot++) {
213 ItemStack nested = contents.getItem(slot);
214 if (nested == null) {
215 continue;
216 }
217 ItemStack copy = nested.clone();
218 if (visit(copy, depth + 1, visitor)) {
219 contents.setItem(slot, copy);
220 nestedChanged = true;
221 }
222 }
223 if (nestedChanged) {
224 blockStateMeta.setBlockState(shulker);
225 item.setItemMeta(blockStateMeta);
226 changed = true;
227 }
228 }
229 return changed;
230 }
231
232 @FunctionalInterface
233 private interface KeyVisitor {
234 boolean visit(ItemStack key);
235 }
236}