9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2025-12-29 11:59:17 +00:00

refactor: refactor command

This commit is contained in:
MC_XiaoHei
2025-08-24 15:37:22 +08:00
parent e7086a8882
commit a994f8bfc8
4 changed files with 56 additions and 44 deletions

View File

@@ -1,11 +1,8 @@
package org.leavesmc.leaves.command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.server.MinecraftServer;
public class LiteralNode extends CommandNode {
@@ -17,19 +14,4 @@ public class LiteralNode extends CommandNode {
protected ArgumentBuilder<CommandSourceStack, ?> compileBase() {
return Commands.literal(name);
}
@SuppressWarnings("unchecked")
public void register() {
MinecraftServer.getServer()
.getCommands()
.getDispatcher()
.register((LiteralArgumentBuilder<CommandSourceStack>) compile());
}
public void unregister() {
CommandDispatcher<CommandSourceStack> dispatcher = MinecraftServer.getServer()
.getCommands()
.getDispatcher();
dispatcher.getRoot().removeCommand(name);
}
}

View File

@@ -0,0 +1,50 @@
package org.leavesmc.leaves.command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.server.MinecraftServer;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import static org.leavesmc.leaves.command.CommandUtils.registerPermissions;
public abstract class RootNode extends LiteralNode {
private final String permissionBase;
public RootNode(String name, String permissionBase) {
super(name);
this.permissionBase = permissionBase;
}
@Override
protected ArgumentBuilder<CommandSourceStack, ?> compile() {
registerPermissions(permissionBase, this.children);
return super.compile();
}
protected static boolean hasPermission(@NotNull CommandSender sender, String subcommand, String permissionBase) {
return sender.hasPermission(permissionBase) || sender.hasPermission(permissionBase + "." + subcommand);
}
@Override
public boolean requires(@NotNull CommandSourceStack source) {
return children.stream().anyMatch(child -> child.requires(source));
}
@SuppressWarnings("unchecked")
public void register() {
MinecraftServer.getServer()
.getCommands()
.getDispatcher()
.register((LiteralArgumentBuilder<CommandSourceStack>) compile());
}
public void unregister() {
CommandDispatcher<CommandSourceStack> dispatcher = MinecraftServer.getServer()
.getCommands()
.getDispatcher();
dispatcher.getRoot().removeCommand(name);
}
}

View File

@@ -4,7 +4,7 @@ import com.mojang.brigadier.builder.ArgumentBuilder;
import net.minecraft.commands.CommandSourceStack;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.leavesmc.leaves.command.LiteralNode;
import org.leavesmc.leaves.command.RootNode;
import org.leavesmc.leaves.command.bot.subcommands.ActionCommand;
import org.leavesmc.leaves.command.bot.subcommands.ConfigCommand;
import org.leavesmc.leaves.command.bot.subcommands.CreateCommand;
@@ -15,12 +15,12 @@ import org.leavesmc.leaves.command.bot.subcommands.SaveCommand;
import static org.leavesmc.leaves.command.CommandUtils.registerPermissions;
public class BotCommand extends LiteralNode {
public class BotCommand extends RootNode {
public static final BotCommand INSTANCE = new BotCommand();
private static final String PERM_BASE = "bukkit.command.bot";
private BotCommand() {
super("bot");
super("bot", PERM_BASE);
this.children(
ListCommand::new,
ConfigCommand::new,
@@ -38,11 +38,6 @@ public class BotCommand extends LiteralNode {
return super.compile();
}
@Override
public boolean requires(@NotNull CommandSourceStack source) {
return children.stream().anyMatch(child -> child.requires(source));
}
public static boolean hasPermission(@NotNull CommandSender sender) {
return sender.hasPermission(PERM_BASE);
}

View File

@@ -1,10 +1,8 @@
package org.leavesmc.leaves.command.leaves;
import com.mojang.brigadier.builder.ArgumentBuilder;
import net.minecraft.commands.CommandSourceStack;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.leavesmc.leaves.command.LiteralNode;
import org.leavesmc.leaves.command.RootNode;
import org.leavesmc.leaves.command.leaves.subcommands.BlockUpdateCommand;
import org.leavesmc.leaves.command.leaves.subcommands.ConfigCommand;
import org.leavesmc.leaves.command.leaves.subcommands.CounterCommand;
@@ -12,14 +10,12 @@ import org.leavesmc.leaves.command.leaves.subcommands.ReloadCommand;
import org.leavesmc.leaves.command.leaves.subcommands.ReportCommand;
import org.leavesmc.leaves.command.leaves.subcommands.UpdateCommand;
import static org.leavesmc.leaves.command.CommandUtils.registerPermissions;
public class LeavesCommand extends LiteralNode {
public class LeavesCommand extends RootNode {
public static final LeavesCommand INSTANCE = new LeavesCommand();
private static final String PERM_BASE = "bukkit.command.leaves";
private LeavesCommand() {
super("leaves");
super("leaves", PERM_BASE);
children(
BlockUpdateCommand::new,
ConfigCommand::new,
@@ -30,17 +26,6 @@ public class LeavesCommand extends LiteralNode {
);
}
@Override
protected ArgumentBuilder<CommandSourceStack, ?> compile() {
registerPermissions(PERM_BASE, this.children);
return super.compile();
}
@Override
public boolean requires(@NotNull CommandSourceStack source) {
return children.stream().anyMatch(child -> child.requires(source));
}
public static boolean hasPermission(@NotNull CommandSender sender, String subcommand) {
return sender.hasPermission(PERM_BASE) || sender.hasPermission(PERM_BASE + "." + subcommand);
}