Skip to content

Commit 75b94ce

Browse files
committed
Add NamespacedKeyProvider
1 parent d4b19a5 commit 75b94ce

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

paper/src/main/java/net/j4c0b3y/api/config/platform/paper/PaperConfigHandler.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.j4c0b3y.api.config.platform.paper.types.WorldReference;
66
import net.kyori.adventure.text.Component;
77
import org.bukkit.Location;
8+
import org.bukkit.NamespacedKey;
89
import org.bukkit.Sound;
910
import org.bukkit.inventory.ItemStack;
1011
import org.bukkit.potion.PotionEffect;
@@ -40,6 +41,7 @@ public PaperConfigHandler(Logger logger, Component prefix) {
4041
this.bind(Sound.class, new SoundProvider());
4142
this.bind(Vector.class, new VectorProvider());
4243
this.bind(WorldReference.class, new WorldProvider());
44+
this.bind(NamespacedKey.class, new NamespacedKeyProvider());
4345
}
4446

4547
/**
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package net.j4c0b3y.api.config.platform.paper.provider;
2+
3+
import net.j4c0b3y.api.config.provider.TypeProvider;
4+
import net.j4c0b3y.api.config.provider.context.LoadContext;
5+
import net.j4c0b3y.api.config.provider.context.SaveContext;
6+
import org.bukkit.NamespacedKey;
7+
import org.jspecify.annotations.NonNull;
8+
import org.jspecify.annotations.Nullable;
9+
10+
public class NamespacedKeyProvider implements TypeProvider<NamespacedKey> {
11+
12+
@Override
13+
public @NonNull NamespacedKey load(@NonNull LoadContext context) {
14+
if (context.getObject() instanceof String fullKey) {
15+
String[] splits = fullKey.split(":");
16+
if (splits.length != 2) {
17+
throw new IllegalStateException(String.format("The key is invalid, it does not follow the 'namespace:key' structure: Key(%s)", fullKey));
18+
}
19+
20+
String namespace = splits[0];
21+
String key = splits[1];
22+
if (namespace.isBlank() || key.isBlank()) {
23+
throw new IllegalStateException(String.format("The key is invalid, it contains a blank namespace or key: Key(%s)", fullKey));
24+
}
25+
26+
return new NamespacedKey(namespace, key);
27+
}
28+
throw new IllegalStateException("Failed to parse NamespacedKey");
29+
}
30+
31+
@Override
32+
public @Nullable Object save(@NonNull SaveContext<NamespacedKey> context) {
33+
return context.getObject().asString();
34+
}
35+
36+
}

0 commit comments

Comments
 (0)