Skip to content

Commit

Permalink
SpigotUtils: Added command getters
Browse files Browse the repository at this point in the history
I didn't find a way to easily get registered commands on velocity, that's the reason why this is only supported on Spigot (And thus paper). Obviously a way to do this would be to store registered commands, but keeping in mind that this is a feature that will most likely be used by very few plugins, I think the extra waste of memory isn't worth it. Plugins that need this most likely will just use a simple workaround of their own.
  • Loading branch information
xDec0de committed Dec 19, 2024
1 parent a932afc commit be16178
Showing 1 changed file with 92 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
Expand Down Expand Up @@ -255,6 +256,10 @@ public SpigotUtils<P> registerEvents(@NotNull Listener... listeners) {
return this;
}

/*
- Commands - Map
*/

/**
* Gets the {@link SimpleCommandMap} instance stored on the {@link Bukkit#getServer() server}.
* <b>Reflection is used</b> in order to get this instance by accessing the
Expand Down Expand Up @@ -284,17 +289,9 @@ protected SimpleCommandMap getCommandMap() {
return null;
}

@Override
@SuppressWarnings("unchecked")
public void registerCommands(@NotNull GlobalCommand<P>... commands) {
if (commands == null || commands.length == 0)
return;
registerCommands(SkyCollections.map(
commands,
new AdaptedSpigotCommand[commands.length],
cmd -> new AdaptedSpigotCommand<>(this, cmd))
);
}
/*
- Commands - Registration
*/

/**
* Registers the specified {@code commands}, allowing them to be executed.
Expand Down Expand Up @@ -341,6 +338,18 @@ public void registerCommands(CustomSpigotCommand<P, ? extends SpigotCommandSende
commandMap.registerAll(getPlugin().getName(), remaining);
}

@Override
@SuppressWarnings("unchecked")
public void registerCommands(@NotNull GlobalCommand<P>... commands) {
if (commands == null || commands.length == 0)
return;
registerCommands(SkyCollections.map(
commands,
new AdaptedSpigotCommand[commands.length],
cmd -> new AdaptedSpigotCommand<>(this, cmd))
);
}

/**
* Unregisters a command by {@code name}. In order to unregister
* a command, the command must have been registered by this
Expand All @@ -364,7 +373,78 @@ public boolean unregisterCommand(@NotNull String name) {
}

/*
* World creation
- Commands - Getters
*/

/**
* Gets a {@link Collection} of all <b>registered</b> commands on
* the {@link Bukkit#getServer() server}, <b>not only</b> those registered
* by the {@link P plugin} that manages this {@link SpigotUtils} instance.
*
* @return A {@link Collection} of all <b>registered</b> commands on
* the {@link Bukkit#getServer() server}
*
* @since SkyUtils 1.0.0
*
* @see #getCommand(String)
* @see #getCommand(Class)
*/
@Nullable
public Collection<Command> getCommands() {
final SimpleCommandMap map = getCommandMap();
return map == null ? null : map.getCommands();
}

/**
* Gets a <b>registered</b> {@link Command} by {@code name}.
* The {@link Command} is not required to be registered by the
* {@link P plugin} that manages this {@link SpigotUtils} instance.
*
* @param name The name of the <b>registered</b> {@link Command} to get.
*
* @return A <b>registered</b> {@link Command} instance that matches the provided
* {@code name}, if found. {@code null} otherwise.
*
* @since SkyUtils 1.0.0
*
* @see #getCommand(Class)
* @see #getCommands()
*/
@Nullable
public Command getCommand(@NotNull String name) {
// TODO: Check if aliases are supported. Docs seem to indicate they aren't.
final SimpleCommandMap map = getCommandMap();
return map == null ? null : map.getCommand(name);
}

/**
* Gets a <b>registered</b> {@link C command} by {@link Class}.
* The {@link Command} is not required to be registered by the
* {@link P plugin} that manages this {@link SpigotUtils} instance.
*
* @param <C> The type of the class, {@code extends} {@link Command}.
*
* @param cmdClass The {@link Class} of the <b>registered</b> {@link C command} to get.
*
* @return A <b>registered</b> {@link C command} instance that matches the provided
* {@code cmdClass}, if found. {@code null} otherwise.
*
* @since SkyUtils 1.0.0
*
* @see #getCommand(String)
* @see #getCommands()
*/
@Nullable
public <C extends Command> C getCommand(@NotNull Class<C> cmdClass) {
final Collection<Command> cmds = getCommands();
if (cmds == null)
return null;
final Command cmd = SkyCollections.get(cmds, c -> cmdClass.isAssignableFrom(c.getClass()));
return cmd == null ? null : cmdClass.cast(cmd);
}

/*
- World creation
*/

/**
Expand Down

0 comments on commit be16178

Please sign in to comment.