Search in sources :

Example 1 with Upgrade

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

the class SkillTreeLoaderJSON method loadSkilltree.

public static void loadSkilltree(JsonObject skilltreeObject) {
    if (!containsKey(skilltreeObject, "ID")) {
        return;
    }
    Skilltree skilltree;
    String skilltreeID = get(skilltreeObject, "ID").getAsString();
    if (MyPetApi.getSkilltreeManager().hasSkilltree(skilltreeID)) {
        return;
    }
    skilltree = new Skilltree(skilltreeID);
    tryToLoad("Name", () -> {
        if (containsKey(skilltreeObject, "Name")) {
            skilltree.setDisplayName(get(skilltreeObject, "Name").getAsString());
        }
    });
    tryToLoad("Permission", () -> {
        if (containsKey(skilltreeObject, "Permission")) {
            String permission = get(skilltreeObject, "Permission").getAsString();
            Settings settings = new Settings("Permission");
            settings.load(permission);
            skilltree.addRequirementSettings(settings);
        // TODO warnung zum aktualisieren
        }
    });
    tryToLoad("Display", () -> {
        if (containsKey(skilltreeObject, "Display")) {
            skilltree.setDisplayName(get(skilltreeObject, "Display").getAsString());
        }
    });
    tryToLoad("MaxLevel", () -> {
        if (containsKey(skilltreeObject, "MaxLevel")) {
            skilltree.setMaxLevel((get(skilltreeObject, "MaxLevel").getAsInt()));
        }
    });
    tryToLoad("RequiredLevel", () -> {
        if (containsKey(skilltreeObject, "RequiredLevel")) {
            skilltree.setRequiredLevel((get(skilltreeObject, "RequiredLevel").getAsInt()));
        }
    });
    tryToLoad("Order", () -> {
        if (containsKey(skilltreeObject, "Order")) {
            skilltree.setOrder((get(skilltreeObject, "Order").getAsInt()));
        }
    });
    tryToLoad("Weight", () -> {
        if (containsKey(skilltreeObject, "Weight")) {
            skilltree.setWeight((get(skilltreeObject, "Weight").getAsDouble()));
        }
    });
    tryToLoad("MobTypes", () -> {
        if (containsKey(skilltreeObject, "MobTypes")) {
            List<MyPetType> availableTypes = Arrays.stream(MyPetType.values()).filter(MyPetType::checkMinecraftVersion).collect(Collectors.toList());
            JsonArray mobTypeArray = get(skilltreeObject, "MobTypes").getAsJsonArray();
            Set<MyPetType> mobTypes = new HashSet<>();
            if (mobTypeArray.size() == 0) {
                mobTypes.addAll(availableTypes);
            } else {
                boolean allNegative = true;
                for (JsonElement o : mobTypeArray) {
                    String type = o.getAsString();
                    if (!type.startsWith("-")) {
                        allNegative = false;
                        break;
                    }
                }
                if (allNegative) {
                    mobTypes.addAll(availableTypes);
                }
                mobTypeArray.forEach(jsonElement -> {
                    String type = jsonElement.getAsString();
                    if (type.equals("*")) {
                        mobTypes.addAll(availableTypes);
                    } else {
                        boolean negative = false;
                        if (type.startsWith("-")) {
                            type = type.substring(1);
                            negative = true;
                        }
                        MyPetType mobType = MyPetType.byName(type, false);
                        if (mobType != null && mobType.checkMinecraftVersion()) {
                            if (negative) {
                                mobTypes.remove(mobType);
                            } else {
                                mobTypes.add(mobType);
                            }
                        }
                    }
                });
            }
            skilltree.setMobTypes(mobTypes);
        }
    });
    tryToLoad("Icon", () -> {
        if (containsKey(skilltreeObject, "Icon")) {
            JsonObject iconObject = get(skilltreeObject, "Icon").getAsJsonObject();
            SkilltreeIcon icon = new SkilltreeIcon();
            tryToLoad("Icon.Material", () -> {
                if (containsKey(iconObject, "Material")) {
                    icon.setMaterial(get(iconObject, "Material").getAsString());
                }
            });
            tryToLoad("Icon.Glowing", () -> {
                if (containsKey(iconObject, "Glowing")) {
                    icon.setGlowing(get(iconObject, "Glowing").getAsBoolean());
                }
            });
            skilltree.setIcon(icon);
        }
    });
    tryToLoad("Inheritance", () -> {
        if (containsKey(skilltreeObject, "Inheritance")) {
            JsonObject inheritanceObject = get(skilltreeObject, "Inheritance").getAsJsonObject();
            if (containsKey(inheritanceObject, "Skilltree")) {
                skilltree.setInheritedSkilltreeName(get(inheritanceObject, "Skilltree").getAsString());
            }
        }
    });
    tryToLoad("Description", () -> {
        if (containsKey(skilltreeObject, "Description")) {
            JsonArray descriptionArray = get(skilltreeObject, "Description").getAsJsonArray();
            descriptionArray.forEach(jsonElement -> skilltree.addDescriptionLine(jsonElement.getAsString()));
        }
    });
    tryToLoad("Notifications", () -> {
        if (containsKey(skilltreeObject, "Notifications")) {
            JsonObject notificationsObject = get(skilltreeObject, "Notifications").getAsJsonObject();
            for (String levelRuleString : notificationsObject.keySet()) {
                tryToLoad("Notification." + levelRuleString, () -> {
                    LevelRule levelRule = loadLevelRule(levelRuleString);
                    String message = notificationsObject.get(levelRuleString).getAsString();
                    skilltree.addNotification(levelRule, message);
                });
            }
        }
    });
    tryToLoad("Requirements", () -> {
        if (containsKey(skilltreeObject, "Requirements")) {
            JsonArray requirementsArray = get(skilltreeObject, "Requirements").getAsJsonArray();
            requirementsArray.forEach(jsonElement -> {
                boolean hasParameter = jsonElement.getAsString().contains(":");
                String[] data = jsonElement.getAsString().split(":", 2);
                Settings settings = new Settings(data[0]);
                if (hasParameter) {
                    tryToLoad("Requirement." + jsonElement.getAsString(), () -> settings.load(data[1]));
                }
                skilltree.addRequirementSettings(settings);
            });
        }
    });
    tryToLoad("Skills", () -> {
        if (containsKey(skilltreeObject, "Skills")) {
            JsonObject skillsObject = get(skilltreeObject, "Skills").getAsJsonObject();
            for (String skillName : skillsObject.keySet()) {
                JsonObject skillObject = skillsObject.getAsJsonObject(skillName);
                tryToLoad("Skills." + skillName + ".Upgrades", () -> {
                    if (containsKey(skillObject, "Upgrades")) {
                        JsonObject upgradesObject = get(skillObject, "Upgrades").getAsJsonObject();
                        for (String levelRuleString : upgradesObject.keySet()) {
                            tryToLoad("Skills." + skillName + ".Upgrades." + levelRuleString, () -> {
                                LevelRule levelRule = loadLevelRule(levelRuleString);
                                JsonObject upgradeObject = upgradesObject.getAsJsonObject(levelRuleString);
                                tryToLoad("Skills." + skillName + ".Upgrades." + levelRuleString + ".Upgrade", () -> {
                                    Upgrade upgrade = loadUpgrade(skillName, upgradeObject);
                                    skilltree.addUpgrade(levelRule, upgrade);
                                });
                            });
                        }
                    }
                });
            }
        }
    });
    MyPetApi.getSkilltreeManager().registerSkilltree(skilltree);
}
Also used : Upgrade(de.Keyle.MyPet.api.skill.Upgrade) MyPetType(de.Keyle.MyPet.api.entity.MyPetType) DynamicLevelRule(de.Keyle.MyPet.api.skill.skilltree.levelrule.DynamicLevelRule) StaticLevelRule(de.Keyle.MyPet.api.skill.skilltree.levelrule.StaticLevelRule) LevelRule(de.Keyle.MyPet.api.skill.skilltree.levelrule.LevelRule) Settings(de.Keyle.MyPet.api.util.configuration.settings.Settings)

Example 2 with Upgrade

use of de.Keyle.MyPet.api.skill.Upgrade 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 3 with Upgrade

use of de.Keyle.MyPet.api.skill.Upgrade 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)

Example 4 with Upgrade

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

the class Skilltree method getUpgrades.

protected List<Upgrade> getUpgrades(int level, Set<String> computedSkilltrees) {
    List<Upgrade> upgrades = new ArrayList<>();
    computedSkilltrees.add(this.skilltreeName);
    if (inheritedSkilltreeName != null && !inheritedSkilltreeName.isEmpty() && !computedSkilltrees.contains(inheritedSkilltreeName)) {
        if (MyPetApi.getSkilltreeManager().hasSkilltree(inheritedSkilltreeName)) {
            upgrades.addAll(MyPetApi.getSkilltreeManager().getSkilltree(inheritedSkilltreeName).getUpgrades(level, computedSkilltrees));
        }
    }
    List<LevelRule> rules = new ArrayList<>(this.upgrades.keySet());
    rules.sort(Comparator.comparingInt(LevelRule::getPriority));
    for (LevelRule rule : rules) {
        if (rule.check(level)) {
            upgrades.add(this.upgrades.get(rule));
        }
    }
    return upgrades;
}
Also used : LevelRule(de.Keyle.MyPet.api.skill.skilltree.levelrule.LevelRule) Upgrade(de.Keyle.MyPet.api.skill.Upgrade)

Example 5 with Upgrade

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

the class LevelListener method on.

@EventHandler
public void on(MyPetLevelUpEvent event) {
    MyPet myPet = event.getPet();
    int lvl = event.getLevel();
    int fromLvl = event.fromLevel();
    if (!event.isQuiet()) {
        int maxlevel = myPet.getSkilltree() != null ? myPet.getSkilltree().getMaxLevel() : 0;
        if (maxlevel != 0 && lvl >= maxlevel) {
            myPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.LevelSystem.ReachedMaxLevel", event.getOwner()), myPet.getPetName(), maxlevel));
        } else {
            myPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.LevelSystem.LevelUp", event.getOwner()), myPet.getPetName(), event.getLevel()));
        }
    }
    Skilltree skilltree = myPet.getSkilltree();
    if (skilltree != null) {
        for (int i = fromLvl + 1; i <= lvl; i++) {
            if (!event.isQuiet()) {
                List<String> notifications = skilltree.getNotifications(i);
                for (String notification : notifications) {
                    notification = notification.replace("{{owner}}", myPet.getOwner().getName()).replace("{{level}}", "" + lvl).replace("{{pet}}", myPet.getPetName());
                    notification = Colorizer.setColors(notification);
                    String[] lines = notification.split("(<br>|\\\\n|\n|<br\\s?/>)");
                    for (String line : lines) {
                        myPet.getOwner().sendMessage(line);
                    }
                }
            }
            Set<Skill> affectedSkills = new HashSet<>();
            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);
                        affectedSkills.add(skill);
                    }
                }
            }
            if (!event.isQuiet()) {
                for (Skill skill : affectedSkills) {
                    String[] messages = skill.getUpgradeMessage();
                    if (messages != null && messages.length > 0) {
                        for (String message : messages) {
                            myPet.getOwner().sendMessage("  " + message);
                        }
                    }
                }
            }
        }
    }
    if (myPet.getStatus() == MyPet.PetState.Here) {
        myPet.getEntity().ifPresent(entity -> {
            entity.getHandle().updateNameTag();
            if (!event.isQuiet()) {
                myPet.setHealth(myPet.getMaxHealth());
                myPet.setSaturation(100);
                new SpiralAnimation(1, entity.getEyeHeight() + 0.5, new EntityLocationHolder(entity)) {

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

Aggregations

Upgrade (de.Keyle.MyPet.api.skill.Upgrade)5 MyPet (de.Keyle.MyPet.api.entity.MyPet)3 SkillName (de.Keyle.MyPet.api.skill.SkillName)3 Skill (de.Keyle.MyPet.api.skill.skilltree.Skill)3 Skilltree (de.Keyle.MyPet.api.skill.skilltree.Skilltree)3 EventHandler (org.bukkit.event.EventHandler)3 LevelRule (de.Keyle.MyPet.api.skill.skilltree.levelrule.LevelRule)2 EntityLocationHolder (de.Keyle.MyPet.api.util.location.EntityLocationHolder)2 Location (org.bukkit.Location)2 MyPetType (de.Keyle.MyPet.api.entity.MyPetType)1 MyPetLevelDownEvent (de.Keyle.MyPet.api.event.MyPetLevelDownEvent)1 MyPetLevelUpEvent (de.Keyle.MyPet.api.event.MyPetLevelUpEvent)1 DynamicLevelRule (de.Keyle.MyPet.api.skill.skilltree.levelrule.DynamicLevelRule)1 StaticLevelRule (de.Keyle.MyPet.api.skill.skilltree.levelrule.StaticLevelRule)1 FixedCircleAnimation (de.Keyle.MyPet.api.util.animation.particle.FixedCircleAnimation)1 SpiralAnimation (de.Keyle.MyPet.api.util.animation.particle.SpiralAnimation)1 Settings (de.Keyle.MyPet.api.util.configuration.settings.Settings)1 HashSet (java.util.HashSet)1