mirror of
https://e.coding.net/circlecloud/YumCore.git
synced 2024-12-24 06:58:51 +00:00
feat: 添加新的命令发送者检测
Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
parent
27a81f7e84
commit
a72f617b3e
@ -13,6 +13,7 @@ import pw.yumc.YumCore.bukkit.Log;
|
||||
import pw.yumc.YumCore.bukkit.P;
|
||||
import pw.yumc.YumCore.commands.annotation.Async;
|
||||
import pw.yumc.YumCore.commands.annotation.Cmd;
|
||||
import pw.yumc.YumCore.commands.annotation.Cmd.Executor;
|
||||
import pw.yumc.YumCore.commands.annotation.Help;
|
||||
import pw.yumc.YumCore.commands.annotation.Sort;
|
||||
import pw.yumc.YumCore.commands.exception.CommandException;
|
||||
@ -27,6 +28,7 @@ public class CommandInfo {
|
||||
public static final CommandInfo Unknow = new CommandInfo();
|
||||
private static final String onlyPlayer = "§c控制台无法使用此命令(§4请在游戏内执行§c)!";
|
||||
private static final String onlyConsole = "§c玩家无法使用此命令(§4请使用控制台执行§c)!";
|
||||
private static final String onlyExecutor = "§c当前命令仅允许 §b%s §c执行!";
|
||||
private static final String losePerm = "§c你需要有 %s 的权限才能执行此命令!";
|
||||
private static final String cmdErr = "§6错误原因: §4命令参数不正确!";
|
||||
private static final String cmdUse = "§6使用方法: §e/%s %s %s";
|
||||
@ -35,6 +37,8 @@ public class CommandInfo {
|
||||
private final Method method;
|
||||
private final String name;
|
||||
private final List<String> aliases;
|
||||
private final List<Executor> executors;
|
||||
private final String executorStr;
|
||||
private final boolean async;
|
||||
private final Cmd command;
|
||||
private final Help help;
|
||||
@ -45,6 +49,8 @@ public class CommandInfo {
|
||||
this.origin = origin;
|
||||
this.name = "".equals(command.value()) ? method.getName().toLowerCase() : command.value();
|
||||
this.aliases = Arrays.asList(command.aliases());
|
||||
this.executors = Arrays.asList(command.executor());
|
||||
this.executorStr = eS(executors);
|
||||
this.command = command;
|
||||
this.help = help;
|
||||
this.async = async;
|
||||
@ -56,6 +62,8 @@ public class CommandInfo {
|
||||
this.origin = null;
|
||||
this.name = "unknow";
|
||||
this.aliases = null;
|
||||
this.executors = null;
|
||||
this.executorStr = null;
|
||||
this.command = null;
|
||||
this.help = null;
|
||||
this.async = false;
|
||||
@ -93,15 +101,9 @@ public class CommandInfo {
|
||||
/**
|
||||
* 执行命令
|
||||
*
|
||||
* @param sender
|
||||
* 命令发送者
|
||||
* @param command
|
||||
* 主命令
|
||||
* @param label
|
||||
* 主命令别名
|
||||
* @param args
|
||||
* 子命令参数
|
||||
* @return
|
||||
* @param cmdArgs
|
||||
* 命令参数
|
||||
* @return 是否执行成功
|
||||
*/
|
||||
public boolean execute(final CommandArgument cmdArgs) {
|
||||
if (method == null) {
|
||||
@ -183,12 +185,33 @@ public class CommandInfo {
|
||||
*/
|
||||
private boolean check(final CommandArgument cmdArgs) {
|
||||
final CommandSender sender = cmdArgs.getSender();
|
||||
return checkArgs(sender, cmdArgs) && checkSender(sender) && checkPerm(sender);
|
||||
}
|
||||
|
||||
private boolean checkArgs(final CommandSender sender, final CommandArgument cmdArgs) {
|
||||
if (cmdArgs.getArgs().length < command.minimumArguments()) {
|
||||
Log.toSender(sender, cmdErr);
|
||||
Log.toSender(sender, String.format(cmdUse, cmdArgs.getAlias(), getName(), help.possibleArguments()));
|
||||
Log.toSender(sender, String.format(cmdDes, help.value()));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean checkPerm(final CommandSender sender) {
|
||||
final String perm = command.permission();
|
||||
if (perm != null && !"".equals(perm) && !sender.hasPermission(perm)) {
|
||||
Log.toSender(sender, String.format(losePerm, perm));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean checkSender(final CommandSender sender) {
|
||||
if (!executors.contains(Executor.ALL) && !executors.contains(Executor.valueOf(sender))) {
|
||||
Log.toSender(sender, String.format(onlyExecutor, executorStr));
|
||||
return false;
|
||||
}
|
||||
if (sender instanceof Player) {
|
||||
if (command.onlyConsole()) {
|
||||
Log.toSender(sender, onlyConsole);
|
||||
@ -200,11 +223,15 @@ public class CommandInfo {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
final String perm = command.permission();
|
||||
if (perm != null && !"".equals(perm) && !sender.hasPermission(perm)) {
|
||||
Log.toSender(sender, String.format(losePerm, perm));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private String eS(final List<Executor> executors) {
|
||||
final StringBuffer str = new StringBuffer();
|
||||
for (final Executor executor : executors) {
|
||||
str.append(executor.getName());
|
||||
str.append(", ");
|
||||
}
|
||||
return str.toString().substring(0, str.length() - 2);
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,13 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.bukkit.command.BlockCommandSender;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.bukkit.command.RemoteConsoleCommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.minecart.CommandMinecart;
|
||||
|
||||
/**
|
||||
* 命令注解
|
||||
*
|
||||
@ -29,28 +36,136 @@ public @interface Cmd {
|
||||
*/
|
||||
String[] aliases() default "";
|
||||
|
||||
/**
|
||||
* @return 命令执行者
|
||||
*/
|
||||
Executor[] executor() default Executor.ALL;
|
||||
|
||||
/**
|
||||
* @return 命令最小参数
|
||||
*/
|
||||
int minimumArguments() default 0;
|
||||
|
||||
/**
|
||||
* @deprecated 详见 {@link Executor}
|
||||
* @return 只允许控制台执行
|
||||
*/
|
||||
@Deprecated
|
||||
boolean onlyConsole() default false;
|
||||
|
||||
/**
|
||||
* @deprecated 详见 {@link Executor}
|
||||
* @return 只允许玩家执行
|
||||
*/
|
||||
@Deprecated
|
||||
boolean onlyPlayer() default false;
|
||||
|
||||
/**
|
||||
* @return 当前命令权限
|
||||
*/
|
||||
String permission() default "";
|
||||
|
||||
/**
|
||||
* @return 命令名称
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
/**
|
||||
* @return 只允许控制台执行
|
||||
* 命令执行者
|
||||
*
|
||||
* @author 喵♂呜
|
||||
* @since 2016年8月26日 下午8:55:15
|
||||
*/
|
||||
boolean onlyConsole() default false;
|
||||
public enum Executor {
|
||||
/**
|
||||
* 玩家
|
||||
*/
|
||||
PLAYER {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "玩家";
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 控制台
|
||||
*/
|
||||
CONSOLE {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "控制台";
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 命令方块
|
||||
*/
|
||||
BLOCK {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "命令方块";
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 命令矿车
|
||||
*/
|
||||
COMMANDMINECART {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "命令矿车";
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 远程控制台
|
||||
*/
|
||||
REMOTECONSOLE {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "远程控制台";
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 所有
|
||||
*/
|
||||
ALL {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "所有执行者";
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 未知
|
||||
*/
|
||||
UNKNOW {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "未知";
|
||||
}
|
||||
};
|
||||
/**
|
||||
* 解析Executor
|
||||
*
|
||||
* @param sender
|
||||
* 命令执行者
|
||||
* @return {@link Executor}
|
||||
*/
|
||||
public static Executor valueOf(final CommandSender sender) {
|
||||
if (sender instanceof Player) {
|
||||
return Executor.PLAYER;
|
||||
} else if (sender instanceof ConsoleCommandSender) {
|
||||
return Executor.CONSOLE;
|
||||
} else if (sender instanceof BlockCommandSender) {
|
||||
return Executor.BLOCK;
|
||||
} else if (sender instanceof CommandMinecart) {
|
||||
return Executor.COMMANDMINECART;
|
||||
} else if (sender instanceof RemoteConsoleCommandSender) {
|
||||
return Executor.REMOTECONSOLE;
|
||||
} else {
|
||||
return Executor.UNKNOW;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 只允许玩家执行
|
||||
*/
|
||||
boolean onlyPlayer() default false;
|
||||
|
||||
/**
|
||||
* @return 当前命令权限
|
||||
*/
|
||||
String permission() default "";
|
||||
/**
|
||||
* @return 执行者名称
|
||||
*/
|
||||
public abstract String getName();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user