Search in sources :

Example 31 with Listener

use of org.spongepowered.api.event.Listener 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 32 with Listener

use of org.spongepowered.api.event.Listener in project TotalEconomy by Erigitic.

the class TotalEconomy method preInit.

@Listener
public void preInit(GamePreInitializationEvent event) {
    setupConfig();
    defaultCurrency = new TECurrency(Text.of(config.getNode("currency", "currency-singular").getValue()), Text.of(config.getNode("currency", "currency-plural").getValue()), Text.of(config.getNode("currency", "symbol").getValue()), 2, true, config.getNode("currency", "prefix-symbol").getBoolean());
    loadJobs = config.getNode("features", "jobs", "enable").getBoolean();
    loadSalary = config.getNode("features", "jobs", "salary").getBoolean();
    databaseActive = config.getNode("database", "enable").getBoolean();
    jobPermissions = config.getNode("features", "jobs", "permissions").getBoolean();
    jobNotifications = config.getNode("features", "jobs", "notifications").getBoolean();
    loadMoneyCap = config.getNode("features", "moneycap", "enable").getBoolean();
    if (databaseActive) {
        databaseUrl = config.getNode("database", "url").getString();
        databaseUser = config.getNode("database", "user").getString();
        databasePassword = config.getNode("database", "password").getString();
        sqlHandler = new SQLHandler(this);
    }
    saveInterval = config.getNode("save-interval").getInt(30);
    accountManager = new AccountManager(this);
    game.getServiceManager().setProvider(this, EconomyService.class, accountManager);
    //Only setup job stuff if config is set to load jobs
    if (loadJobs) {
        teJobManager = new TEJobManager(this);
    }
    if (loadMoneyCap == true) {
        moneyCap = BigDecimal.valueOf(config.getNode("features", "moneycap", "amount").getFloat()).setScale(2, BigDecimal.ROUND_DOWN);
    }
}
Also used : TECurrency(com.erigitic.config.TECurrency) SQLHandler(com.erigitic.sql.SQLHandler) AccountManager(com.erigitic.config.AccountManager) TEJobManager(com.erigitic.jobs.TEJobManager) Listener(org.spongepowered.api.event.Listener)

Example 33 with Listener

use of org.spongepowered.api.event.Listener 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 34 with Listener

use of org.spongepowered.api.event.Listener in project TotalEconomy by Erigitic.

the class TEJobManager 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.getItemStackTransaction().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();
                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("catch", fishName);
                }
                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) FishData(org.spongepowered.api.data.manipulator.mutable.item.FishData) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Listener(org.spongepowered.api.event.Listener)

Example 35 with Listener

use of org.spongepowered.api.event.Listener in project HuskyCrates-Sponge by codeHusky.

the class HuskyCrates method entityInteract.

@Listener
public void entityInteract(InteractEntityEvent.Secondary.MainHand event) {
    //System.out.println(event.getTargetEntity().toContainer().get(DataQuery.of("UnsafeData","crateID")));
    if (event.getCause().root() instanceof Player) {
        Player plr = (Player) event.getCause().root();
        if (plr.getItemInHand(HandTypes.MAIN_HAND).isPresent() && plr.hasPermission("huskycrates.wand")) {
            ItemStack hand = plr.getItemInHand(HandTypes.MAIN_HAND).get();
            if (hand.getItem() == ItemTypes.BLAZE_ROD) {
                if (hand.toContainer().get(DataQuery.of("UnsafeData", "crateID")).isPresent()) {
                    if (!crateUtilities.physicalCrates.containsKey(event.getTargetEntity().getLocation())) {
                        //System.out.println(event.getTargetEntity().getLocation().getBlockPosition());
                        event.getTargetEntity().offer(Keys.AI_ENABLED, false);
                        event.getTargetEntity().offer(Keys.IS_SILENT, true);
                        crateUtilities.physicalCrates.put(event.getTargetEntity().getLocation(), new PhysicalCrate(event.getTargetEntity().getLocation(), hand.toContainer().get(DataQuery.of("UnsafeData", "crateID")).get().toString(), this));
                        crateUtilities.physicalCrates.get(event.getTargetEntity().getLocation()).createHologram();
                        updatePhysicalCrates();
                    } else {
                        event.getTargetEntity().offer(Keys.AI_ENABLED, true);
                        event.getTargetEntity().offer(Keys.IS_SILENT, false);
                        crateUtilities.physicalCrates.get(event.getTargetEntity().getLocation()).as.remove();
                        crateUtilities.physicalCrates.remove(event.getTargetEntity().getLocation());
                        updatePhysicalCrates();
                    }
                    event.setCancelled(true);
                    return;
                }
            }
        }
        if (crateUtilities.physicalCrates.containsKey(event.getTargetEntity().getLocation())) {
            String crateType = crateUtilities.physicalCrates.get(event.getTargetEntity().getLocation()).vc.id;
            VirtualCrate vc = crateUtilities.getVirtualCrate(crateType);
            crateUtilities.physicalCrates.get(event.getTargetEntity().getLocation()).createHologram();
            //crateUtilities.recognizeChest(te.getLocation());
            event.setCancelled(true);
            if (plr.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
                ItemStack inhand = plr.getItemInHand(HandTypes.MAIN_HAND).get();
                if (inhand.getItem() == vc.getKeyType()) {
                    if (inhand.toContainer().get(DataQuery.of("UnsafeData", "crateID")).isPresent()) {
                        String id = inhand.toContainer().get(DataQuery.of("UnsafeData", "crateID")).get().toString();
                        if (id.equals(crateType)) {
                            if (!plr.hasPermission("huskycrates.tester")) {
                                if (inhand.getQuantity() == 1)
                                    plr.setItemInHand(HandTypes.MAIN_HAND, null);
                                else {
                                    ItemStack tobe = inhand.copy();
                                    tobe.setQuantity(tobe.getQuantity() - 1);
                                    plr.setItemInHand(HandTypes.MAIN_HAND, tobe);
                                }
                            }
                            Task.Builder upcoming = scheduler.createTaskBuilder();
                            upcoming.execute(() -> {
                                crateUtilities.launchCrateForPlayer(crateType, plr, this);
                            }).delayTicks(1).submit(this);
                            return;
                        }
                    }
                }
            }
            plr.playSound(SoundTypes.BLOCK_ANVIL_LAND, event.getTargetEntity().getLocation().getPosition(), 0.3);
            try {
                plr.sendMessage(TextSerializers.FORMATTING_CODE.deserialize(vc.langData.formatter(vc.langData.prefix + vc.langData.noKeyMessage, null, plr, vc, null)));
            } catch (Exception e) {
                plr.sendMessage(Text.of(TextColors.RED, "Critical crate failure, contact the administrator. (Admins, check console!)"));
                e.printStackTrace();
            }
        }
    }
}
Also used : VirtualCrate(pw.codehusky.huskycrates.crate.VirtualCrate) Player(org.spongepowered.api.entity.living.player.Player) Task(org.spongepowered.api.scheduler.Task) PhysicalCrate(pw.codehusky.huskycrates.crate.PhysicalCrate) ItemStack(org.spongepowered.api.item.inventory.ItemStack) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) IOException(java.io.IOException) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) Listener(org.spongepowered.api.event.Listener)

Aggregations

Listener (org.spongepowered.api.event.Listener)93 Player (org.spongepowered.api.entity.living.player.Player)41 World (org.spongepowered.api.world.World)28 Location (org.spongepowered.api.world.Location)21 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)18 ItemStack (org.spongepowered.api.item.inventory.ItemStack)18 Entity (org.spongepowered.api.entity.Entity)14 Text (org.spongepowered.api.text.Text)14 BlockType (org.spongepowered.api.block.BlockType)11 ItemStackFactory.newItemStack (com.skelril.nitro.item.ItemStackFactory.newItemStack)10 ItemStack (net.minecraft.item.ItemStack)10 Task (org.spongepowered.api.scheduler.Task)10 Vector3d (com.flowpowered.math.vector.Vector3d)6 EntityPlayer (net.minecraft.entity.player.EntityPlayer)6 ItemDropper (com.skelril.nitro.item.ItemDropper)5 ModifierService (com.skelril.skree.service.ModifierService)5 RegionService (com.skelril.skree.service.RegionService)5 WorldService (com.skelril.skree.service.WorldService)5 Region (com.skelril.skree.service.internal.region.Region)5 UUID (java.util.UUID)5