yay
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package com.smithy.celestianeconomy.command;
|
||||
|
||||
import com.smithy.celestianeconomy.CelestianUtils;
|
||||
import com.smithy.celestianeconomy.config.CelestianEconomyConfig;
|
||||
import com.smithy.celestianeconomy.sql.DatabaseManager;
|
||||
import com.mojang.brigadier.arguments.BoolArgumentType;
|
||||
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import net.minecraft.commands.arguments.EntityArgument;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class SetCommand {
|
||||
public static LiteralArgumentBuilder<CommandSourceStack> buildCommand(){
|
||||
return Commands.literal(CelestianEconomyConfig.getInstance().setCommandName)
|
||||
.requires((permission) -> permission.hasPermission(CelestianEconomyConfig.getInstance().opCommandsPermissionLevel))
|
||||
.then(
|
||||
Commands.argument("players", EntityArgument.players())
|
||||
.then(
|
||||
Commands.argument("amount", IntegerArgumentType.integer(0))
|
||||
.executes(e -> {
|
||||
int amount = IntegerArgumentType.getInteger(e, "amount");
|
||||
return setCommand(e, EntityArgument.getPlayers(e, "players").stream().toList(), amount);
|
||||
}))
|
||||
)
|
||||
.then(
|
||||
Commands.argument("amount", IntegerArgumentType.integer(0))
|
||||
.then(
|
||||
Commands.argument("shouldModifyAll", BoolArgumentType.bool())
|
||||
.executes(e -> {
|
||||
int amount = IntegerArgumentType.getInteger(e, "amount");
|
||||
boolean shouldModifyAll = BoolArgumentType.getBool(e, "shouldModifyAll");
|
||||
return setCommand(e, amount, shouldModifyAll);
|
||||
})
|
||||
)
|
||||
.executes(e -> {
|
||||
int amount = IntegerArgumentType.getInteger(e, "amount");
|
||||
return setCommand(e, amount, false);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
public static int setCommand(CommandContext<CommandSourceStack> ctx, Collection<ServerPlayer> players, int amount) {
|
||||
DatabaseManager dm = CelestianUtils.getDatabaseManager();
|
||||
players.forEach(player -> dm.setBalance(player.getStringUUID(), amount));
|
||||
ctx.getSource().sendSuccess(() -> Component.literal("Updated balance of " + players.size() + " players to " + amount), true);
|
||||
return players.size();
|
||||
}
|
||||
|
||||
public static int setCommand(CommandContext<CommandSourceStack> ctx, int amount, boolean shouldModifyAll) throws CommandSyntaxException {
|
||||
if (shouldModifyAll) {
|
||||
CelestianUtils.getDatabaseManager().setAllBalance(amount);
|
||||
ctx.getSource().sendSuccess(() -> Component.literal("All accounts balance to " + amount), true);
|
||||
} else {
|
||||
CelestianUtils.getDatabaseManager().setBalance(ctx.getSource().getPlayerOrException().getStringUUID(), amount);
|
||||
ctx.getSource().sendSuccess(() -> Component.literal("Updated your balance to " + amount), true);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user