Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
Add virtual payment commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Flibio committed Nov 23, 2016
1 parent 2113a4b commit 5e53a5a
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions src/main/java/io/github/flibio/economylite/EconomyLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -144,6 +146,8 @@ public void onServerInitialize(GameInitializationEvent event) {
new VirtualAddCommand(),
new VirtualSetCommand(),
new VirtualRemoveCommand(),
new VirtualPayCommand(),
new PayVirtualCommand(),
new MigrateCommand()
);
// Register currency registry
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Player> {

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.<String>getOne("target").get();
BigDecimal amount = BigDecimal.valueOf(args.<Double>getOne("amount").get());
Optional<Account> aOpt = EconomyLite.getEconomyService().getOrCreateAccount(target);
Optional<UniqueAccount> 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"));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,6 +41,7 @@
import io.github.flibio.utils.message.MessageStorage;

import java.math.BigDecimal;
import java.util.Locale;
import java.util.Optional;

@AsyncCommand
Expand Down Expand Up @@ -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"));
}
Expand All @@ -84,5 +84,4 @@ public void run(CommandSource src, CommandContext args) {
src.sendMessage(messageStorage.getMessage("command.error"));
}
}

}
Original file line number Diff line number Diff line change
@@ -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<CommandSource> {

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.<String>getOne("account").get();
User target = args.<User>getOne("target").get();
BigDecimal amount = BigDecimal.valueOf(args.<Double>getOne("amount").get());
Optional<Account> aOpt = EconomyLite.getEconomyService().getOrCreateAccount(account);
Optional<UniqueAccount> 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"));
}
}

}

0 comments on commit 5e53a5a

Please sign in to comment.