Skip to content

Commit deadb19

Browse files
committed
allow customizing the type of dialog manager
1 parent 66be610 commit deadb19

2 files changed

Lines changed: 99 additions & 26 deletions

File tree

src/main/java/me/hsgamer/bettergui/betterdialogs/BetterDialogs.java

Lines changed: 84 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,62 @@
88
import me.hsgamer.bettergui.api.addon.GetLogger;
99
import me.hsgamer.bettergui.api.addon.GetPlugin;
1010
import me.hsgamer.bettergui.api.addon.Reloadable;
11+
import me.hsgamer.bettergui.betterdialogs.config.MainConfig;
1112
import me.hsgamer.bettergui.betterdialogs.menu.ConfirmationDialogMenu;
1213
import me.hsgamer.bettergui.betterdialogs.menu.MultiActionDialogMenu;
1314
import me.hsgamer.bettergui.betterdialogs.menu.NoticeDialogMenu;
1415
import me.hsgamer.bettergui.betterdialogs.menu.ServerLinksDialogMenu;
1516
import me.hsgamer.bettergui.builder.MenuBuilder;
17+
import me.hsgamer.hscore.bukkit.config.BukkitConfig;
1618
import me.hsgamer.hscore.bukkit.utils.VersionUtils;
1719
import me.hsgamer.hscore.common.Validate;
20+
import me.hsgamer.hscore.config.proxy.ConfigGenerator;
1821
import me.hsgamer.hscore.expansion.common.Expansion;
22+
import me.hsgamer.hscore.expansion.extra.expansion.DataFolder;
1923
import me.hsgamer.hscore.logger.common.LogLevel;
2024
import me.hsgamer.hscore.logger.common.Logger;
2125
import org.bukkit.Bukkit;
2226
import org.bukkit.entity.Player;
27+
import org.bukkit.plugin.Plugin;
2328
import org.jetbrains.annotations.Nullable;
2429

30+
import java.io.File;
2531
import java.util.UUID;
32+
import java.util.function.BooleanSupplier;
33+
import java.util.function.Function;
2634

27-
public final class BetterDialogs implements Expansion, GetLogger, GetPlugin, Reloadable {
35+
public final class BetterDialogs implements Expansion, GetLogger, GetPlugin, Reloadable, DataFolder {
36+
private final MainConfig mainConfig = ConfigGenerator.newInstance(MainConfig.class, new BukkitConfig(new File(getDataFolder(), "config.yml")));
2837
private DialogManager<?, ?, ?, ?, ?> dialogManager;
2938

3039
@Override
3140
public boolean onLoad() {
32-
if (Platform.PAPER.isPlatform() && VersionUtils.isAtLeast(21, 7)) {
33-
dialogManager = new PaperDialogManager(getPlugin(), "betterdialogs");
34-
} else if (Bukkit.getPluginManager().getPlugin("packetevents") != null) {
35-
dialogManager = new PocketEventsDialogManager("betterdialogs") {
36-
@Override
37-
protected @Nullable Player getPlayer(UUID uuid) {
38-
return Bukkit.getPlayer(uuid);
41+
String dialogManagerName = mainConfig.dialogManager().toLowerCase();
42+
Logger logger = getLogger();
43+
if (dialogManagerName.equals("auto")) {
44+
for (DialogManagerType type : DialogManagerType.values()) {
45+
if (type.isAvailable()) {
46+
dialogManager = type.create(getPlugin());
47+
logger.log(LogLevel.INFO, "Using " + type.getRequirement() + " for BetterDialogs");
48+
return true;
3949
}
40-
41-
@Override
42-
protected UUID getPlayerId(Object player) {
43-
Player p = (Player) player;
44-
return p.getUniqueId();
45-
}
46-
};
47-
} else if (Validate.isClassLoaded("net.md_5.bungee.api.dialog.Dialog")) {
48-
dialogManager = new SpigotDialogManager(getPlugin(), "betterdialogs");
50+
}
51+
logger.log(LogLevel.WARN, "No available dialog manager found.");
4952
} else {
50-
Logger logger = getLogger();
51-
logger.log(LogLevel.WARN, "BetterDialogs is not supported on this platform.");
52-
logger.log(LogLevel.WARN, "The only supported platforms are:");
53-
logger.log(LogLevel.WARN, "- PocketEvents (with Packetevents plugin)");
54-
logger.log(LogLevel.WARN, "- Paper 1.21.7+");
55-
logger.log(LogLevel.WARN, "- Spigot 1.21.6+ (with BungeeCord Dialog API)");
56-
logger.log(LogLevel.WARN, "Please use the correct platform to use BetterDialogs.");
57-
return false;
53+
try {
54+
DialogManagerType type = DialogManagerType.valueOf(dialogManagerName.toUpperCase());
55+
if (type.isAvailable()) {
56+
dialogManager = type.create(getPlugin());
57+
logger.log(LogLevel.INFO, "Using " + type.getRequirement() + " for BetterDialogs");
58+
return true;
59+
} else {
60+
logger.log(LogLevel.WARN, "The specified dialog manager '" + dialogManagerName + "' is not available.");
61+
}
62+
} catch (IllegalArgumentException e) {
63+
logger.log(LogLevel.WARN, "Invalid dialog manager specified: " + dialogManagerName);
64+
}
5865
}
59-
return true;
66+
return false;
6067
}
6168

6269
@Override
@@ -81,4 +88,55 @@ public void onDisable() {
8188
public DialogManager<?, ?, ?, ?, ?> dialogManager() {
8289
return dialogManager;
8390
}
91+
92+
private enum DialogManagerType {
93+
PAPER(
94+
"Paper 1.21.7+",
95+
() -> Platform.PAPER.isPlatform() && VersionUtils.isAtLeast(21, 7),
96+
plugin -> new PaperDialogManager(plugin, "betterdialogs")
97+
),
98+
PACKETEVENTS(
99+
"PocketEvents",
100+
() -> Bukkit.getPluginManager().getPlugin("packetevents") != null,
101+
plugin -> new PocketEventsDialogManager("betterdialogs") {
102+
@Override
103+
protected @Nullable Player getPlayer(UUID uuid) {
104+
return Bukkit.getPlayer(uuid);
105+
}
106+
107+
@Override
108+
protected UUID getPlayerId(Object player) {
109+
Player p = (Player) player;
110+
return p.getUniqueId();
111+
}
112+
}
113+
),
114+
SPIGOT(
115+
"Spigot 1.21.6+",
116+
() -> Validate.isClassLoaded("net.md_5.bungee.api.dialog.Dialog"),
117+
plugin -> new SpigotDialogManager(plugin, "betterdialogs")
118+
);
119+
120+
private final String requirement;
121+
private final BooleanSupplier isAvailable;
122+
private final Function<Plugin, DialogManager<?, ?, ?, ?, ?>> constructor;
123+
124+
DialogManagerType(String requirement, BooleanSupplier isAvailable, Function<Plugin, DialogManager<?, ?, ?, ?, ?>> constructor) {
125+
this.requirement = requirement;
126+
this.isAvailable = isAvailable;
127+
this.constructor = constructor;
128+
}
129+
130+
public String getRequirement() {
131+
return requirement;
132+
}
133+
134+
public boolean isAvailable() {
135+
return isAvailable.getAsBoolean();
136+
}
137+
138+
public DialogManager<?, ?, ?, ?, ?> create(Plugin plugin) {
139+
return constructor.apply(plugin);
140+
}
141+
}
84142
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package me.hsgamer.bettergui.betterdialogs.config;
2+
3+
import me.hsgamer.hscore.config.annotation.Comment;
4+
import me.hsgamer.hscore.config.annotation.ConfigPath;
5+
6+
public interface MainConfig {
7+
@ConfigPath("dialog-manager")
8+
@Comment({
9+
"The dialog manager to use",
10+
"Available options: paper, packetevents, spigot, auto",
11+
})
12+
default String dialogManager() {
13+
return "auto";
14+
}
15+
}

0 commit comments

Comments
 (0)