/**
   * Load all Channels from the Channels.yml If it doesn't exist, create it from the resource in
   * this jar.
   *
   * @param filepath
   * @param defaultRes
   * @return true if the channels were loaded
   */
  @SuppressWarnings("unchecked")
  public boolean loadChannels(String filepath, String defaultRes) {

    String filename = filepath + FileMgmt.fileSeparator() + defaultRes;

    Map<String, Object> file = FileMgmt.getFile(filename, defaultRes, null);
    if (file != null) {

      for (String rootNode : file.keySet()) {

        if (rootNode.equalsIgnoreCase("Channels")) {
          // Parse the channels
          Map<String, Object> allChannelNodes = (Map<String, Object>) file.get(rootNode);

          // Load channels if the file is NOT empty
          if (allChannelNodes != null) {
            for (String channelKey : allChannelNodes.keySet()) {
              if (channelKey.equalsIgnoreCase("spam_time"))
                ChatSettings.setSpam_time((Double) allChannelNodes.get(channelKey));

              Map<String, Object> thisChannelNode =
                  (Map<String, Object>) allChannelNodes.get(channelKey);
              Channel channel = new StandardChannel(plugin, channelKey.toLowerCase());

              for (String key : thisChannelNode.keySet()) {
                Object element = thisChannelNode.get(key);

                if (key.equalsIgnoreCase("commands"))
                  if (element instanceof ArrayList) channel.setCommands((List<String>) element);
                  else if (element instanceof String)
                    channel.setCommands(Arrays.asList(element.toString()));

                if (key.equalsIgnoreCase("type"))
                  if (element instanceof String)
                    channel.setType(channelTypes.valueOf(element.toString()));

                if (key.equalsIgnoreCase("channeltag"))
                  if (element instanceof String) channel.setChannelTag(element.toString());

                if (key.equalsIgnoreCase("messagecolour"))
                  if (element instanceof String) channel.setMessageColour(element.toString());

                if (key.equalsIgnoreCase("permission"))
                  if (element instanceof String) channel.setPermission(element.toString());

                if (key.equalsIgnoreCase("craftIRCTag"))
                  if (element instanceof String) channel.setCraftIRCTag(element.toString());

                if (key.equalsIgnoreCase("range"))
                  channel.setRange(Double.valueOf(element.toString()));
              }

              plugin.getChannelsHandler().addChannel(channel);

              // System.out.print("Channel: " + channel.getName() + " : Type : " +
              // channel.getType().name());
            }
            return true;
          }
        }
      }
    }
    return false;
  }
  /**
   * Load the ChatConfig.yml
   *
   * <p>If it doesn't exist, create it from the resource in this jar.
   *
   * @param filepath
   * @param defaultRes
   * @return true if the config was loaded
   */
  @SuppressWarnings({"unchecked"})
  public boolean loadConfig(String filepath, String defaultRes) {

    String filename = filepath + FileMgmt.fileSeparator() + defaultRes;

    // Pass the plugin reference so it can load the defaults if needed.
    Map<String, Object> file = FileMgmt.getFile(filename, defaultRes, plugin);

    if (file != null) {

      for (String Key : file.keySet()) {

        if (Key.equalsIgnoreCase("spam_time"))
          ChatSettings.setSpam_time(Double.parseDouble((file.get(Key)).toString()));

        if (Key.equalsIgnoreCase("HeroicDeathToIRC")) {
          Map<String, Object> subNodes = (Map<String, Object>) file.get(Key);

          for (String element : subNodes.keySet()) {
            if (element.equalsIgnoreCase("enabled"))
              ChatSettings.setHeroicDeathToIRC(Boolean.valueOf(subNodes.get(element).toString()));

            if (element.equalsIgnoreCase("craftIRCTags"))
              ChatSettings.setheroicDeathTags(subNodes.get(element).toString());
          }
        }

        if (Key.equalsIgnoreCase("modify_chat")) {
          Map<String, Object> subNodes = (Map<String, Object>) file.get(Key);

          for (String element : subNodes.keySet()) {
            if (element.equalsIgnoreCase("enable"))
              ChatSettings.setModify_chat(Boolean.valueOf(subNodes.get(element).toString()));

            if (element.equalsIgnoreCase("per_world"))
              ChatSettings.setPer_world(Boolean.valueOf(subNodes.get(element).toString()));
          }
        }

        if (Key.equalsIgnoreCase("colour")) {
          Map<String, Object> subNodes = (Map<String, Object>) file.get(Key);

          for (String element : subNodes.keySet()) {
            if (element.equalsIgnoreCase("king"))
              ChatSettings.setKingColour(subNodes.get(element).toString());

            if (element.equalsIgnoreCase("mayor"))
              ChatSettings.setMayorColour(subNodes.get(element).toString());

            if (element.equalsIgnoreCase("resident"))
              ChatSettings.setResidentColour(subNodes.get(element).toString());
          }
        }

        if (Key.equalsIgnoreCase("tag_formats")) {
          Map<String, Object> subNodes = (Map<String, Object>) file.get(Key);

          for (String element : subNodes.keySet()) {
            if (element.equalsIgnoreCase("world"))
              ChatSettings.setWorldTag(subNodes.get(element).toString());

            if (element.equalsIgnoreCase("town"))
              ChatSettings.setTownTag(subNodes.get(element).toString());

            if (element.equalsIgnoreCase("nation"))
              ChatSettings.setNationTag(subNodes.get(element).toString());

            if (element.equalsIgnoreCase("both"))
              ChatSettings.setBothTags(subNodes.get(element).toString());
          }
        }

        if (Key.equalsIgnoreCase("channel_formats")) {
          Map<String, Object> subNodes = (Map<String, Object>) file.get(Key);

          channelFormats group = new channelFormats(Key);

          for (String element : subNodes.keySet()) {
            if (element.equalsIgnoreCase("global"))
              group.setGLOBAL(subNodes.get(element).toString());

            if (element.equalsIgnoreCase("town")) group.setTOWN(subNodes.get(element).toString());

            if (element.equalsIgnoreCase("nation"))
              group.setNATION(subNodes.get(element).toString());

            if (element.equalsIgnoreCase("default"))
              group.setDEFAULT(subNodes.get(element).toString());
          }
          ChatSettings.addFormatGroup(group);
        }

        if (Key.equalsIgnoreCase("worlds")) {
          Map<String, Object> allWorlds = (Map<String, Object>) file.get(Key);

          if (allWorlds != null) {
            for (String worlds : allWorlds.keySet()) {
              Map<String, Object> world = (Map<String, Object>) allWorlds.get(worlds);

              channelFormats group = new channelFormats(worlds);

              for (String element : world.keySet()) {
                if (element.equalsIgnoreCase("global"))
                  group.setGLOBAL(world.get(element).toString());

                if (element.equalsIgnoreCase("town")) group.setTOWN(world.get(element).toString());

                if (element.equalsIgnoreCase("nation"))
                  group.setNATION(world.get(element).toString());

                if (element.equalsIgnoreCase("default"))
                  group.setDEFAULT(world.get(element).toString());
              }
              ChatSettings.addFormatGroup(group);
            }
          }
        }
      }

      if (ChatSettings.populateWorldFormats()) saveConfig(filename);

      return true;
    }

    return false;
  }