Search in sources :

Example 1 with EconomyService

use of org.spongepowered.api.service.economy.EconomyService in project Nucleus by NucleusPowered.

the class EconHelper method withdrawFromPlayer.

public boolean withdrawFromPlayer(Player src, double cost, boolean message) {
    Optional<EconomyService> oes = Sponge.getServiceManager().provide(EconomyService.class);
    if (oes.isPresent()) {
        // Check balance.
        EconomyService es = oes.get();
        Optional<UniqueAccount> a = es.getOrCreateAccount(src.getUniqueId());
        if (!a.isPresent()) {
            src.sendMessage(NucleusPlugin.getNucleus().getMessageProvider().getTextMessageWithFormat("cost.noaccount"));
            return false;
        }
        TransactionResult tr = a.get().withdraw(es.getDefaultCurrency(), BigDecimal.valueOf(cost), CauseStackHelper.createCause(src));
        if (tr.getResult() == ResultType.ACCOUNT_NO_FUNDS) {
            if (message) {
                src.sendMessage(NucleusPlugin.getNucleus().getMessageProvider().getTextMessageWithFormat("cost.nofunds", getCurrencySymbol(cost)));
            }
            return false;
        } else if (tr.getResult() != ResultType.SUCCESS) {
            src.sendMessage(NucleusPlugin.getNucleus().getMessageProvider().getTextMessageWithFormat("cost.error"));
            return false;
        }
        if (message) {
            src.sendMessage(NucleusPlugin.getNucleus().getMessageProvider().getTextMessageWithFormat("cost.complete", getCurrencySymbol(cost)));
        }
    }
    return true;
}
Also used : TransactionResult(org.spongepowered.api.service.economy.transaction.TransactionResult) EconomyService(org.spongepowered.api.service.economy.EconomyService) UniqueAccount(org.spongepowered.api.service.economy.account.UniqueAccount)

Example 2 with EconomyService

use of org.spongepowered.api.service.economy.EconomyService in project Nucleus by NucleusPowered.

the class EconHelper method hasBalance.

public boolean hasBalance(Player src, double balance) {
    Optional<EconomyService> oes = Sponge.getServiceManager().provide(EconomyService.class);
    if (oes.isPresent()) {
        // Check balance.
        EconomyService es = oes.get();
        Optional<UniqueAccount> ua = es.getOrCreateAccount(src.getUniqueId());
        return ua.isPresent() && ua.get().getBalance(es.getDefaultCurrency()).doubleValue() >= balance;
    }
    // No economy
    return true;
}
Also used : EconomyService(org.spongepowered.api.service.economy.EconomyService) UniqueAccount(org.spongepowered.api.service.economy.account.UniqueAccount)

Example 3 with EconomyService

use of org.spongepowered.api.service.economy.EconomyService in project Nucleus by NucleusPowered.

the class EconHelper method depositInPlayer.

public boolean depositInPlayer(User src, double cost, boolean message) {
    Optional<EconomyService> oes = Sponge.getServiceManager().provide(EconomyService.class);
    if (oes.isPresent()) {
        // Check balance.
        EconomyService es = oes.get();
        Optional<UniqueAccount> a = es.getOrCreateAccount(src.getUniqueId());
        if (!a.isPresent()) {
            src.getPlayer().ifPresent(x -> x.sendMessage(NucleusPlugin.getNucleus().getMessageProvider().getTextMessageWithFormat("cost.noaccount")));
            return false;
        }
        TransactionResult tr = a.get().deposit(es.getDefaultCurrency(), BigDecimal.valueOf(cost), CauseStackHelper.createCause(src));
        if (tr.getResult() != ResultType.SUCCESS && src.isOnline()) {
            src.getPlayer().ifPresent(x -> x.sendMessage(NucleusPlugin.getNucleus().getMessageProvider().getTextMessageWithFormat("cost.error")));
            return false;
        }
        if (message && src.isOnline()) {
            src.getPlayer().ifPresent(x -> x.sendMessage(NucleusPlugin.getNucleus().getMessageProvider().getTextMessageWithFormat("cost.refund", getCurrencySymbol(cost))));
        }
    }
    return true;
}
Also used : TransactionResult(org.spongepowered.api.service.economy.transaction.TransactionResult) EconomyService(org.spongepowered.api.service.economy.EconomyService) UniqueAccount(org.spongepowered.api.service.economy.account.UniqueAccount)

Example 4 with EconomyService

use of org.spongepowered.api.service.economy.EconomyService in project Almura by AlmuraDev.

the class MembershipHandler method requestClientGui.

public void requestClientGui(Player player) {
    final EconomyService service = Sponge.getServiceManager().provide(EconomyService.class).orElse(null);
    final LuckPerms permService = Sponge.getServiceManager().provide(LuckPerms.class).orElse(null);
    int currentMembershipLevel = -1;
    if (player.hasPermission("almura.membership.gui.open")) {
        if (service != null && skillsManager != null && permService != null) {
            final Account account = service.getOrCreateAccount(player.getUniqueId()).orElse(null);
            BigDecimal balance;
            if (account != null) {
                final Currency currency = service.getDefaultCurrency();
                balance = account.getBalance(currency);
                String currentGroup = permService.getUserManager().getUser(player.getUniqueId()).getPrimaryGroup();
                if (currentGroup.equalsIgnoreCase("default")) {
                    currentMembershipLevel = -1;
                // should be impossible to reach this unless permissions system is screwed up.
                } else if (currentGroup.equalsIgnoreCase("survivor")) {
                    currentMembershipLevel = 0;
                } else if (currentGroup.equalsIgnoreCase("citizen")) {
                    currentMembershipLevel = 1;
                } else if (currentGroup.equalsIgnoreCase("explorer")) {
                    currentMembershipLevel = 2;
                } else if (currentGroup.equalsIgnoreCase("pioneer")) {
                    currentMembershipLevel = 3;
                }
                this.network.sendTo(player, new ClientboundMembershipGuiOpenPacket(player.hasPermission("almura.membership.admin"), skillsManager.getTotalSkillLevel(player), balance.doubleValue(), currentMembershipLevel));
            }
        } else {
            serverNotificationManager.sendWindowMessage(player, Text.of("Init Error"), Text.of("Economy Service offline!"));
        }
    } else {
        serverNotificationManager.sendPopupNotification(player, Text.of("Insufficient Permissions"), Text.of("Missing [almura.membership.gui.open]"), 5);
    }
}
Also used : Account(org.spongepowered.api.service.economy.account.Account) LuckPerms(net.luckperms.api.LuckPerms) EconomyService(org.spongepowered.api.service.economy.EconomyService) Currency(org.spongepowered.api.service.economy.Currency) BigDecimal(java.math.BigDecimal) ClientboundMembershipGuiOpenPacket(com.almuradev.almura.feature.membership.network.ClientboundMembershipGuiOpenPacket)

Example 5 with EconomyService

use of org.spongepowered.api.service.economy.EconomyService in project Almura by AlmuraDev.

the class CoinExchange method onBlockActivated.

@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer mcPlayer, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    if (world.isRemote) {
        return true;
    }
    final Player player = (Player) mcPlayer;
    if (mcPlayer.getHeldItemMainhand().isEmpty()) {
        serverNotificationManager.sendPopupNotification(player, Text.of("Coin Exchange"), Text.of("Place coins in hand prior to clicking on Coin Exchange"), 5);
        return true;
    }
    final EconomyService service = Sponge.getServiceManager().provide(EconomyService.class).orElse(null);
    if (service != null) {
        final Account account = service.getOrCreateAccount(player.getUniqueId()).orElse(null);
        if (account != null) {
            final Currency currency = service.getDefaultCurrency();
            final double coinValue = getCoinValue(player.getItemInHand(HandTypes.MAIN_HAND).get());
            if (coinValue == 0) {
                serverNotificationManager.sendPopupNotification(player, Text.of("Coin Exchange"), Text.of("This item is not a coin..."), 5);
                // Not Coins
                return true;
            }
            final BigDecimal depositAmount = new BigDecimal((coinValue * player.getItemInHand(HandTypes.MAIN_HAND).get().getQuantity()));
            account.deposit(currency, depositAmount, Sponge.getCauseStackManager().getCurrentCause());
            // Clear ItemStack from Players hand.
            ((Player) mcPlayer).setItemInHand(HandTypes.MAIN_HAND, null);
        }
    }
    return true;
}
Also used : Account(org.spongepowered.api.service.economy.account.Account) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Player(org.spongepowered.api.entity.living.player.Player) EconomyService(org.spongepowered.api.service.economy.EconomyService) Currency(org.spongepowered.api.service.economy.Currency) BigDecimal(java.math.BigDecimal)

Aggregations

EconomyService (org.spongepowered.api.service.economy.EconomyService)13 BigDecimal (java.math.BigDecimal)8 UniqueAccount (org.spongepowered.api.service.economy.account.UniqueAccount)8 Currency (org.spongepowered.api.service.economy.Currency)6 Account (org.spongepowered.api.service.economy.account.Account)5 UUID (java.util.UUID)4 Player (org.spongepowered.api.entity.living.player.Player)4 Listener (org.spongepowered.api.event.Listener)4 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 Nullable (javax.annotation.Nullable)3 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)3 ItemStack (net.minecraft.item.ItemStack)3 IItemHandler (net.minecraftforge.items.IItemHandler)3 DSLContext (org.jooq.DSLContext)3 Results (org.jooq.Results)3 CauseStackManager (org.spongepowered.api.event.CauseStackManager)3