Search in sources :

Example 1 with Skill

use of de.Keyle.MyPet.api.skill.skilltree.Skill in project MyPet by xXKeyleXx.

the class MyPet method schedule.

public void schedule() {
    if (status != PetState.Despawned && getOwner().isOnline()) {
        if (status == PetState.Dead) {
            if (!Configuration.Respawn.DISABLE_AUTO_RESPAWN) {
                respawnTime--;
            }
            if (respawnTime <= 0) {
                respawnPet();
            } else if (MyPetApi.getPluginHookManager().isHookActive(VaultHook.class) && getOwner().hasAutoRespawnEnabled() && respawnTime >= getOwner().getAutoRespawnMin() && Permissions.has(getOwner().getPlayer(), "MyPet.user.respawn")) {
                double cost = respawnTime * Configuration.Respawn.COSTS_FACTOR + Configuration.Respawn.COSTS_FIXED;
                VaultHook vaultHook = MyPetApi.getPluginHookManager().getHook(VaultHook.class);
                if (vaultHook.canPay(getOwner().getPlayer(), cost)) {
                    vaultHook.pay(getOwner().getPlayer(), cost);
                    getOwner().sendMessage(Util.formatText(Translation.getString("Message.Command.Respawn.Paid", petOwner.getLanguage()), petName, cost + " " + vaultHook.currencyNameSingular()));
                    respawnTime = 0;
                }
            }
        }
        if (status == PetState.Here) {
            for (Skill skill : skills.all()) {
                if (skill instanceof Scheduler) {
                    ((Scheduler) skill).schedule();
                }
            }
            if (Configuration.HungerSystem.USE_HUNGER_SYSTEM) {
                if (saturation > 1 && --hungerTime <= 0) {
                    saturation--;
                    hungerTime = Configuration.HungerSystem.HUNGER_SYSTEM_TIME;
                    if (saturation == 66) {
                        getOwner().sendMessage(Util.formatText(Translation.getString("Message.Hunger.Rumbling", getOwner()), getPetName()));
                    } else if (saturation == 33) {
                        getOwner().sendMessage(Util.formatText(Translation.getString("Message.Hunger.Hungry", getOwner()), getPetName()));
                    } else if (saturation == 1) {
                        getOwner().sendMessage(Util.formatText(Translation.getString("Message.Hunger.Starving", getOwner()), getPetName()));
                    }
                }
                if (saturation == 1 && getHealth() >= 2) {
                    getEntity().ifPresent(entity -> entity.damage(1.));
                }
            }
        }
    }
}
Also used : Skill(de.Keyle.MyPet.api.skill.skilltree.Skill) Scheduler(de.Keyle.MyPet.api.util.Scheduler) VaultHook(de.Keyle.MyPet.util.hooks.VaultHook)

Example 2 with Skill

use of de.Keyle.MyPet.api.skill.skilltree.Skill in project MyPet by xXKeyleXx.

the class MyPet method save.

@Override
public TagCompound save() {
    TagCompound petNBT = new TagCompound();
    petNBT.getCompoundData().put("UUID", new TagString(getUUID().toString()));
    petNBT.getCompoundData().put("Type", new TagString(this.getPetType().name()));
    petNBT.getCompoundData().put("Health", new TagDouble(this.getHealth()));
    petNBT.getCompoundData().put("Respawntime", new TagInt(this.respawnTime));
    petNBT.getCompoundData().put("Hunger", new TagDouble(this.saturation));
    petNBT.getCompoundData().put("Name", new TagString(this.petName));
    petNBT.getCompoundData().put("WorldGroup", new TagString(this.worldGroup));
    petNBT.getCompoundData().put("Exp", new TagDouble(this.getExp()));
    petNBT.getCompoundData().put("LastUsed", new TagLong(this.lastUsed));
    petNBT.getCompoundData().put("Info", writeExtendedInfo());
    petNBT.getCompoundData().put("Internal-Owner-UUID", new TagString(this.petOwner.getInternalUUID().toString()));
    petNBT.getCompoundData().put("Wants-To-Respawn", new TagByte(wantsToRespawn));
    if (this.skilltree != null) {
        petNBT.getCompoundData().put("Skilltree", new TagString(skilltree.getName()));
    }
    TagCompound skillsNBT = new TagCompound();
    Collection<Skill> skillList = this.getSkills().all();
    if (skillList.size() > 0) {
        for (Skill skill : skillList) {
            if (skill instanceof NBTStorage) {
                NBTStorage storageSkill = (NBTStorage) skill;
                TagCompound s = storageSkill.save();
                if (s != null) {
                    skillsNBT.getCompoundData().put(skill.getName(), s);
                }
            }
        }
    }
    petNBT.getCompoundData().put("Skills", skillsNBT);
    return petNBT;
}
Also used : Skill(de.Keyle.MyPet.api.skill.skilltree.Skill) NBTStorage(de.Keyle.MyPet.api.util.NBTStorage)

Example 3 with Skill

use of de.Keyle.MyPet.api.skill.skilltree.Skill in project MyPet by xXKeyleXx.

the class MyPetEntityListener method onMyPet.

@EventHandler
public void onMyPet(final EntityDamageByEntityEvent event) {
    // noinspection ConstantConditions
    if (event.getEntity() == null) {
        // catch invalid events (i.e. EnchantmentAPI)
        return;
    }
    if (WorldGroup.getGroupByWorld(event.getEntity().getWorld()).isDisabled()) {
        return;
    }
    if (event.getEntity() instanceof MyPetBukkitEntity) {
        MyPetBukkitEntity craftMyPet = (MyPetBukkitEntity) event.getEntity();
        MyPet myPet = craftMyPet.getMyPet();
        if (event.getDamager() instanceof Player || (event.getDamager() instanceof Projectile && ((Projectile) event.getDamager()).getShooter() instanceof Player)) {
            Player damager;
            if (event.getDamager() instanceof Projectile) {
                damager = (Player) ((Projectile) event.getDamager()).getShooter();
            } else {
                damager = (Player) event.getDamager();
            }
            ItemStack leashItem;
            if (MyPetApi.getCompatUtil().compareWithMinecraftVersion("1.9") >= 0) {
                leashItem = damager.getEquipment().getItemInMainHand();
            } else {
                leashItem = damager.getItemInHand();
            }
            if (MyPetApi.getMyPetInfo().getLeashItem(myPet.getPetType()).compare(leashItem)) {
                boolean infoShown = false;
                if (CommandInfo.canSee(PetInfoDisplay.Name.adminOnly, damager, myPet)) {
                    damager.sendMessage(ChatColor.AQUA + myPet.getPetName() + ChatColor.RESET + ":");
                    infoShown = true;
                }
                if (CommandInfo.canSee(PetInfoDisplay.Owner.adminOnly, damager, myPet) && myPet.getOwner().getPlayer() != damager) {
                    damager.sendMessage("   " + Translation.getString("Name.Owner", damager) + ": " + myPet.getOwner().getName());
                    infoShown = true;
                }
                if (CommandInfo.canSee(PetInfoDisplay.HP.adminOnly, damager, myPet)) {
                    String msg;
                    double health = myPet.getHealth();
                    double maxHealth = myPet.getMaxHealth();
                    if (health > maxHealth / 3 * 2) {
                        msg = "" + ChatColor.GREEN;
                    } else if (health > maxHealth / 3) {
                        msg = "" + ChatColor.YELLOW;
                    } else {
                        msg = "" + ChatColor.RED;
                    }
                    msg += String.format("%1.2f", health) + ChatColor.WHITE + "/" + String.format("%1.2f", maxHealth);
                    damager.sendMessage("   " + Translation.getString("Name.HP", damager) + ": " + msg);
                    infoShown = true;
                }
                if (myPet.getStatus() == PetState.Dead && CommandInfo.canSee(PetInfoDisplay.RespawnTime.adminOnly, damager, myPet)) {
                    damager.sendMessage("   " + Translation.getString("Name.Respawntime", damager) + ": " + myPet.getRespawnTime());
                    infoShown = true;
                }
                if (myPet.getDamage() > 0 && CommandInfo.canSee(PetInfoDisplay.Damage.adminOnly, damager, myPet)) {
                    damager.sendMessage("   " + Translation.getString("Name.Damage", damager) + ": " + String.format("%1.2f", myPet.getDamage()));
                    infoShown = true;
                }
                if (myPet.getRangedDamage() > 0 && CommandInfo.canSee(PetInfoDisplay.RangedDamage.adminOnly, damager, myPet)) {
                    damager.sendMessage("   " + Translation.getString("Name.RangedDamage", damager) + ": " + String.format("%1.2f", myPet.getRangedDamage()));
                    infoShown = true;
                }
                if (Configuration.HungerSystem.USE_HUNGER_SYSTEM && CommandInfo.canSee(PetInfoDisplay.Hunger.adminOnly, damager, myPet)) {
                    damager.sendMessage("   " + Translation.getString("Name.Hunger", damager) + ": " + Math.round(myPet.getSaturation()));
                    FancyMessage m = new FancyMessage("   " + Translation.getString("Name.Food", damager) + ": ");
                    boolean comma = false;
                    for (ConfigItem material : MyPetApi.getMyPetInfo().getFood(myPet.getPetType())) {
                        ItemStack is = material.getItem();
                        if (is == null) {
                            continue;
                        }
                        if (comma) {
                            m.then(", ");
                        }
                        if (is.hasItemMeta() && is.getItemMeta().hasDisplayName()) {
                            m.then(is.getItemMeta().getDisplayName());
                        } else {
                            try {
                                m.thenTranslate(MyPetApi.getPlatformHelper().getVanillaName(is));
                            } catch (Exception e) {
                                MyPetApi.getLogger().warning("A food item for \"" + myPet.getPetType().name() + "\" caused an error. If you think this is a bug please report it to the MyPet developer.");
                                MyPetApi.getLogger().warning(is.toString());
                                e.printStackTrace();
                                continue;
                            }
                        }
                        m.color(ChatColor.GOLD);
                        ItemTooltip it = new ItemTooltip();
                        it.setMaterial(is.getType());
                        if (is.hasItemMeta()) {
                            if (is.getItemMeta().hasDisplayName()) {
                                it.setTitle(is.getItemMeta().getDisplayName());
                            }
                            if (is.getItemMeta().hasLore()) {
                                it.setLore(is.getItemMeta().getLore().toArray(new String[0]));
                            }
                        }
                        m.itemTooltip(it);
                        comma = true;
                    }
                    MyPetApi.getPlatformHelper().sendMessageRaw(damager, m.toJSONString());
                    infoShown = true;
                }
                if (myPet.getSkills().has(Behavior.class) && CommandInfo.canSee(PetInfoDisplay.Behavior.adminOnly, damager, myPet)) {
                    Behavior behavior = myPet.getSkills().get(Behavior.class);
                    damager.sendMessage("   " + Translation.getString("Name.Skill.Behavior", damager) + ": " + Translation.getString("Name." + behavior.getBehavior().name(), damager));
                    infoShown = true;
                }
                if (CommandInfo.canSee(PetInfoDisplay.Skilltree.adminOnly, damager, myPet) && myPet.getSkilltree() != null) {
                    damager.sendMessage("   " + Translation.getString("Name.Skilltree", damager) + ": " + Colorizer.setColors(myPet.getSkilltree().getDisplayName()));
                    infoShown = true;
                }
                if (CommandInfo.canSee(PetInfoDisplay.Level.adminOnly, damager, myPet)) {
                    int lvl = myPet.getExperience().getLevel();
                    damager.sendMessage("   " + Translation.getString("Name.Level", damager) + ": " + lvl);
                    infoShown = true;
                }
                int maxLevel = myPet.getSkilltree() != null ? myPet.getSkilltree().getMaxLevel() : Configuration.LevelSystem.Experience.LEVEL_CAP;
                if (CommandInfo.canSee(PetInfoDisplay.Exp.adminOnly, damager, myPet) && myPet.getExperience().getLevel() < maxLevel) {
                    double exp = myPet.getExperience().getCurrentExp();
                    double reqEXP = myPet.getExperience().getRequiredExp();
                    damager.sendMessage("   " + Translation.getString("Name.Exp", damager) + ": " + String.format("%1.2f", exp) + "/" + String.format("%1.2f", reqEXP));
                    infoShown = true;
                }
                if (myPet.getOwner().getDonationRank() != DonateCheck.DonationRank.None) {
                    infoShown = true;
                    String donationMessage = "" + ChatColor.GOLD;
                    donationMessage += myPet.getOwner().getDonationRank().getDefaultIcon();
                    donationMessage += " " + Translation.getString("Name.Title." + myPet.getOwner().getDonationRank().name(), damager) + " ";
                    donationMessage += myPet.getOwner().getDonationRank().getDefaultIcon();
                    damager.sendMessage("   " + donationMessage);
                }
                if (!infoShown) {
                    damager.sendMessage(Translation.getString("Message.No.NothingToSeeHere", myPet.getOwner()));
                }
                event.setCancelled(true);
            } else if (myPet.getOwner().equals(damager) && (!Configuration.Misc.OWNER_CAN_ATTACK_PET)) {
                event.setCancelled(true);
            } else if (!myPet.getOwner().equals(damager) && !MyPetApi.getHookHelper().canHurt(damager, myPet.getOwner().getPlayer(), true)) {
                event.setCancelled(true);
            }
        }
        if (event.getDamager() instanceof CraftMyPetProjectile) {
            EntityMyPetProjectile projectile = ((CraftMyPetProjectile) event.getDamager()).getMyPetProjectile();
            if (projectile != null && projectile.getShooter() != null) {
                if (myPet == projectile.getShooter().getMyPet()) {
                    event.setCancelled(true);
                }
                if (!MyPetApi.getHookHelper().canHurt(projectile.getShooter().getOwner().getPlayer(), myPet.getOwner().getPlayer(), true)) {
                    event.setCancelled(true);
                }
            }
        }
        if (!event.isCancelled() && event.getDamager() instanceof LivingEntity) {
            LivingEntity damager = (LivingEntity) event.getDamager();
            if (damager instanceof Player) {
                if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), (Player) damager, true)) {
                    return;
                }
            }
            if (!isSkillActive) {
                for (Skill skill : myPet.getSkills().all()) {
                    if (skill instanceof OnDamageByEntitySkill) {
                        OnDamageByEntitySkill damageByEntitySkill = (OnDamageByEntitySkill) skill;
                        if (damageByEntitySkill.trigger()) {
                            isSkillActive = true;
                            damageByEntitySkill.apply(damager, event);
                            isSkillActive = false;
                        }
                    }
                }
            }
        }
    }
}
Also used : MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) EntityMyPetProjectile(de.Keyle.MyPet.api.entity.skill.ranged.EntityMyPetProjectile) ConfigItem(de.Keyle.MyPet.api.util.ConfigItem) FancyMessage(de.Keyle.MyPet.api.util.chat.FancyMessage) OnDamageByEntitySkill(de.Keyle.MyPet.api.skill.OnDamageByEntitySkill) EntityMyPetProjectile(de.Keyle.MyPet.api.entity.skill.ranged.EntityMyPetProjectile) CraftMyPetProjectile(de.Keyle.MyPet.api.entity.skill.ranged.CraftMyPetProjectile) ItemTooltip(de.Keyle.MyPet.api.util.chat.parts.ItemTooltip) OnDamageByEntitySkill(de.Keyle.MyPet.api.skill.OnDamageByEntitySkill) Skill(de.Keyle.MyPet.api.skill.skilltree.Skill) OnHitSkill(de.Keyle.MyPet.api.skill.OnHitSkill) CraftMyPetProjectile(de.Keyle.MyPet.api.entity.skill.ranged.CraftMyPetProjectile) Behavior(de.Keyle.MyPet.api.skill.skills.Behavior) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Example 4 with Skill

use of de.Keyle.MyPet.api.skill.skilltree.Skill in project MyPet by xXKeyleXx.

the class LevelListener method on.

@EventHandler
public void on(MyPetLevelEvent event) {
    if (event instanceof MyPetLevelUpEvent || event instanceof MyPetLevelDownEvent) {
        return;
    }
    MyPet myPet = event.getPet();
    int lvl = event.getLevel();
    myPet.getSkills().all().forEach(Skill::reset);
    Skilltree skilltree = myPet.getSkilltree();
    if (skilltree != null) {
        for (int i = 1; i <= lvl; i++) {
            List<Upgrade> upgrades = skilltree.getUpgrades(i);
            for (Upgrade upgrade : upgrades) {
                SkillName sn = Util.getClassAnnotation(upgrade.getClass(), SkillName.class);
                if (sn != null) {
                    Skill skill = myPet.getSkills().get(sn.value());
                    if (skill != null) {
                        upgrade.apply(skill);
                    }
                }
            }
        }
    }
}
Also used : MyPetLevelUpEvent(de.Keyle.MyPet.api.event.MyPetLevelUpEvent) Skilltree(de.Keyle.MyPet.api.skill.skilltree.Skilltree) Skill(de.Keyle.MyPet.api.skill.skilltree.Skill) SkillName(de.Keyle.MyPet.api.skill.SkillName) MyPetLevelDownEvent(de.Keyle.MyPet.api.event.MyPetLevelDownEvent) MyPet(de.Keyle.MyPet.api.entity.MyPet) Upgrade(de.Keyle.MyPet.api.skill.Upgrade) EventHandler(org.bukkit.event.EventHandler)

Example 5 with Skill

use of de.Keyle.MyPet.api.skill.skilltree.Skill in project MyPet by xXKeyleXx.

the class LevelListener method on.

@EventHandler
public void on(MyPetLevelDownEvent event) {
    MyPet myPet = event.getPet();
    int lvl = event.getLevel();
    int fromLvl = event.fromLevel();
    if (!event.isQuiet()) {
        myPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.LevelSystem.LevelDown", event.getOwner()), myPet.getPetName(), event.getLevel()));
    }
    Skilltree skilltree = myPet.getSkilltree();
    if (skilltree != null) {
        for (int i = fromLvl; i > lvl; i--) {
            List<Upgrade> upgrades = skilltree.getUpgrades(i);
            for (Upgrade upgrade : upgrades) {
                SkillName sn = Util.getClassAnnotation(upgrade.getClass(), SkillName.class);
                if (sn != null) {
                    Skill skill = myPet.getSkills().get(sn.value());
                    if (skill != null) {
                        upgrade.invert(skill);
                    }
                }
            }
        }
    }
    if (myPet.getStatus() == MyPet.PetState.Here) {
        myPet.getEntity().ifPresent(entity -> {
            entity.getHandle().updateNameTag();
            if (!event.isQuiet()) {
                myPet.setHealth(myPet.getMaxHealth());
                myPet.setSaturation(100);
                new FixedCircleAnimation(1, entity.getEyeHeight() + 0.5, 10, new EntityLocationHolder(entity)) {

                    @Override
                    protected void playParticleEffect(Location location) {
                        MyPetApi.getPlatformHelper().playParticleEffect(location, ParticleCompat.BLOCK_CRACK.get(), 0, 0, 0, 0, 1, 32, ParticleCompat.REDSTONE_BLOCK_DATA);
                    }
                }.once();
                entity.getWorld().playSound(entity.getLocation(), Sound.valueOf(SoundCompat.LEVEL_DOWN.get()), 1F, 0.7F);
            }
        });
    }
}
Also used : Skilltree(de.Keyle.MyPet.api.skill.skilltree.Skilltree) SkillName(de.Keyle.MyPet.api.skill.SkillName) Skill(de.Keyle.MyPet.api.skill.skilltree.Skill) MyPet(de.Keyle.MyPet.api.entity.MyPet) FixedCircleAnimation(de.Keyle.MyPet.api.util.animation.particle.FixedCircleAnimation) EntityLocationHolder(de.Keyle.MyPet.api.util.location.EntityLocationHolder) Upgrade(de.Keyle.MyPet.api.skill.Upgrade) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Skill (de.Keyle.MyPet.api.skill.skilltree.Skill)11 MyPet (de.Keyle.MyPet.api.entity.MyPet)5 EventHandler (org.bukkit.event.EventHandler)5 NBTStorage (de.Keyle.MyPet.api.util.NBTStorage)4 SkillName (de.Keyle.MyPet.api.skill.SkillName)3 Upgrade (de.Keyle.MyPet.api.skill.Upgrade)3 Skilltree (de.Keyle.MyPet.api.skill.skilltree.Skilltree)3 CraftMyPetProjectile (de.Keyle.MyPet.api.entity.skill.ranged.CraftMyPetProjectile)2 EntityMyPetProjectile (de.Keyle.MyPet.api.entity.skill.ranged.EntityMyPetProjectile)2 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)2 OnDamageByEntitySkill (de.Keyle.MyPet.api.skill.OnDamageByEntitySkill)2 OnHitSkill (de.Keyle.MyPet.api.skill.OnHitSkill)2 EntityLocationHolder (de.Keyle.MyPet.api.util.location.EntityLocationHolder)2 Location (org.bukkit.Location)2 StoredMyPet (de.Keyle.MyPet.api.entity.StoredMyPet)1 MyPetActivatedEvent (de.Keyle.MyPet.api.event.MyPetActivatedEvent)1 MyPetDamageEvent (de.Keyle.MyPet.api.event.MyPetDamageEvent)1 MyPetLevelDownEvent (de.Keyle.MyPet.api.event.MyPetLevelDownEvent)1 MyPetLevelUpEvent (de.Keyle.MyPet.api.event.MyPetLevelUpEvent)1 MyPetLoadEvent (de.Keyle.MyPet.api.event.MyPetLoadEvent)1