1818package net .elytrium .velocitytools ;
1919
2020import com .google .inject .Inject ;
21+ import com .velocitypowered .api .command .Command ;
22+ import com .velocitypowered .api .command .CommandMeta ;
2123import com .velocitypowered .api .event .Subscribe ;
2224import com .velocitypowered .api .event .proxy .ProxyInitializeEvent ;
2325import com .velocitypowered .api .plugin .Plugin ;
26+ import com .velocitypowered .api .plugin .PluginContainer ;
2427import com .velocitypowered .api .plugin .annotation .DataDirectory ;
2528import com .velocitypowered .api .proxy .ProxyServer ;
29+ import com .velocitypowered .proxy .plugin .virtual .VelocityVirtualPlugin ;
2630import com .velocitypowered .proxy .protocol .StateRegistry ;
2731import com .velocitypowered .proxy .util .ratelimit .Ratelimiter ;
2832import com .velocitypowered .proxy .util .ratelimit .Ratelimiters ;
3135import java .net .InetAddress ;
3236import java .nio .file .Files ;
3337import java .nio .file .Path ;
38+ import java .util .ArrayList ;
3439import java .util .List ;
3540import java .util .Objects ;
41+ import java .util .stream .Collectors ;
3642import net .elytrium .commons .kyori .serialization .Serializer ;
3743import net .elytrium .commons .kyori .serialization .Serializers ;
3844import net .elytrium .commons .utils .updates .UpdatesChecker ;
@@ -136,6 +142,9 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
136142
137143 @ SuppressFBWarnings (value = "NP_NULL_ON_SOME_PATH" , justification = "LEGACY_AMPERSAND can't be null in velocity." )
138144 public void reload () {
145+ // Unregister previously registered commands.
146+ this .unregisterCommands ();
147+
139148 Settings .IMP .reload (this .dataDirectory .resolve ("config.yml" ));
140149
141150 ComponentSerializer <Component , Component , String > serializer = Settings .IMP .SERIALIZER .getSerializer ();
@@ -147,31 +156,26 @@ public void reload() {
147156 }
148157
149158 setRatelimiter (Ratelimiters .createWithMilliseconds (Settings .IMP .COMMANDS .RATELIMIT_DELAY ));
159+ this .unregisterCommands ();
150160
151161 List <String > aliases = Settings .IMP .COMMANDS .HUB .ALIASES ;
152- aliases .forEach (alias -> this .server .getCommandManager ().unregister (alias ));
153162 if (Settings .IMP .COMMANDS .HUB .ENABLED && !aliases .isEmpty ()) {
154- this .server . getCommandManager (). register (aliases .get (0 ), new HubCommand (this .server ), aliases .toArray (new String [0 ]));
163+ this .registerCommand (aliases .get (0 ), new HubCommand (this .server ), aliases .toArray (new String [0 ]));
155164 }
156165
157- this .server .getCommandManager ().unregister ("alert" );
158- this .server .getCommandManager ().unregister ("find" );
159- this .server .getCommandManager ().unregister ("send" );
160- this .server .getCommandManager ().unregister ("velocitytools" );
161-
162166 if (Settings .IMP .COMMANDS .ALERT .ENABLED ) {
163- this .server . getCommandManager (). register ("alert" , new AlertCommand (this .server ));
167+ this .registerCommand ("alert" , new AlertCommand (this .server ));
164168 }
165169
166170 if (Settings .IMP .COMMANDS .FIND .ENABLED ) {
167- this .server . getCommandManager (). register ("find" , new FindCommand (this .server ));
171+ this .registerCommand ("find" , new FindCommand (this .server ));
168172 }
169173
170174 if (Settings .IMP .COMMANDS .SEND .ENABLED ) {
171- this .server . getCommandManager (). register ("send" , new SendCommand (this .server ));
175+ this .registerCommand ("send" , new SendCommand (this .server ));
172176 }
173177
174- this .server . getCommandManager (). register ("velocitytools" , new VelocityToolsCommand (this ), "vtools" );
178+ this .registerCommand ("velocitytools" , new VelocityToolsCommand (this ), "vtools" );
175179
176180 this .server .getEventManager ().unregisterListeners (this );
177181
@@ -190,6 +194,60 @@ public void reload() {
190194 HandshakeHook .reload (this .packetFactory );
191195 }
192196
197+ private void registerCommand (String alias , Command command , String ... otherAliases ) {
198+ List <CommandMeta > unregisteredCommands = new ArrayList <>();
199+
200+ CommandMeta meta = this .server .getCommandManager ().getCommandMeta (alias );
201+ if (meta != null ) {
202+ unregisteredCommands .add (meta );
203+ this .server .getCommandManager ().unregister (alias );
204+ }
205+
206+ for (String otherAlias : otherAliases ) {
207+ meta = this .server .getCommandManager ().getCommandMeta (otherAlias );
208+ if (meta != null ) {
209+ unregisteredCommands .add (meta );
210+ this .server .getCommandManager ().unregister (otherAlias );
211+ }
212+ }
213+
214+ if (!unregisteredCommands .isEmpty ()) {
215+ getLogger ().warn ("Unregistered command(s) from other plugin(s): {}" , unregisteredCommands .stream ().map (commandMeta -> {
216+ String pluginName = "unknown" ;
217+ Object plugin = commandMeta .getPlugin ();
218+ if (plugin instanceof VelocityVirtualPlugin ) {
219+ pluginName = "Velocity" ;
220+ } else if (plugin != null ) {
221+ PluginContainer container = this .server .getPluginManager ().fromInstance (plugin ).orElse (null );
222+ if (container != null && container .getDescription () != null ) {
223+ pluginName = container .getDescription ().getName ().orElse (plugin .toString ());
224+ }
225+ }
226+
227+ return String .join (", " , commandMeta .getAliases ()) + " from " + pluginName ;
228+ }).collect (Collectors .joining (", " )));
229+ }
230+
231+ this .server .getCommandManager ().register (this .server .getCommandManager ().metaBuilder (alias ).plugin (this ).aliases (otherAliases ).build (), command );
232+ }
233+
234+ private void unregisterCommands () {
235+ List <String > aliases = Settings .IMP .COMMANDS .HUB .ALIASES ;
236+ if (aliases != null ) {
237+ aliases .forEach (this ::unregisterCommand );
238+ }
239+ this .unregisterCommand ("alert" );
240+ this .unregisterCommand ("find" );
241+ this .unregisterCommand ("send" );
242+ this .unregisterCommand ("velocitytools" );
243+ }
244+
245+ private void unregisterCommand (String command ) {
246+ CommandMeta meta = this .server .getCommandManager ().getCommandMeta (command );
247+ if (meta != null && meta .getPlugin () == this ) {
248+ this .server .getCommandManager ().unregister (meta );
249+ }
250+ }
193251
194252 private static void setLogger (Logger logger ) {
195253 LOGGER = logger ;
0 commit comments