Search in sources :

Example 11 with Currency

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

the class ServerHeadUpDisplayManager method createPlayerCurrencyPacket.

@Nullable
private ClientboundPlayerCurrencyPacket createPlayerCurrencyPacket(final Player player) {
    final EconomyService service = Sponge.getServiceManager().provide(EconomyService.class).orElse(null);
    if (service != null) {
        final Account account = service.getOrCreateAccount(player.getUniqueId()).orElse(null);
        BigDecimal balance = BigDecimal.ZERO;
        if (account != null) {
            final Currency currency = service.getDefaultCurrency();
            balance = account.getBalance(currency);
        }
        return new ClientboundPlayerCurrencyPacket(balance);
    }
    return null;
}
Also used : Account(org.spongepowered.api.service.economy.account.Account) UniqueAccount(org.spongepowered.api.service.economy.account.UniqueAccount) EconomyService(org.spongepowered.api.service.economy.EconomyService) Currency(org.spongepowered.api.service.economy.Currency) ClientboundPlayerCurrencyPacket(com.almuradev.almura.feature.hud.network.ClientboundPlayerCurrencyPacket) BigDecimal(java.math.BigDecimal) Nullable(javax.annotation.Nullable)

Example 12 with Currency

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

the class ServerClaimManager method sendUpdate.

protected void sendUpdate(final Player player, Claim claim) {
    if (GriefDefender.getCore() != null) {
        boolean isWorldOrilla = false;
        boolean isWorldAsgard = false;
        if (Sponge.getServer().getWorld("Orilla").isPresent()) {
            isWorldOrilla = Sponge.getServer().getWorld("Orilla").get().getUniqueId() == claim.getWorldUniqueId();
        }
        if (Sponge.getServer().getWorld("Asgard").isPresent()) {
            isWorldAsgard = Sponge.getServer().getWorld("Asgard").get().getUniqueId() == claim.getWorldUniqueId();
        }
        if (GriefDefender.getPermissionManager() != null) {
            if (claim == null) {
                // This is intended to catch when a delayed task is sent to this method and claim is purposely left null.
                claim = GriefDefender.getCore().getClaimManager(player.getWorld().getUniqueId()).getClaimAt(player.getLocation().getPosition().toInt());
            }
            if (claim != null) {
                boolean isClaim = true;
                boolean hasWECUI = false;
                boolean isForSale = false;
                boolean showWarnings = true;
                String claimName = "";
                String claimGreeting = "";
                String claimFarewell = "";
                String dateCreated = "";
                String claimOwner = "";
                String claimType = "";
                double claimEconBalance = 0.0;
                double claimTaxRate = 0.0;
                double claimTaxes = 0.0;
                double claimBlockCost = 0.0;
                double claimBlockSell = 0.0;
                int claimSize = 0;
                double claimTaxBalance = 0.0;
                double claimSalePrice = 0.0;
                final boolean isWilderness = claim.isWilderness();
                final boolean isTownClaim = claim.isTown();
                final boolean isAdminClaim = claim.isAdminClaim();
                final boolean isBasicClaim = claim.isBasicClaim();
                final boolean isSubdivision = claim.isSubdivision();
                if (claim.getData() != null) {
                    showWarnings = claim.getData().allowDenyMessages();
                }
                if (player != null && GriefDefender.getCore().getWorldEditProvider() != null) {
                    hasWECUI = GriefDefender.getCore().getWorldEditProvider().hasCUISupport(player.getUniqueId());
                }
                if (claim.getOwnerName() != null) {
                    claimOwner = claim.getOwnerName();
                }
                if (claim.getData() != null && claim.getData().getGreeting().isPresent()) {
                    claimGreeting = ((TextComponent) claim.getData().getGreeting().get()).content();
                }
                if (claim.getData() != null && claim.getData().getFarewell().isPresent()) {
                    claimFarewell = ((TextComponent) claim.getData().getFarewell().get()).content();
                }
                if (claim.getDisplayName() == null) {
                    claimName = "Name Not Set";
                } else {
                    claimName = claim.getDisplayName();
                }
                claimType = claim.getType().getName();
                dateCreated = claim.getData().getDateCreated().toString();
                if (claim.getData() != null) {
                    final EconomyService service = Sponge.getServiceManager().provide(EconomyService.class).orElse(null);
                    if (service != null && player != null) {
                        if (claim.getEconomyData().isForSale()) {
                            isForSale = true;
                            claimSalePrice = this.claimSalePrice(claim);
                        }
                        // Todo: implement the rest of the econ stuffz.
                        claimTaxRate = this.claimTaxRate(claim);
                        claimTaxes = this.claimTaxes(claim);
                        claimBlockCost = this.claimBlockCost(claim);
                        claimBlockSell = this.claimBlockSell(claim);
                        final Currency currency = service.getDefaultCurrency();
                        if (claim.getEconomyData() != null) {
                            claimTaxBalance = this.claimTaxBalance(claim);
                            UUID accountID = claim.getEconomyAccountId();
                            if (!(accountID == null)) {
                                final UniqueAccount claimAccount = service.getOrCreateAccount(accountID).orElse(null);
                                claimEconBalance = claimAccount.getBalance(currency).doubleValue();
                            }
                        }
                    }
                }
                if (!claim.isWilderness()) {
                    claimSize = claim.getArea();
                }
                // Set custom Claim Name for Protected Area's
                if (isWorldAsgard || isWorldOrilla) {
                    claimName = "Server Protected Area";
                }
                if (player != null) {
                    if (debug)
                        System.out.println("Sending Claim packet update to: [" + player.getName() + "] for Claim: [" + claim.getDisplayName() + "] Wilderness: [" + isWilderness + "]");
                    this.network.sendTo(player, new ClientboundClaimDataPacket(isClaim, claimName, claimOwner, isWilderness, isTownClaim, isAdminClaim, isBasicClaim, isSubdivision, claimEconBalance, claimGreeting, claimFarewell, dateCreated, claimType, claimSize, isForSale, showWarnings, claimTaxRate, claimTaxes, claimBlockCost, claimBlockSell, hasWECUI, claimTaxBalance, claimSalePrice));
                }
            }
        }
    }
}
Also used : EconomyService(org.spongepowered.api.service.economy.EconomyService) UniqueAccount(org.spongepowered.api.service.economy.account.UniqueAccount) Currency(org.spongepowered.api.service.economy.Currency) ClientboundClaimDataPacket(com.almuradev.almura.feature.claim.network.ClientboundClaimDataPacket) UUID(java.util.UUID)

Example 13 with Currency

use of org.spongepowered.api.service.economy.Currency in project TotalEconomy by Erigitic.

the class JobManager method onPlayerBlockBreak.

/**
 * Used for the break option in jobs. Will check if the job has the break node and if it does it will check if the
 * block that was broken is present in the config of the player's job. If it is, it will grab the job exp reward as
 * well as the pay.
 *
 * @param event ChangeBlockEvent.Break
 */
@Listener
public void onPlayerBlockBreak(ChangeBlockEvent.Break event) {
    if (event.getCause().first(Player.class).isPresent()) {
        Player player = event.getCause().first(Player.class).get();
        UUID playerUUID = player.getUniqueId();
        String playerJob = getPlayerJob(player);
        Optional<TEJob> optPlayerJob = getJob(playerJob, true);
        BlockState state = event.getTransactions().get(0).getOriginal().getState();
        String blockName = state.getType().getName();
        Optional<UUID> blockCreator = event.getTransactions().get(0).getOriginal().getCreator();
        // Enable admins to determine block information by displaying it to them - WHEN they have the flag enabled
        if (accountManager.getUserOption("totaleconomy:block-break-info", player).orElse("0").equals("1")) {
            List<BlockTrait<?>> traits = new ArrayList<>(state.getTraits());
            int count = traits.size();
            List<Text> traitTexts = new ArrayList<>(count);
            for (int i = 0; i < count; i++) {
                Object traitValue = state.getTraitValue(traits.get(i)).orElse(null);
                traitTexts.add(i, Text.of(traits.get(i).getName(), '=', traitValue != null ? traitValue.toString() : "null"));
            }
            Text t = Text.of(TextColors.GRAY, "TRAITS:\n    ", Text.joinWith(Text.of(",\n    "), traitTexts.toArray(new Text[traits.size()])));
            player.sendMessage(Text.of("Block-Name: ", blockName));
            player.sendMessage(t);
        }
        if (optPlayerJob.isPresent()) {
            Optional<TEActionReward> reward = Optional.empty();
            List<String> sets = optPlayerJob.get().getSets();
            for (String s : sets) {
                Optional<TEJobSet> optSet = getJobSet(s);
                if (!optSet.isPresent()) {
                    logger.warn("Job " + playerJob + " has the nonexistent set \"" + s + "\"");
                    continue;
                }
                Optional<TEAction> action = optSet.get().getActionFor("break", blockName);
                if (!action.isPresent()) {
                    continue;
                }
                Optional<TEActionReward> currentReward = action.get().evaluateBreak(logger, state, blockCreator.orElse(null));
                if (!reward.isPresent()) {
                    reward = currentReward;
                    continue;
                }
                if (!currentReward.isPresent()) {
                    continue;
                }
                // Use the one giving higher exp in case of duplicates
                if (currentReward.get().getExpReward() > reward.get().getExpReward()) {
                    reward = currentReward;
                }
            }
            if (reward.isPresent()) {
                TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(player.getUniqueId()).get();
                boolean notify = getNotificationState(playerUUID);
                int expAmount = reward.get().getExpReward();
                BigDecimal payAmount = new BigDecimal(reward.get().getMoneyReward());
                Currency currency = totalEconomy.getDefaultCurrency();
                if (reward.get().getCurrencyId() != null) {
                    Optional<Currency> currencyOpt = totalEconomy.getTECurrencyRegistryModule().getById("totaleconomy:" + reward.get().getCurrencyId());
                    if (currencyOpt.isPresent()) {
                        currency = currencyOpt.get();
                    }
                }
                if (notify) {
                    notifyPlayer(player, payAmount, currency);
                }
                addExp(player, expAmount);
                playerAccount.deposit(currency, payAmount, event.getCause());
                checkForLevel(player);
            }
        }
    }
}
Also used : BlockTrait(org.spongepowered.api.block.trait.BlockTrait) TEAccount(com.erigitic.config.TEAccount) Currency(org.spongepowered.api.service.economy.Currency) Player(org.spongepowered.api.entity.living.player.Player) Text(org.spongepowered.api.text.Text) BigDecimal(java.math.BigDecimal) BlockState(org.spongepowered.api.block.BlockState) Listener(org.spongepowered.api.event.Listener)

Example 14 with Currency

use of org.spongepowered.api.service.economy.Currency in project TotalEconomy by Erigitic.

the class JobManager method onPlayerFish.

/**
 * Used for the catch option in jobs. Will check if the job has the catch node and if it does it will check if the
 * item that was caught is present in the config of the player's job. If it is, it will grab the job exp reward as
 * well as the pay.
 *
 * @param event FishingEvent.Stop
 */
@Listener
public void onPlayerFish(FishingEvent.Stop event) {
    if (event.getCause().first(Player.class).isPresent()) {
        // no transaction, so execution can stop
        if (event.getTransactions().size() == 0) {
            return;
        }
        Transaction<ItemStackSnapshot> itemTransaction = event.getItemStackTransaction().get(0);
        ItemStack itemStack = itemTransaction.getFinal().createStack();
        Player player = event.getCause().first(Player.class).get();
        UUID playerUUID = player.getUniqueId();
        String playerJob = getPlayerJob(player);
        Optional<TEJob> optPlayerJob = getJob(playerJob, true);
        if (optPlayerJob.isPresent()) {
            if (itemStack.get(FishData.class).isPresent()) {
                FishData fishData = itemStack.get(FishData.class).get();
                String fishName = fishData.type().get().getName();
                // Enable admins to determine fish information by displaying it to them - WHEN they have the flag enabled
                if (accountManager.getUserOption("totaleconomy:entity-fish-info", player).orElse("0").equals("1")) {
                    player.sendMessage(Text.of("Fish-Name: ", fishName));
                }
                Optional<TEActionReward> reward = Optional.empty();
                List<String> sets = optPlayerJob.get().getSets();
                for (String s : sets) {
                    Optional<TEJobSet> optSet = getJobSet(s);
                    if (!optSet.isPresent()) {
                        logger.warn("Job " + playerJob + " has the nonexistent set \"" + s + "\"");
                        continue;
                    }
                    Optional<TEAction> action = optSet.get().getActionFor("catch", fishName);
                    if (!action.isPresent()) {
                        continue;
                    }
                    Optional<TEActionReward> currentReward = action.get().getReward();
                    if (!reward.isPresent()) {
                        reward = currentReward;
                        continue;
                    }
                    if (!currentReward.isPresent()) {
                        continue;
                    }
                    // Use the one giving higher exp in case of duplicates
                    if (currentReward.get().getExpReward() > reward.get().getExpReward()) {
                        reward = currentReward;
                    }
                }
                if (reward.isPresent()) {
                    TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(player.getUniqueId()).get();
                    boolean notify = getNotificationState(playerUUID);
                    int expAmount = reward.get().getExpReward();
                    BigDecimal payAmount = new BigDecimal(reward.get().getMoneyReward());
                    Currency currency = totalEconomy.getDefaultCurrency();
                    if (reward.get().getCurrencyId() != null) {
                        Optional<Currency> currencyOpt = totalEconomy.getTECurrencyRegistryModule().getById("totaleconomy:" + reward.get().getCurrencyId());
                        if (currencyOpt.isPresent()) {
                            currency = currencyOpt.get();
                        }
                    }
                    if (notify) {
                        notifyPlayer(player, payAmount, currency);
                    }
                    addExp(player, expAmount);
                    playerAccount.deposit(currency, payAmount, event.getCause());
                    checkForLevel(player);
                }
            }
        }
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) TEAccount(com.erigitic.config.TEAccount) BigDecimal(java.math.BigDecimal) FishData(org.spongepowered.api.data.manipulator.mutable.item.FishData) Currency(org.spongepowered.api.service.economy.Currency) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Listener(org.spongepowered.api.event.Listener)

Example 15 with Currency

use of org.spongepowered.api.service.economy.Currency in project TotalEconomy by Erigitic.

the class JobManager method onPlayerKillEntity.

/**
 * Used for the break option in jobs. Will check if the job has the break node and if it does it will check if the
 * block that was broken is present in the config of the player's job. If it is, it will grab the job exp reward as
 * well as the pay.
 *
 * @param event DestructEntityEvent.Death
 */
@Listener
public void onPlayerKillEntity(DestructEntityEvent.Death event) {
    Optional<EntityDamageSource> optDamageSource = event.getCause().first(EntityDamageSource.class);
    if (optDamageSource.isPresent()) {
        EntityDamageSource damageSource = optDamageSource.get();
        Entity killer = damageSource.getSource();
        Entity victim = event.getTargetEntity();
        if (!(killer instanceof Player)) {
            // If a projectile was shot to kill an entity, this will grab the player who shot it
            Optional<UUID> damageCreator = damageSource.getSource().getCreator();
            if (damageCreator.isPresent()) {
                killer = Sponge.getServer().getPlayer(damageCreator.get()).get();
            }
        }
        if (killer instanceof Player) {
            Player player = (Player) killer;
            UUID playerUUID = player.getUniqueId();
            String victimName = victim.getType().getName();
            String playerJob = getPlayerJob(player);
            Optional<TEJob> optPlayerJob = getJob(playerJob, true);
            // Enable admins to determine victim information by displaying it to them - WHEN they have the flag enabled
            if (accountManager.getUserOption("totaleconomy:entity-kill-info", player).orElse("0").equals("1")) {
                player.sendMessage(Text.of("Victim-Name: ", victimName));
            }
            if (optPlayerJob.isPresent()) {
                Optional<TEActionReward> reward = Optional.empty();
                List<String> sets = optPlayerJob.get().getSets();
                for (String s : sets) {
                    Optional<TEJobSet> optSet = getJobSet(s);
                    if (!optSet.isPresent()) {
                        logger.warn("Job " + playerJob + " has the nonexistent set \"" + s + "\"");
                        continue;
                    }
                    Optional<TEAction> action = optSet.get().getActionFor("kill", victimName);
                    if (!action.isPresent()) {
                        continue;
                    }
                    Optional<TEActionReward> currentReward = action.get().getReward();
                    if (!reward.isPresent()) {
                        reward = currentReward;
                        continue;
                    }
                    if (!currentReward.isPresent()) {
                        continue;
                    }
                    // Use the one giving higher exp in case of duplicates
                    if (currentReward.get().getExpReward() > reward.get().getExpReward()) {
                        reward = currentReward;
                    }
                }
                if (reward.isPresent()) {
                    TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(player.getUniqueId()).get();
                    boolean notify = getNotificationState(playerUUID);
                    int expAmount = reward.get().getExpReward();
                    BigDecimal payAmount = new BigDecimal(reward.get().getMoneyReward());
                    Currency currency = totalEconomy.getDefaultCurrency();
                    if (reward.get().getCurrencyId() != null) {
                        Optional<Currency> currencyOpt = totalEconomy.getTECurrencyRegistryModule().getById("totaleconomy:" + reward.get().getCurrencyId());
                        if (currencyOpt.isPresent()) {
                            currency = currencyOpt.get();
                        }
                    }
                    if (notify) {
                        notifyPlayer(player, payAmount, currency);
                    }
                    addExp(player, expAmount);
                    playerAccount.deposit(currency, payAmount, event.getCause());
                    checkForLevel(player);
                }
            }
        }
    }
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) Entity(org.spongepowered.api.entity.Entity) Player(org.spongepowered.api.entity.living.player.Player) TEAccount(com.erigitic.config.TEAccount) BigDecimal(java.math.BigDecimal) EntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource) Currency(org.spongepowered.api.service.economy.Currency) Listener(org.spongepowered.api.event.Listener)

Aggregations

Currency (org.spongepowered.api.service.economy.Currency)21 BigDecimal (java.math.BigDecimal)11 TEAccount (com.erigitic.config.TEAccount)6 Player (org.spongepowered.api.entity.living.player.Player)6 EconomyService (org.spongepowered.api.service.economy.EconomyService)6 Listener (org.spongepowered.api.event.Listener)5 Account (org.spongepowered.api.service.economy.account.Account)5 UUID (java.util.UUID)4 TECurrency (com.erigitic.config.TECurrency)3 Text (org.spongepowered.api.text.Text)3 LuckPerms (net.luckperms.api.LuckPerms)2 BlockState (org.spongepowered.api.block.BlockState)2 BlockTrait (org.spongepowered.api.block.trait.BlockTrait)2 CommandException (org.spongepowered.api.command.CommandException)2 UniqueAccount (org.spongepowered.api.service.economy.account.UniqueAccount)2 ClientboundClaimDataPacket (com.almuradev.almura.feature.claim.network.ClientboundClaimDataPacket)1 ClientboundPlayerDiedPacket (com.almuradev.almura.feature.death.network.ClientboundPlayerDiedPacket)1 ClientboundPlayerCurrencyPacket (com.almuradev.almura.feature.hud.network.ClientboundPlayerCurrencyPacket)1 ClientboundMembershipGuiOpenPacket (com.almuradev.almura.feature.membership.network.ClientboundMembershipGuiOpenPacket)1 ClientboundMembershipSuccessPacket (com.almuradev.almura.feature.membership.network.ClientboundMembershipSuccessPacket)1