Get data from string

Discussion in 'Spigot Plugin Development' started by MaTaMoR_, May 24, 2015.

  1. Hi, i was wondering which is the best way to get data from a string like this :

    On this moment im using this, but i don't know if it is efficient :

    Code (Text):

    //To get the string
        @Override
        public Map<WeaponInfo, Object> getInfo() {
            Map<WeaponInfo, Object> info = new LinkedHashMap<>();
                info.put(WeaponInfo.WEAPON_TYPE, getType());
                info.put(WeaponInfo.MATERIAL, getMaterial());
                info.put(WeaponInfo.NAME, getName());
                info.put(WeaponInfo.AMMO_TYPE, atype);
                info.put(WeaponInfo.MIN_DAMAGE, getMinDamage());
                info.put(WeaponInfo.MAX_DAMAGE, getMaxDamage());
                info.put(WeaponInfo.SPEED, 10D);
                info.put(WeaponInfo.AMMO_TYPE, atype);
                info.put(WeaponInfo.MAX_AMMO, maxammo);
                info.put(WeaponInfo.AMMO, ammo);
                info.put(WeaponInfo.ZOOM_TYPE, ztype);
                info.put(WeaponInfo.ZOOM_LEVEL, zoomlevel);
                info.put(WeaponInfo.WEAPON_MODE, mode);
           
            return info;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
       
            for(Map.Entry<WeaponInfo, Object> entry : getInfo().entrySet()) {
                sb.append(entry.getKey()).append("=").append(entry.getValue()).append(",");
            }
       
            return sb.toString().substring(0, sb.length() - 1);
        }

    //To get the data from the string
    private Object get(WeaponInfo info, String value) {
        switch(info) {
            case MATERIAL: {
                return Material.getMaterial(value);
            }
            case NAME: {
                return value;
            }
            case WEAPON_TYPE: {
                return WeaponType.get(value);
            }
            case MIN_DAMAGE: {
                return Double.parseDouble(value);
            }
            case MAX_DAMAGE: {
                return Double.parseDouble(value);
            }
            case AMMO_TYPE: {
                return AmmoType.get(value);
            }
            case MAX_AMMO: {
                return Integer.parseInt(value);
            }
            case AMMO: {
                return Integer.parseInt(value);
            }
            case ZOOM_TYPE: {
                return ZoomType.get(value);
            }
            case ZOOM_LEVEL: {
                return Integer.parseInt(value);
            }
            case WEAPON_MODE: {
                return WeaponMode.get(value);
            }
            default: return null;
        }
    }

    public Map<String, String> values(String data, String split, String separator) {
        Map<String, String> map = new LinkedHashMap<>();

        String[] splits = data.split(split);
        if(splits.length < 2) return map;

        for(String string : splits) {
            String[] values = string.split(separator);
            if(values.length < 2) continue;
       
            map.put(values[0], values[1]);
        }
        return map;
    }

    public Map<WeaponInfo, Object> getData(String string) {
        Map<WeaponInfo, Object> data = new LinkedHashMap<>();

        String[] type = string.split(":");
        if(!type[0].equals("Weapon"))  return data;

        for(Entry<String, String> entry : Util.values(type[1], ",", "=").entrySet()) {
            WeaponInfo info = WeaponInfo.get(entry.getKey());
            data.put(info, get(info, entry.getValue()));
        }
     
        return data;
    }
     
     
  2. Ouch, That's nasty. The best way to go about this would be to import the official JSON library into your project.

    If you look at this, you will see strings are generated in the format {color: blue, ammo: "5"} etc. etc.
     
  3. That will only change how i get the strings from the string.