|
15 | 15 | */ |
16 | 16 | package me.hsgamer.bettergui.betterdialogs; |
17 | 17 |
|
18 | | -import io.github.projectunified.minelib.scheduler.common.util.Platform; |
19 | | -import io.github.projectunified.unidialog.core.DialogManager; |
20 | | -import io.github.projectunified.unidialog.packetevents.PocketEventsDialogManager; |
21 | | -import io.github.projectunified.unidialog.paper.PaperDialogManager; |
22 | | -import io.github.projectunified.unidialog.spigot.SpigotDialogManager; |
23 | 18 | import me.hsgamer.bettergui.api.addon.GetLogger; |
24 | 19 | import me.hsgamer.bettergui.api.addon.GetPlugin; |
25 | 20 | import me.hsgamer.bettergui.api.addon.Reloadable; |
|
31 | 26 | import me.hsgamer.bettergui.builder.MenuBuilder; |
32 | 27 | import me.hsgamer.bettergui.util.SchedulerUtil; |
33 | 28 | import me.hsgamer.hscore.bukkit.config.BukkitConfig; |
34 | | -import me.hsgamer.hscore.bukkit.utils.VersionUtils; |
35 | | -import me.hsgamer.hscore.common.Validate; |
36 | 29 | import me.hsgamer.hscore.config.proxy.ConfigGenerator; |
37 | 30 | import me.hsgamer.hscore.expansion.common.Expansion; |
38 | 31 | import me.hsgamer.hscore.expansion.extra.expansion.DataFolder; |
|
41 | 34 | import me.hsgamer.hscore.license.polymart.PolymartLicenseChecker; |
42 | 35 | import me.hsgamer.hscore.license.spigotmc.SpigotLicenseChecker; |
43 | 36 | import me.hsgamer.hscore.logger.common.LogLevel; |
44 | | -import me.hsgamer.hscore.logger.common.Logger; |
45 | | -import org.bukkit.Bukkit; |
46 | | -import org.bukkit.entity.Player; |
47 | | -import org.bukkit.plugin.Plugin; |
48 | | -import org.jetbrains.annotations.Nullable; |
49 | 37 |
|
50 | 38 | import java.io.File; |
51 | | -import java.util.UUID; |
52 | | -import java.util.function.BooleanSupplier; |
53 | | -import java.util.function.Function; |
54 | 39 |
|
55 | 40 | public final class BetterDialogs implements Expansion, GetLogger, GetPlugin, Reloadable, DataFolder { |
56 | 41 | private final MainConfig mainConfig = ConfigGenerator.newInstance(MainConfig.class, new BukkitConfig(new File(getDataFolder(), "config.yml"))); |
57 | | - private DialogManager<?, ?, ?, ?, ?> dialogManager; |
58 | 42 |
|
59 | 43 | @Override |
60 | 44 | public boolean onLoad() { |
61 | | - String dialogManagerName = mainConfig.dialogManager().toLowerCase(); |
62 | | - Logger logger = getLogger(); |
63 | | - if (dialogManagerName.equals("auto")) { |
64 | | - for (DialogManagerType type : DialogManagerType.values()) { |
65 | | - if (type.isAvailable()) { |
66 | | - dialogManager = type.create(getPlugin()); |
67 | | - logger.log(LogLevel.INFO, "Using " + type.name() + " for BetterDialogs"); |
68 | | - return true; |
69 | | - } |
70 | | - } |
71 | | - logger.log(LogLevel.WARN, "No available dialog manager found."); |
72 | | - } else { |
73 | | - try { |
74 | | - DialogManagerType type = DialogManagerType.valueOf(dialogManagerName.toUpperCase()); |
75 | | - if (type.isAvailable()) { |
76 | | - dialogManager = type.create(getPlugin()); |
77 | | - logger.log(LogLevel.INFO, "Using " + type.name() + " for BetterDialogs"); |
78 | | - return true; |
79 | | - } else { |
80 | | - logger.log(LogLevel.WARN, "The specified dialog manager '" + dialogManagerName + "' is not available."); |
81 | | - } |
82 | | - } catch (IllegalArgumentException e) { |
83 | | - logger.log(LogLevel.WARN, "Invalid dialog manager specified: " + dialogManagerName); |
84 | | - } |
85 | | - } |
86 | | - return false; |
| 45 | + return DialogManagerProvider.init( |
| 46 | + mainConfig.dialogManager().toLowerCase(), |
| 47 | + getPlugin(), |
| 48 | + getLogger() |
| 49 | + ); |
87 | 50 | } |
88 | 51 |
|
89 | 52 | @Override |
90 | 53 | public void onEnable() { |
91 | 54 | checkLicense(); |
92 | | - dialogManager.register(); |
93 | | - MenuBuilder.INSTANCE.register(config -> new ConfirmationDialogMenu(this, config), "confirmation-dialog", "confirm-dialog"); |
94 | | - MenuBuilder.INSTANCE.register(config -> new MultiActionDialogMenu(this, config), "multi-action-dialog", "action-dialog"); |
95 | | - MenuBuilder.INSTANCE.register(config -> new NoticeDialogMenu(this, config), "notice-dialog"); |
96 | | - MenuBuilder.INSTANCE.register(config -> new ServerLinksDialogMenu(this, config), "server-links-dialog", "links-dialog", "server-link-dialog", "link-dialog"); |
| 55 | + DialogManagerProvider.dialogManager().register(); |
| 56 | + MenuBuilder.INSTANCE.register(ConfirmationDialogMenu::new, "confirmation-dialog", "confirm-dialog"); |
| 57 | + MenuBuilder.INSTANCE.register(MultiActionDialogMenu::new, "multi-action-dialog", "action-dialog"); |
| 58 | + MenuBuilder.INSTANCE.register(NoticeDialogMenu::new, "notice-dialog"); |
| 59 | + MenuBuilder.INSTANCE.register(ServerLinksDialogMenu::new, "server-links-dialog", "links-dialog", "server-link-dialog", "link-dialog"); |
97 | 60 | } |
98 | 61 |
|
99 | 62 | @Override |
100 | 63 | public void onReload() { |
101 | | - dialogManager.unregisterAllCustomActions(); |
| 64 | + DialogManagerProvider.dialogManager().unregisterAllCustomActions(); |
102 | 65 | } |
103 | 66 |
|
104 | 67 | @Override |
105 | 68 | public void onDisable() { |
106 | | - dialogManager.unregister(); |
| 69 | + DialogManagerProvider.dialogManager().unregister(); |
107 | 70 | } |
108 | 71 |
|
109 | 72 | private void checkLicense() { |
@@ -136,50 +99,4 @@ private void checkLicense() { |
136 | 99 | } |
137 | 100 | }); |
138 | 101 | } |
139 | | - |
140 | | - public DialogManager<?, ?, ?, ?, ?> dialogManager() { |
141 | | - return dialogManager; |
142 | | - } |
143 | | - |
144 | | - private enum DialogManagerType { |
145 | | - PAPER( |
146 | | - () -> Platform.PAPER.isPlatform() && VersionUtils.isAtLeast(21, 7), |
147 | | - plugin -> new PaperDialogManager(plugin, "betterdialogs") |
148 | | - ), |
149 | | - PACKETEVENTS( |
150 | | - () -> Bukkit.getPluginManager().getPlugin("packetevents") != null, |
151 | | - plugin -> new PocketEventsDialogManager("betterdialogs") { |
152 | | - @Override |
153 | | - protected @Nullable Player getPlayer(UUID uuid) { |
154 | | - return Bukkit.getPlayer(uuid); |
155 | | - } |
156 | | - |
157 | | - @Override |
158 | | - protected UUID getPlayerId(Object player) { |
159 | | - Player p = (Player) player; |
160 | | - return p.getUniqueId(); |
161 | | - } |
162 | | - } |
163 | | - ), |
164 | | - SPIGOT( |
165 | | - () -> Validate.isClassLoaded("net.md_5.bungee.api.dialog.Dialog"), |
166 | | - plugin -> new SpigotDialogManager(plugin, "betterdialogs") |
167 | | - ); |
168 | | - |
169 | | - private final BooleanSupplier isAvailable; |
170 | | - private final Function<Plugin, DialogManager<?, ?, ?, ?, ?>> constructor; |
171 | | - |
172 | | - DialogManagerType(BooleanSupplier isAvailable, Function<Plugin, DialogManager<?, ?, ?, ?, ?>> constructor) { |
173 | | - this.isAvailable = isAvailable; |
174 | | - this.constructor = constructor; |
175 | | - } |
176 | | - |
177 | | - public boolean isAvailable() { |
178 | | - return isAvailable.getAsBoolean(); |
179 | | - } |
180 | | - |
181 | | - public DialogManager<?, ?, ?, ?, ?> create(Plugin plugin) { |
182 | | - return constructor.apply(plugin); |
183 | | - } |
184 | | - } |
185 | 102 | } |
0 commit comments