DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
DreamEnchantmentRegistry.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.DreamEnchantment;
25
26import org.bukkit.NamespacedKey;
27import org.bukkit.enchantments.Enchantment;
28import org.bukkit.inventory.ItemStack;
29import org.jetbrains.annotations.NotNull;
30import org.jetbrains.annotations.Nullable;
31
32import java.util.*;
33
42
43public final class DreamEnchantmentRegistry {
44
45 private DreamEnchantmentRegistry() {}
46
50 private static final Map<NamespacedKey, IDreamEnchantment> ENCHANTS = new LinkedHashMap<>();
51
55 private static final Map<NamespacedKey, Enchantment> WRAPPERS = new LinkedHashMap<>();
56
66
67 public static synchronized @NotNull Enchantment register(@NotNull IDreamEnchantment impl) {
68 var key = impl.getKey();
69 var existing = WRAPPERS.get(key);
70 if (existing != null) return existing;
71
72 ENCHANTS.put(key, impl);
73 var wrapper = impl.returnEnchantment();
74 WRAPPERS.put(key, wrapper);
75 return wrapper;
76 }
77
84
85 public static @Nullable Enchantment getWrapper(@NotNull NamespacedKey key) {
86 return WRAPPERS.get(key);
87 }
88
95
96 public static @Nullable IDreamEnchantment get(@NotNull NamespacedKey key) {
97 return ENCHANTS.get(key);
98 }
99
105
106 public static @NotNull Collection<Enchantment> allWrappers() {
107 return Collections.unmodifiableCollection(WRAPPERS.values());
108 }
109
115
116 public static @NotNull Collection<IDreamEnchantment> all() {
117 return Collections.unmodifiableCollection(ENCHANTS.values());
118 }
119
120 // ---------- item helpers ----------
121
131
132 public static @NotNull List<IDreamEnchantment> findOn(@Nullable ItemStack stack) {
133 if (stack == null || stack.getItemMeta() == null) return List.of();
134 var meta = stack.getItemMeta();
135 var out = new ArrayList<IDreamEnchantment>();
136 for (var impl : ENCHANTS.values()) {
137 var ench = impl.returnEnchantment(); // NOTE: creates a new wrapper per call (see suggestion).
138 if (meta.hasEnchant(ench)) out.add(impl);
139 }
140 return Collections.unmodifiableList(out);
141 }
142
150
151 public static boolean add(@NotNull ItemStack stack, @NotNull NamespacedKey key) {
152 var impl = ENCHANTS.get(key);
153 return impl != null && impl.addToItem(stack);
154 }
155}
static List< IDreamEnchantment > findOn(@Nullable ItemStack stack)
/
static boolean add(@NotNull ItemStack stack, @NotNull NamespacedKey key)
/
static synchronized Enchantment register(@NotNull IDreamEnchantment impl)
/