|
|
|
|
@ -1,10 +1,8 @@ |
|
|
|
|
package xyz.soukup.ecoCraftCore.commands; |
|
|
|
|
|
|
|
|
|
import com.mojang.brigadier.LiteralMessage; |
|
|
|
|
import com.mojang.brigadier.arguments.FloatArgumentType; |
|
|
|
|
import com.mojang.brigadier.arguments.StringArgumentType; |
|
|
|
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder; |
|
|
|
|
import com.mojang.brigadier.builder.RequiredArgumentBuilder; |
|
|
|
|
import com.mojang.brigadier.context.CommandContext; |
|
|
|
|
import com.mojang.brigadier.exceptions.CommandSyntaxException; |
|
|
|
|
import io.papermc.paper.command.brigadier.CommandSourceStack; |
|
|
|
|
@ -20,71 +18,66 @@ import xyz.soukup.ecoCraftCore.utilities.PHHM; |
|
|
|
|
|
|
|
|
|
@SuppressWarnings("UnstableApiUsage") |
|
|
|
|
public class MoneyCommand { |
|
|
|
|
public static LiteralArgumentBuilder<CommandSourceStack> createCommand() { |
|
|
|
|
|
|
|
|
|
RequiredArgumentBuilder<CommandSourceStack, PlayerSelectorArgumentResolver> playerArgument = Commands.argument("player", ArgumentTypes.player()); |
|
|
|
|
RequiredArgumentBuilder<CommandSourceStack, Float> amountArgument = Commands.argument("amount", FloatArgumentType.floatArg(0.1F)); |
|
|
|
|
RequiredArgumentBuilder<CommandSourceStack, String> ownerArgument = Commands.argument("owner", StringArgumentType.word()); |
|
|
|
|
RequiredArgumentBuilder<CommandSourceStack, String> typeArgument = Commands.argument("type", StringArgumentType.word()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static LiteralArgumentBuilder<CommandSourceStack> createCommand() { |
|
|
|
|
// 1. Send Branch
|
|
|
|
|
LiteralArgumentBuilder<CommandSourceStack> send = Commands.literal("send") |
|
|
|
|
.requires(source -> source.getSender() instanceof Player) |
|
|
|
|
.then(playerArgument.then(amountArgument.executes(MoneyCommand::sendMoney))); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RequiredArgumentBuilder<CommandSourceStack, Float> selfGive = amountArgument |
|
|
|
|
.requires(source -> source.getSender() instanceof Player) |
|
|
|
|
.executes(MoneyCommand::giveMoneySelf); |
|
|
|
|
|
|
|
|
|
RequiredArgumentBuilder<CommandSourceStack, PlayerSelectorArgumentResolver> playerGive = playerArgument |
|
|
|
|
.then(amountArgument.executes(MoneyCommand::giveMoneyPlayer)); |
|
|
|
|
|
|
|
|
|
RequiredArgumentBuilder<CommandSourceStack, String> giveOther = typeArgument.then(ownerArgument.then(amountArgument.executes(MoneyCommand::giveMoneyOther))); |
|
|
|
|
.then(Commands.argument("player", ArgumentTypes.player()) |
|
|
|
|
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F)) |
|
|
|
|
.executes(MoneyCommand::sendMoney))); |
|
|
|
|
|
|
|
|
|
// 2. Give Branch
|
|
|
|
|
LiteralArgumentBuilder<CommandSourceStack> give = Commands.literal("give") |
|
|
|
|
.then(selfGive) |
|
|
|
|
.then(playerGive) |
|
|
|
|
.then(giveOther); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RequiredArgumentBuilder<CommandSourceStack, Float> removeSelf = amountArgument |
|
|
|
|
// Case: /money give <player> <amount>
|
|
|
|
|
.then(Commands.argument("player", ArgumentTypes.player()) |
|
|
|
|
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F)) |
|
|
|
|
.executes(MoneyCommand::giveMoneyPlayer))) |
|
|
|
|
// Case: /money give <amount> (Self)
|
|
|
|
|
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F)) |
|
|
|
|
.requires(source -> source.getSender() instanceof Player) |
|
|
|
|
.executes(MoneyCommand::removeMoneySelf); |
|
|
|
|
|
|
|
|
|
RequiredArgumentBuilder<CommandSourceStack, PlayerSelectorArgumentResolver> removePlayer = playerArgument |
|
|
|
|
.then(amountArgument.executes(MoneyCommand::removeMoneyPlayer)); |
|
|
|
|
|
|
|
|
|
RequiredArgumentBuilder<CommandSourceStack, String> removeOther = typeArgument |
|
|
|
|
.then(ownerArgument |
|
|
|
|
.then(amountArgument.executes(MoneyCommand::removeMoneyOther))); |
|
|
|
|
|
|
|
|
|
.executes(MoneyCommand::giveMoneySelf)) |
|
|
|
|
// Case: /money give <type> <owner> <amount>
|
|
|
|
|
.then(Commands.argument("type", StringArgumentType.word()) |
|
|
|
|
.then(Commands.argument("owner", StringArgumentType.word()) |
|
|
|
|
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F)) |
|
|
|
|
.executes(MoneyCommand::giveMoneyOther)))); |
|
|
|
|
|
|
|
|
|
// 3. Remove Branch
|
|
|
|
|
LiteralArgumentBuilder<CommandSourceStack> remove = Commands.literal("remove") |
|
|
|
|
.then(removeSelf) |
|
|
|
|
.then(removePlayer) |
|
|
|
|
.then(removeOther); |
|
|
|
|
|
|
|
|
|
// Case: /money remove <player> <amount>
|
|
|
|
|
.then(Commands.argument("player", ArgumentTypes.player()) |
|
|
|
|
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F)) |
|
|
|
|
.executes(MoneyCommand::removeMoneyPlayer))) |
|
|
|
|
// Case: /money remove <amount> (Self)
|
|
|
|
|
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F)) |
|
|
|
|
.requires(source -> source.getSender() instanceof Player) |
|
|
|
|
.executes(MoneyCommand::removeMoneySelf)) |
|
|
|
|
// Case: /money remove <type> <owner> <amount>
|
|
|
|
|
.then(Commands.argument("type", StringArgumentType.word()) |
|
|
|
|
.then(Commands.argument("owner", StringArgumentType.word()) |
|
|
|
|
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F)) |
|
|
|
|
.executes(MoneyCommand::removeMoneyOther)))); |
|
|
|
|
|
|
|
|
|
// Main command: /money
|
|
|
|
|
return Commands.literal("money") |
|
|
|
|
.then(send) |
|
|
|
|
.then(give) |
|
|
|
|
.then(remove) |
|
|
|
|
.executes(MoneyCommand::getBalance); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static int getBalance(CommandContext<CommandSourceStack> context){ |
|
|
|
|
private static int getBalance(CommandContext<CommandSourceStack> context) { |
|
|
|
|
CommandSender commandSender = context.getSource().getSender(); |
|
|
|
|
if (!(commandSender instanceof Player player)){ |
|
|
|
|
if (!(commandSender instanceof Player player)) { |
|
|
|
|
Messages.send(commandSender, "generic.error.not-player"); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
Account account = Account.getOrCreate(player); |
|
|
|
|
Messages.send(player, "money.balance.self", PHHM.get(account)); |
|
|
|
|
return 1; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static int sendMoney(CommandContext<CommandSourceStack> context) throws CommandSyntaxException { |
|
|
|
|
PlayerSelectorArgumentResolver receiverResolver = context.getArgument("player", PlayerSelectorArgumentResolver.class); |
|
|
|
|
Player receiver = receiverResolver.resolve(context.getSource()).getFirst(); |
|
|
|
|
@ -94,12 +87,12 @@ public class MoneyCommand { |
|
|
|
|
|
|
|
|
|
Account senderAccount = Account.getOrCreate(sender); |
|
|
|
|
|
|
|
|
|
if (amount > senderAccount.getBalance()){ |
|
|
|
|
if (amount > senderAccount.getBalance()) { |
|
|
|
|
Messages.send(sender, "generic.error.no-funds"); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Transaction transaction = new Transaction(amount,"player", sender.getName(), "player", receiver.getName(), "player"); |
|
|
|
|
Transaction transaction = new Transaction(amount, "player", sender.getName(), "player", receiver.getName(), "player"); |
|
|
|
|
transaction.process(); |
|
|
|
|
|
|
|
|
|
Messages.send(sender, "money.send.player", PHHM.get(transaction)); |
|
|
|
|
@ -110,82 +103,75 @@ public class MoneyCommand { |
|
|
|
|
|
|
|
|
|
private static int giveMoneyPlayer(CommandContext<CommandSourceStack> context) throws CommandSyntaxException { |
|
|
|
|
CommandSender commandSender = context.getSource().getSender(); |
|
|
|
|
|
|
|
|
|
PlayerSelectorArgumentResolver receiverResolver = context.getArgument("player", PlayerSelectorArgumentResolver.class); |
|
|
|
|
Player receiver = receiverResolver.resolve(context.getSource()).getFirst(); |
|
|
|
|
|
|
|
|
|
Float amount = context.getArgument("amount", Float.class); |
|
|
|
|
|
|
|
|
|
Transaction transaction = new Transaction(amount,"admin", commandSender.getName(), "player", receiver.getName(), "admin"); |
|
|
|
|
Transaction transaction = new Transaction(amount, "admin", commandSender.getName(), "player", receiver.getName(), "admin"); |
|
|
|
|
transaction.process(); |
|
|
|
|
|
|
|
|
|
Messages.send(commandSender, "money.give.player", PHHM.get(transaction)); |
|
|
|
|
|
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static int giveMoneySelf(CommandContext<CommandSourceStack> context){ |
|
|
|
|
private static int giveMoneySelf(CommandContext<CommandSourceStack> context) { |
|
|
|
|
Player player = (Player) context.getSource().getSender(); |
|
|
|
|
Float amount = context.getArgument("amount", Float.class); |
|
|
|
|
|
|
|
|
|
Transaction transaction = new Transaction(amount,"admin", player.getName(), "player", player.getName(), "admin"); |
|
|
|
|
Transaction transaction = new Transaction(amount, "admin", player.getName(), "player", player.getName(), "admin"); |
|
|
|
|
transaction.process(); |
|
|
|
|
|
|
|
|
|
Messages.send(player, "money.give.self", PHHM.get(transaction)); |
|
|
|
|
|
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static int giveMoneyOther(CommandContext<CommandSourceStack> context){ |
|
|
|
|
private static int giveMoneyOther(CommandContext<CommandSourceStack> context) { |
|
|
|
|
CommandSender commandSender = context.getSource().getSender(); |
|
|
|
|
Float amount = context.getArgument("amount", Float.class); |
|
|
|
|
String owner = context.getArgument("owner", String.class); |
|
|
|
|
String type = context.getArgument("type", String.class); |
|
|
|
|
|
|
|
|
|
Transaction transaction = new Transaction(amount,"admin", commandSender.getName(), type, owner, "admin"); |
|
|
|
|
Transaction transaction = new Transaction(amount, "admin", commandSender.getName(), type, owner, "admin"); |
|
|
|
|
transaction.process(); |
|
|
|
|
|
|
|
|
|
Messages.send(commandSender, "money.give.other", PHHM.get(transaction)); |
|
|
|
|
|
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static int removeMoneyPlayer(CommandContext<CommandSourceStack> context) throws CommandSyntaxException { |
|
|
|
|
CommandSender commandSender = context.getSource().getSender(); |
|
|
|
|
|
|
|
|
|
PlayerSelectorArgumentResolver senderResolver = context.getArgument("player", PlayerSelectorArgumentResolver.class); |
|
|
|
|
Player sender = senderResolver.resolve(context.getSource()).getFirst(); |
|
|
|
|
|
|
|
|
|
Float amount = context.getArgument("amount", Float.class); |
|
|
|
|
|
|
|
|
|
Transaction transaction = new Transaction(amount, "player", sender.getName(),"admin", commandSender.getName(), "admin"); |
|
|
|
|
Transaction transaction = new Transaction(amount, "player", sender.getName(), "admin", commandSender.getName(), "admin"); |
|
|
|
|
transaction.process(); |
|
|
|
|
|
|
|
|
|
Messages.send(commandSender, "money.remove.player", PHHM.get(transaction)); |
|
|
|
|
|
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
private static int removeMoneySelf(CommandContext<CommandSourceStack> context){ |
|
|
|
|
|
|
|
|
|
private static int removeMoneySelf(CommandContext<CommandSourceStack> context) { |
|
|
|
|
Player player = (Player) context.getSource().getSender(); |
|
|
|
|
Float amount = context.getArgument("amount", Float.class); |
|
|
|
|
|
|
|
|
|
Transaction transaction = new Transaction(amount,"player", player.getName(),"admin", player.getName(), "admin"); |
|
|
|
|
Transaction transaction = new Transaction(amount, "player", player.getName(), "admin", player.getName(), "admin"); |
|
|
|
|
transaction.process(); |
|
|
|
|
|
|
|
|
|
Messages.send(player, "money.remove.self", PHHM.get(transaction)); |
|
|
|
|
|
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
private static int removeMoneyOther(CommandContext<CommandSourceStack> context){ |
|
|
|
|
|
|
|
|
|
private static int removeMoneyOther(CommandContext<CommandSourceStack> context) { |
|
|
|
|
CommandSender commandSender = context.getSource().getSender(); |
|
|
|
|
Float amount = context.getArgument("amount", Float.class); |
|
|
|
|
String owner = context.getArgument("owner", String.class); |
|
|
|
|
String type = context.getArgument("type", String.class); |
|
|
|
|
|
|
|
|
|
Transaction transaction = new Transaction(amount, type, owner,"admin", commandSender.getName(), "admin"); |
|
|
|
|
Transaction transaction = new Transaction(amount, type, owner, "admin", commandSender.getName(), "admin"); |
|
|
|
|
transaction.process(); |
|
|
|
|
|
|
|
|
|
Messages.send(commandSender, "money.remove.other", PHHM.get(transaction)); |
|
|
|
|
|
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
} |