Skip to content

Commit c65cded

Browse files
authored
GH-71 Support 1.21.3 bukkit platform, better adventure support (#71)
1 parent e3a3f72 commit c65cded

File tree

15 files changed

+339
-62
lines changed

15 files changed

+339
-62
lines changed

buildSrc/src/main/kotlin/Versions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object Versions {
1010
const val ASSERTJ_CORE = "3.27.2"
1111
const val AWAITILITY = "4.2.2"
1212

13-
const val SPIGOT_API = "1.19.4-R0.1-SNAPSHOT"
13+
const val SPIGOT_API = "1.21.4-R0.1-SNAPSHOT"
1414

1515
const val JETBRAINS_ANNOTATIONS = "26.0.1"
1616

examples/bukkit/build.gradle.kts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
plugins {
22
id("java")
3-
id("com.github.johnrengelman.shadow") version "8.1.1"
3+
id("com.gradleup.shadow") version "9.0.0-beta4"
44
id("net.minecrell.plugin-yml.bukkit") version "0.6.0"
5-
id("xyz.jpenilla.run-paper") version "2.3.0"
5+
id("xyz.jpenilla.run-paper") version "2.3.1"
66
}
77

88
version = "1.0.0-SNAPSHOT"
@@ -42,8 +42,8 @@ tasks.shadowJar {
4242
archiveFileName.set("$pluginName v${project.version}.jar")
4343

4444
listOf(
45-
"com.eternalcode.multification",
46-
"net.dzikoysk.cdn",
45+
// "com.eternalcode.multification",
46+
// "net.dzikoysk.cdn",
4747
"panda.std",
4848
"panda.utilities",
4949
"net.kyori",
@@ -56,5 +56,5 @@ sourceSets.test {
5656
}
5757

5858
tasks.runServer {
59-
minecraftVersion("1.20.4")
59+
minecraftVersion("1.21.4")
6060
}

examples/bukkit/src/main/java/com/eternalcode/example/bukkit/ExamplePlugin.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.eternalcode.example.bukkit;
22

33
import com.eternalcode.example.bukkit.command.GiveCommand;
4+
import com.eternalcode.example.bukkit.command.ReloadCommand;
45
import com.eternalcode.example.bukkit.command.TeleportCommand;
56
import com.eternalcode.example.bukkit.command.timer.TimerCommand;
67
import com.eternalcode.example.bukkit.command.timer.TimerManager;
@@ -39,6 +40,7 @@ public void onEnable() {
3940
new TeleportCommand(multification),
4041
new FlyCommand(multification),
4142
new GiveCommand(multification),
43+
new ReloadCommand(configurationManager, multification),
4244
new TimerCommand(new TimerManager(this.getServer().getScheduler(), this, multification))
4345
)
4446
.build();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.eternalcode.example.bukkit.command;
2+
3+
import com.eternalcode.example.bukkit.config.ConfigurationManager;
4+
import com.eternalcode.example.bukkit.multification.YourMultification;
5+
import dev.rollczi.litecommands.annotations.command.Command;
6+
import dev.rollczi.litecommands.annotations.context.Context;
7+
import dev.rollczi.litecommands.annotations.execute.Execute;
8+
import org.bukkit.command.CommandSender;
9+
import org.bukkit.entity.Player;
10+
11+
@Command(name = "reload-config")
12+
public class ReloadCommand {
13+
14+
private final ConfigurationManager configurationManager;
15+
private final YourMultification multification;
16+
17+
public ReloadCommand(ConfigurationManager configurationManager, YourMultification multification) {
18+
this.configurationManager = configurationManager;
19+
this.multification = multification;
20+
}
21+
22+
@Execute
23+
public void execute(@Context CommandSender sender) {
24+
this.configurationManager.reload();
25+
26+
if (sender instanceof Player player) {
27+
this.multification.create()
28+
.player(player.getUniqueId())
29+
.notice(messagesConfig -> messagesConfig.reloadMessage)
30+
.send();
31+
return;
32+
}
33+
34+
this.multification.create()
35+
.console()
36+
.notice(messagesConfig -> messagesConfig.reloadMessage)
37+
.send();
38+
}
39+
40+
}

examples/bukkit/src/main/java/com/eternalcode/example/bukkit/config/ConfigurationManager.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.eternalcode.multification.notice.Notice;
55
import com.eternalcode.multification.notice.resolver.NoticeResolverRegistry;
66
import java.io.File;
7+
import java.util.HashSet;
8+
import java.util.Set;
79
import net.dzikoysk.cdn.Cdn;
810
import net.dzikoysk.cdn.CdnFactory;
911
import net.dzikoysk.cdn.reflect.Visibility;
@@ -12,6 +14,7 @@ public class ConfigurationManager {
1214

1315
private final Cdn cdn;
1416
private final File dataFolder;
17+
private final Set<ReloadableConfig> configs = new HashSet<>();
1518

1619
public ConfigurationManager(File dataFolder, NoticeResolverRegistry resolverRegistry) {
1720
this.dataFolder = dataFolder;
@@ -30,7 +33,19 @@ public <T> T load(T config, String fileName) {
3033
this.cdn.render(config, Source.of(this.dataFolder, fileName))
3134
.orThrow(RuntimeException::new);
3235

36+
this.configs.add(new ReloadableConfig(fileName, config));
37+
3338
return config;
3439
}
3540

41+
public void reload() {
42+
for (ReloadableConfig config : configs) {
43+
this.cdn.load(Source.of(this.dataFolder, config.fileName()), config.instance())
44+
.orThrow(RuntimeException::new);
45+
}
46+
}
47+
48+
private record ReloadableConfig(String fileName, Object instance) {
49+
}
50+
3651
}

examples/bukkit/src/main/java/com/eternalcode/example/bukkit/config/MessagesConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ public class MessagesConfig {
3737
.bossBar(BossBar.Color.RED, Duration.ofSeconds(1), "<red>Timer: <yellow>{time}")
3838
.build();
3939

40+
public Notice reloadMessage = Notice.builder()
41+
.chat("<pride:pride>Configuration has been reloaded!")
42+
.sound("ambient.basalt_deltas.additions", 1.0F, 1.0F)
43+
.build();
44+
4045
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.eternalcode.multification.bukkit.notice.resolver.sound;
2+
3+
import java.lang.reflect.Method;
4+
import org.bukkit.Sound;
5+
6+
public final class SoundAccessor {
7+
8+
private static final Method VALUE_OF_METHOD;
9+
private static final Method nameMethod;
10+
11+
static {
12+
try {
13+
VALUE_OF_METHOD = Sound.class.getMethod("valueOf", String.class);
14+
VALUE_OF_METHOD.setAccessible(true);
15+
16+
nameMethod = Sound.class.getMethod("name");
17+
nameMethod.setAccessible(true);
18+
}
19+
catch (NoSuchMethodException noSuchMethodException) {
20+
throw new RuntimeException(noSuchMethodException);
21+
}
22+
}
23+
24+
private SoundAccessor() {
25+
}
26+
27+
public static Sound valueOf(String name) {
28+
try {
29+
return (Sound) VALUE_OF_METHOD.invoke(null, name);
30+
}
31+
catch (ReflectiveOperationException reflectiveOperationException) {
32+
throw new RuntimeException(reflectiveOperationException);
33+
}
34+
}
35+
36+
public static String name(Sound sound) {
37+
try {
38+
return (String) nameMethod.invoke(sound);
39+
}
40+
catch (ReflectiveOperationException reflectiveOperationException) {
41+
throw new RuntimeException(reflectiveOperationException);
42+
}
43+
}
44+
45+
}

multification-bukkit/src/com/eternalcode/multification/bukkit/notice/resolver/sound/SoundBukkitResolver.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.eternalcode.multification.notice.NoticeKey;
55
import com.eternalcode.multification.notice.resolver.NoticeSerdesResult;
66
import com.eternalcode.multification.notice.resolver.NoticeResolver;
7+
import java.util.Locale;
78
import java.util.Optional;
89
import net.kyori.adventure.audience.Audience;
910
import net.kyori.adventure.key.Key;
@@ -40,24 +41,38 @@ public void send(Audience audience, ComponentSerializer<Component, Component, St
4041
public NoticeSerdesResult serialize(SoundBukkit content) {
4142
if (content.category() == null) {
4243
if (content.pitch() == SoundBukkit.PITCH_UNSET || content.volume() == SoundBukkit.VOLUME_UNSET) {
43-
return new NoticeSerdesResult.Single(String.format(MUSIC, content.sound().name()));
44+
return new NoticeSerdesResult.Single(String.format(MUSIC, getNameOrLegacyName(content)));
4445
}
4546

4647
return new NoticeSerdesResult.Single(String.format(MUSIC_WITH_PITCH_VOLUME,
47-
content.sound().name(),
48+
getNameOrLegacyName(content),
4849
content.pitch(),
4950
content.volume()
5051
));
5152
}
5253

5354
return new NoticeSerdesResult.Single(String.format(MUSIC_FULL,
54-
content.sound().name(),
55+
getNameOrLegacyName(content),
5556
content.category().name(),
5657
content.pitch(),
5758
content.volume()
5859
));
5960
}
6061

62+
/**
63+
* From 1.21.3, the sound name is returned in "SOME.EPIC.SOUND" format.
64+
* We want to return it in "some.epic.sound" format, because that's how it's stored in the sound registry.
65+
* Old versions of Bukkit return the sound name in "SOME_EPIC_SOUND" enum format.
66+
*/
67+
private static String getNameOrLegacyName(SoundBukkit content) {
68+
String name = SoundAccessor.name(content.sound());
69+
if (name.contains(".")) {
70+
return name.toLowerCase(Locale.ROOT);
71+
}
72+
73+
return name;
74+
}
75+
6176
@Override
6277
public Optional<SoundBukkit> deserialize(NoticeSerdesResult result) {
6378
Optional<String> firstElement = result.firstElement();
@@ -69,14 +84,14 @@ public Optional<SoundBukkit> deserialize(NoticeSerdesResult result) {
6984
String[] music = firstElement.get().split(" ");
7085

7186
if (music.length == 1) {
72-
return Optional.of(new SoundBukkit(org.bukkit.Sound.valueOf(music[0]), null, SoundBukkit.PITCH_UNSET, SoundBukkit.VOLUME_UNSET));
87+
return Optional.of(new SoundBukkit(SoundAccessor.valueOf(music[0]), null, SoundBukkit.PITCH_UNSET, SoundBukkit.VOLUME_UNSET));
7388
}
7489

7590
if (music.length != 4 && music.length != 3) {
7691
throw new IllegalArgumentException("Invalid sound format: " + firstElement.get());
7792
}
7893

79-
org.bukkit.Sound sound = org.bukkit.Sound.valueOf(music[0]);
94+
org.bukkit.Sound sound = SoundAccessor.valueOf(music[0]);
8095
SoundCategory category = music.length == 3 ? null : SoundCategory.valueOf(music[1]);
8196
float pitch = Float.parseFloat(music[music.length - 2]);
8297
float volume = Float.parseFloat(music[music.length - 1]);

0 commit comments

Comments
 (0)