3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-03 14:50:30 +01:00

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

Dieser Commit ist enthalten in:
Matsv 2016-03-31 17:48:15 +02:00
Ursprung ecc79b4f49
Commit 34cc783cac
5 geänderte Dateien mit 53 neuen und 6 gelöschten Zeilen

Datei anzeigen

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

Datei anzeigen

@ -1,10 +1,27 @@
package us.myles.ViaVersion.api.command; package us.myles.ViaVersion.api.command;
public interface ViaVersionCommand { 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; 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); boolean hasSubCommand(String name);
/**
* Get subcommand instance by name
*
* @param name subcommand name
* @return ViaSubCommand instance
*/
ViaSubCommand getSubCommand(String name); ViaSubCommand getSubCommand(String name);
} }

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

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