Search in sources :

Example 1 with Player

use of org.spongepowered.api.entity.living.player.Player in project TotalEconomy by Erigitic.

the class TEJobManager method onSignInteract.

/**
     * Called when a player clicks a sign. If the clicked sign is a "Job Changing" sign then the player's job will
     * be changed on click.
     *
     * @param event InteractBlockEvent
     */
@Listener
public void onSignInteract(InteractBlockEvent event) {
    if (event.getCause().first(Player.class).isPresent()) {
        Player player = event.getCause().first(Player.class).get();
        if (event.getTargetBlock().getLocation().isPresent()) {
            Optional<TileEntity> tileEntityOpt = event.getTargetBlock().getLocation().get().getTileEntity();
            if (tileEntityOpt.isPresent()) {
                TileEntity tileEntity = tileEntityOpt.get();
                if (tileEntity instanceof Sign) {
                    Sign sign = (Sign) tileEntity;
                    Optional<SignData> data = sign.getOrCreate(SignData.class);
                    if (data.isPresent()) {
                        SignData signData = data.get();
                        Text lineOneText = signData.lines().get(0);
                        Text lineTwoText = signData.lines().get(1);
                        String lineOne = lineOneText.toPlain();
                        String lineTwo = lineTwoText.toPlain().toLowerCase();
                        if (lineOne.equals("[TEJobs]")) {
                            if (jobExists(lineTwo)) {
                                if (setJob(player, lineTwo)) {
                                    player.sendMessage(Text.of(TextColors.GRAY, "Job changed to: ", TextColors.GOLD, lineTwo));
                                } else {
                                    player.sendMessage(Text.of(TextColors.RED, "[TE] Failed to set job. Contact your administrator."));
                                }
                            } else {
                                player.sendMessage(Text.of(TextColors.RED, "[TE] Sorry, this job does not exist"));
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) Player(org.spongepowered.api.entity.living.player.Player) SignData(org.spongepowered.api.data.manipulator.mutable.tileentity.SignData) Sign(org.spongepowered.api.block.tileentity.Sign) Text(org.spongepowered.api.text.Text) Listener(org.spongepowered.api.event.Listener)

Example 2 with Player

use of org.spongepowered.api.entity.living.player.Player in project TotalEconomy by Erigitic.

the class TEJobManager method onPlayerPlaceBlock.

/**
     * Used for the place option in jobs. Will check if the job has the place node and if it does it will check if the
     * block that was placed 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.Place
     */
@Listener
public void onPlayerPlaceBlock(ChangeBlockEvent.Place 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);
        String blockName = event.getTransactions().get(0).getFinal().getState().getType().getName();
        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("[TE] Job " + getPlayerJob(player) + " has nonexistent set \"" + s + "\"");
                    continue;
                }
                reward = optSet.get().getRewardFor("place", blockName);
            }
            if (reward.isPresent()) {
                int expAmount = reward.get().getExpReward();
                BigDecimal payAmount = reward.get().getMoneyReward();
                boolean notify = accountConfig.getNode(playerUUID.toString(), "jobnotifications").getBoolean();
                TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(player.getUniqueId()).get();
                if (notify) {
                    notifyPlayer(player, payAmount);
                }
                addExp(player, expAmount);
                playerAccount.deposit(totalEconomy.getDefaultCurrency(), payAmount, Cause.of(NamedCause.of("TotalEconomy", totalEconomy.getPluginContainer())));
                checkForLevel(player);
            }
        }
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) TEAccount(com.erigitic.config.TEAccount) BigDecimal(java.math.BigDecimal) Listener(org.spongepowered.api.event.Listener)

Example 3 with Player

use of org.spongepowered.api.entity.living.player.Player in project TotalEconomy by Erigitic.

the class TEJobManager 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);
            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("[TE] Job " + getPlayerJob(player) + " has nonexistent set \"" + s + "\"");
                        continue;
                    }
                    reward = optSet.get().getRewardFor("kill", victimName);
                }
                if (reward.isPresent()) {
                    int expAmount = reward.get().getExpReward();
                    BigDecimal payAmount = reward.get().getMoneyReward();
                    boolean notify = accountConfig.getNode(playerUUID.toString(), "jobnotifications").getBoolean();
                    TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(player.getUniqueId()).get();
                    if (notify) {
                        notifyPlayer(player, payAmount);
                    }
                    addExp(player, expAmount);
                    playerAccount.deposit(totalEconomy.getDefaultCurrency(), payAmount, Cause.of(NamedCause.of("TotalEconomy", totalEconomy.getPluginContainer())));
                    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) Listener(org.spongepowered.api.event.Listener)

Example 4 with Player

use of org.spongepowered.api.entity.living.player.Player in project TotalEconomy by Erigitic.

the class TotalEconomy method onPlayerJoin.

@Listener
public void onPlayerJoin(ClientConnectionEvent.Join event) {
    Player player = event.getTargetEntity();
    accountManager.getOrCreateAccount(player.getUniqueId());
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) Listener(org.spongepowered.api.event.Listener)

Example 5 with Player

use of org.spongepowered.api.entity.living.player.Player in project TotalEconomy by Erigitic.

the class PayCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    String strAmount = (String) args.getOne("amount").get();
    Player recipient = (Player) args.getOne("player").get();
    if (src instanceof Player) {
        Player sender = (Player) src;
        if (sender.getUniqueId().equals(recipient.getUniqueId())) {
            throw new CommandException(Text.of("You cannot pay yourself!"));
        }
        Pattern amountPattern = Pattern.compile("^[+]?(\\d*\\.)?\\d+$");
        Matcher m = amountPattern.matcher(strAmount);
        if (m.matches()) {
            BigDecimal amount = new BigDecimal(strAmount).setScale(2, BigDecimal.ROUND_DOWN);
            TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(sender.getUniqueId()).get();
            TEAccount recipientAccount = (TEAccount) accountManager.getOrCreateAccount(recipient.getUniqueId()).get();
            TransferResult transferResult = playerAccount.transfer(recipientAccount, totalEconomy.getDefaultCurrency(), amount, Cause.of(NamedCause.of("TotalEconomy", totalEconomy.getPluginContainer())));
            if (transferResult.getResult() == ResultType.SUCCESS) {
                sender.sendMessage(Text.of(TextColors.GRAY, "You have sent ", TextColors.GOLD, defaultCurrency.format(amount), TextColors.GRAY, " to ", TextColors.GOLD, recipient.getName(), TextColors.GRAY, "."));
                recipient.sendMessage(Text.of(TextColors.GRAY, "You have received ", TextColors.GOLD, defaultCurrency.format(amount), TextColors.GRAY, " from ", TextColors.GOLD, sender.getName(), TextColors.GRAY, "."));
                return CommandResult.success();
            } else if (transferResult.getResult() == ResultType.ACCOUNT_NO_FUNDS) {
                throw new CommandException(Text.of("Insufficient funds!"));
            }
        } else {
            throw new CommandException(Text.of("Invalid amount! Must be a positive number!"));
        }
    }
    return CommandResult.empty();
}
Also used : Pattern(java.util.regex.Pattern) Player(org.spongepowered.api.entity.living.player.Player) Matcher(java.util.regex.Matcher) CommandException(org.spongepowered.api.command.CommandException) TEAccount(com.erigitic.config.TEAccount) TransferResult(org.spongepowered.api.service.economy.transaction.TransferResult) BigDecimal(java.math.BigDecimal)

Aggregations

Player (org.spongepowered.api.entity.living.player.Player)113 Listener (org.spongepowered.api.event.Listener)34 ItemStack (org.spongepowered.api.item.inventory.ItemStack)28 Entity (org.spongepowered.api.entity.Entity)25 World (org.spongepowered.api.world.World)19 BigDecimal (java.math.BigDecimal)16 Text (org.spongepowered.api.text.Text)13 ItemStackFactory.newItemStack (com.skelril.nitro.item.ItemStackFactory.newItemStack)12 Vector3d (com.flowpowered.math.vector.Vector3d)10 PlayerCombatParser (com.skelril.nitro.combat.PlayerCombatParser)10 Living (org.spongepowered.api.entity.living.Living)10 Location (org.spongepowered.api.world.Location)10 List (java.util.List)9 MarketService (com.skelril.skree.service.MarketService)8 Optional (java.util.Optional)8 ItemStack (net.minecraft.item.ItemStack)8 DamageEntityEvent (org.spongepowered.api.event.entity.DamageEntityEvent)8 TextColors (org.spongepowered.api.text.format.TextColors)8 TEAccount (com.erigitic.config.TEAccount)7 ArrayList (java.util.ArrayList)7