Solved Removing Key and Value from Config Completely

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

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

    jthort

    I have the following configuration file
    Code:
    hey:
      Name: hey
      location:
        x: -156.0
        y: 80.0
        z: 371.0
        World: world
      Leader: NebulaSkies
      Open: false
      Members: []
      Admins: []
      Tier: 1
    so:
      Name: so
      location:
        x: 121.0
        y: 74.0
        z: 165.0
        World: world
      Leader: iiaann_
      Open: false
      Members: []
      Admins: []
      Tier: 1
    When I call this method to remove everything under and including "hey"
    Code:
    public void remove(){
              mainClass.getConfig().set("hey", null);
              mainClass.saveConfig();
            }
    Nothing happens to the config file

    What am I doing wrong?
     
  2. Offline

    Timbo_KZ

    I think you can't access the config like this (i.e. from another class).

    Try moving method remove() to the main class and then calling remove() from the initial class.
     
  3. Offline

    amhokies

    No, you can access the config from any class as long as you have access to your Plugin's instance.
     
  4. Offline

    jthort

    Timbo_KZ I passed it through a constructor, i can grab values from the config and set values fine.So there is no reason why it wouldnt work. Here is where I call the method (Yes it runs the method I tested it)
    Code:java
    1. else if(args[0].equalsIgnoreCase("disband") && sender instanceof Player){
    2. Player player = (Player) sender;
    3. if(playerIsInTown(player.getName())){
    4. Town town = getPlayersTown(player.getName());
    5. if(town.getTownLeader().equals(player.getName())){
    6. town.sendToAll(ChatColor.RED + "[Towns] Your faction has been disbanned by " + player.getName());
    7. town.remove();
    8. }else{
    9. player.sendMessage(ChatColor.RED + "[Towny] You must be the town leader to use this command!");
    10. }
    11. }else{
    12. player.sendMessage(ChatColor.RED + "[Towny] You have to be a admin of a town to disband");
    13. }
    14. }


    Town Class (Where i'm grabbing the method from
    Code:java
    1. package Towns;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Location;
    9. import org.bukkit.Material;
    10. import org.bukkit.block.Block;
    11. import org.bukkit.entity.Player;
    12.  
    13. /*
    14. * Arguments to create a new Town:
    15. * TownLocation TownName TownLeader(create) ClassName
    16. */
    17.  
    18. /*
    19. * Configuration town setup:
    20. * <TownName>:
    21. * Name: <TownName>
    22. * location:
    23. * x: <double>
    24. * y: <double>
    25. * z: <double>
    26. * world: <world>
    27. * leader: <username>
    28. * Open: <boolean>
    29. */
    30.  
    31. /*
    32. * Town Creation Defaults
    33. * Open = false
    34. * Leader = creator
    35. * Location = Players location on creation
    36. * town name = create command argument 1
    37. */
    38.  
    39. /*
    40. * Notes
    41. * Use mainClass.getConfig().getString(this.townName+".Name"); to get the towns name!
    42. */
    43.  
    44. /*
    45. * Rank permissions:
    46. * Member = nothin :(
    47. * Admin = promote, invite, kick
    48. * leader = promote, invite, kick, setowner
    49. */
    50.  
    51.  
    52. public class Town {
    53.  
    54. public Main mainClass;
    55. String townName;
    56. List<String> members = new ArrayList<String>();
    57. List<String> admins = new ArrayList<String>();
    58.  
    59. public Town(Location townLocation, String townName, String townLeader,Main mainClass){
    60. //Set up variables
    61. this.mainClass = mainClass;
    62. this.townName = townName;
    63. //Create starting Block
    64. Block startingBlock = townLocation.getWorld().getBlockAt(townLocation);
    65. if(!(startingBlock.getType().equals(Material.GOLD_BLOCK))){
    66. startingBlock.setType(Material.GOLD_BLOCK);
    67. }
    68.  
    69.  
    70. mainClass.getConfig().addDefault(townName+".Name",townName);
    71. mainClass.getConfig().addDefault(townName+".location."+"x",startingBlock.getLocation().getX());
    72. mainClass.getConfig().addDefault(townName+".location."+"y",startingBlock.getLocation().getY());
    73. mainClass.getConfig().addDefault(townName+".location."+"z",startingBlock.getLocation().getZ());
    74. mainClass.getConfig().addDefault(townName+".location."+"World",startingBlock.getLocation().getWorld().getName().toString());
    75. mainClass.getConfig().addDefault(townName+".Leader",townLeader);
    76. mainClass.getConfig().addDefault(townName+".Open", false);
    77. mainClass.getConfig().addDefault(townName+".Members", members);
    78. mainClass.getConfig().addDefault(townName+".Admins", admins);
    79. mainClass.getConfig().addDefault(townName+".Tier", 1);
    80. }
    81.  
    82. public Location getTownCenter(){
    83. return (new Location(
    84. Bukkit.getWorld(mainClass.getConfig().getString(this.townName+".location."+"World")),
    85. mainClass.getConfig().getDouble(this.townName+".location."+"x"),
    86. mainClass.getConfig().getDouble(this.townName+".location."+"y"),
    87. mainClass.getConfig().getDouble(this.townName+".location."+"z")
    88. ));
    89. }
    90. public int getTownTier(){
    91. return mainClass.getConfig().getInt(this.townName+".Tier");
    92. }
    93. public void setTownTier(int amount){
    94. mainClass.getConfig().set(this.townName+".Tier", amount);
    95. }
    96. public String getTownLeader(){
    97. return mainClass.getConfig().getString(this.townName+".Leader");
    98. }
    99. public String getTownName(){
    100. return mainClass.getConfig().getString(this.townName+".Name");
    101. }
    102. public void setTownName(String townName){
    103. mainClass.getConfig().set(this.townName+".Name", townName);
    104. mainClass.saveConfig();
    105. }
    106. public void setTownLeader(String newLeader){
    107. mainClass.getConfig().set(this.townName+".Leader", newLeader);
    108. mainClass.saveConfig();
    109. }
    110. public void setOpen(boolean tf){
    111. if(tf){
    112. mainClass.getConfig().set(this.townName+".Open", true);
    113. }else{
    114. mainClass.getConfig().set(this.townName+".Open", false);
    115. }
    116. mainClass.saveConfig();
    117. }
    118. public boolean isOpen(){
    119. return mainClass.getConfig().getBoolean(this.townName+".Open");
    120. }
    121. public void addMember(String player){
    122. members.add(player);
    123. mainClass.getConfig().set(this.townName+".Members", members);
    124. mainClass.saveConfig();
    125. }
    126. public void addAdmin(String player){
    127. admins.add(player);
    128. mainClass.getConfig().set(this.townName+".Admins", admins);
    129. mainClass.saveConfig();
    130. }
    131. public List<String> getMembers(){
    132. return members;
    133. }
    134. public List<String> getAdmins(){
    135. return admins;
    136. }
    137. public void remove(String player){
    138. admins.remove(player);
    139. members.remove(player);
    140. mainClass.getConfig().set(this.townName+".Members", members);
    141. mainClass.saveConfig();
    142. }
    143. public boolean isTownLeader(String player){
    144. if(getTownLeader().equals(player)){
    145. return true;
    146. }
    147. return false;
    148. }
    149. public void sendToAdmins(String message){
    150. for(String player: mainClass.getConfig().getStringList(this.townName+".Admins")){
    151. try{
    152. Player players = Bukkit.getPlayer(player);
    153. players.sendMessage(message);
    154. }catch(Exception ex){
    155.  
    156. }
    157. }
    158. try{
    159. Player player = Bukkit.getPlayer(getTownLeader());
    160. player.sendMessage(message);
    161. }catch(Exception ex){
    162.  
    163. }
    164. }
    165. public void sendToAll(String message){
    166. for(String player: mainClass.getConfig().getStringList(this.townName+".Admins")){
    167. try{
    168. Player players = Bukkit.getPlayer(player);
    169. players.sendMessage(message);
    170. }catch(Exception ex){
    171.  
    172. }
    173. }
    174. try{
    175. Player player = Bukkit.getPlayer(getTownLeader());
    176. player.sendMessage(message);
    177. }catch(Exception ex){
    178.  
    179. }
    180. for(String player: mainClass.getConfig().getStringList(this.townName+".Members")){
    181. try{
    182. Player players = Bukkit.getPlayer(player);
    183. players.sendMessage(message);
    184. }catch(Exception ex){
    185.  
    186. }
    187. }
    188. }
    189. public void remove(){
    190. mainClass.getConfig().set("hey", null);
    191. mainClass.saveConfig();
    192. }
    193.  
    194. }
    195.  


    Edit: I have no idea what's going on, the config file is saving but it doesn't change.
     
  5. Offline

    Timbo_KZ

    jthort
    Just for the sake of experiment, try setting hey to "" (empty string) rather than null and see what happens. Technically it should delete all children of hey and change into a string so be careful if you don't want to lose data under hey.
     
  6. Offline

    jthort

    Timbo_KZ I've tried that and still nothing
     
  7. Offline

    Timbo_KZ

    jthort
    Well, if everything else in Town object works properly and this doesn't (given you don't see any errors in console), try adding Bukkit.getConsoleSender().sendMessage("Remove Attempt"); to the remove() method. In theory, this should fire in server console each time this method is called. See if the message gets displayed, if it does, then the method is obviously called correctly.
     
  8. Offline

    jthort

    Timbo_KZ it's firing correctly and throwing no errors. could it have something to do with me setting the defaults in the town class?

    And as I said before the config is saving so that remove method is being called. And the message before calling the method is working. I've tried multiple ways to remove it from the config and none work. I've also tried looping through the keys and setting them to "" and null and still nothing
     
  9. Offline

    Timbo_KZ

  10. Offline

    jthort

    Timbo_KZ yeah just for debugging, I'll try set again when I get the chance although I'm almost sure I tried it before. If anyone is willing to test out this code themselves and verify that it's not just me that would be great

    Still nothing, it's got to be something I'm doing wrong..
     
  11. Offline

    jthort

    Bump

    Maybe it's possible through input and output streams and just remove it manually?
     
  12. Offline

    jthort

    Timbo_KZ Alright sorry for the triple post but for anyone who came across this thread I just wanted to post what happened...

    I was using copy defaults true when I made the config so every time I would delete it, it would come back. Thanks for the help solved.
     
    gogobebe2 likes this.
Thread Status:
Not open for further replies.

Share This Page