Search in sources :

Example 1 with LevelRule

use of de.Keyle.MyPet.api.skill.skilltree.levelrule.LevelRule 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 LevelRule

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

the class Skilltree method getNotifications.

public List<String> getNotifications(int level) {
    List<String> notifications = new ArrayList<>();
    List<LevelRule> rules = new ArrayList<>(this.notifications.keySet());
    rules.sort(Comparator.comparingInt(LevelRule::getPriority));
    for (LevelRule rule : rules) {
        if (rule.check(level)) {
            notifications.add(this.notifications.get(rule));
        }
    }
    return notifications;
}
Also used : LevelRule(de.Keyle.MyPet.api.skill.skilltree.levelrule.LevelRule)

Example 3 with LevelRule

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

the class SkillTreeLoaderJSON method loadLevelRule.

private static LevelRule loadLevelRule(String levelRuleString) {
    LevelRule levelRule;
    if (levelRuleString.contains("%")) {
        int modulo = 1;
        int min = 0;
        int max = 0;
        Matcher matcher = LEVEL_RULE_REGEX.matcher(levelRuleString);
        while (matcher.find()) {
            if (matcher.group(0).startsWith("%")) {
                modulo = Math.max(1, Integer.parseInt(matcher.group(1)));
            } else if (matcher.group(0).startsWith(">")) {
                min = Integer.parseInt(matcher.group(3));
            } else if (matcher.group(0).startsWith("<")) {
                max = Integer.parseInt(matcher.group(2));
            }
        }
        levelRule = new DynamicLevelRule(modulo, min, max);
    } else {
        String[] levelStrings = levelRuleString.split(",");
        List<Integer> levels = new ArrayList<>();
        for (String levelString : levelStrings) {
            if (Util.isInt(levelString.trim())) {
                levels.add(Integer.parseInt(levelString.trim()));
            }
        }
        levelRule = new StaticLevelRule(levels);
    }
    return levelRule;
}
Also used : Matcher(java.util.regex.Matcher) DynamicLevelRule(de.Keyle.MyPet.api.skill.skilltree.levelrule.DynamicLevelRule) StaticLevelRule(de.Keyle.MyPet.api.skill.skilltree.levelrule.StaticLevelRule) 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)

Example 4 with LevelRule

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

Aggregations

LevelRule (de.Keyle.MyPet.api.skill.skilltree.levelrule.LevelRule)4 Upgrade (de.Keyle.MyPet.api.skill.Upgrade)2 DynamicLevelRule (de.Keyle.MyPet.api.skill.skilltree.levelrule.DynamicLevelRule)2 StaticLevelRule (de.Keyle.MyPet.api.skill.skilltree.levelrule.StaticLevelRule)2 MyPetType (de.Keyle.MyPet.api.entity.MyPetType)1 Settings (de.Keyle.MyPet.api.util.configuration.settings.Settings)1 Matcher (java.util.regex.Matcher)1