Fix missing description, add javadocs, make sure subcommand is valid, check permission

This commit is contained in:
Matsv 2016-03-31 17:48:15 +02:00
parent ecc79b4f49
commit 34cc783cac
5 changed files with 53 additions and 6 deletions

View File

@ -5,14 +5,34 @@ import org.bukkit.entity.Player;
import us.myles.ViaVersion.commands.ViaCommandHandler;
public abstract class ViaSubCommand {
/**
* Subcommand name
*
* @return your input
*/
public abstract String name();
/**
* subcommand description, this'll show in /viaversion list
*
* @return your input
*/
public abstract String description();
/**
* Usage example:
* "playerversion [name]"
*
* @return your input
*/
public String usage(){
return name();
}
/**
* Permission, null for everyone
* @return
*/
public String permission(){
return "viaversion.admin";
}

View File

@ -1,10 +1,27 @@
package us.myles.ViaVersion.api.command;
public interface ViaVersionCommand {
/**
* Register your own subcommand inside ViaVersion
*
* @param command Your own SubCommand instance to handle it.
* @throws Exception throws an exception when the subcommand already exists or if it's not valid, example: spacee
*/
void registerSubCommand(ViaSubCommand command) throws Exception;
/**
* Check if a subcommand is registered.
*
* @param name Subcommand name
* @return true if it exists
*/
boolean hasSubCommand(String name);
/**
* Get subcommand instance by name
*
* @param name subcommand name
* @return ViaSubCommand instance
*/
ViaSubCommand getSubCommand(String name);
}

View File

@ -1,6 +1,7 @@
package us.myles.ViaVersion.commands;
import lombok.NonNull;
import org.apache.commons.lang.Validate;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@ -25,6 +26,7 @@ public class ViaCommandHandler implements us.myles.ViaVersion.api.command.ViaVer
@Override
public void registerSubCommand(@NonNull ViaSubCommand command) throws Exception {
Validate.isTrue(command.name().matches("^[a-z0-9_-]{3,15}$"), command.name() + " is not a valid subcommand name");
if (hasSubCommand(command.name()))
throw new Exception("ViaSubCommand " + command.name() + " does already exists!"); //Maybe another exception later.
commandMap.put(command.name().toLowerCase(), command);
@ -52,8 +54,13 @@ public class ViaCommandHandler implements us.myles.ViaVersion.api.command.ViaVer
showHelp(sender);
return false;
}
ViaSubCommand handler = getSubCommand(args[0]);
if (!hasPermission(sender, handler.permission())){
sender.sendMessage(color("&cYou are not allowed to use this command!"));
return false;
}
String[] subArgs = Arrays.copyOfRange(args, 1, args.length);
boolean result = handler.execute(sender, subArgs);
if (!result)
@ -64,7 +71,7 @@ public class ViaCommandHandler implements us.myles.ViaVersion.api.command.ViaVer
public void showHelp(CommandSender sender) {
Set<ViaSubCommand> allowed = calculateAllowedCommands(sender);
if (allowed.size() == 0){
sender.sendMessage("&cYou are not allowed to use this command!");
sender.sendMessage(color("&cYou are not allowed to use this command!"));
return;
}
sender.sendMessage(color("&aViaVersion &c" + ViaVersion.getInstance().getVersion()));
@ -77,11 +84,15 @@ public class ViaCommandHandler implements us.myles.ViaVersion.api.command.ViaVer
private Set<ViaSubCommand> calculateAllowedCommands(CommandSender sender) {
Set<ViaSubCommand> cmds = new HashSet<>();
for (ViaSubCommand sub : commandMap.values())
if (sub.permission() == null || sender.hasPermission(sub.permission()))
if (hasPermission(sender, sub.permission()))
cmds.add(sub);
return cmds;
}
private boolean hasPermission(CommandSender sender, String permission){
return permission == null || sender.hasPermission(permission);
}
public static String color(String string) {
try {

View File

@ -13,7 +13,7 @@ public class DontBugMeSubCmd extends ViaSubCommand {
@Override
public String description() {
return null;
return "Toggle checking for updates";
}
@Override

View File

@ -7,5 +7,4 @@ loadbefore: [ProtocolLib, ProxyPipe]
commands:
viaversion:
description: Shows ViaVersion Version and more.
permission: viaversion.admin
aliases: [viaver]