Search in sources :

Example 1 with Settings

use of de.Keyle.MyPet.api.util.configuration.settings.Settings 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 Settings

use of de.Keyle.MyPet.api.util.configuration.settings.Settings in project MyPet by xXKeyleXx.

the class EntityListener method on.

@EventHandler(priority = EventPriority.MONITOR)
public void on(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().isDead() && !(event.getEntity() instanceof MyPetBukkitEntity)) {
        if (MyPetApi.getMyPetInfo().isLeashableEntityType(event.getEntity().getType())) {
            ItemStack leashItem = null;
            ItemStack leashItemArrow = null;
            Player player;
            if (Configuration.Misc.ALLOW_RANGED_LEASHING && event.getDamager() instanceof Projectile) {
                Projectile projectile = (Projectile) event.getDamager();
                if (!(projectile.getShooter() instanceof Player)) {
                    return;
                }
                player = (Player) projectile.getShooter();
                List<MetadataValue> metaList;
                if (projectile.hasMetadata("MyPetLeashItem")) {
                    metaList = projectile.getMetadata("MyPetLeashItem");
                    for (MetadataValue meta : metaList) {
                        if (meta.getOwningPlugin().getName().equals("MyPet")) {
                            leashItem = (ItemStack) meta.value();
                            break;
                        }
                    }
                    if (leashItem == null) {
                        return;
                    }
                    projectile.removeMetadata("MyPetLeashItem", MyPetApi.getPlugin());
                }
                if (projectile.hasMetadata("MyPetLeashItemArrow")) {
                    metaList = projectile.getMetadata("MyPetLeashItemArrow");
                    for (MetadataValue meta : metaList) {
                        if (meta.getOwningPlugin().getName().equals("MyPet")) {
                            leashItemArrow = (ItemStack) meta.value();
                            break;
                        }
                    }
                    if (leashItemArrow == null) {
                        return;
                    }
                    projectile.removeMetadata("MyPetLeashItemArrow", MyPetApi.getPlugin());
                }
            } else if (event.getDamager() instanceof Player) {
                player = (Player) event.getDamager();
                if (MyPetApi.getCompatUtil().compareWithMinecraftVersion("1.9") >= 0) {
                    leashItem = player.getEquipment().getItemInMainHand();
                } else {
                    leashItem = player.getItemInHand();
                }
            } else {
                return;
            }
            if (!MyPetApi.getMyPetManager().hasActiveMyPet(player) && !justLeashed.contains(player.getUniqueId())) {
                LivingEntity leashTarget = (LivingEntity) event.getEntity();
                MyPetType petType = MyPetType.byEntityTypeName(leashTarget.getType().name());
                ConfigItem neededLeashItem = MyPetApi.getMyPetInfo().getLeashItem(petType);
                if (!Permissions.has(player, "MyPet.leash." + petType.name())) {
                    return;
                }
                boolean usedArrow = false;
                if (!neededLeashItem.compare(leashItem)) {
                    if (leashItemArrow == null || !neededLeashItem.compare(leashItemArrow)) {
                        return;
                    } else {
                        usedArrow = true;
                    }
                }
                for (LeashHook hook : MyPetApi.getPluginHookManager().getHooks(LeashHook.class)) {
                    if (!hook.canLeash(player, leashTarget)) {
                        return;
                    }
                }
                boolean willBeLeashed = true;
                for (Settings flagSettings : MyPetApi.getMyPetInfo().getLeashFlagSettings(petType)) {
                    String flagName = flagSettings.getName();
                    LeashFlag flag = MyPetApi.getLeashFlagManager().getLeashFlag(flagName);
                    if (flag == null) {
                        MyPetApi.getLogger().warning("\"" + flagName + "\" is not a valid leash requirement!");
                        continue;
                    }
                    MyPetPlayer myPetPlayer = null;
                    if (MyPetApi.getPlayerManager().isMyPetPlayer(player)) {
                        myPetPlayer = MyPetApi.getPlayerManager().getMyPetPlayer(player);
                    }
                    if (!flag.check(player, leashTarget, event.getDamage(), flagSettings)) {
                        willBeLeashed = false;
                        if (myPetPlayer != null) {
                            if (myPetPlayer.isCaptureHelperActive()) {
                                String message = flag.getMissingMessage(player, leashTarget, event.getDamage(), flagSettings);
                                if (message != null) {
                                    myPetPlayer.sendMessage(LeashFlag.getMessagePrefix(false) + message, 10000);
                                }
                            }
                        }
                    } else {
                        if (myPetPlayer != null) {
                            if (myPetPlayer.isCaptureHelperActive()) {
                                String message = flag.getMissingMessage(player, leashTarget, event.getDamage(), flagSettings);
                                if (message != null) {
                                    myPetPlayer.sendMessage(LeashFlag.getMessagePrefix(true) + message, 10000);
                                }
                            }
                        }
                    }
                }
                if (willBeLeashed) {
                    event.setCancelled(true);
                    final MyPetPlayer owner;
                    if (MyPetApi.getPlayerManager().isMyPetPlayer(player)) {
                        owner = MyPetApi.getPlayerManager().getMyPetPlayer(player);
                    } else {
                        owner = MyPetApi.getPlayerManager().registerMyPetPlayer(player);
                    }
                    final InactiveMyPet inactiveMyPet = new InactiveMyPet(owner);
                    inactiveMyPet.setPetType(petType);
                    inactiveMyPet.setPetName(Translation.getString("Name." + petType.name(), inactiveMyPet.getOwner()));
                    WorldGroup worldGroup = WorldGroup.getGroupByWorld(player.getWorld().getName());
                    inactiveMyPet.setWorldGroup(worldGroup.getName());
                    inactiveMyPet.getOwner().setMyPetForWorldGroup(worldGroup, inactiveMyPet.getUUID());
                    /*
                        if(leashTarget.getCustomName() != null)
                        {
                            inactiveMyPet.setPetName(leashTarget.getCustomName());
                        }
                        */
                    Optional<EntityConverterService> converter = MyPetApi.getServiceManager().getService(EntityConverterService.class);
                    converter.ifPresent(service -> inactiveMyPet.setInfo(service.convertEntity(leashTarget)));
                    boolean remove = true;
                    for (LeashEntityHook hook : MyPetApi.getPluginHookManager().getHooks(LeashEntityHook.class)) {
                        if (!hook.prepare(leashTarget)) {
                            remove = false;
                        }
                    }
                    if (remove) {
                        leashTarget.remove();
                    }
                    if (!usedArrow) {
                        if (Configuration.Misc.CONSUME_LEASH_ITEM && player.getGameMode() != GameMode.CREATIVE && leashItem != null) {
                            if (leashItem.getAmount() > 1) {
                                leashItem.setAmount(leashItem.getAmount() - 1);
                            } else {
                                if (MyPetApi.getCompatUtil().compareWithMinecraftVersion("1.9") >= 0) {
                                    player.getEquipment().setItemInMainHand(null);
                                } else {
                                    player.setItemInHand(null);
                                }
                            }
                        }
                    }
                    MyPetCreateEvent createEvent = new MyPetCreateEvent(inactiveMyPet, MyPetCreateEvent.Source.Leash);
                    Bukkit.getServer().getPluginManager().callEvent(createEvent);
                    MyPetSaveEvent saveEvent = new MyPetSaveEvent(inactiveMyPet);
                    Bukkit.getServer().getPluginManager().callEvent(saveEvent);
                    justLeashed.add(player.getUniqueId());
                    MyPetApi.getPlugin().getRepository().addMyPet(inactiveMyPet, new RepositoryCallback<Boolean>() {

                        @Override
                        public void callback(Boolean value) {
                            owner.sendMessage(Translation.getString("Message.Leash.Add", owner));
                            Optional<MyPet> myPet = MyPetApi.getMyPetManager().activateMyPet(inactiveMyPet);
                            myPet.ifPresent(MyPet::createEntity);
                            if (owner.isCaptureHelperActive()) {
                                owner.setCaptureHelperActive(false);
                                owner.sendMessage(Util.formatText(Translation.getString("Message.Command.CaptureHelper.Mode", owner), Translation.getString("Name.Disabled", owner)));
                            }
                            justLeashed.remove(player.getUniqueId());
                        }
                    });
                }
            }
        }
    }
}
Also used : EntityConverterService(de.Keyle.MyPet.api.util.service.types.EntityConverterService) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) InactiveMyPet(de.Keyle.MyPet.entity.InactiveMyPet) ConfigItem(de.Keyle.MyPet.api.util.ConfigItem) MyPetSaveEvent(de.Keyle.MyPet.api.event.MyPetSaveEvent) MyPetType(de.Keyle.MyPet.api.entity.MyPetType) Settings(de.Keyle.MyPet.api.util.configuration.settings.Settings) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) MetadataValue(org.bukkit.metadata.MetadataValue) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) LeashEntityHook(de.Keyle.MyPet.api.util.hooks.types.LeashEntityHook) WorldGroup(de.Keyle.MyPet.api.WorldGroup) MyPetCreateEvent(de.Keyle.MyPet.api.event.MyPetCreateEvent) LeashFlag(de.Keyle.MyPet.api.entity.leashing.LeashFlag) ItemStack(org.bukkit.inventory.ItemStack) LeashHook(de.Keyle.MyPet.api.util.hooks.types.LeashHook) MyPetBukkitEntity(de.Keyle.MyPet.api.entity.MyPetBukkitEntity) EventHandler(org.bukkit.event.EventHandler)

Example 3 with Settings

use of de.Keyle.MyPet.api.util.configuration.settings.Settings in project MyPet by xXKeyleXx.

the class MyPetPlayerImpl method checkTamable.

protected boolean checkTamable(LivingEntity leashTarget, Player p) {
    for (Settings flagSettings : MyPetApi.getMyPetInfo().getLeashFlagSettings(MyPetType.byEntityTypeName(leashTarget.getType().name()))) {
        String flagName = flagSettings.getName();
        LeashFlag flag = MyPetApi.getLeashFlagManager().getLeashFlag(flagName);
        if (flag != null && (flag.ignoredByHelper() || !flag.check(p, leashTarget, 0, flagSettings))) {
            return false;
        }
    }
    return true;
}
Also used : LeashFlag(de.Keyle.MyPet.api.entity.leashing.LeashFlag) Settings(de.Keyle.MyPet.api.util.configuration.settings.Settings)

Example 4 with Settings

use of de.Keyle.MyPet.api.util.configuration.settings.Settings in project MyPet by xXKeyleXx.

the class Skilltree method checkRequirements.

public boolean checkRequirements(MyPet pet) {
    boolean usable = true;
    for (Settings flagSettings : requirementSettings) {
        String reqName = flagSettings.getName();
        Requirement requirement = MyPetApi.getSkilltreeManager().getRequirement(reqName);
        if (requirement == null) {
            MyPetApi.getLogger().warning("\"" + reqName + "\" is not a valid skilltree requirement!");
            continue;
        }
        if (!requirement.check(this, pet, flagSettings)) {
            usable = false;
        }
        if (!usable) {
            break;
        }
    }
    return usable;
}
Also used : Requirement(de.Keyle.MyPet.api.skill.skilltree.requirements.Requirement) Settings(de.Keyle.MyPet.api.util.configuration.settings.Settings)

Example 5 with Settings

use of de.Keyle.MyPet.api.util.configuration.settings.Settings in project MyPet by xXKeyleXx.

the class ConfigurationLoader method loadLeashFlags.

public static void loadLeashFlags(MyPetType type, List<String> leashFlagStrings) {
    MyPetApi.getMyPetInfo().clearLeashFlagSettings(type);
    for (String leashFlagString : leashFlagStrings) {
        boolean hasParameter = leashFlagString.contains(":");
        String[] data = leashFlagString.split(":", 2);
        Settings settings = new Settings(data[0]);
        if (hasParameter) {
            settings.load(data[1]);
        }
        MyPetApi.getMyPetInfo().addLeashFlagSetting(type, settings);
    }
}
Also used : Settings(de.Keyle.MyPet.api.util.configuration.settings.Settings)

Aggregations

Settings (de.Keyle.MyPet.api.util.configuration.settings.Settings)5 MyPetType (de.Keyle.MyPet.api.entity.MyPetType)2 LeashFlag (de.Keyle.MyPet.api.entity.leashing.LeashFlag)2 WorldGroup (de.Keyle.MyPet.api.WorldGroup)1 MyPetBukkitEntity (de.Keyle.MyPet.api.entity.MyPetBukkitEntity)1 MyPetCreateEvent (de.Keyle.MyPet.api.event.MyPetCreateEvent)1 MyPetSaveEvent (de.Keyle.MyPet.api.event.MyPetSaveEvent)1 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)1 Upgrade (de.Keyle.MyPet.api.skill.Upgrade)1 DynamicLevelRule (de.Keyle.MyPet.api.skill.skilltree.levelrule.DynamicLevelRule)1 LevelRule (de.Keyle.MyPet.api.skill.skilltree.levelrule.LevelRule)1 StaticLevelRule (de.Keyle.MyPet.api.skill.skilltree.levelrule.StaticLevelRule)1 Requirement (de.Keyle.MyPet.api.skill.skilltree.requirements.Requirement)1 ConfigItem (de.Keyle.MyPet.api.util.ConfigItem)1 LeashEntityHook (de.Keyle.MyPet.api.util.hooks.types.LeashEntityHook)1 LeashHook (de.Keyle.MyPet.api.util.hooks.types.LeashHook)1 EntityConverterService (de.Keyle.MyPet.api.util.service.types.EntityConverterService)1 InactiveMyPet (de.Keyle.MyPet.entity.InactiveMyPet)1 EventHandler (org.bukkit.event.EventHandler)1 ItemStack (org.bukkit.inventory.ItemStack)1