Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.TabCompleter;
import org.bukkit.plugin.Plugin;
import java.util.List;
/**
* A builder-styled class for {@link PluginCommand}
*/
public class PluginCommandBuilder {
/**
* The plugin command
*/
private final PluginCommand command;
/**
* Creates a new builder with the specified name
*
* @param name Name of the command
*/
public PluginCommandBuilder(String name, Plugin plugin) {
this.command = PluginCommandFactory.create(name, plugin);
}
/**
* Sets the command logic
*
* @param logic Command logic to run
* @return This builder instance
*/
public PluginCommandBuilder command(CommandExecutor logic) {
command.setExecutor(logic);
return this;
}
/**
* Sets the tab logic
*
* @param tabCompleter Tab completer to use
* @return This builder instance
*/
public PluginCommandBuilder tab(TabCompleter tabCompleter) {
command.setTabCompleter(tabCompleter);
return this;
}
/**
* Sets a brief description of this command.
*
* @param description new command description
* @return This builder instance
*/
public PluginCommandBuilder description(String description) {
command.setDescription(description);
return this;
}
/**
* Sets the example usage of this command
*
* @param usage new example usage
* @return This builder instance
*/
public PluginCommandBuilder usage(String usage) {
command.setUsage(usage);
return this;
}
/**
* Sets the list of aliases to request on registration for this command.
*
* @param aliases aliases to register to this command
* @return This builder instance
*/
public PluginCommandBuilder aliases(List<String> aliases) {
command.setAliases(aliases);
return this;
}
/**
* Sets the permission required by users to be able to perform this
* command
*
* @param permission Permission name or null
* @return This builder instance
*/
public PluginCommandBuilder permission(String permission) {
command.setPermission(permission);
return this;
}
/**
* Sets the message sent when a permission check fails
*
* @param permissionMessage new permission message, null to indicate
* default message, or an empty string to indicate no message
* @return This builder instance
*/
public PluginCommandBuilder permissionMessage(String permissionMessage) {
command.setPermissionMessage(permissionMessage);
return this;
}
/**
* Registers this command
*
* @return This builder instance
*/
public PluginCommandBuilder register() {
PluginCommandFactory.register(command);
return this;
}
/**
* Returns the constructed {@link PluginCommand}
*
* @return The plugin command
*/
public PluginCommand build() {
return command;
}
}
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.plugin.Plugin;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
/**
* A class for handling reflections with {@link PluginCommand} and {@link SimpleCommandMap}
*/
public class PluginCommandFactory {
/**
* Constructor of {@link PluginCommand}
*/
private static Constructor<PluginCommand> constructor;
/**
* Field to retrieve a {@link org.bukkit.command.SimpleCommandMap} from the {@link org.bukkit.plugin.PluginManager}
*/
private static Field commandMapField;
/**
* Creates a new instance of a plugin command
*
* @param name Name of the command
* @return The command
*/
public static PluginCommand create(String name, Plugin plugin) {
try {
return constructor.newInstance(name, plugin);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
/**
* Returns the {@link org.bukkit.command.CommandMap} that is specialized for plugin commands
*
* @return The command map
*/
public static SimpleCommandMap getCommandMap() {
try {
return (SimpleCommandMap) commandMapField.get(Bukkit.getPluginManager());
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
/**
* Registers the specified {@link PluginCommand}
*
* @param command Command to register
*/
public static void register(PluginCommand command) {
getCommandMap().register(command.getPlugin().getDescription().getName(), command);
}
static {
try {
constructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
constructor.setAccessible(true);
commandMapField = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
commandMapField.setAccessible(true);
} catch (NoSuchMethodException | NoSuchFieldException e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.