DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
DreamItemStacks.java
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 Dreamfire Studio
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24package com.dreamfirestudios.dreamcore.DreamItems;
25
26import net.kyori.adventure.text.Component;
27import org.bukkit.Material;
28import org.bukkit.NamespacedKey;
29import org.bukkit.enchantments.Enchantment;
30import org.bukkit.inventory.Inventory;
31import org.bukkit.inventory.ItemStack;
32import org.bukkit.inventory.meta.ItemMeta;
33import org.bukkit.persistence.PersistentDataContainer;
34import org.bukkit.persistence.PersistentDataType;
35import org.bukkit.plugin.Plugin;
36
37import java.util.*;
38import java.util.function.Function;
39
66public final class DreamItemStacks {
67
68 private DreamItemStacks() {}
69
75 public static NamespacedKey keyId(Plugin plugin) {
76 return new NamespacedKey(plugin, "dream_item_id");
77 }
78
79 /* --------------------------------------------------------------------- */
80 /* Build */
81 /* --------------------------------------------------------------------- */
82
98 public static ItemStack build(Plugin plugin, IDreamItemStack def) {
99 Objects.requireNonNull(plugin, "plugin");
100 Objects.requireNonNull(def, "definition");
101
102 ItemStack stack = new ItemStack(def.type(), Math.max(1, def.amount()));
103 ItemMeta meta = stack.getItemMeta();
104
105 // Name
106 Component name = def.displayName();
107 if (name != null) meta.displayName(name);
108
109 // Lore
110 List<Component> lore = def.lore();
111 if (lore != null && !lore.isEmpty()) meta.lore(lore);
112
113 // Custom model data
114 def.customModelData().ifPresent(meta::setCustomModelData);
115
116 // Unbreakable, flags
117 meta.setUnbreakable(def.unbreakable());
118 Set<?> flags = def.flags();
119 if (flags != null && !flags.isEmpty()) {
120 meta.addItemFlags(def.flags().toArray(new org.bukkit.inventory.ItemFlag[0]));
121 }
122
123 // Attributes
124 Map<?, ?> attrs = def.attributeModifiers();
125 if (attrs != null && !attrs.isEmpty()) {
126 def.attributeModifiers().forEach((attr, mods) -> {
127 if (attr == null || mods == null) return;
128 for (var mod : mods) if (mod != null) meta.addAttributeModifier(attr, mod);
129 });
130 }
131
132 // PDC
133 PersistentDataContainer pdc = meta.getPersistentDataContainer();
134 def.id().ifPresent(id -> pdc.set(keyId(plugin), PersistentDataType.STRING, id));
135 def.writePdc(plugin, pdc);
136
137 stack.setItemMeta(meta);
138
139 // Enchantments last
140 Map<Enchantment, Integer> ench = def.enchantments();
141 if (ench != null && !ench.isEmpty()) {
142 ench.forEach((e, lvl) -> {
143 if (e != null && lvl != null && lvl > 0) stack.addUnsafeEnchantment(e, lvl);
144 });
145 }
146
147 return stack;
148 }
149
150 /* --------------------------------------------------------------------- */
151 /* Resolve */
152 /* --------------------------------------------------------------------- */
153
165 public static Optional<String> readId(Plugin plugin, ItemStack stack) {
166 if (stack == null || stack.getType() == Material.AIR) return Optional.empty();
167 ItemMeta meta = stack.getItemMeta();
168 if (meta == null) return Optional.empty();
169 String id = meta.getPersistentDataContainer().get(keyId(plugin), PersistentDataType.STRING);
170 return Optional.ofNullable(id);
171 }
172
188 public static Optional<IDreamItemStack> resolveById(
189 Plugin plugin,
190 ItemStack stack,
191 Function<String, IDreamItemStack> registryLookup
192 ) {
193 Objects.requireNonNull(registryLookup, "registryLookup");
194 return readId(plugin, stack).map(registryLookup);
195 }
196
209 public static Optional<IDreamItemStack> resolveById(
210 Plugin plugin,
211 ItemStack stack,
212 Collection<IDreamItemStack> registry
213 ) {
214 Optional<String> id = readId(plugin, stack);
215 if (id.isEmpty()) return Optional.empty();
216 String key = id.get();
217 for (IDreamItemStack def : registry) {
218 if (def != null && def.id().isPresent() && def.id().get().equals(key)) return Optional.of(def);
219 }
220 return Optional.empty();
221 }
222
223 /* --------------------------------------------------------------------- */
224 /* Equality / counting */
225 /* --------------------------------------------------------------------- */
226
243 public static boolean isSame(Plugin plugin, ItemStack a, ItemStack b) {
244 if (a == b) return true;
245 if (a == null || b == null) return false;
246
247 Optional<String> idA = readId(plugin, a);
248 Optional<String> idB = readId(plugin, b);
249 if (idA.isPresent() && idB.isPresent()) {
250 return idA.get().equals(idB.get());
251 }
252 return a.isSimilar(b);
253 }
254
267 public static int count(Plugin plugin, Inventory inv, ItemStack probe) {
268 Objects.requireNonNull(inv, "inventory");
269 Objects.requireNonNull(probe, "probe");
270 int total = 0;
271 for (ItemStack s : inv.getContents()) {
272 if (s == null) continue;
273 if (isSame(plugin, s, probe)) total += s.getAmount();
274 }
275 return total;
276 }
277
289 public static int count(Inventory inv, Material material) {
290 Objects.requireNonNull(inv, "inventory");
291 Objects.requireNonNull(material, "material");
292 int total = 0;
293 for (ItemStack s : inv.getContents()) {
294 if (s == null) continue;
295 if (s.getType() == material) total += s.getAmount();
296 }
297 return total;
298 }
299}
Utilities for building, resolving, comparing, and counting custom items.
static boolean isSame(Plugin plugin, ItemStack a, ItemStack b)
Safer equality check for items:
static int count(Inventory inv, Material material)
Counts items by Material (null-safe).
static Optional< IDreamItemStack > resolveById(Plugin plugin, ItemStack stack, Collection< IDreamItemStack > registry)
Resolves a definition by scanning a collection for a matching ID.
static ItemStack build(Plugin plugin, IDreamItemStack def)
Builds an ItemStack from a definition and writes its ID (if any) to PDC.
static Optional< String > readId(Plugin plugin, ItemStack stack)
Reads the stored item ID from PDC, if present.
static Optional< IDreamItemStack > resolveById(Plugin plugin, ItemStack stack, Function< String, IDreamItemStack > registryLookup)
Resolves a definition by PDC ID using a registry lookup function.
static NamespacedKey keyId(Plugin plugin)
Generates the PDC key used to store the optional custom item ID.
static int count(Plugin plugin, Inventory inv, ItemStack probe)
Counts how many items equivalent to probe exist in an inventory.
default List< Component > lore()
Lore lines (Adventure).
default Optional< String > id()
Optional stable ID.
default Material type()
Base material type for the item.
default Map< Attribute, Collection< AttributeModifier > > attributeModifiers()
Attribute modifiers.
default Component displayName()
Display name (Adventure).
default Set< ItemFlag > flags()
Item flags to apply (e.g., hide attributes).
default int amount()
Initial stack amount (clamped to ≥ 1).
default void writePdc(Plugin plugin, PersistentDataContainer pdc)
Hook to write any custom Persistent Data Container values.
default Map< Enchantment, Integer > enchantments()
Enchantments to apply after meta is set.
default OptionalInt customModelData()
Optional custom model data.
default boolean unbreakable()
Whether the item is unbreakable.