Solved check if parameter is boolean/string/integer/list

Discussion in 'BungeeCord Plugin Development' started by Tanju42, Feb 23, 2020.

  1. so here is a example of a config.yml:
    Code (YAML):
    first:
      string
    : a string
      integer
    : 34
      alist
    :
      - 'A list'
      enabled
    : false
    Now i want to get the parameter first.integer but I dont know if its a integer or something else
    I tried something like Config.isInteger() but this is not implemented in bungeecord only in spigot :<

    I need the type(integer,string) for my plugin;

    i tried something like this but its not working properly:
    Code (Java):
    public static boolean isString(Configuration Config, String Parameter) {
            try {
                String.valueOf(Config.get(Parameter));
            } catch(Exception e) {
                return false;
            }
            return true;
        }
        public static boolean isInteger(Configuration Config, String Parameter) {
            try {
                Integer.parseInt((String) Config.get(Parameter));
            } catch(Exception e) {
                return false;
            }
            return true;
        }
        public static boolean isBoolean(Configuration Config, String Parameter) {
            if ( Config.get(Parameter).equals("true") || Config.get(Parameter).equals("false") ) {
                return true;
            }
            return false;
        }
        @SuppressWarnings({ "unchecked", "unused" })
        public static boolean isList(Configuration Config, String Parameter) {
            try {
                ArrayList<String> Array = new ArrayList<String>();
                if (Config.get(Parameter).equals("{}")) {
                    return true;
                }
                Array = (ArrayList<String>) Config.getList(Parameter);
            } catch(Exception e) {
                return false;
            }
            return true;
        }
    Pls someone I need help
     
    #1 Tanju42, Feb 23, 2020
    Last edited: Feb 23, 2020
  2. I fixxed it myself basically when you do Config.get() its really trycki and gets something that I dont need so I did String.valueOf(Config.get()) and viola it worked like a charm

    Here is the Code when someone needs it:
    Code (Java):
        public static boolean isInteger(Configuration Config, String Parameter) {
            try {
                Integer.parseInt(String.valueOf(Config.get(Parameter)));
            } catch(Exception e) {
                return false;
            }
            return true;
        }
        public static boolean isDouble(Configuration Config, String Parameter) {
            try {
                Double.parseDouble(String.valueOf(Config.get(Parameter)));
            } catch(Exception e) {
                return false;
            }
            return true;
        }
        public static boolean isBoolean(Configuration Config, String Parameter) {
            if ( String.valueOf(Config.get(Parameter)).equalsIgnoreCase("true") || String.valueOf(Config.get(Parameter)).equalsIgnoreCase("false") ) {
                return true;
            }
            return false;
        }
        public static boolean isList(Configuration Config, String Parameter) {
            if ( Config.getStringList(Parameter).size() == 0 ) {
                return false;
            }
            return true;
        }