Skip to content

Commit 77dac99

Browse files
author
Oribuin
committed
Swap plugin to use KeyRegistry
Took 5 minutes
1 parent 511d91e commit 77dac99

File tree

5 files changed

+83
-48
lines changed

5 files changed

+83
-48
lines changed

src/main/java/dev/oribuin/fishing/api/recipe/type/AugmentRecipeItem.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import dev.oribuin.fishing.api.recipe.RecipeItem;
44
import dev.oribuin.fishing.model.augment.Augment;
55
import dev.oribuin.fishing.model.augment.AugmentRegistry;
6-
import dev.oribuin.fishing.storage.util.PersistKeys;
6+
import dev.oribuin.fishing.storage.util.KeyRegistry;
77
import dev.rosewood.rosegarden.config.CommentedConfigurationSection;
88
import org.bukkit.inventory.ItemStack;
99
import org.bukkit.persistence.PersistentDataContainer;
@@ -29,7 +29,7 @@ public boolean check(ItemStack item) {
2929
if (item == null) return false;
3030

3131
PersistentDataContainer container = item.getItemMeta().getPersistentDataContainer();
32-
String augmentType = container.get(PersistKeys.AUGMENT_TYPE, PersistentDataType.STRING);
32+
String augmentType = container.get(KeyRegistry.AUGMENT_TYPE, PersistentDataType.STRING);
3333

3434
return augmentType != null && augmentType.equalsIgnoreCase(this.item.name());
3535
}

src/main/java/dev/oribuin/fishing/model/fish/Fish.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import dev.oribuin.fishing.manager.TierManager;
77
import dev.oribuin.fishing.model.condition.CatchCondition;
88
import dev.oribuin.fishing.model.condition.ConditionRegistry;
9-
import dev.oribuin.fishing.storage.util.PersistKeys;
9+
import dev.oribuin.fishing.storage.util.KeyRegistry;
1010
import dev.rosewood.rosegarden.config.CommentedConfigurationSection;
1111
import dev.rosewood.rosegarden.config.CommentedFileConfiguration;
1212
import dev.rosewood.rosegarden.utils.StringPlaceholders;
@@ -127,8 +127,8 @@ public ItemStack createItemStack() {
127127

128128
// fish data :-)
129129
PersistentDataContainer container = itemMeta.getPersistentDataContainer();
130-
container.set(PersistKeys.FISH_TYPE, PersistentDataType.STRING, this.name);
131-
container.set(PersistKeys.FISH_TYPE, PersistentDataType.STRING, this.tier);
130+
container.set(KeyRegistry.FISH_TYPE, PersistentDataType.STRING, this.name);
131+
container.set(KeyRegistry.FISH_TYPE, PersistentDataType.STRING, this.tier);
132132
});
133133

134134
this.itemStack = itemStack;

src/main/java/dev/oribuin/fishing/model/totem/Totem.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import dev.oribuin.fishing.manager.TotemManager;
88
import dev.oribuin.fishing.model.item.ItemConstruct;
99
import dev.oribuin.fishing.model.item.ItemRegistry;
10-
import dev.oribuin.fishing.storage.util.PersistKeys;
10+
import dev.oribuin.fishing.storage.util.KeyRegistry;
1111
import dev.oribuin.fishing.util.FishUtils;
1212
import dev.oribuin.fishing.util.math.MathL;
1313
import dev.rosewood.rosegarden.utils.StringPlaceholders;
@@ -221,13 +221,13 @@ public void saveTo(ItemStack itemStack) {
221221
* @param container The container to save the values to
222222
*/
223223
public void saveToContainer(PersistentDataContainer container) {
224-
container.set(PersistKeys.TOTEM_OWNER, DataType.UUID, this.owner);
225-
container.set(PersistKeys.TOTEM_OWNERNAME, DataType.STRING, this.ownerName);
226-
container.set(PersistKeys.TOTEM_RADIUS, DataType.INTEGER, this.radius);
227-
container.set(PersistKeys.TOTEM_ACTIVE, DataType.BOOLEAN, this.active);
228-
container.set(PersistKeys.TOTEM_DURATION, DataType.LONG, this.duration.toMillis());
229-
container.set(PersistKeys.TOTEM_COOLDOWN, DataType.LONG, this.cooldown.toMillis());
230-
container.set(PersistKeys.TOTEM_LASTACTIVE, DataType.LONG, this.lastActive);
224+
container.set(KeyRegistry.TOTEM_OWNER, DataType.UUID, this.owner);
225+
container.set(KeyRegistry.TOTEM_OWNERNAME, DataType.STRING, this.ownerName);
226+
container.set(KeyRegistry.TOTEM_RADIUS, DataType.INTEGER, this.radius);
227+
container.set(KeyRegistry.TOTEM_ACTIVE, DataType.BOOLEAN, this.active);
228+
container.set(KeyRegistry.TOTEM_DURATION, DataType.LONG, this.duration.toMillis());
229+
container.set(KeyRegistry.TOTEM_COOLDOWN, DataType.LONG, this.cooldown.toMillis());
230+
container.set(KeyRegistry.TOTEM_LASTACTIVE, DataType.LONG, this.lastActive);
231231
}
232232

233233
/**
@@ -238,13 +238,13 @@ public void saveToContainer(PersistentDataContainer container) {
238238
* @return The totem object
239239
*/
240240
public static Totem fromContainer(PersistentDataContainer container) {
241-
UUID owner = container.get(PersistKeys.TOTEM_OWNER, DataType.UUID);
242-
String ownerName = container.getOrDefault(PersistKeys.TOTEM_OWNERNAME, DataType.STRING, "N/A");
243-
Integer radius = container.get(PersistKeys.TOTEM_RADIUS, DataType.INTEGER);
244-
boolean active = container.getOrDefault(PersistKeys.TOTEM_ACTIVE, DataType.BOOLEAN, false);
245-
Long duration = container.get(PersistKeys.TOTEM_DURATION, DataType.LONG);
246-
Long cooldown = container.get(PersistKeys.TOTEM_COOLDOWN, DataType.LONG);
247-
long lastActive = container.getOrDefault(PersistKeys.TOTEM_LASTACTIVE, DataType.LONG, System.currentTimeMillis());
241+
UUID owner = container.get(KeyRegistry.TOTEM_OWNER, DataType.UUID);
242+
String ownerName = container.getOrDefault(KeyRegistry.TOTEM_OWNERNAME, DataType.STRING, "N/A");
243+
Integer radius = container.get(KeyRegistry.TOTEM_RADIUS, DataType.INTEGER);
244+
boolean active = container.getOrDefault(KeyRegistry.TOTEM_ACTIVE, DataType.BOOLEAN, false);
245+
Long duration = container.get(KeyRegistry.TOTEM_DURATION, DataType.LONG);
246+
Long cooldown = container.get(KeyRegistry.TOTEM_COOLDOWN, DataType.LONG);
247+
long lastActive = container.getOrDefault(KeyRegistry.TOTEM_LASTACTIVE, DataType.LONG, System.currentTimeMillis());
248248

249249
if (owner == null || radius == null || duration == null || cooldown == null) return null;
250250

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package dev.oribuin.fishing.storage.util;
2+
3+
import dev.oribuin.fishing.FishingPlugin;
4+
import org.bukkit.NamespacedKey;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
/**
10+
* Registry for all the {@link NamespacedKey} used in the plugin, for easy access.
11+
* <p>
12+
* This is quite unnecessary, but it's a good practice to keep all the keys in one place.
13+
* Especially when another plugin wants to establish their own keys
14+
*/
15+
public class KeyRegistry {
16+
17+
private static final Map<String, NamespacedKey> keys = new HashMap<>();
18+
19+
public static NamespacedKey FISH_TYPE = register("fish_type");
20+
public static final NamespacedKey AUGMENT_TYPE = register("augment_type");
21+
public static final NamespacedKey TOTEM_OWNER = register("totem_owner");
22+
public static final NamespacedKey TOTEM_OWNERNAME = register("totem_ownername");
23+
public static final NamespacedKey TOTEM_RADIUS = register("totem_radius");
24+
public static final NamespacedKey TOTEM_ACTIVE = register("totem_active");
25+
public static final NamespacedKey TOTEM_DURATION = register("totem_duration");
26+
public static final NamespacedKey TOTEM_COOLDOWN = register("totem_cooldown");
27+
public static final NamespacedKey TOTEM_LASTACTIVE = register("totem_lastactive");
28+
29+
/**
30+
* Get a {@link NamespacedKey} from the registry
31+
*
32+
* @param key The key to get
33+
*
34+
* @return The {@link NamespacedKey} from the registry
35+
*/
36+
public static NamespacedKey register(String key) {
37+
NamespacedKey namespacedKey = new NamespacedKey(FishingPlugin.get(), key);
38+
keys.put(key, namespacedKey);
39+
return namespacedKey;
40+
}
41+
42+
/**
43+
* Get a {@link NamespacedKey} from the registry
44+
*
45+
* @param key The key to get
46+
*
47+
* @return The {@link NamespacedKey} from the registry
48+
*/
49+
public static NamespacedKey get(String key) {
50+
return keys.get(key);
51+
}
52+
53+
/**
54+
* Get all the keys in the registry
55+
*
56+
* @return All the keys in the registry
57+
*/
58+
public static Map<String, NamespacedKey> getKeys() {
59+
return keys;
60+
}
61+
62+
}
63+

src/main/java/dev/oribuin/fishing/storage/util/PersistKeys.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)