Skip to content

Commit 20ed4b3

Browse files
committed
Sound and potion effects
1 parent 0d659f5 commit 20ed4b3

4 files changed

Lines changed: 137 additions & 0 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
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.Sound;
89
import org.bukkit.inventory.ItemStack;
10+
import org.bukkit.potion.PotionEffect;
11+
import org.bukkit.potion.PotionEffectType;
912
import org.bukkit.util.Vector;
1013

1114
import java.util.logging.Logger;
@@ -32,6 +35,9 @@ public PaperConfigHandler(Logger logger, Component prefix) {
3235

3336
this.bind(ItemStack.class, new ItemStackProvider());
3437
this.bind(Location.class, new LocationProvider());
38+
this.bind(PotionEffectType.class, new PotionEffectTypeProvider());
39+
this.bind(PotionEffect.class, new PotionEffectProvider());
40+
this.bind(Sound.class, new SoundProvider());
3541
this.bind(Vector.class, new VectorProvider());
3642
this.bind(WorldReference.class, new WorldProvider());
3743
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.bukkit.Registry;
8+
import org.bukkit.potion.PotionEffect;
9+
import org.bukkit.potion.PotionEffectType;
10+
import org.jetbrains.annotations.NotNull;
11+
import org.jetbrains.annotations.Nullable;
12+
13+
import java.util.LinkedHashMap;
14+
import java.util.Map;
15+
import java.util.Objects;
16+
17+
public class PotionEffectProvider implements TypeProvider<PotionEffect> {
18+
19+
@NotNull
20+
@Override
21+
public PotionEffect load(@NotNull LoadContext context) {
22+
if (context.getObject() instanceof Map<?,?> map) {
23+
String key = (String) map.get("effect");
24+
PotionEffectType effect = Registry.MOB_EFFECT.get(Objects.requireNonNull(NamespacedKey.fromString(key)));
25+
if (effect == null) {
26+
throw new IllegalStateException("Potion effect does not exist: '" + key + "'");
27+
}
28+
return new PotionEffect(
29+
effect,
30+
(int) map.get("duration"),
31+
(int) map.get("amplifier"),
32+
(boolean) map.get("ambient"),
33+
(boolean) map.get("has-particles"),
34+
(boolean) map.get("has-icon")
35+
);
36+
}
37+
throw new IllegalStateException("Failed to parse PotionEffect");
38+
}
39+
40+
@Nullable
41+
@Override
42+
public Object save(@NotNull SaveContext<PotionEffect> context) {
43+
Map<String, Object> map = new LinkedHashMap<>();
44+
map.put("effect", context.getObject().getType());
45+
map.put("duration", context.getObject().getDuration());
46+
map.put("amplifier", context.getObject().getAmplifier());
47+
map.put("ambient", context.getObject().isAmbient());
48+
map.put("has-particles", context.getObject().hasParticles());
49+
map.put("has-icon", context.getObject().hasIcon());
50+
return map;
51+
}
52+
53+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.bukkit.Registry;
8+
import org.bukkit.potion.PotionEffectType;
9+
import org.jetbrains.annotations.NotNull;
10+
import org.jetbrains.annotations.Nullable;
11+
12+
public class PotionEffectTypeProvider implements TypeProvider<PotionEffectType> {
13+
14+
@NotNull
15+
@Override
16+
public PotionEffectType load(@NotNull LoadContext context) {
17+
if (context.getObject() instanceof String string) {
18+
NamespacedKey key = NamespacedKey.fromString(string);
19+
if (key == null) {
20+
throw new IllegalArgumentException("Potion effect key in invalid format: '" + string + "'");
21+
}
22+
PotionEffectType effect = Registry.MOB_EFFECT.get(key);
23+
if (effect == null) {
24+
throw new IllegalArgumentException("Potion effect does not exist: '" + string + "'");
25+
}
26+
return effect;
27+
}
28+
throw new IllegalStateException("Failed to parse PotionEffectType");
29+
}
30+
31+
@Nullable
32+
@Override
33+
public Object save(@NotNull SaveContext<PotionEffectType> context) {
34+
return context.getObject().getKey().asString();
35+
}
36+
37+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.bukkit.Registry;
8+
import org.bukkit.Sound;
9+
import org.jetbrains.annotations.NotNull;
10+
import org.jetbrains.annotations.Nullable;
11+
12+
public class SoundProvider implements TypeProvider<Sound> {
13+
14+
@NotNull
15+
@Override
16+
public Sound load(@NotNull LoadContext context) {
17+
if (context.getObject() instanceof String string) {
18+
NamespacedKey key = NamespacedKey.fromString(string);
19+
if (key == null) {
20+
throw new IllegalArgumentException("Sound key in invalid format: '" + string + "'");
21+
}
22+
Sound sound = Registry.SOUNDS.get(key);
23+
if (sound == null) {
24+
throw new IllegalArgumentException("Sound does not exist: '" + string + "'");
25+
}
26+
return sound;
27+
}
28+
throw new IllegalStateException("Failed to parse Sound");
29+
}
30+
31+
@Nullable
32+
@Override
33+
public Object save(@NotNull SaveContext<Sound> context) {
34+
NamespacedKey key = Registry.SOUNDS.getKey(context.getObject());
35+
if (key == null) {
36+
return null;
37+
}
38+
return key.asString();
39+
}
40+
41+
}

0 commit comments

Comments
 (0)