This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Flibio
committed
Nov 23, 2016
1 parent
2113a4b
commit 5e53a5a
Showing
5 changed files
with
154 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/io/github/flibio/economylite/commands/virtual/PayVirtualCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/io/github/flibio/economylite/commands/virtual/VirtualPayCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} | ||
|
||
} |