1
mirror of https://github.com/CarmJos/MoeTeleport.git synced 2026-06-04 07:48:16 +08:00

feat(command): 为各个指令的功能添加单独权限,修复接受指令错误的问题

This commit is contained in:
2023-03-17 22:36:14 +08:00
parent 8c1a4530c0
commit 4393cc9ce2
11 changed files with 76 additions and 160 deletions
@@ -2,6 +2,7 @@ package cc.carm.plugin.moeteleport;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.easyplugin.EasyPlugin;
import cc.carm.lib.easyplugin.command.alias.AliasCommandManager;
import cc.carm.lib.easyplugin.updatechecker.GHUpdateChecker;
import cc.carm.lib.mineconfiguration.bukkit.MineConfiguration;
import cc.carm.plugin.moeteleport.command.MainCommands;
@@ -15,6 +16,7 @@ import cc.carm.plugin.moeteleport.storage.StorageMethod;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.SimplePie;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import xyz.xenondevs.particle.utils.ReflectionUtils;
public class Main extends EasyPlugin {
@@ -28,7 +30,7 @@ public class Main extends EasyPlugin {
protected UserManager userManager;
protected RequestManager requestManager;
protected TeleportManager teleportManager;
protected CommandManager commandManager;
protected AliasCommandManager commandManager;
public Main() {
instance = this;
@@ -85,14 +87,15 @@ public class Main extends EasyPlugin {
log("注册指令...");
registerCommand("MoeTeleport", new MainCommands(this));
try {
this.commandManager = new CommandManager(this);
if (PluginConfig.COMMAND.ENABLE.getNotNull()) {
if (PluginConfig.COMMAND.ENABLE.getNotNull()) {
log("注册简化指令映射...");
try {
this.commandManager = new AliasCommandManager(this);
PluginConfig.COMMAND.ALIAS.getNotNull().forEach(commandManager::register);
} catch (Exception e) {
log("注册简化指令失败: " + e.getMessage());
e.printStackTrace();
}
} catch (Exception e) {
log("注册简化指令失败: " + e.getMessage());
e.printStackTrace();
}
if (PluginConfig.METRICS.getNotNull()) {
@@ -116,8 +119,8 @@ public class Main extends EasyPlugin {
@Override
protected void shutdown() {
log("清空简化指令...");
if (this.commandManager != null) {
if (PluginConfig.COMMAND.ENABLE.getNotNull() && this.commandManager != null) {
log("清空简化指令...");
this.commandManager.unregisterAll();
}
@@ -39,4 +39,9 @@ public class BackCommand extends SubCommand<MainCommands> {
return null;
}
@Override
public boolean hasPermission(CommandSender sender) {
return sender.hasPermission("MoeTeleport.back");
}
}
@@ -48,4 +48,9 @@ public class HomeCommands extends CommandHandler {
return main.noPermission(sender);
}
@Override
public boolean hasPermission(CommandSender sender) {
return sender.hasPermission("MoeTeleport.home");
}
}
@@ -35,7 +35,7 @@ public class ReloadCommand extends SubCommand<MainCommands> {
@Override
public boolean hasPermission(CommandSender sender) {
return sender.hasPermission("moeteleport.reload");
return sender.hasPermission("MoeTeleport.admin");
}
}
@@ -22,7 +22,7 @@ public class TeleportCommands extends CommandHandler {
registerSubCommand(new TeleportRequestCommand(this, TeleportRequest.Type.TPA_TO, "to"));
registerSubCommand(new TeleportRequestCommand(this, TeleportRequest.Type.TPA_HERE, "here"));
registerSubCommand(new TeleportHandleCommand(this, true, "cancel"));
registerSubCommand(new TeleportHandleCommand(this, true, "accept", "agree"));
registerSubCommand(new TeleportHandleCommand(this, false, "deny", "refuse"));
registerSubCommand(new TeleportCancelCommand(this, "cancel"));
}
@@ -38,4 +38,9 @@ public class TeleportCommands extends CommandHandler {
return main.noPermission(sender);
}
@Override
public boolean hasPermission(CommandSender sender) {
return sender.hasPermission("MoeTeleport.teleport");
}
}
@@ -49,4 +49,10 @@ public class WarpCommands extends CommandHandler {
return main.noPermission(sender);
}
@Override
public boolean hasPermission(CommandSender sender) {
return sender.hasPermission("MoeTeleport.warp");
}
}
@@ -1,122 +0,0 @@
package cc.carm.plugin.moeteleport.manager;
import cc.carm.plugin.moeteleport.Main;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.plugin.SimplePluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.Field;
import java.util.*;
public class CommandManager {
protected final JavaPlugin plugin;
protected final SimpleCommandMap commandMap;
protected final Field knownCommandsFiled;
protected final Map<String, AliasCommand> registeredCommands = new HashMap<>();
public CommandManager(JavaPlugin plugin) throws Exception {
this.plugin = plugin;
SimplePluginManager manager = (SimplePluginManager) Bukkit.getPluginManager();
Field commandMapField = SimplePluginManager.class.getDeclaredField("commandMap");
commandMapField.setAccessible(true);
this.commandMap = (SimpleCommandMap) commandMapField.get(manager);
this.knownCommandsFiled = SimpleCommandMap.class.getDeclaredField("knownCommands");
this.knownCommandsFiled.setAccessible(true);
}
@SuppressWarnings("unchecked")
protected Map<String, Command> getKnownCommands() {
try {
return (Map<String, Command>) knownCommandsFiled.get(commandMap);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return new HashMap<>();
}
public String getPrefix() {
return this.plugin.getName().toLowerCase() + " ";
}
protected SimpleCommandMap getCommandMap() {
return commandMap;
}
public void register(String alias, String target) {
AliasCommand current = this.registeredCommands.get(alias);
if (current != null) current.unregister(getCommandMap());
AliasCommand cmd = new AliasCommand(alias, this, getPrefix() + target);
this.registeredCommands.put(alias, cmd);
getCommandMap().register(Main.getInstance().getName(), cmd);
}
public void unregister(String alias) {
AliasCommand current = this.registeredCommands.remove(alias);
if (current != null) {
getKnownCommands().remove(alias);
current.unregister(getCommandMap());
}
}
public void unregisterAll() {
registeredCommands.forEach((k, v) -> {
getKnownCommands().remove(k);
v.unregister(getCommandMap());
});
registeredCommands.clear();
}
public static class AliasCommand extends Command {
protected final CommandManager commandManager;
protected final String targetCommand;
public AliasCommand(String name, CommandManager commandManager, String targetCommand) {
super(name);
this.commandManager = commandManager;
this.targetCommand = targetCommand;
}
protected SimpleCommandMap getCommandMap() {
return this.commandManager.getCommandMap();
}
protected String buildCommand(String[] args) {
return this.targetCommand + " " + String.join(" ", args);
}
@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
return getCommandMap().dispatch(sender, buildCommand(args));
}
@NotNull
@Override
public List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias,
@NotNull String[] args, @Nullable Location location) throws IllegalArgumentException {
return Optional.ofNullable(getCommandMap().tabComplete(sender, buildCommand(args))).orElse(Collections.emptyList());
}
@NotNull
@Override
public List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException {
return tabComplete(sender, alias, args, null);
}
}
}
+17 -5
View File
@@ -13,18 +13,30 @@ softdepend:
- CMI
permissions:
"MoeTeleport":
description: "插件的主权限节点"
default: false
"MoeTeleport.admin":
description: "插件的管理员权限节点"
default: op
"MoeTeleport.teleport":
description: "使用传送请求相关指令的权限。"
default: true
"MoeTeleport.home":
description: "使用家相关指令的权限。"
default: true
"MoeTeleport.warp":
description: "使用传送请求相关指令的权限。"
default: true
"MoeTeleport.back":
description: "使用返回相关指令的权限。"
default: true
commands:
"MoeTeleport":
description: "插件的主命令,用于重载插件或查看插件信息。"
permission: "MoeTeleport.admin"
usage: "/MoeTeleport reload"
usage: "您可以输入 /MoeTeleport help 查看插件的相关帮助。"
aliases:
- mt