Skip to content

Commit

Permalink
Merge pull request #16 from 17TheWord/dev
Browse files Browse the repository at this point in the history
✨ 对命令添加权限检查(Beta)
  • Loading branch information
17TheWord authored Sep 11, 2024
2 parents e794e84 + a3a0d8a commit 0a988ca
Show file tree
Hide file tree
Showing 23 changed files with 135 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.mojang.brigadier.Command;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;

import static com.github.theword.queqiao.tool.utils.Tool.handleCommandReturnMessageService;

Expand All @@ -17,6 +16,7 @@ public class HelpCommand extends HelpCommandAbstract implements FabricSubCommand

@Override
public int onCommand(CommandContext<ServerCommandSource> context) {
if (!handleCommandReturnMessageService.hasPermission(context, getPermissionNode())) return 0;
handleCommandReturnMessageService.handleCommandReturnMessage(context, "-------------------");
for (SubCommand subCommand : new CommandManager().getSubCommandList()) {
handleCommandReturnMessageService.handleCommandReturnMessage(context, subCommand.getUsage() + "---" + subCommand.getDescription());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.command.ServerCommandSource;

import static com.github.theword.queqiao.tool.utils.Tool.websocketManager;
import static com.github.theword.queqiao.tool.utils.Tool.handleCommandReturnMessageService;


public class ReloadCommand extends ReloadCommandAbstract implements FabricSubCommand {
Expand All @@ -18,7 +18,8 @@ public class ReloadCommand extends ReloadCommandAbstract implements FabricSubCom
*/
@Override
public int onCommand(CommandContext<ServerCommandSource> context) {
websocketManager.reloadWebsocket(true, context);
if (!handleCommandReturnMessageService.hasPermission(context, getPermissionNode())) return 0;
execute(context, true);
return Command.SINGLE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.command.ServerCommandSource;

import static com.github.theword.queqiao.tool.utils.Tool.websocketManager;
import static com.github.theword.queqiao.tool.utils.Tool.handleCommandReturnMessageService;

public class ReconnectAllCommand extends ReconnectCommandAbstract implements FabricSubCommand {
/**
Expand All @@ -15,7 +15,8 @@ public class ReconnectAllCommand extends ReconnectCommandAbstract implements Fab
*/
@Override
public int onCommand(CommandContext<ServerCommandSource> context) {
websocketManager.reconnectWebsocketClients(true, context);
if (!handleCommandReturnMessageService.hasPermission(context, getPermissionNode())) return 0;
execute(context, true);
return Command.SINGLE_SUCCESS;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.command.ServerCommandSource;

import static com.github.theword.queqiao.tool.utils.Tool.websocketManager;
import static com.github.theword.queqiao.tool.utils.Tool.handleCommandReturnMessageService;


public class ReconnectCommand extends ReconnectCommandAbstract implements FabricSubCommand {
Expand All @@ -17,7 +17,8 @@ public class ReconnectCommand extends ReconnectCommandAbstract implements Fabric
*/
@Override
public int onCommand(CommandContext<ServerCommandSource> context) {
websocketManager.reconnectWebsocketClients(false, context);
if (!handleCommandReturnMessageService.hasPermission(context, getPermissionNode())) return 0;
execute(context, false);
return Command.SINGLE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.theword.queqiao.handle;

import com.github.theword.queqiao.tool.constant.BaseConstant;
import com.github.theword.queqiao.tool.handle.HandleCommandReturnMessageService;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.command.ServerCommandSource;
Expand All @@ -18,9 +19,26 @@ public void handleCommandReturnMessage(Object object, String message) {
CommandContext<ServerCommandSource> context = (CommandContext<ServerCommandSource>) object;
if (context.getSource().getEntity() instanceof ServerPlayerEntity)
// IF >= fabric-1.20
// context.getSource().sendFeedback(() -> Text.of(message), false);
// ELSE
// context.getSource().sendFeedback(() -> Text.of(message), false);
// ELSE
// context.getSource().sendFeedback(Text.of(message), false);
// END IF
}

/**
* 判断发送者是否有权执行命令
* <p>MOD端中无权限节点,权限等级为2</p>
*
* @param object 命令返回者
* @param node 权限节点
* @return boolean 是否有权限
*/
@Override
@SuppressWarnings("unchecked")
public boolean hasPermission(Object object, String node) {
CommandContext<ServerCommandSource> context = (CommandContext<ServerCommandSource>) object;
if (context.getSource().hasPermissionLevel(BaseConstant.MOD_PERMISSION_LEVEL)) return true;
handleCommandReturnMessage(object, "你没有权限执行此命令");
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class HelpCommand extends HelpCommandAbstract implements ForgeSubCommand
// ELSE
// public int onCommand(CommandContext<net.minecraft.command.CommandSource> context) {
// END IF
if (!handleCommandReturnMessageService.hasPermission(context, getPermissionNode())) return 0;
handleCommandReturnMessageService.handleCommandReturnMessage(context, "-------------------");
for (ForgeSubCommand forgeSubCommand : new CommandManager().getSubCommandList()) {
handleCommandReturnMessageService.handleCommandReturnMessage(context, forgeSubCommand.getUsage() + "---" + forgeSubCommand.getDescription());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mojang.brigadier.Command;
import com.mojang.brigadier.context.CommandContext;

import static com.github.theword.queqiao.tool.utils.Tool.handleCommandReturnMessageService;
import static com.github.theword.queqiao.tool.utils.Tool.websocketManager;


Expand All @@ -13,10 +14,11 @@ public class ReloadCommand extends ReloadCommandAbstract implements ForgeSubComm
@Override
// IF > forge-1.16.5
// public int onCommand(CommandContext<net.minecraft.commands.CommandSourceStack> context) {
// ELSE
// ELSE
// public int onCommand(CommandContext<net.minecraft.command.CommandSource> context) {
// END IF
websocketManager.reloadWebsocket(true, context);
if (!handleCommandReturnMessageService.hasPermission(context, getPermissionNode())) return 0;
execute(context, true);
return Command.SINGLE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
import com.mojang.brigadier.Command;
import com.mojang.brigadier.context.CommandContext;

import static com.github.theword.queqiao.tool.utils.Tool.websocketManager;
import static com.github.theword.queqiao.tool.utils.Tool.handleCommandReturnMessageService;


public class ReconnectAllCommand extends ReconnectCommandAbstract implements ForgeSubCommand {

@Override
// IF > forge-1.16.5
// public int onCommand(CommandContext<net.minecraft.commands.CommandSourceStack> context) {
// ELSE
// ELSE
// public int onCommand(CommandContext<net.minecraft.command.CommandSource> context) {
// END IF

websocketManager.reconnectWebsocketClients(true, context);
if (!handleCommandReturnMessageService.hasPermission(context, getPermissionNode())) return 0;
execute(context, true);
return Command.SINGLE_SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
import com.mojang.brigadier.Command;
import com.mojang.brigadier.context.CommandContext;

import static com.github.theword.queqiao.tool.utils.Tool.websocketManager;
import static com.github.theword.queqiao.tool.utils.Tool.handleCommandReturnMessageService;


public class ReconnectCommand extends ReconnectCommandAbstract implements ForgeSubCommand {

@Override
// IF > forge-1.16.5
// public int onCommand(CommandContext<net.minecraft.commands.CommandSourceStack> context) {
// ELSE
// ELSE
// public int onCommand(CommandContext<net.minecraft.command.CommandSource> context) {
// END IF
websocketManager.reconnectWebsocketClients(false, context);
if (!handleCommandReturnMessageService.hasPermission(context, getPermissionNode())) return 0;
execute(context, false);
return Command.SINGLE_SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.theword.queqiao.handle;

import com.github.theword.queqiao.tool.constant.BaseConstant;
import com.github.theword.queqiao.tool.handle.HandleCommandReturnMessageService;
import com.mojang.brigadier.context.CommandContext;
// IF >= forge-1.21
Expand Down Expand Up @@ -27,4 +28,25 @@ public void handleCommandReturnMessage(Object object, String message) {
// context.getSource().sendSuccess(new net.minecraft.util.text.StringTextComponent(message), false);
// END IF
}

/**
* 判断发送者是否有权执行命令
* <p>MOD端中无权限节点,权限等级为2</p>
*
* @param object 命令返回者
* @param node 权限节点
* @return boolean 是否有权限
*/
@Override
@SuppressWarnings("unchecked")
public boolean hasPermission(Object object, String node) {
// IF > forge-1.16.5
// CommandContext<net.minecraft.commands.CommandSourceStack> context = (CommandContext<net.minecraft.commands.CommandSourceStack>) object;
// ELSE
// CommandContext<net.minecraft.command.CommandSource> context = (CommandContext<net.minecraft.command.CommandSource>) object;
// END IF
if (context.getSource().hasPermission(BaseConstant.MOD_PERMISSION_LEVEL)) return true;
handleCommandReturnMessage(object, "您没有权限执行此命令");
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import com.github.theword.queqiao.command.SpigotSubCommand;
import com.github.theword.queqiao.command.subCommand.client.ReconnectCommand;
import com.github.theword.queqiao.tool.command.subCommand.ClientCommandAbstract;
import org.bukkit.command.CommandSender;

import java.util.ArrayList;
import java.util.List;

public class ClientCommand implements SpigotSubCommand {
import static com.github.theword.queqiao.tool.utils.Tool.handleCommandReturnMessageService;

public class ClientCommand extends ClientCommandAbstract implements SpigotSubCommand {
@Override
public boolean onCommand(CommandSender commandSender, String[] args) {
if (!handleCommandReturnMessageService.hasPermission(commandSender, getPermissionNode())) return false;
if (args.length == 0) {
commandSender.sendMessage("§c请输入子命令");
return false;
Expand All @@ -23,6 +27,7 @@ public boolean onCommand(CommandSender commandSender, String[] args) {
public List<String> getSubCommands(CommandSender commandSender, String[] args) {
return new ArrayList<String>() {{
add("reconnect");
add("list");
}};
}

Expand All @@ -45,4 +50,4 @@ public String getDescription() {
public String getUsage() {
return "使用:/mcqq client <args>";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import java.util.ArrayList;
import java.util.List;

import static com.github.theword.queqiao.tool.utils.Tool.handleCommandReturnMessageService;

public class HelpCommand extends HelpCommandAbstract implements SpigotSubCommand {
@Override
public boolean onCommand(CommandSender commandSender, String[] args) {
if (!handleCommandReturnMessageService.hasPermission(commandSender, getPermissionNode())) return false;
commandSender.sendMessage("-------------------");
for (SubCommand subCommand : new CommandManager().getSubCommandList()) {
commandSender.sendMessage(subCommand.getUsage() + "---" + subCommand.getDescription());
Expand All @@ -30,4 +33,4 @@ public List<String> getSubCommands(CommandSender commandSender, String[] args) {
public String getPrefix() {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import java.util.ArrayList;
import java.util.List;

import static com.github.theword.queqiao.tool.utils.Tool.websocketManager;
import static com.github.theword.queqiao.tool.utils.Tool.handleCommandReturnMessageService;


public class ReloadCommand extends ReloadCommandAbstract implements SpigotSubCommand {
@Override
public boolean onCommand(CommandSender commandSender, String[] args) {
websocketManager.reloadWebsocket(false, commandSender);
if (!handleCommandReturnMessageService.hasPermission(commandSender, getPermissionNode())) return false;
execute(commandSender, false);
return true;
}

Expand All @@ -26,4 +27,4 @@ public List<String> getSubCommands(CommandSender commandSender, String[] args) {
public String getPrefix() {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public class ReconnectCommand extends ReconnectCommandAbstract implements Spigot
public boolean onCommand(CommandSender commandSender, String[] args) {
if (args.length == 2) {
if (args[1].equalsIgnoreCase("reconnect")) {
websocketManager.reconnectWebsocketClients(false, commandSender);
execute(commandSender, false);
return true;
}
} else if (args.length == 3) {
if (args[2].equalsIgnoreCase("all")) {
websocketManager.reconnectWebsocketClients(true, commandSender);
execute(commandSender, true);
return true;
}
}
Expand All @@ -40,4 +40,4 @@ public List<String> getSubCommands(CommandSender commandSender, String[] args) {
public String getPrefix() {
return "client";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,31 @@
import org.bukkit.command.CommandSender;

public class HandleCommandReturnMessageImpl implements HandleCommandReturnMessageService {

/**
* 处理命令返回消息
*
* @param object 命令发送者
* @param message 消息
*/
@Override
public void handleCommandReturnMessage(Object object, String message) {
CommandSender commandSender = (CommandSender) object;
commandSender.sendMessage(message);
}

/**
* 判断命令发送者是否有权限执行命令
*
* @param object 命令发送者
* @param node 权限节点
* @return 是否有权限
*/
@Override
public boolean hasPermission(Object object, String node) {
CommandSender commandSender = (CommandSender) object;
if (commandSender.hasPermission(node)) return true;
commandSender.sendMessage("您没有权限执行当前命令");
return false;
}
}
2 changes: 1 addition & 1 deletion tool_version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.3
0.1.5
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
// The constants are replaced before compilation
public class BuildConstants {

public static final String VERSION = "0.0.4";
public static final String VERSION = "0.0.5";
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

public class HelpCommand extends HelpCommandAbstract implements VelocitySubCommand {


@Override
public int onCommand(CommandContext<CommandSource> context) {
if (!handleCommandReturnMessageService.hasPermission(context, getPermissionNode())) return 0;
handleCommandReturnMessageService.handleCommandReturnMessage(context, "-------------------");
for (VelocitySubCommand forgeSubCommand : new CommandManager().getSubCommandList()) {
handleCommandReturnMessageService.handleCommandReturnMessage(context, forgeSubCommand.getUsage() + "---" + forgeSubCommand.getDescription());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import com.mojang.brigadier.context.CommandContext;
import com.velocitypowered.api.command.CommandSource;

import static com.github.theword.queqiao.tool.utils.Tool.handleCommandReturnMessageService;
import static com.github.theword.queqiao.tool.utils.Tool.websocketManager;

public class ReloadCommand extends ReloadCommandAbstract implements VelocitySubCommand {
@Override
public int onCommand(CommandContext<CommandSource> context) {
websocketManager.reloadWebsocket(true, context);
if (!handleCommandReturnMessageService.hasPermission(context, getPermissionNode())) return 0;
execute(context, false);
return Command.SINGLE_SUCCESS;
}
}
}
Loading

0 comments on commit 0a988ca

Please sign in to comment.