11package com .jaimemartz .playerbalancer ;
22
33import com .google .common .reflect .TypeToken ;
4- import com .jaimemartz .playerbalancer .commands .FallbackCommand ;
5- import com .jaimemartz .playerbalancer .commands .MainCommand ;
6- import com .jaimemartz .playerbalancer .commands .ManageCommand ;
4+ import com .jaimemartz .playerbalancer .commands .*;
75import com .jaimemartz .playerbalancer .connection .ServerAssignRegistry ;
6+ import com .jaimemartz .playerbalancer .helper .NetworkManager ;
7+ import com .jaimemartz .playerbalancer .helper .PasteHelper ;
8+ import com .jaimemartz .playerbalancer .helper .PlayerLocker ;
89import com .jaimemartz .playerbalancer .listeners .*;
9- import com .jaimemartz .playerbalancer .manager .NetworkManager ;
10- import com .jaimemartz .playerbalancer .manager .PasteHelper ;
11- import com .jaimemartz .playerbalancer .manager .PlayerLocker ;
1210import com .jaimemartz .playerbalancer .ping .StatusManager ;
1311import com .jaimemartz .playerbalancer .section .SectionManager ;
1412import com .jaimemartz .playerbalancer .settings .SettingsHolder ;
@@ -41,7 +39,7 @@ public class PlayerBalancer extends Plugin {
4139 private final StringBuilder logsBuilder = new StringBuilder ();
4240
4341 private FallbackCommand fallbackCommand ;
44- private Command mainCommand , manageCommand ;
42+ private Command mainCommand , manageCommand , findCommand , listCommand , serverCommand ;
4543 private Listener connectListener , kickListener , reloadListener , pluginMessageListener ;
4644
4745 @ Override
@@ -181,23 +179,27 @@ private void execStart() {
181179 getProxy ().getPluginManager ().registerListener (this , kickListener );
182180 }
183181
182+ //After the modules have loaded (hopefully?)
184183 getProxy ().getScheduler ().schedule (this , () -> {
185184 if (settings .getFeaturesProps ().getCustomFindCommandProps ().isEnabled ()) {
186- Plugin plugin = getProxy ().getPluginManager ().getPlugin ("cmd_find" );
187- if (plugin != null ) {
188- getProxy ().getPluginManager ().unregisterCommands (plugin );
189- getLogger ().info ("Unregistered commands of the plugin: " + plugin .getDescription ().getName ());
190- }
185+ tryUnregisterCommands ("cmd_find" );
186+ findCommand = new CustomFindCommand (this );
187+ getProxy ().getPluginManager ().registerCommand (this , findCommand );
191188 }
192189
193190 if (settings .getFeaturesProps ().getCustomListCommandProps ().isEnabled ()) {
194- Plugin plugin = getProxy ().getPluginManager ().getPlugin ("cmd_list" );
195- if (plugin != null ) {
196- getProxy ().getPluginManager ().unregisterCommands (plugin );
197- getLogger ().info ("Unregistered commands of the plugin: " + plugin .getDescription ().getName ());
198- }
191+ tryUnregisterCommands ("cmd_list" );
192+ listCommand = new CustomListCommand (this );
193+ getProxy ().getPluginManager ().registerCommand (this , listCommand );
199194 }
200- }, 1L , TimeUnit .SECONDS );
195+
196+ if (settings .getFeaturesProps ().getCustomServerCommandProps ().isEnabled ()) {
197+ tryUnregisterCommands ("cmd_server" );
198+ serverCommand = new CustomServerCommand (this );
199+ getProxy ().getPluginManager ().registerCommand (this , serverCommand );
200+ }
201+
202+ }, 5L , TimeUnit .SECONDS );
201203
202204 PasteHelper .reset ();
203205 getLogger ().info ("The plugin has finished loading without any problems" );
@@ -214,6 +216,16 @@ private void execStart() {
214216 }
215217 }
216218
219+ private void tryUnregisterCommands (String pluginName ) {
220+ Plugin plugin = getProxy ().getPluginManager ().getPlugin (pluginName );
221+ if (plugin != null ) {
222+ getProxy ().getPluginManager ().unregisterCommands (plugin );
223+ getLogger ().info ("Unregistered all commands of the plugin: " + pluginName );
224+ } else {
225+ getLogger ().warning ("Could not find the plugin: " + pluginName );
226+ }
227+ }
228+
217229 private void execStop () {
218230 if (mainCommand != null ) {
219231 getProxy ().getPluginManager ().unregisterCommand (mainCommand );
@@ -269,6 +281,27 @@ private void execStop() {
269281 manageCommand = null ;
270282 }
271283
284+ if (settings .getFeaturesProps ().getCustomFindCommandProps ().isEnabled ()) {
285+ if (findCommand != null ) {
286+ getProxy ().getPluginManager ().unregisterCommand (findCommand );
287+ findCommand = null ;
288+ }
289+ }
290+
291+ if (settings .getFeaturesProps ().getCustomListCommandProps ().isEnabled ()) {
292+ if (listCommand != null ) {
293+ getProxy ().getPluginManager ().unregisterCommand (listCommand );
294+ listCommand = null ;
295+ }
296+ }
297+
298+ if (settings .getFeaturesProps ().getCustomServerCommandProps ().isEnabled ()) {
299+ if (serverCommand != null ) {
300+ getProxy ().getPluginManager ().unregisterCommand (serverCommand );
301+ serverCommand = null ;
302+ }
303+ }
304+
272305 if (sectionManager != null ) {
273306 sectionManager .flush ();
274307 }
0 commit comments