Solved Grabbing Values from the config.yml

Discussion in 'Plugin Development' started by jthort, Apr 11, 2014.

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

    jthort

    I'm creating a setter and getter method, and it's constantly going to be changing values. The problem is the class is going to be grabbing from the config and setting config values every time I change a variable.

    Would it be faster to directly change the config by using
    Code:
    String townLocation = mainClass.getConfig().getString("Town.Location");
    In each of my get methods, or set the default to a value like this
    Code:
    mainClass.getConfig().addDefault(townName+".Location",townLocation);
    Then grab it by simply returning townLocation?

    Right now this is my setter and getter
    Code:java
    1. package Towns;
    2.  
    3. import org.bukkit.Location;
    4. import org.bukkit.Material;
    5. import org.bukkit.block.Block;
    6. /*
    7. * Arguments to create a new Town:
    8. * TownLocation TownName TownLeader(create) ClassName
    9. */
    10.  
    11. /*
    12. * Configuration town setup:
    13. * <TownName>
    14. *
    15. */
    16.  
    17. public class Town {
    18. Block startingBlock;
    19. public Main mainClass;
    20. String townName;
    21. String townLeader;
    22. //TODO: boolean townIsPrivate = true;
    23. public Town(Location townLocation, String townName, String townLeader, Main mainClass){
    24. //Set up variables
    25. this.mainClass = mainClass;
    26. this.townName = townName;
    27. this.townLeader = townLeader;
    28. //Create starting Block
    29. startingBlock = townLocation.getWorld().getHighestBlockAt(townLocation);
    30. startingBlock.setType(Material.GOLD_BLOCK);
    31.  
    32. mainClass.getConfig().addDefault(townName+".Name",townName);
    33. mainClass.getConfig().addDefault(townName+".location."+"x",startingBlock.getLocation().getX());
    34. mainClass.getConfig().addDefault(townName+".location."+"y",startingBlock.getLocation().getY());
    35. mainClass.getConfig().addDefault(townName+".location."+"z",startingBlock.getLocation().getZ());
    36. mainClass.getConfig().addDefault(townName+".location."+"World",startingBlock.getLocation().getWorld().getName().toString());
    37. mainClass.getConfig().addDefault(townName+".Leader",townLeader);
    38. }
    39.  
    40. public Location getTownCenter(){
    41. return startingBlock.getLocation();
    42. }
    43. public String getTownLeader(){
    44. return townLeader;
    45. }
    46. public String getTownName(){
    47. return townName;
    48. }
    49. public void setTownLeader(String newLeader){
    50. townLeader = newLeader;
    51. }
    52.  
    53.  
    54. }
    55.  


    Would it just be more efficiant to leave out all the variables and just use the config file? Thanks

    N3rdFall Sorry it's hard to explain, i'll try to explain it a different way

    I did the following
    Code:
    mainClass.getConfig().addDefault(townName+".Leader",townLeader);
    But when I change townLeader to something else,

    Code:
    townLeader = "me";
    it doesnt change the config value. Yes I used the saveConfig function. Is it supposed to change on it's own? Or do I have to reset it to the new value every time using the set function

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page