为ammo命令添加选择器参数

This commit is contained in:
Light_Quanta 2024-05-16 19:07:35 +08:00
parent 65bb2de90c
commit 8a9f6ba3e7
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959

View file

@ -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;
}))))
);
}))))));
}
}