|
| 1 | +/* |
| 2 | + * Numismatics |
| 3 | + * Copyright (c) 2023-2024 The Railways Team |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Lesser General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +package dev.ithundxr.createnumismatics.registry.commands; |
| 20 | + |
| 21 | +import com.mojang.brigadier.builder.ArgumentBuilder; |
| 22 | +import com.mojang.brigadier.context.CommandContext; |
| 23 | +import com.simibubi.create.foundation.utility.Components; |
| 24 | +import dev.ithundxr.createnumismatics.Numismatics; |
| 25 | +import dev.ithundxr.createnumismatics.content.backend.BankAccount; |
| 26 | +import dev.ithundxr.createnumismatics.content.backend.BankAccount.Type; |
| 27 | +import dev.ithundxr.createnumismatics.content.backend.Coin; |
| 28 | +import dev.ithundxr.createnumismatics.registry.commands.arguments.EnumArgument; |
| 29 | +import net.minecraft.commands.CommandSourceStack; |
| 30 | + |
| 31 | +import java.util.Set; |
| 32 | +import java.util.UUID; |
| 33 | + |
| 34 | +import static com.mojang.brigadier.arguments.IntegerArgumentType.getInteger; |
| 35 | +import static com.mojang.brigadier.arguments.IntegerArgumentType.integer; |
| 36 | +import static net.minecraft.commands.Commands.argument; |
| 37 | +import static net.minecraft.commands.Commands.literal; |
| 38 | + |
| 39 | +public class PayAllCommand { |
| 40 | + public static ArgumentBuilder<CommandSourceStack, ?> register() { |
| 41 | + return literal("payall") |
| 42 | + .requires(cs -> cs.hasPermission(2)) |
| 43 | + .then(argument("amount", integer(0)) |
| 44 | + .executes(ctx -> execute(ctx, getInteger(ctx, "amount"))) |
| 45 | + .then(argument("coin", EnumArgument.enumArgument(Coin.class)) |
| 46 | + .executes(ctx -> execute(ctx, getInteger(ctx, "amount"), ctx.getArgument("coin", Coin.class))) |
| 47 | + ) |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + private static int execute(CommandContext<CommandSourceStack> ctx, int amount) { |
| 52 | + return execute(ctx, amount, Coin.SPUR); |
| 53 | + } |
| 54 | + |
| 55 | + private static int execute(CommandContext<CommandSourceStack> ctx, int amount, Coin coin) { |
| 56 | + int spurValue = coin.toSpurs(amount); |
| 57 | + int sum = 0; |
| 58 | + |
| 59 | + Set<UUID> uuids = Numismatics.BANK.accounts.keySet(); |
| 60 | + |
| 61 | + for (UUID uuid: uuids) { |
| 62 | + BankAccount account = Numismatics.BANK.getAccount(uuid); |
| 63 | + |
| 64 | + if (account != null && account.type == Type.PLAYER) { |
| 65 | + account.deposit(spurValue); |
| 66 | + |
| 67 | + sum ++; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + int finalSum = sum; |
| 72 | + ctx.getSource().sendSuccess(() -> Components.literal("Paid "+amount+" "+coin.getName(amount)+" to "+ finalSum +" account(s)."), true); |
| 73 | + |
| 74 | + return sum; |
| 75 | + } |
| 76 | +} |
0 commit comments