File Not Writing/Reading

Discussion in 'Plugin Development' started by GaminForDummys, Aug 28, 2013.

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

    GaminForDummys

    Hey guys, I'm trying to get my Economy class to read/save the player's money from their personal yml file. Its not writing or reading anything other than the defaults... There may be some bad code from me trying to fix it :p No errors are given.

    PlayerFile Code:
    Code:java
    1. package gaminfordummys.util;
    2.  
    3. import gaminfordummys.GaminForDummys;
    4.  
    5. import java.io.File;
    6. import java.io.IOException;
    7.  
    8. import org.bukkit.configuration.file.YamlConfiguration;
    9.  
    10. public class PlayerFile {
    11.  
    12. static GaminForDummys plugin;
    13.  
    14. public PlayerFile(GaminForDummys _plugin) {
    15. plugin = _plugin;
    16. }
    17.  
    18. public void createPlayer(String PlayerName) {
    19. File customConfigFile = new File("plugins/GaminForDummys/UserData/" + PlayerName + ".yml");
    20. if(!customConfigFile.exists()){
    21. customConfigFile = new File("plugins/GaminForDummys/UserData/" + PlayerName + ".yml");
    22. YamlConfiguration config = YamlConfiguration.loadConfiguration(customConfigFile);
    23. config.set("Money", 0);
    24. config.set("Bounty", 0);
    25. config.set("Nickname", PlayerName);
    26. config.set("Kills", 0);
    27. config.set("Deaths", 0);
    28. try {
    29. config.save(customConfigFile);
    30. } catch (IOException e) {
    31. e.printStackTrace();
    32. }
    33. }
    34. }
    35.  
    36. public YamlConfiguration getPlayer(String PlayerName) {
    37. File customConfigFile = new File("plugins/GaminForDummys/UserData/" + PlayerName + ".yml");
    38. YamlConfiguration config;
    39. config = YamlConfiguration.loadConfiguration(customConfigFile);
    40. if(!customConfigFile.exists()){
    41. customConfigFile = new File("plugins/GaminForDummys/UserData/" + PlayerName + ".yml");
    42. config = YamlConfiguration.loadConfiguration(customConfigFile);
    43. }
    44. return config;
    45. }
    46.  
    47. public File getFile(String PlayerName) {
    48. File customConfigFile = new File("plugins/GaminForDummys/UserData/" + PlayerName + ".yml");
    49. return customConfigFile;
    50. }
    51.  
    52. }


    Economy Code:
    Code:java
    1. package gaminfordummys.util;
    2.  
    3. import java.io.IOException;
    4. import java.util.HashMap;
    5.  
    6. import gaminfordummys.GaminForDummys;
    7.  
    8. public class Economy {
    9.  
    10. public static HashMap<String, Integer> balance = new HashMap<String, Integer>();
    11.  
    12. static PlayerFile config;
    13. static GaminForDummys plugin;
    14. public Economy(GaminForDummys _plugin) {
    15. plugin = _plugin;
    16. }
    17.  
    18. public static boolean hasAccount(String PlayerName) {
    19. return balance.containsKey(PlayerName);
    20. }
    21.  
    22. public static int getBalance(String PlayerName) {
    23. if (balance.containsKey(PlayerName)) {
    24. return ((Integer)balance.get(PlayerName)).intValue();
    25. }
    26. return 0;
    27. }
    28.  
    29. public static boolean addBalance(String PlayerName, int Amount) {
    30. if (balance.containsKey(PlayerName)) {
    31. int result = ((Integer)balance.get(PlayerName)).intValue() + Amount;
    32. balance.put(PlayerName, Integer.valueOf(result));
    33. config.getPlayer(PlayerName).set("Money", Integer.valueOf(result));
    34. try {
    35. config.getPlayer(PlayerName).set("Money", Integer.valueOf(result));
    36. config.getPlayer(PlayerName).save(config.getFile(PlayerName));
    37. } catch (IOException e) {
    38. e.printStackTrace();
    39. }
    40. return true;
    41. }
    42. return false;
    43. }
    44.  
    45. public static boolean removeBalance(String PlayerName, int Amount) {
    46. if ((balance.containsKey(PlayerName)) &&
    47. (((Integer)balance.get(PlayerName)).intValue() - Amount >= 0.0D)) {
    48. int result = ((Integer)balance.get(PlayerName)).intValue() - Amount;
    49. balance.put(PlayerName, Integer.valueOf(result));
    50. try {
    51. config.getPlayer(PlayerName).set("Money", Integer.valueOf(result));
    52. config.getPlayer(PlayerName).save(config.getFile(PlayerName));
    53. } catch (IOException e) {
    54. e.printStackTrace();
    55. }
    56. return true;
    57. }
    58.  
    59. return false;
    60. }
    61.  
    62. public static boolean hasBalance(String PlayerName, int Amount) {
    63. if (balance.containsKey(PlayerName)) {
    64. return ((Integer)balance.get(PlayerName)).intValue() - Amount >= 0;
    65. }
    66. return false;
    67. }
    68. }
     
  2. Offline

    FurmigaHumana

    Ive made some changes in some methods of both classes, comments inside.

    PlayerFile code:

    Show Spoiler

    Code:java
    1.  
    2. //
    3. public void setPlayer(String PlayerName, String node, Integer value) {
    4. try {
    5. YamlConfiguration config = getPlayer(PlayerName); // get the player config
    6.  
    7. config.set(node, value); // set the value
    8.  
    9. config.save(getPlayer(PlayerName)); // save it
    10. } catch (Exception ex) {
    11. ex.printStackTrace();
    12. }
    13. }
    14.  
    15. public void createPlayer(String PlayerName) {
    16. File customConfigFile = getFile(PlayerName); // use your method to get the file, this way you dont have to type the path tons of times
    17.  
    18. if(!customConfigFile.exists()){ // only create a new one if it does not already exists
    19.  
    20. YamlConfiguration config = new YamlConfiguration(); // creates a new empty YamlConfiguration
    21.  
    22. config.set("Money", 0); // set things
    23. config.set("Bounty", 0);
    24. config.set("Nickname", PlayerName);
    25. config.set("Kills", 0);
    26. config.set("Deaths", 0);
    27.  
    28. try {
    29. config.save(customConfigFile); // save to the file
    30. } catch (IOException e) {
    31. e.printStackTrace();
    32. }
    33. }
    34. }
    35.  
    36. public YamlConfiguration getPlayer(String PlayerName) {
    37. File customConfigFile = getFile(PlayerName); // use your method here too
    38.  
    39. if(!customConfigFile.exists()){ // check if the file exists
    40. createPlayer(PlayerName); // if dont exists, create new a new one using your createPlayer method
    41. }
    42.  
    43. YamlConfiguration config = YamlConfiguration.loadConfiguration(customConfigFile); // now that the file problably already exists, load it.
    44.  
    45. return config; // return the config.
    46. }
    47.  
    48. public File getFile(String PlayerName) {
    49. return new File("plugins/GaminForDummys/UserData/" + PlayerName + ".yml");
    50. }



    Economy code:
    Show Spoiler

    Code:java
    1. public static boolean addBalance(String PlayerName, int Amount) {
    2. if (balance.containsKey(PlayerName)) {
    3. int result = ((Integer)balance.get(PlayerName)).intValue() + Amount;
    4. balance.put(PlayerName, Integer.valueOf(result));
    5. config.setPlayer(PlayerName, "Money", Integer.valueOf(result));
    6. /*config.getPlayer(PlayerName).set("Money", Integer.valueOf(result));
    7.   try {
    8.   config.getPlayer(PlayerName).set("Money", Integer.valueOf(result));
    9.   config.getPlayer(PlayerName).save(config.getFile(PlayerName));
    10.   } catch (IOException e) {
    11.   e.printStackTrace();
    12.   }*/
    13. return true;
    14. }
    15. return false;
    16. }
    17.  
    18. public static boolean removeBalance(String PlayerName, int Amount) {
    19. if ((balance.containsKey(PlayerName)) &&
    20. (((Integer)balance.get(PlayerName)).intValue() - Amount >= 0.0D)) {
    21.  
    22. int result = ((Integer)balance.get(PlayerName)).intValue() - Amount;
    23. balance.put(PlayerName, Integer.valueOf(result));
    24.  
    25. config.setPlayer(PlayerName, "Money", Integer.valueOf(result));
    26. /*try {
    27.   config.getPlayer(PlayerName).set("Money", Integer.valueOf(result));
    28.   config.getPlayer(PlayerName).save(config.getFile(PlayerName));
    29.   } catch (IOException e) {
    30.   e.printStackTrace();
    31.   }*/
    32. return true;
    33. }
    34.  
    35. return false;
    36. }



    See what you can do with that, may have syntax errors, written on notepad :)
     
  3. Offline

    GaminForDummys

    I'm getting an error when I mine to add money.

    Error:
    Code:
    2013-08-21 19:18:54 [SEVERE] Could not pass event BlockBreakEvent to GaminForDummys v0.1
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.TimedRegisteredListener.callEvent(TimedRegisteredListener.java:30)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:478)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:463)
        at net.minecraft.server.v1_6_R2.PlayerInteractManager.breakBlock(PlayerInteractManager.java:280)
        at net.minecraft.server.v1_6_R2.PlayerInteractManager.dig(PlayerInteractManager.java:125)
        at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java:538)
        at net.minecraft.server.v1_6_R2.Packet14BlockDig.handle(SourceFile:46)
        at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:230)
        at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java:116)
        at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
        at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java:125)
        at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:592)
        at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:239)
        at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:481)
        at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:413)
        at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.NullPointerException
        at gaminfordummys.util.Economy.addBalance(Economy.java:32)
        at gaminfordummys.evts.onBlockBreak(evts.java:183)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 17 more
    evts(Note on line giving error):
    Code:java
    1. if(b.getType() == Material.GOLD_ORE) {
    2. Economy.addBalance(p.getName(), 50); //Line giving error
    3. p.sendMessage(ChatColor.GRAY + "50 Coins Added");
    4. evt.setCancelled(true);
    5. evt.getBlock().setType(Material.AIR);
    6. evt.getBlock().getWorld().dropItemNaturally(evt.getBlock().getLocation(), new ItemStack (Material.GOLD_INGOT));
    7. }


    FurmigaHumana

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

Share This Page