注册命令

This commit is contained in:
Light_Quanta 2025-03-28 20:48:33 +08:00
parent 250d021cd9
commit cadbdd079a
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
3 changed files with 105 additions and 0 deletions

View file

@ -0,0 +1,62 @@
package com.atsuishio.superbwarfare.command;
import com.atsuishio.superbwarfare.tools.AmmoType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.network.chat.Component;
import net.neoforged.neoforge.server.command.EnumArgument;
public class AmmoCommand {
public static LiteralArgumentBuilder<CommandSourceStack> get() {
// mojang你看看你写的是个牛魔Builder😅
return Commands.literal("ammo").requires(s -> s.hasPermission(0))
.then(Commands.literal("get").then(Commands.argument("player", EntityArgument.player()).then(Commands.argument("type", EnumArgument.enumArgument(AmmoType.class)).executes(context -> {
var player = EntityArgument.getPlayer(context, "player");
var source = context.getSource();
// 权限不足时只允许玩家查询自己的弹药数量
if (source.isPlayer() && !source.hasPermission(2)) {
if (source.getPlayer() != null && !source.getPlayer().getUUID().equals(player.getUUID())) {
context.getSource().sendFailure(Component.translatable("commands.ammo.no_permission"));
return 0;
}
}
var type = context.getArgument("type", AmmoType.class);
var value = type.get(player);
context.getSource().sendSuccess(() -> Component.translatable("commands.ammo.get", Component.translatable(type.translatableKey), value), true);
return 0;
}))))
.then(Commands.literal("set").requires(s -> s.hasPermission(2)).then(Commands.argument("players", EntityArgument.players()).then(Commands.argument("type", EnumArgument.enumArgument(AmmoType.class)).then(Commands.argument("value", IntegerArgumentType.integer(0)).executes(context -> {
var players = EntityArgument.getPlayers(context, "players");
var type = context.getArgument("type", AmmoType.class);
var value = IntegerArgumentType.getInteger(context, "value");
for (var player : players) {
type.set(player, value);
}
context.getSource().sendSuccess(() -> Component.translatable("commands.ammo.set", Component.translatable(type.translatableKey), value, players.size()), true);
return 0;
})))))
.then(Commands.literal("add").requires(s -> s.hasPermission(2)).then(Commands.argument("players", EntityArgument.players()).then(Commands.argument("type", EnumArgument.enumArgument(AmmoType.class)).then(Commands.argument("value", IntegerArgumentType.integer(0)).executes(context -> {
var players = EntityArgument.getPlayers(context, "players");
var type = context.getArgument("type", AmmoType.class);
var value = IntegerArgumentType.getInteger(context, "value");
for (var player : players) {
type.add(player, value);
}
context.getSource().sendSuccess(() -> Component.translatable("commands.ammo.add", Component.translatable(type.translatableKey), value, players.size()), true);
return 0;
})))));
}
}

View file

@ -0,0 +1,19 @@
package com.atsuishio.superbwarfare.command;
import com.atsuishio.superbwarfare.ModUtils;
import net.minecraft.commands.Commands;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.event.RegisterCommandsEvent;
@EventBusSubscriber(modid = ModUtils.MODID)
public class CommandRegister {
@SubscribeEvent
public static void registerCommand(RegisterCommandsEvent event) {
var command = Commands.literal("sbw");
command.then(AmmoCommand.get());
command.then(ConfigCommand.get());
event.getDispatcher().register(command);
}
}

View file

@ -0,0 +1,24 @@
package com.atsuishio.superbwarfare.command;
import com.atsuishio.superbwarfare.config.server.ExplosionConfig;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
public class ConfigCommand {
public static LiteralArgumentBuilder<CommandSourceStack> get() {
return Commands.literal("config").requires(s -> s.hasPermission(0))
.then(Commands.literal("explosionDestroy").requires(s -> s.hasPermission(2)).then(Commands.argument("value", BoolArgumentType.bool()).executes(context -> {
var value = BoolArgumentType.getBool(context, "value");
ExplosionConfig.EXPLOSION_DESTROY.set(value);
context.getSource().sendSuccess(() -> Component.translatable(value ? "commands.config.explosion_destroy.enabled" : "commands.config.explosion_destroy.disabled"), true);
return 0;
})));
}
}