Section Creation

Discussion in 'Plugin Development' started by Dreeass, Apr 30, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Dreeass

    So I'm trying to create a list that looks like this:

    homes:
    hashmap key 1
    hashmap key 2
    hashmap key 3
    hashmap key 3
    hashmap key 3

    This will be in a config file, but I went through a lot of the options for it and they all give NPE when trying to call/use it:

    Code:java
    1.  
    2. FileConfiguration.createPath(HomesConfig, "homes");
    3. for (Entry<String, Location> e:homes.entrySet()) {
    4. HomesConfig.set("homes."+e.getKey(), e.getValue());
    5. }
    6.  
     
  2. You can't save locations like that since they are not serializable. You will have to write your own location class or save a String representation of a location so you can just create a new location with what you saved.

    Other than that, I offered a piece of code in the Resources section that can save HashMaps to configuration files.
    --> http://forums.bukkit.org/threads/res-intermediate-method-to-store-hash-maps-in-configurations.49347/

    An example of your own location class would be like the following class:
    https://github.com/Pandemoneus/MobT...r/pandemoneus/mobTrigger/util/MTLocation.java
     
  3. Offline

    Dreeass

    This is what I got actually:
    Code:java
    1.  
    2. public FileConfiguration getHomesConfig() {
    3. if (HomesConfig == null) {
    4. File CMBhomesFile = new File(getDataFolder(), "homes.csv");
    5. Boolean cmb = this.getConfig().getBoolean("Commandbookhomes");
    6. if(cmb) {
    7. BufferedReader r = null;
    8. try {
    9. r = new BufferedReader(new FileReader(CMBhomesFile));
    10. } catch (FileNotFoundException e1) {
    11. logger.info("FileNotFoundExeption:");
    12. logger.info(e1.toString());
    13. e1.printStackTrace();
    14. }
    15. try {
    16. while((q = r.readLine()) != null) {
    17. String[] info = q.split(",");
    18. Location l = new Location(null, 0, 0, 0, 0, 0);
    19.  
    20. l.setWorld(Bukkit.getWorld(info[1].replaceAll("\"", "").trim()));
    21. l.setX(Double.parseDouble(info[3].replaceAll("\"", "").trim()));
    22. l.setY(Double.parseDouble(info[4].replaceAll("\"", "").trim()));
    23. l.setZ(Double.parseDouble(info[5].replaceAll("\"", "").trim()));
    24. l.setYaw(Float.parseFloat(info[6].replaceAll("\"", "").trim()));
    25. l.setPitch(Float.parseFloat(info[7].replaceAll("\"", "").trim()));
    26.  
    27. homes.put(info[0].replaceAll("\"", "").trim(), l);
    28. //HomesConfig.set("homes", homes);
    29. FileConfiguration.createPath(HomesConfig, "homes");
    30. for (Entry<String, Location> e:homes.entrySet()) {
    31. HomesConfig.set("homes."+e.getKey(), e.getValue());
    32. }
    33. }
    34. } catch (NumberFormatException e) {
    35. logger.info("NumberFormatException: ");
    36. logger.info(e.toString());
    37. e.printStackTrace();
    38. } catch (IOException e) {
    39. logger.info("IOException: ");
    40. logger.info(e.toString());
    41. e.printStackTrace();
    42. }
    43. }
    44. reloadHomesConfig();
    45. }
    46. return HomesConfig;
    47. }
    48.  


    So I have to create the Hashmapwriter like you and then call it?
     
Thread Status:
Not open for further replies.

Share This Page