Skip to content

Commit e3ae9af

Browse files
authored
Add /payall command (#85)
1 parent 7c48427 commit e3ae9af

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

common/src/main/java/dev/ithundxr/createnumismatics/registry/NumismaticsCommands.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222
import com.mojang.brigadier.tree.CommandNode;
2323
import com.mojang.brigadier.tree.LiteralCommandNode;
2424
import com.simibubi.create.infrastructure.command.AllCommands;
25-
import dev.ithundxr.createnumismatics.registry.commands.DeductCommand;
26-
import dev.ithundxr.createnumismatics.registry.commands.PayCommand;
27-
import dev.ithundxr.createnumismatics.registry.commands.ReloadCommandsCommand;
28-
import dev.ithundxr.createnumismatics.registry.commands.ViewCommand;
25+
import dev.ithundxr.createnumismatics.registry.commands.*;
2926
import dev.ithundxr.createnumismatics.util.Utils;
3027
import net.minecraft.commands.CommandSourceStack;
3128

@@ -40,6 +37,7 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher, bo
4037
.then(PayCommand.register())
4138
.then(DeductCommand.register())
4239
.then(ViewCommand.register())
40+
.then(PayAllCommand.register())
4341
//.then(ClearCasingCacheCommand.register())
4442
//.then(SplitTrainCommand.register())
4543
//.then(TrainInfoCommand.register());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)