Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 /ping的子命令与carpet-extra权限设置冲突 #123

Closed
wants to merge 7 commits into from
11 changes: 9 additions & 2 deletions src/main/java/club/mcams/carpet/commands/RegisterCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public static void registerCommands(

LeaderCommandRegistry.register(dispatcher);

PingCommandRegistry.register(dispatcher);

GetSaveSizeCommandRegistry.register(dispatcher);

GetSystemInfoCommandRegistry.register(dispatcher);
Expand All @@ -79,4 +77,13 @@ public static void registerCommands(

GetPlayerSkullCommandRegistry.register(dispatcher);
}

public static void registerPostCommands(
CommandDispatcher<ServerCommandSource> dispatcher
//#if MC>=11900
//$$ , final CommandRegistryAccess commandBuildContext
//#endif
) {
PingCommandRegistry.register(dispatcher);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package club.mcams.carpet.commands.commandNodes;

public class ExtensiveLiteralArgumentBuilder {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package club.mcams.carpet.commands.commandNodes;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.RedirectModifier;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.LiteralCommandNode;

import java.util.function.Predicate;

public class ExtensiveLiteralCommandNode<S> extends LiteralCommandNode<S> {
private final Predicate<S> command_requirement;

public ExtensiveLiteralCommandNode(String literal, Command<S> command, Predicate<S> requirement, CommandNode<S> redirect, RedirectModifier<S> modifier, boolean forks) {
super(literal, command, requirement, redirect, modifier, forks);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
package club.mcams.carpet.commands.rule.commandCustomCommandPermissionLevel;

import club.mcams.carpet.AmsServerSettings;
import club.mcams.carpet.commands.suggestionProviders.ListSuggestionProvider;
import club.mcams.carpet.commands.suggestionProviders.LiteralCommandSuggestionProvider;
import club.mcams.carpet.commands.suggestionProviders.SetSuggestionProvider;
import club.mcams.carpet.mixin.rule.commandCustomCommandPermissionLevel.CommandNodeInvoker;
import club.mcams.carpet.translations.Translator;
import club.mcams.carpet.utils.CommandHelper;
import club.mcams.carpet.utils.Messenger;
Expand All @@ -30,18 +34,19 @@
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;

import com.mojang.brigadier.tree.CommandNode;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Formatting;

import java.util.HashMap;
import java.util.Map;

public class CustomCommandPermissionLevelRegistry {
private static final Translator translator = new Translator("command.customCommandPermissionLevel");
private static final String needRestartServerMessage = translator.tr("need_restart_server_msg").getString();
private static final String MSG_HEAD = "<customCommandPermissionLevel> ";
public static final Map<String, Integer> COMMAND_PERMISSION_MAP = new HashMap<>();

Expand All @@ -50,8 +55,9 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
CommandManager.literal("customCommandPermissionLevel")
.requires(source -> CommandHelper.canUseCommand(source, AmsServerSettings.commandCustomCommandPermissionLevel))
.then(CommandManager.literal("set")
.then(CommandManager.argument("command", StringArgumentType.string())
.then(CommandManager.argument("command", StringArgumentType.string()).suggests(new LiteralCommandSuggestionProvider())
.then(CommandManager.argument("permissionLevel", IntegerArgumentType.integer())
.suggests(ListSuggestionProvider.of(CommandHelper.permissionLevels))
.executes(context -> set(
context.getSource().getServer(),
context.getSource().getPlayer(),
Expand All @@ -60,6 +66,7 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
)))))
.then(CommandManager.literal("remove")
.then(CommandManager.argument("command", StringArgumentType.string())
.suggests(SetSuggestionProvider.of(COMMAND_PERMISSION_MAP.keySet()))
.executes(context -> remove(
context.getSource().getServer(),
context.getSource().getPlayer(),
Expand All @@ -73,29 +80,36 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
.executes(context -> help(context.getSource().getPlayer()))));
}

private static int set(MinecraftServer server, PlayerEntity player, String command, int permissionLevel) {
private static int set(MinecraftServer server, ServerPlayerEntity player, String command, int permissionLevel) {
if (COMMAND_PERMISSION_MAP.containsKey(command)) {
int oldPermissionLevel = COMMAND_PERMISSION_MAP.get(command);
player.sendMessage(
Messenger.s(
String.format("%s%s %d -> %d", MSG_HEAD, command, oldPermissionLevel, permissionLevel)
).formatted(Formatting.GREEN), false
);
sendNeedRestartServerMessage(player);
} else {
player.sendMessage(
Messenger.s(
String.format("%s+ %s/%d", MSG_HEAD, command, permissionLevel)
).formatted(Formatting.GREEN), false
);
sendNeedRestartServerMessage(player);
}
COMMAND_PERMISSION_MAP.put(command, permissionLevel);
saveToJson(server);
setPermission(server,player, command, permissionLevel);
return 1;
}

private static int remove(MinecraftServer server, PlayerEntity player, String command) {
@SuppressWarnings("unchecked")
private static void setPermission(MinecraftServer server, ServerPlayerEntity player, String command, int permissionLevel) {
CommandDispatcher<ServerCommandSource> dispatcher = server.getCommandManager().getDispatcher();
CommandNode<ServerCommandSource> target = dispatcher.getRoot().getChild(command);
((CommandNodeInvoker<ServerCommandSource>)target).setRequirement(source -> source.hasPermissionLevel(permissionLevel));
CommandHelper.notifyPlayersCommandsChanged(player);
}

private static int remove(MinecraftServer server, ServerPlayerEntity player, String command) {
if (COMMAND_PERMISSION_MAP.containsKey(command)) {
COMMAND_PERMISSION_MAP.remove(command);
saveToJson(server);
Expand All @@ -104,7 +118,7 @@ private static int remove(MinecraftServer server, PlayerEntity player, String co
String.format("%s- %s", MSG_HEAD, command)
).formatted(Formatting.RED, Formatting.ITALIC), false
);
sendNeedRestartServerMessage(player);
CommandHelper.notifyPlayersCommandsChanged(player);
} else {
player.sendMessage(
Messenger.s(
Expand All @@ -115,15 +129,17 @@ private static int remove(MinecraftServer server, PlayerEntity player, String co
return 1;
}

private static int removeAll(MinecraftServer server, PlayerEntity player) {
COMMAND_PERMISSION_MAP.clear();
saveToJson(server);
private static int removeAll(MinecraftServer server, ServerPlayerEntity player) {
if (!COMMAND_PERMISSION_MAP.isEmpty()) {
COMMAND_PERMISSION_MAP.clear();
saveToJson(server);
}
player.sendMessage(
Messenger.s(
MSG_HEAD + translator.tr("removeAll").getString()
).formatted(Formatting.RED, Formatting.ITALIC), false
);
sendNeedRestartServerMessage(player);
CommandHelper.notifyPlayersCommandsChanged(player);
return 1;
}

Expand Down Expand Up @@ -162,13 +178,4 @@ private static void saveToJson(MinecraftServer server) {
final String CONFIG_PATH = CustomCommandPermissionLevelConfig.getPath(server);
CustomCommandPermissionLevelConfig.saveToJson(COMMAND_PERMISSION_MAP, CONFIG_PATH);
}

// TODO: 热重载咕咕咕
private static void sendNeedRestartServerMessage(PlayerEntity player) {
player.sendMessage(
Messenger.s(
MSG_HEAD + needRestartServerMessage
).formatted(Formatting.YELLOW), false
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,15 @@ public class PingCommandRegistry {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(
CommandManager.literal("ping")
.requires(source -> CommandHelper.canUseCommand(source, AmsServerSettings.commandPacketInternetGroper))
.then(argument("targetIpOrDomainName", StringArgumentType.string())
.then(literal("stop").requires(source -> CommandHelper.canUseCommand(source, AmsServerSettings.commandPacketInternetGroper)).executes(context -> stopPing(context.getSource().getPlayer())))
.then(literal("help").requires(source -> CommandHelper.canUseCommand(source, AmsServerSettings.commandPacketInternetGroper)).executes(context -> help(context.getSource().getPlayer())))
.then(argument("targetIpOrDomainName", StringArgumentType.string()).requires(source -> CommandHelper.canUseCommand(source, AmsServerSettings.commandPacketInternetGroper))
.then(argument("pingQuantity", IntegerArgumentType.integer())
.executes(context -> executePing(
context.getSource().getPlayer(),
StringArgumentType.getString(context, "targetIpOrDomainName"),
IntegerArgumentType.getInteger(context, "pingQuantity")
))))
.then(literal("stop").executes(context -> stopPing(context.getSource().getPlayer())))
.then(literal("help").executes(context -> help(context.getSource().getPlayer())))
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package club.mcams.carpet.commands.suggestionProviders;

import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;

import net.minecraft.server.command.ServerCommandSource;

import java.util.List;
import java.util.concurrent.CompletableFuture;

public class ListSuggestionProvider<E> implements SuggestionProvider<ServerCommandSource> {
private final List<E> options;

public ListSuggestionProvider(List<E> options) {
this.options = options;
}

public static ListSuggestionProvider<?> of(List<?> options) {
return new ListSuggestionProvider<>(options);
}

@Override
public CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) {
options.forEach(option -> builder.suggest(option.toString()));
return builder.buildFuture();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* This file is part of the Carpet AMS Addition project, licensed under the
* GNU Lesser General Public License v3.0
*
* Copyright (C) 2024 A Minecraft Server and contributors
*
* Carpet AMS Addition is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Carpet AMS Addition is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Carpet AMS Addition. If not, see <https://www.gnu.org/licenses/>.
*/

package club.mcams.carpet.commands.suggestionProviders;

import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;

import net.minecraft.server.command.ServerCommandSource;

import java.util.concurrent.CompletableFuture;

public class LiteralCommandSuggestionProvider implements SuggestionProvider<ServerCommandSource> {
@Override
public CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) {
context.getRootNode().getChildren().forEach(node -> {
if (node != null && node.getName() != null) {
builder.suggest(node.getName());
}
});
return builder.buildFuture();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package club.mcams.carpet.commands.suggestionProviders;

import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;

import net.minecraft.server.command.ServerCommandSource;

import java.util.Set;
import java.util.concurrent.CompletableFuture;

public class SetSuggestionProvider<E> implements SuggestionProvider<ServerCommandSource> {
private final Set<E> options;

public SetSuggestionProvider(Set<E> options) {
this.options = options;
}

public static SetSuggestionProvider<?> of(Set<?> options) {
return new SetSuggestionProvider<>(options);
}

@Override
public CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) {
options.forEach(option -> builder.suggest(option.toString()));
return builder.buildFuture();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,58 @@

package club.mcams.carpet.mixin.carpet;

import club.mcams.carpet.utils.compat.DummyClass;
import carpet.CarpetServer;

import club.mcams.carpet.commands.RegisterCommands;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
//#if MC>=11900
//$$ import net.minecraft.command.CommandRegistryAccess;
//#endif

import org.spongepowered.asm.mixin.Mixin;

import top.byteeeee.annotationtoolbox.annotation.GameVersion;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(value = CarpetServer.class, remap = false)
public abstract class CarpetServerMixin {
@Inject(
method =
//#if MC<=11605
//$$ "registerCarpetCommands",
//#elseif MC<11904
"registerCarpetCommands(Lcom/mojang/brigadier/CommandDispatcher;Lnet/minecraft/server/command/CommandManager$RegistrationEnvironment;)V",
//#else
//$$ "registerCarpetCommands(Lcom/mojang/brigadier/CommandDispatcher;Lnet/minecraft/server/command/CommandManager$RegistrationEnvironment;Lnet/minecraft/command/CommandRegistryAccess;)V",
//#endif
at = @At("TAIL")
)
private static void registerCarpetCommands(CommandDispatcher<ServerCommandSource> dispatcher,
//#if MC>11605
CommandManager.RegistrationEnvironment environment,
//#endif
//#if MC>=11904
//$$ final CommandRegistryAccess commandBuildContext,
//#endif
CallbackInfo ci) {
RegisterCommands.registerPostCommands(dispatcher
//#if MC>=11904
//$$ , commandBuildContext
//#endif
);
}


@GameVersion(version = "Minecraft >= 1.19.4", desc = "Just a fix for https://github.com/gnembon/fabric-carpet/issues/1908")
@Mixin(DummyClass.class)
public class CarpetServerMixin {}
//#if MC>=11904
//$$ @Inject(method = "onServerClosed(Lnet/minecraft/server/MinecraftServer;)V", at = @At("HEAD"), cancellable = true)
//$$ private static void onlyCallIfServerNotnull(MinecraftServer server, CallbackInfo ci) {
//$$ if (server == null) {
//$$ ci.cancel();
//$$ }
//$$ }
//#endif
}
Loading
Loading