Solved Data of a file deleting every time a reload happens.

Discussion in 'Plugin Development' started by Gonmarte, Apr 10, 2016.

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

    Gonmarte

    I did. In onenable before loadfiles.
    I do not understand why my config has that line. Im sure something is wrong, cant you compare to your code?
    Code:java
    1. package me.gonmarte;
    2.  
    3. import java.io.*;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.configuration.InvalidConfigurationException;
    7. import org.bukkit.configuration.file.YamlConfiguration;
    8. import org.bukkit.configuration.serialization.ConfigurationSerialization;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Main extends JavaPlugin{
    13.  
    14. public File ClansData = new File("plugins/" + File.separator + "Clans", "ClansData.yml");
    15. public YamlConfiguration cd = YamlConfiguration.loadConfiguration(ClansData);
    16. @Override
    17. public void onEnable() {
    18. ConfigurationSerialization.registerClass(Dog.class);
    19. loadFiles();
    20. }
    21.  
    22. @Override
    23. public void onDisable() {
    24. saveFiles();
    25. }
    26.  
    27.  
    28. @Override
    29. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    30. if(command.getName().equalsIgnoreCase("dog") && sender instanceof Player){
    31. Dog myDog = new Dog("Billy" , 15);
    32. cd.set("dog", myDog);
    33. return true;
    34. }else if(command.getName().equalsIgnoreCase("mydog")){
    35. sender.sendMessage(" "+ cd.get("dog." + "age"));
    36. return true;
    37. }
    38. return false;
    39. }
    40.  
    41. public void saveFiles() {
    42. try {
    43. this.cd.save(this.ClansData);
    44. } catch (IOException e) {
    45. e.printStackTrace();
    46.  
    47. }
    48. }
    49.  
    50. public void loadFiles() {
    51. if (this.ClansData.exists())
    52. try {
    53.  
    54. this.cd.load(this.ClansData);
    55.  
    56. }catch(IOException e){
    57. e.printStackTrace();
    58.  
    59. } catch (InvalidConfigurationException e) {
    60. e.printStackTrace();
    61. }else{
    62.  
    63. try {
    64.  
    65. this.cd.save(this.ClansData);
    66.  
    67. } catch (IOException e) {
    68.  
    69. e.printStackTrace();
    70. }
    71. }
    72.  
    73. }
    74. }
    75.  


    Code:java
    1. package me.gonmarte;
    2.  
    3. import java.util.HashMap;
    4. import java.util.Map;
    5.  
    6. import org.bukkit.configuration.serialization.ConfigurationSerializable;
    7.  
    8. public class Dog implements ConfigurationSerializable{
    9.  
    10. public int age;
    11. public String name;
    12.  
    13. public Dog(String name, int age){
    14. this.name=name;
    15. this.age = age;
    16. }
    17.  
    18. public Dog(Map<String, Object> map){
    19. this.age = (int) map.get("age");
    20. this.name = (String) map.get("name");
    21. }
    22.  
    23. @Override
    24. public Map<String, Object> serialize() {
    25. Map<String, Object> map = new HashMap<>();
    26. map.put("age", this.age);
    27. map.put("name", this.name);
    28. return map;
    29. }
    30. }[/code]
     
  2. Offline

    I Al Istannen

  3. Offline

    Gonmarte

    It is up.
     
  4. Offline

    I Al Istannen

    @Gonmarte
    public YamlConfiguration cd = YamlConfiguration.loadConfiguration(ClansData);
    This gets evaluated before the onEnable() method. Intialize it inside the onEnable method. (After the registration)
     
    Gonmarte likes this.
  5. Offline

    Gonmarte

    I did, it still gets the error IllegalArgumentException. When reload
     
  6. Offline

    I Al Istannen

    @Gonmarte
    There are two reasons that can cause the class not found thing.
    1. Not registered
    2. Not exported

    Could you post your updated code?
     
  7. Offline

    Gonmarte

    You were right, sorry my bad, it worked ;).
    My file is still getting that line that doesnt let me get anything from the config.
    Code:java
    1.  
    2. package me.gonmarte;
    3.  
    4. import java.io.*;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.configuration.InvalidConfigurationException;
    8. import org.bukkit.configuration.file.YamlConfiguration;
    9. import org.bukkit.configuration.serialization.ConfigurationSerialization;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Main extends JavaPlugin{
    14.  
    15. public File ClansData = new File("plugins/" + File.separator + "Clans", "ClansData.yml");
    16. public YamlConfiguration cd;
    17. @Override
    18. public void onEnable() {
    19. ConfigurationSerialization.registerClass(Dog.class);
    20. cd = YamlConfiguration.loadConfiguration(ClansData);
    21. loadFiles();
    22. }
    23.  
    24. @Override
    25. public void onDisable() {
    26. saveFiles();
    27. }
    28.  
    29.  
    30. @Override
    31. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    32. if(command.getName().equalsIgnoreCase("dog") && sender instanceof Player){
    33. Dog myDog = new Dog("Billy" , 15);
    34. cd.set("dog", myDog);
    35. return true;
    36. }else if(command.getName().equalsIgnoreCase("mydog")){
    37. sender.sendMessage(" "+ cd.get("dog." + "age"));
    38. }
    39. return false;
    40. }
    41.  
    42. public void saveFiles() {
    43. try {
    44. this.cd.save(this.ClansData);
    45. } catch (IOException e) {
    46. e.printStackTrace();
    47.  
    48. }
    49. }
    50.  
    51. public void loadFiles() {
    52. if (this.ClansData.exists())
    53. try {
    54.  
    55. this.cd.load(this.ClansData);
    56.  
    57. }catch(IOException e){
    58. e.printStackTrace();
    59.  
    60. } catch (InvalidConfigurationException e) {
    61. e.printStackTrace();
    62. }else{
    63.  
    64. try {
    65.  
    66. this.cd.save(this.ClansData);
    67.  
    68. } catch (IOException e) {
    69.  
    70. e.printStackTrace();
    71. }
    72. }
    73.  
    74. }
    75. }
    76.  
    77.  


    Code:java
    1.  
    2. package me.gonmarte;
    3.  
    4. import java.util.HashMap;
    5. import java.util.Map;
    6.  
    7. import org.bukkit.configuration.serialization.ConfigurationSerializable;
    8.  
    9. public class Dog implements ConfigurationSerializable{
    10.  
    11. public int age;
    12. public String name;
    13.  
    14. public Dog(String name, int age){
    15. this.name=name;
    16. this.age = age;
    17. }
    18.  
    19. public Dog(Map<String, Object> map){
    20. this.age = (int) map.get("age");
    21. this.name = (String) map.get("name");
    22. }
    23.  
    24. @Override
    25. public Map<String, Object> serialize() {
    26. Map<String, Object> map = new HashMap<>();
    27. map.put("age", this.age);
    28. map.put("name", this.name);
    29. return map;
    30. }
    31. }
    32.  


    Code:java
    1.  
    2. dog:
    3. ==: me.gonmarte.Dog
    4. name: Billy
    5. age: 15
     
    Last edited: Apr 11, 2016
  8. Offline

    I Al Istannen

    @Gonmarte
    "My file is still getting that line that doesnt let me get anything from the config."

    Im sorry, but I don't quite know what you mean with that.
     
    Gonmarte likes this.
  9. Offline

    Gonmarte

    Code:java
    1.  
    2. dog:
    3. //WHAT IS THIS!!??
    4. ==: me.gonmarte.Dog
    5. name: Billy
    6. age: 15
     
  10. Offline

    I Al Istannen

    @Gonmarte
    That just explains what the class of the Object is. It shows that the "name" and "age" belong to a "Dog" Object. I explained that in an earlier post I think. No need to get that for you. You just need to get the config.get("dog"); and then you can cast it to a dog. No need to get the "name" and "age" things directly.
     
    Gonmarte likes this.
  11. Offline

    Gonmarte

    Ahh. Of course, now all makes sense.
    Its working :D
    I finally understood! Of course, the map is similar to the config that why we are using it to save and load, and then the bukkit serializes it, by itself.
    Thank you so much!!
    This is actually really useful, now im going to try to implement it in my Clans plugin to save my Object Clan.
    Thank you again ;)
     
    I Al Istannen likes this.
  12. Offline

    mcdorli

    Just because you create a file with new File, if you don't call createNewFile, it won't exist in your directiory and the cinfig can't save to it.
     
  13. Offline

    I Al Istannen

    @mcdorli
    Quote from the javadoc:
    Taken from here: FileConfiguration#save(File)
     
  14. Offline

    mcdorli

    If the path to the file (e.g. the datafolder of the plugin) doesn't exist, file.createNewFile failes and returns false, thus the save method throws an exception.
     
  15. Offline

    I Al Istannen

    @mcdorli
    See what you mean. Haven't tried, but not calling getParent().mkdirs() seems pointless. Well, you may be right. Future will show ;)
     
  16. Offline

    Gonmarte

Thread Status:
Not open for further replies.

Share This Page