Skip to content

Commit bfd497b

Browse files
committed
Migrate advancements handling to multification-bukkit and introduce PacketEventsUtil for dynamic PacketEvents checks.
1 parent dae1ace commit bfd497b

4 files changed

Lines changed: 53 additions & 11 deletions

File tree

multification-bukkit/src/com/eternalcode/multification/bukkit/BukkitMultification.java

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

33
import com.eternalcode.multification.Multification;
4+
import com.eternalcode.multification.bukkit.notice.resolver.advancement.AdvancementResolver;
45
import com.eternalcode.multification.bukkit.notice.resolver.sound.SoundBukkitResolver;
6+
import com.eternalcode.multification.bukkit.util.PacketEventsUtil;
57
import com.eternalcode.multification.executor.AsyncExecutor;
68
import com.eternalcode.multification.locate.LocaleProvider;
79
import com.eternalcode.multification.viewer.ViewerProvider;
@@ -17,7 +19,13 @@ public abstract class BukkitMultification<TRANSLATION> extends Multification<Com
1719

1820
protected BukkitMultification() {
1921
super();
22+
23+
// TODO: add special registration method for it?
2024
this.noticeRegistry.registerResolver(new SoundBukkitResolver());
25+
26+
if (PacketEventsUtil.isPacketEventsLoaded()) {
27+
this.noticeRegistry.registerResolver(new AdvancementResolver());
28+
}
2129
}
2230

2331
@Override

multification-bukkit/src/com/eternalcode/multification/bukkit/notice/resolver/advancement/AdvancementResolver.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.bukkit.Bukkit;
2222
import org.bukkit.entity.Player;
2323
import org.bukkit.plugin.Plugin;
24+
import org.bukkit.plugin.java.JavaPlugin;
2425
import org.jetbrains.annotations.NotNull;
2526
import org.jetbrains.annotations.Nullable;
2627

@@ -40,17 +41,11 @@ public class AdvancementResolver implements TextContentResolver<AdvancementConte
4041

4142
private static final String FORMAT = "%s|%s|%s|%s|%s|%b|%b|%f|%f|%s";
4243

43-
private final NoticeKey<AdvancementContent> key;
44-
private final Plugin plugin;
45-
46-
public AdvancementResolver(Plugin plugin) {
47-
this.key = PacketEventsNoticeKey.ADVANCEMENT;
48-
this.plugin = plugin;
49-
}
44+
private final Plugin plugin = JavaPlugin.getProvidingPlugin(AdvancementResolver.class);
5045

5146
@Override
5247
public NoticeKey<AdvancementContent> noticeKey() {
53-
return this.key;
48+
return PacketEventsNoticeKey.ADVANCEMENT;
5449
}
5550

5651
@Override

multification-bukkit/src/com/eternalcode/multification/bukkit/notice/resolver/advancement/PacketEventsNotice.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.eternalcode.multification.bukkit.notice.resolver.advancement;
22

33
import com.eternalcode.multification.notice.Notice;
4-
import com.eternalcode.multification.packetevents.notice.resolver.AdvancementContent;
5-
import com.eternalcode.multification.packetevents.notice.resolver.AdvancementFrameType;
6-
import com.eternalcode.multification.packetevents.notice.resolver.PacketEventsNoticeKey;
74

85
import java.time.Duration;
96

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.eternalcode.multification.bukkit.util;
2+
3+
public class PacketEventsUtil {
4+
5+
private static Boolean packetEventsLoaded = null;
6+
7+
private static final String[] PACKETEVENTS_CLASSES = {
8+
"com.github.retrooper.packetevents.PacketEvents",
9+
"io.github.retrooper.packetevents.PacketEvents",
10+
"com.github.retrooper.packetevents.PacketEventsAPI",
11+
"io.github.retrooper.packetevents.PacketEventsAPI"
12+
};
13+
14+
/**
15+
* Checks if PacketEvents is loaded via Class.forName()
16+
* Handles different relocations
17+
*
18+
* @return true if PacketEvents is available, false otherwise
19+
*/
20+
public static boolean isPacketEventsLoaded() {
21+
if (packetEventsLoaded == null) {
22+
packetEventsLoaded = checkPacketEventsClass();
23+
}
24+
return packetEventsLoaded;
25+
}
26+
27+
/**
28+
* Checks if any PacketEvents class exists
29+
*
30+
* @return true if PacketEvents class was found
31+
*/
32+
private static boolean checkPacketEventsClass() {
33+
for (String className : PACKETEVENTS_CLASSES) {
34+
try {
35+
Class.forName(className);
36+
return true;
37+
}
38+
catch (ClassNotFoundException ignored) {}
39+
}
40+
return false;
41+
}
42+
}

0 commit comments

Comments
 (0)