11package me .hsgamer .bettergui ;
22
33import io .github .projectunified .minelib .plugin .base .BasePlugin ;
4- import me .hsgamer .bettergui .api .addon .PostEnable ;
4+ import io .github .projectunified .minelib .plugin .command .CommandComponent ;
5+ import io .github .projectunified .minelib .plugin .postenable .PostEnable ;
6+ import io .github .projectunified .minelib .plugin .postenable .PostEnableComponent ;
57import me .hsgamer .bettergui .api .menu .Menu ;
68import me .hsgamer .bettergui .builder .*;
79import me .hsgamer .bettergui .command .*;
3032import org .bukkit .Bukkit ;
3133import org .bukkit .plugin .java .JavaPlugin ;
3234
33- import java .util .Collections ;
34- import java .util .HashMap ;
35- import java .util .List ;
36- import java .util .Map ;
35+ import java .util .*;
3736
3837/**
3938 * The main class of the plugin
4039 */
41- public final class BetterGUI extends BasePlugin {
40+ public final class BetterGUI extends BasePlugin implements PostEnable {
4241 private static final CachedValue <BetterGUI > INSTANCE_CACHE = CachedValue .of (() -> JavaPlugin .getPlugin (BetterGUI .class ));
43- private final MainConfig mainConfig = ConfigGenerator .newInstance (MainConfig .class , new BukkitConfig (this , "config.yml" ));
44- private final MessageConfig messageConfig = ConfigGenerator .newInstance (MessageConfig .class , new BukkitConfig (this , "messages.yml" ));
45- private final TemplateConfig templateButtonConfig = new TemplateConfig (this );
46- private final MenuManager menuManager = new MenuManager (this );
47- private final MenuCommandManager menuCommandManager = new MenuCommandManager (this );
48- private final AddonManager addonManager = new AddonManager (this );
49- private final AddonDownloader addonDownloader = new AddonDownloader (this );
50- private final VariableBundle variableBundle = new VariableBundle ();
5142
5243 /**
5344 * Get the instance of the plugin
@@ -59,59 +50,75 @@ public static BetterGUI getInstance() {
5950 }
6051
6152 @ Override
62- public void load () {
63- MessageUtils .setPrefix (messageConfig ::getPrefix );
64-
53+ protected List <Object > getComponents () {
54+ VariableBundle variableBundle = new VariableBundle ();
6555 CommonVariableBundle .registerVariables (variableBundle );
6656 BukkitVariableBundle .registerVariables (variableBundle );
6757 variableBundle .register ("menu_" , StringReplacer .of ((original , uuid ) -> {
6858 String [] split = original .split ("_" , 2 );
6959 String menuName = split [0 ].trim ();
7060 String variable = split .length > 1 ? split [1 ].trim () : "" ;
71- Menu menu = menuManager .getMenu (menuName );
61+ Menu menu = get ( MenuManager . class ) .getMenu (menuName );
7262 if (menu == null ) {
7363 return null ;
7464 }
7565 return menu .getVariableManager ().setVariables (StringReplacerApplier .normalizeQuery (variable ), uuid );
7666 }));
77- }
7867
79- @ Override
80- public void enable () {
81- BukkitGUIListener .init (this );
68+ List <Object > components = new ArrayList <>(Arrays .asList (
69+ variableBundle ,
70+
71+ new PostEnableComponent (this ),
72+
73+ new Permissions (this ),
74+ new CommandComponent (this ,
75+ new OpenCommand (this ),
76+ new MainCommand (this ),
77+ new GetAddonsCommand (this ),
78+ new ReloadCommand (this ),
79+ new GetVariablesCommand (this ),
80+ new GetTemplateButtonsCommand (this )
81+ ),
8282
83- addonManager .loadExpansions ();
83+ ConfigGenerator .newInstance (MainConfig .class , new BukkitConfig (this , "config.yml" )),
84+ ConfigGenerator .newInstance (MessageConfig .class , new BukkitConfig (this , "messages.yml" )),
85+ new TemplateConfig (this ),
8486
85- registerCommand (new OpenCommand (this ));
86- registerCommand (new MainCommand (this ));
87- registerCommand (new GetAddonsCommand (this ));
88- registerCommand (new ReloadCommand (this ));
89- registerCommand (new GetVariablesCommand (this ));
90- registerCommand (new GetTemplateButtonsCommand (this ));
87+ new AddonManager (this ),
88+ new MenuManager (this ),
89+ new MenuCommandManager (this ),
90+ new AddonDownloader (this )
91+ ));
9192
9293 if (Bukkit .getPluginManager ().getPlugin ("PlaceholderAPI" ) != null ) {
93- ExtraPlaceholderExpansion expansion = new ExtraPlaceholderExpansion (this );
94- expansion .register ();
95- addDisableFunction (expansion ::unregister );
94+ components .add (new ExtraPlaceholderExpansion (this ));
9695 }
96+
97+ return components ;
98+ }
99+
100+ @ Override
101+ public void load () {
102+ MessageUtils .setPrefix (() -> get (MessageConfig .class ).getPrefix ());
103+ }
104+
105+ @ Override
106+ public void enable () {
107+ BukkitGUIListener .init (this );
97108 }
98109
99110 @ Override
100111 public void postEnable () {
101- addonManager .enableExpansions ();
102- addonDownloader .setup ();
103- templateButtonConfig .setup ();
104- menuManager .loadMenuConfig ();
105- addonManager .call (PostEnable .class , PostEnable ::onPostEnable );
112+ get (AddonManager .class ).call (me .hsgamer .bettergui .api .addon .PostEnable .class , me .hsgamer .bettergui .api .addon .PostEnable ::onPostEnable );
106113
107114 Metrics metrics = new Metrics (this , 6609 );
108115 metrics .addCustomChart (new DrilldownPie ("addon" , () -> {
109116 Map <String , Map <String , Integer >> map = new HashMap <>();
110- Map <String , Integer > addons = addonManager .getExpansionCount ();
117+ Map <String , Integer > addons = get ( AddonManager . class ) .getExpansionCount ();
111118 map .put (String .valueOf (addons .size ()), addons );
112119 return map ;
113120 }));
114- metrics .addCustomChart (new AdvancedPie ("addon_count" , addonManager ::getExpansionCount ));
121+ metrics .addCustomChart (new AdvancedPie ("addon_count" , get ( AddonManager . class ) ::getExpansionCount ));
115122
116123 if (getDescription ().getVersion ().contains ("SNAPSHOT" )) {
117124 getLogger ().warning ("You are using the development version" );
@@ -133,98 +140,12 @@ public void postEnable() {
133140
134141 @ Override
135142 public void disable () {
136- menuCommandManager .clearMenuCommand ();
137- menuManager .clear ();
138- templateButtonConfig .clear ();
139- addonManager .disableExpansions ();
140- addonManager .clearExpansions ();
141- }
142-
143- @ Override
144- public void postDisable () {
145143 ActionBuilder .INSTANCE .clear ();
146144 ButtonBuilder .INSTANCE .clear ();
147145 ItemModifierBuilder .INSTANCE .clear ();
148146 MenuBuilder .INSTANCE .clear ();
149147 RequirementBuilder .INSTANCE .clear ();
150- variableBundle .unregisterAll ();
148+ get ( VariableBundle . class ) .unregisterAll ();
151149 INSTANCE_CACHE .clearCache ();
152150 }
153-
154- @ Override
155- private List <Class <?>> getPermissionClasses () {
156- return Collections .singletonList (Permissions .class );
157- }
158-
159- /**
160- * Get the main config
161- *
162- * @return the main config
163- */
164- public MainConfig getMainConfig () {
165- return mainConfig ;
166- }
167-
168- /**
169- * Get the message config
170- *
171- * @return the message config
172- */
173- public MessageConfig getMessageConfig () {
174- return messageConfig ;
175- }
176-
177- /**
178- * Get the template button config
179- *
180- * @return the template button config
181- */
182- public TemplateConfig getTemplateButtonConfig () {
183- return templateButtonConfig ;
184- }
185-
186- /**
187- * Get the menu manager
188- *
189- * @return the menu manager
190- */
191- public MenuManager getMenuManager () {
192- return menuManager ;
193- }
194-
195- /**
196- * Get the menu command manager
197- *
198- * @return the menu command manager
199- */
200- public MenuCommandManager getMenuCommandManager () {
201- return menuCommandManager ;
202- }
203-
204- /**
205- * Get the addon manager
206- *
207- * @return the addon manager
208- */
209- public AddonManager getAddonManager () {
210- return addonManager ;
211- }
212-
213- /**
214- * Get the addon downloader
215- *
216- * @return the addon downloader
217- */
218- public AddonDownloader getAddonDownloader () {
219- return addonDownloader ;
220- }
221-
222- /**
223- * Get the variable bundle
224- *
225- * @return the variable bundle
226- */
227- public VariableBundle getVariableBundle () {
228- return variableBundle ;
229- }
230151}
0 commit comments