From 8a9f6ba3e76f176da4b6f292f057ad1990d82387 Mon Sep 17 00:00:00 2001 From: Light_Quanta Date: Thu, 16 May 2024 19:07:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BAammo=E5=91=BD=E4=BB=A4=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E9=80=89=E6=8B=A9=E5=99=A8=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mcreator/target/command/AmmoCommand.java | 80 ++++++++++--------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/src/main/java/net/mcreator/target/command/AmmoCommand.java b/src/main/java/net/mcreator/target/command/AmmoCommand.java index b1a17debe..42a1b8620 100644 --- a/src/main/java/net/mcreator/target/command/AmmoCommand.java +++ b/src/main/java/net/mcreator/target/command/AmmoCommand.java @@ -5,6 +5,7 @@ import com.mojang.brigadier.arguments.IntegerArgumentType; import net.mcreator.target.network.TargetModVariables; import net.mcreator.target.tools.GunInfo; import net.minecraft.commands.Commands; +import net.minecraft.commands.arguments.EntityArgument; import net.minecraft.network.chat.Component; import net.minecraftforge.event.RegisterCommandsEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; @@ -17,9 +18,8 @@ public class AmmoCommand { public static void registerCommand(RegisterCommandsEvent event) { // mojang你看看你写的是个牛魔Builder😅 event.getDispatcher().register(Commands.literal("ammo").requires(s -> s.hasPermission(4)) - .then(Commands.literal("get").then(Commands.argument("type", EnumArgument.enumArgument(GunInfo.Type.class)).executes(context -> { - var player = context.getSource().getPlayer(); - if (player == null) return 0; + .then(Commands.literal("get").then(Commands.argument("player", EntityArgument.player()).then(Commands.argument("type", EnumArgument.enumArgument(GunInfo.Type.class)).executes(context -> { + var player = EntityArgument.getPlayer(context, "player"); var type = context.getArgument("type", GunInfo.Type.class); @@ -33,50 +33,52 @@ public class AmmoCommand { ).orElse(0); context.getSource().sendSuccess(() -> Component.literal("Current " + type.name + " ammo: " + value), true); return 0; - }))) - .then(Commands.literal("set").then(Commands.argument("type", EnumArgument.enumArgument(GunInfo.Type.class)).then(Commands.argument("value", IntegerArgumentType.integer(0)).executes(context -> { - var player = context.getSource().getPlayer(); - if (player == null) return 0; - + })))) + .then(Commands.literal("set").then(Commands.argument("players", EntityArgument.players()).then(Commands.argument("type", EnumArgument.enumArgument(GunInfo.Type.class)).then(Commands.argument("value", IntegerArgumentType.integer(0)).executes(context -> { + var players = EntityArgument.getPlayers(context, "players"); var type = context.getArgument("type", GunInfo.Type.class); var value = IntegerArgumentType.getInteger(context, "value"); - player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { - switch (type) { - case HANDGUN -> capability.handgunAmmo = value; - case RIFLE -> capability.rifleAmmo = value; - case SHOTGUN -> capability.shotgunAmmo = value; - case SNIPER -> capability.sniperAmmo = value; - } - capability.syncPlayerVariables(player); - }); - context.getSource().sendSuccess(() -> Component.literal("Set " + type.name + " ammo to: " + value), true); + for (var player : players) { + player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { + switch (type) { + case HANDGUN -> capability.handgunAmmo = value; + case RIFLE -> capability.rifleAmmo = value; + case SHOTGUN -> capability.shotgunAmmo = value; + case SNIPER -> capability.sniperAmmo = value; + } + capability.syncPlayerVariables(player); + }); + } + + context.getSource().sendSuccess(() -> Component.literal("Set " + type.name + " ammo to " + value + " for " + players.size() + " player(s)"), true); return 0; - })))) - .then(Commands.literal("add").then(Commands.argument("type", EnumArgument.enumArgument(GunInfo.Type.class)).then(Commands.argument("value", IntegerArgumentType.integer(0)).executes(context -> { - var player = context.getSource().getPlayer(); - if (player == null) return 0; + }))))) + .then(Commands.literal("add").then(Commands.argument("players", EntityArgument.players()).then(Commands.argument("type", EnumArgument.enumArgument(GunInfo.Type.class)).then(Commands.argument("value", IntegerArgumentType.integer(0)).executes(context -> { + var players = EntityArgument.getPlayers(context, "players"); var type = context.getArgument("type", GunInfo.Type.class); var value = IntegerArgumentType.getInteger(context, "value"); - player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { - switch (type) { - case HANDGUN -> capability.handgunAmmo += value; - case RIFLE -> capability.rifleAmmo += value; - case SHOTGUN -> capability.shotgunAmmo += value; - case SNIPER -> capability.sniperAmmo += value; - } - // 迫真溢出检测 - if (capability.handgunAmmo < 0) capability.handgunAmmo = Integer.MAX_VALUE; - if (capability.rifleAmmo < 0) capability.rifleAmmo = Integer.MAX_VALUE; - if (capability.shotgunAmmo < 0) capability.shotgunAmmo = Integer.MAX_VALUE; - if (capability.sniperAmmo < 0) capability.sniperAmmo = Integer.MAX_VALUE; + for (var player : players) { + player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { + switch (type) { + case HANDGUN -> capability.handgunAmmo += value; + case RIFLE -> capability.rifleAmmo += value; + case SHOTGUN -> capability.shotgunAmmo += value; + case SNIPER -> capability.sniperAmmo += value; + } + // 迫真溢出检测 + if (capability.handgunAmmo < 0) capability.handgunAmmo = Integer.MAX_VALUE; + if (capability.rifleAmmo < 0) capability.rifleAmmo = Integer.MAX_VALUE; + if (capability.shotgunAmmo < 0) capability.shotgunAmmo = Integer.MAX_VALUE; + if (capability.sniperAmmo < 0) capability.sniperAmmo = Integer.MAX_VALUE; - capability.syncPlayerVariables(player); - }); - context.getSource().sendSuccess(() -> Component.literal("Added " + type.name + " ammo of amount " + value), true); + capability.syncPlayerVariables(player); + }); + } + + context.getSource().sendSuccess(() -> Component.literal("Added " + type.name + " ammo of amount " + value + " for " + players.size() + " player(s)"), true); return 0; - })))) - ); + })))))); } }