diff --git a/gradle.properties b/gradle.properties index 1f47d4b..2f8a84f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,5 +2,5 @@ name=EconomyLite owner=Flibio inceptionYear=2015 currentYear=2016 -version=2.3.0 +version=2.4.0 apiVersion=5.0.0-SNAPSHOT diff --git a/src/main/java/io/github/flibio/economylite/EconomyLite.java b/src/main/java/io/github/flibio/economylite/EconomyLite.java index f08ae51..f3e18df 100644 --- a/src/main/java/io/github/flibio/economylite/EconomyLite.java +++ b/src/main/java/io/github/flibio/economylite/EconomyLite.java @@ -41,9 +41,11 @@ import io.github.flibio.economylite.commands.currency.CurrencyCreateCommand; import io.github.flibio.economylite.commands.currency.CurrencyDeleteCommand; import io.github.flibio.economylite.commands.currency.CurrencySetCommand; +import io.github.flibio.economylite.commands.virtual.PayVirtualCommand; import io.github.flibio.economylite.commands.virtual.VirtualAddCommand; import io.github.flibio.economylite.commands.virtual.VirtualBalanceCommand; import io.github.flibio.economylite.commands.virtual.VirtualEconCommand; +import io.github.flibio.economylite.commands.virtual.VirtualPayCommand; import io.github.flibio.economylite.commands.virtual.VirtualRemoveCommand; import io.github.flibio.economylite.commands.virtual.VirtualSetCommand; import io.github.flibio.economylite.impl.CurrencyService; @@ -144,6 +146,8 @@ public void onServerInitialize(GameInitializationEvent event) { new VirtualAddCommand(), new VirtualSetCommand(), new VirtualRemoveCommand(), + new VirtualPayCommand(), + new PayVirtualCommand(), new MigrateCommand() ); // Register currency registry diff --git a/src/main/java/io/github/flibio/economylite/commands/virtual/PayVirtualCommand.java b/src/main/java/io/github/flibio/economylite/commands/virtual/PayVirtualCommand.java new file mode 100644 index 0000000..be60a76 --- /dev/null +++ b/src/main/java/io/github/flibio/economylite/commands/virtual/PayVirtualCommand.java @@ -0,0 +1,66 @@ +package io.github.flibio.economylite.commands.virtual; + +import io.github.flibio.economylite.EconomyLite; +import io.github.flibio.utils.commands.AsyncCommand; +import io.github.flibio.utils.commands.BaseCommandExecutor; +import io.github.flibio.utils.commands.Command; +import io.github.flibio.utils.message.MessageStorage; +import org.spongepowered.api.command.args.CommandContext; +import org.spongepowered.api.command.args.GenericArguments; +import org.spongepowered.api.command.spec.CommandSpec; +import org.spongepowered.api.command.spec.CommandSpec.Builder; +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.event.cause.Cause; +import org.spongepowered.api.event.cause.NamedCause; +import org.spongepowered.api.service.economy.EconomyService; +import org.spongepowered.api.service.economy.account.Account; +import org.spongepowered.api.service.economy.account.UniqueAccount; +import org.spongepowered.api.service.economy.transaction.ResultType; +import org.spongepowered.api.text.Text; + +import java.math.BigDecimal; +import java.util.Optional; + +@AsyncCommand +@Command(aliases = {"payvirtual", "payv"}, permission = "economylite.virtual.pay") +public class PayVirtualCommand extends BaseCommandExecutor { + + private MessageStorage messageStorage = EconomyLite.getMessageStorage(); + private EconomyService ecoService = EconomyLite.getEconomyService(); + + @Override + public Builder getCommandSpecBuilder() { + return CommandSpec + .builder() + .executor(this) + .arguments(GenericArguments.string(Text.of("target")), GenericArguments.doubleNum(Text.of("amount"))); + } + + @Override + public void run(Player src, CommandContext args) { + if (args.getOne("target").isPresent() && args.getOne("amount").isPresent()) { + String target = args.getOne("target").get(); + BigDecimal amount = BigDecimal.valueOf(args.getOne("amount").get()); + Optional aOpt = EconomyLite.getEconomyService().getOrCreateAccount(target); + Optional uOpt = EconomyLite.getEconomyService().getOrCreateAccount(src.getUniqueId()); + // Check for negative payments + if (amount.compareTo(BigDecimal.ONE) == -1) { + src.sendMessage(messageStorage.getMessage("command.pay.invalid")); + } else { + if (aOpt.isPresent() && uOpt.isPresent()) { + Account receiver = aOpt.get(); + UniqueAccount payer = uOpt.get(); + if (payer.transfer(receiver, ecoService.getDefaultCurrency(), amount, Cause.of(NamedCause.owner(EconomyLite.getInstance()))) + .getResult().equals(ResultType.SUCCESS)) { + src.sendMessage(messageStorage.getMessage("command.pay.success", "target", receiver.getDisplayName())); + } else { + src.sendMessage(messageStorage.getMessage("command.pay.failed", "target", receiver.getDisplayName())); + } + } + } + } else { + src.sendMessage(messageStorage.getMessage("command.error")); + } + } + +} diff --git a/src/main/java/io/github/flibio/economylite/commands/virtual/VirtualBalanceCommand.java b/src/main/java/io/github/flibio/economylite/commands/virtual/VirtualBalanceCommand.java index b303fbd..1ff9721 100644 --- a/src/main/java/io/github/flibio/economylite/commands/virtual/VirtualBalanceCommand.java +++ b/src/main/java/io/github/flibio/economylite/commands/virtual/VirtualBalanceCommand.java @@ -32,9 +32,7 @@ import org.spongepowered.api.service.economy.Currency; import org.spongepowered.api.service.economy.account.Account; import org.spongepowered.api.text.Text; - import com.google.common.collect.ImmutableMap; - import io.github.flibio.economylite.EconomyLite; import io.github.flibio.economylite.api.CurrencyEconService; import io.github.flibio.utils.commands.AsyncCommand; @@ -43,6 +41,7 @@ import io.github.flibio.utils.message.MessageStorage; import java.math.BigDecimal; +import java.util.Locale; import java.util.Optional; @AsyncCommand @@ -73,7 +72,8 @@ public void run(CommandSource src, CommandContext args) { label = currency.getDisplayName(); } src.sendMessage(messageStorage.getMessage("command.balanceother", - ImmutableMap.of("player", aOpt.get().getDisplayName(), "balance", currency.format(bal), "label", label))); + ImmutableMap.of("player", aOpt.get().getDisplayName(), "balance", Text.of(String.format(Locale.ENGLISH, "%,.2f", bal)), + "label", label))); } else { src.sendMessage(messageStorage.getMessage("command.error")); } @@ -84,5 +84,4 @@ public void run(CommandSource src, CommandContext args) { src.sendMessage(messageStorage.getMessage("command.error")); } } - } diff --git a/src/main/java/io/github/flibio/economylite/commands/virtual/VirtualPayCommand.java b/src/main/java/io/github/flibio/economylite/commands/virtual/VirtualPayCommand.java new file mode 100644 index 0000000..0bc85b4 --- /dev/null +++ b/src/main/java/io/github/flibio/economylite/commands/virtual/VirtualPayCommand.java @@ -0,0 +1,80 @@ +package io.github.flibio.economylite.commands.virtual; + +import io.github.flibio.economylite.EconomyLite; +import io.github.flibio.utils.commands.AsyncCommand; +import io.github.flibio.utils.commands.BaseCommandExecutor; +import io.github.flibio.utils.commands.Command; +import io.github.flibio.utils.message.MessageStorage; +import org.spongepowered.api.command.CommandSource; +import org.spongepowered.api.command.args.CommandContext; +import org.spongepowered.api.command.args.GenericArguments; +import org.spongepowered.api.command.spec.CommandSpec; +import org.spongepowered.api.command.spec.CommandSpec.Builder; +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.entity.living.player.User; +import org.spongepowered.api.event.cause.Cause; +import org.spongepowered.api.event.cause.NamedCause; +import org.spongepowered.api.service.economy.EconomyService; +import org.spongepowered.api.service.economy.account.Account; +import org.spongepowered.api.service.economy.account.UniqueAccount; +import org.spongepowered.api.service.economy.transaction.ResultType; +import org.spongepowered.api.text.Text; + +import java.math.BigDecimal; +import java.util.Locale; +import java.util.Optional; + +@AsyncCommand +@Command(aliases = {"vpay"}, permission = "economylite.admin.virtual.pay") +public class VirtualPayCommand extends BaseCommandExecutor { + + private MessageStorage messageStorage = EconomyLite.getMessageStorage(); + private EconomyService ecoService = EconomyLite.getEconomyService(); + + @Override + public Builder getCommandSpecBuilder() { + return CommandSpec + .builder() + .executor(this) + .arguments(GenericArguments.string(Text.of("account")), GenericArguments.user(Text.of("target")), + GenericArguments.doubleNum(Text.of("amount"))); + } + + @Override + public void run(CommandSource src, CommandContext args) { + if (args.getOne("account").isPresent() && args.getOne("target").isPresent() && args.getOne("amount").isPresent()) { + String account = args.getOne("account").get(); + User target = args.getOne("target").get(); + BigDecimal amount = BigDecimal.valueOf(args.getOne("amount").get()); + Optional aOpt = EconomyLite.getEconomyService().getOrCreateAccount(account); + Optional uOpt = EconomyLite.getEconomyService().getOrCreateAccount(target.getUniqueId()); + // Check for negative payments + if (amount.compareTo(BigDecimal.ONE) == -1) { + src.sendMessage(messageStorage.getMessage("command.pay.invalid")); + } else { + if (aOpt.isPresent() && uOpt.isPresent()) { + Account payer = aOpt.get(); + UniqueAccount receiver = uOpt.get(); + if (payer.transfer(receiver, ecoService.getDefaultCurrency(), amount, Cause.of(NamedCause.owner(EconomyLite.getInstance()))) + .getResult().equals(ResultType.SUCCESS)) { + src.sendMessage(messageStorage.getMessage("command.pay.success", "target", target.getName())); + if (target instanceof Player) { + Text label = ecoService.getDefaultCurrency().getPluralDisplayName(); + if (amount.equals(BigDecimal.ONE)) { + label = ecoService.getDefaultCurrency().getDisplayName(); + } + ((Player) target).sendMessage(messageStorage.getMessage("command.pay.target", "amountandlabel", + Text.of(String.format(Locale.ENGLISH, "%,.2f", amount) + " ").toBuilder().append(label).build(), "sender", + payer.getDisplayName())); + } + } else { + src.sendMessage(messageStorage.getMessage("command.pay.failed", "target", target.getName())); + } + } + } + } else { + src.sendMessage(messageStorage.getMessage("command.error")); + } + } + +}