Saving Economy Hashmap

Discussion in 'Plugin Development' started by GaminForDummys, Jul 4, 2013.

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

    GaminForDummys

    Hey guys I recently coded an Economy for my all in one plugin and I have no idea how to save it, I'm new to saving/loading stuff from a file, I'd prefer it not to load from the config but to my Accounts.yml. Any help is greatly appreciated.


    Main Class:
    Code:java
    1. package me.mrgamingdwarf.gaminfordummys;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import me.mrgamingdwarf.gaminfordummys.cmd.*;
    6. import me.mrgamingdwarf.gaminfordummys.evts.evts;
    7.  
    8. import org.bukkit.plugin.PluginManager;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class GaminForDummys extends JavaPlugin {
    12.  
    13. Logger log = Logger.getLogger("Minecraft");
    14. //public static String mdir = "plugins/GaminForDummys/";
    15. //public static File Accounts = new File(mdir + "Accounts.yml");
    16.  
    17. @Override
    18. public void onEnable() {
    19. //new File(mdir).mkdir();
    20. //if (!Accounts.exists()) {
    21. // try {
    22. // Accounts.createNewFile();
    23. // } catch (IOException e) {
    24. // e.printStackTrace();
    25. // }
    26. //}
    27.  
    28. PluginManager pm = this.getServer().getPluginManager();
    29.  
    30. pm.registerEvents(new evts(), this);
    31.  
    32. getCommand("Money").setExecutor(new Money());
    33. // getCommand("Pay").setExecutor(new Pay());
    34. getCommand("GM").setExecutor(new GM());
    35.  
    36. log.info("[GaminForDummys] v1.0 Enabled.");
    37. }
    38.  
    39. @Override
    40. public void onDisable() {
    41. log.info("[GaminForDummys] Disabled.");
    42. }
    43. }


    Economy Class:
    Code:java
    1. package me.mrgamingdwarf.gaminfordummys.economy;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import me.mrgamingdwarf.gaminfordummys.GaminForDummys;
    6.  
    7. public class Economy
    8. {
    9. protected static HashMap<String, Double> balance = new HashMap<String, Double>();
    10. private static double startingBalance = 1000.0D;
    11. private static GaminForDummys p;
    12.  
    13. public Economy(GaminForDummys pg)
    14. {
    15. p = pg;
    16. }
    17.  
    18. public static void loadAccounts() {
    19. balance.clear();
    20.  
    21. for (String str : p.getConfig().getConfigurationSection("Accounts").getKeys(false)) {
    22. balance.put(str, Double.valueOf(p.getConfig().getDouble("Accounts." + str)));
    23. }
    24. }
    25.  
    26. public static boolean createAccount(String PlayerName)
    27. {
    28. if (!balance.containsKey(PlayerName)) {
    29. balance.put(PlayerName, Double.valueOf(startingBalance));
    30. p.getConfig().set("Accounts." + PlayerName, Double.valueOf(startingBalance));
    31. return true;
    32. }
    33. return false;
    34. }
    35.  
    36. public static boolean hasAccount(String playerName) {
    37. return balance.containsKey(playerName);
    38. }
    39. public static boolean deleteAccount(String PlayerName) {
    40. if (balance.containsKey(PlayerName)) {
    41. balance.remove(PlayerName);
    42.  
    43. p.getConfig().set("Accounts." + PlayerName, null);
    44.  
    45. return true;
    46. }
    47. return false;
    48. }
    49.  
    50. public static double getBalance(String PlayerName) {
    51. if (balance.containsKey(PlayerName)) {
    52. return ((Double)balance.get(PlayerName)).doubleValue();
    53. }
    54. return 0.0D;
    55. }
    56. public static boolean addBalance(String PlayerName, double Amount) {
    57. if (balance.containsKey(PlayerName)) {
    58. double result = ((Double)balance.get(PlayerName)).doubleValue() + Amount;
    59. balance.put(PlayerName, Double.valueOf(result));
    60. p.getConfig().set("Accounts." + PlayerName, Double.valueOf(result));
    61. return true;
    62. }
    63. return false;
    64. }
    65. public static boolean removeBalance(String PlayerName, double Amount) {
    66. if ((balance.containsKey(PlayerName)) &&
    67. (((Double)balance.get(PlayerName)).doubleValue() - Amount >= 0.0D)) {
    68. double result = ((Double)balance.get(PlayerName)).doubleValue() - Amount;
    69. balance.put(PlayerName, Double.valueOf(result));
    70. p.getConfig().set("Accounts." + PlayerName, Double.valueOf(result));
    71. return true;
    72. }
    73.  
    74. return false;
    75. }
    76. public static boolean hasBalance(String PlayerName, double Amount) {
    77. if (balance.containsKey(PlayerName)) {
    78. return ((Double)balance.get(PlayerName)).doubleValue() - Amount >= 0.0D;
    79. }
    80. return false;
    81. }
    82. }





    So the put player would replace my p.getconfig.set()? Sorry, Im new to loading/saving.



    I did that, but when I open accounts.yml it has nothing in it and when I type /money it just comes up 0.0.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  2. Offline

    JoshArgent

  3. Offline

    GaminForDummys

    Error In Console Now. Also before I couldn't use my other commands, only /Money worked.



    Code:
    2013-07-04 14:24:17 [SEVERE] Error occurred while enabling GaminForDummys v0.2 (Is it up to date?)
    java.lang.IllegalArgumentException: The embedded resource 'accounts.yml' cannot be found in plugins\GaminForDummys.jar
        at org.bukkit.plugin.java.JavaPlugin.saveResource(JavaPlugin.java:149)
        at me.mrgamingdwarf.gaminfordummys.util.ConfigurationLoader.saveDefaultConfig(ConfigurationLoader.java:67)
        at me.mrgamingdwarf.gaminfordummys.GaminForDummys.onEnable(GaminForDummys.java:27)
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
        at org.bukkit.craftbukkit.v1_6_R1.CraftServer.loadPlugin(CraftServer.java:282)
        at org.bukkit.craftbukkit.v1_6_R1.CraftServer.enablePlugins(CraftServer.java:264)
        at net.minecraft.server.v1_6_R1.MinecraftServer.l(MinecraftServer.java:313)
        at net.minecraft.server.v1_6_R1.MinecraftServer.f(MinecraftServer.java:290)
        at net.minecraft.server.v1_6_R1.MinecraftServer.a(MinecraftServer.java:250)
        at net.minecraft.server.v1_6_R1.DedicatedServer.init(DedicatedServer.java:151)
        at net.minecraft.server.v1_6_R1.MinecraftServer.run(MinecraftServer.java:391)
        at net.minecraft.server.v1_6_R1.ThreadServerApplication.run(SourceFile:582)


    Sorry. Still nothing in the accounts.yml, also my events aren't working.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  4. Offline

    GaminForDummys


    Everything works normal I recoded everything and now the Economy is not saving, but it works, just not saving. Also am I suppose to add anything to my accounts.yml? Sorry I'm very new to saving and loading. Thanks so much for the help so far.

    Main Class Now:
    Code:java
    1. package me.mrgamingdwarf.gaminfordummys;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import me.mrgamingdwarf.gaminfordummys.cmd.*;
    6. import me.mrgamingdwarf.gaminfordummys.evts.evts;
    7. import me.mrgamingdwarf.gaminfordummys.util.ConfigurationLoader;
    8.  
    9. import org.bukkit.configuration.file.FileConfiguration;
    10. import org.bukkit.plugin.PluginManager;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class GaminForDummys extends JavaPlugin
    14. {
    15. Logger log = Logger.getLogger("Minecraft");
    16. ConfigurationLoader save;
    17. public FileConfiguration config;
    18.  
    19. public void onEnable() {
    20. save = new ConfigurationLoader(this, "accounts.yml");
    21. config = save.getConfig();
    22. config.options().copyDefaults(true);
    23. save.saveDefaultConfig();
    24. save.saveConfig();
    25.  
    26. PluginManager pm = getServer().getPluginManager();
    27.  
    28. pm.registerEvents(new evts(), this);
    29.  
    30. getCommand("Lag").setExecutor(new Lag());
    31. getCommand("GM").setExecutor(new GM());
    32. getCommand("KillAll").setExecutor(new KillAll());
    33. getCommand("GM").setExecutor(new GM());
    34. getCommand("Money").setExecutor(new Money());
    35.  
    36. this.log.info("[GaminForDummys] v1.0 Enabled.");
    37. }
    38.  
    39. public void onDisable()
    40. {
    41. save.saveConfig();
    42.  
    43. this.log.info("[GaminForDummys] Disabled.");
    44. }
    45.  
    46. }


    My Economy Class Now:
    Code:java
    1. package me.mrgamingdwarf.gaminfordummys.economy;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import me.mrgamingdwarf.gaminfordummys.GaminForDummys;
    6.  
    7. public class Economy
    8. {
    9. protected static HashMap<String, Double> balance = new HashMap<String, Double>();
    10. private static double startingBalance = 1000;
    11. private static GaminForDummys p;
    12.  
    13. public Economy(GaminForDummys pg)
    14. {
    15. p = pg;
    16. }
    17.  
    18. public static boolean createAccount(String PlayerName)
    19. {
    20. if (!balance.containsKey(PlayerName)) {
    21. balance.put(PlayerName, Double.valueOf(startingBalance));
    22. p.config.set("Accounts."+ PlayerName, Double.valueOf(startingBalance));
    23. return true;
    24. }
    25. return false;
    26. }
    27.  
    28. public static boolean hasAccount(String PlayerName) {
    29. return balance.containsKey(PlayerName);
    30. }
    31.  
    32. public static boolean deleteAccount(String PlayerName) {
    33. if (balance.containsKey(PlayerName)) {
    34. balance.remove(PlayerName);
    35.  
    36. p.config.set("Accounts."+ PlayerName, null);
    37.  
    38. return true;
    39. }
    40. return false;
    41. }
    42.  
    43. public static double getBalance(String PlayerName) {
    44. if (balance.containsKey(PlayerName)) {
    45. return ((Double)balance.get(PlayerName)).doubleValue();
    46. }
    47. return 0;
    48. }
    49. public static boolean addBalance(String PlayerName, double Amount) {
    50. if (balance.containsKey(PlayerName)) {
    51. double result = ((Double)balance.get(PlayerName)).doubleValue() + Amount;
    52. balance.put(PlayerName, Double.valueOf(result));
    53. p.config.set("Accounts."+ PlayerName, Double.valueOf(result));
    54. return true;
    55. }
    56. return false;
    57. }
    58. public static boolean removeBalance(String PlayerName, double Amount) {
    59. if ((balance.containsKey(PlayerName)) &&
    60. (((Double)balance.get(PlayerName)).doubleValue() - Amount >= 0.0D)) {
    61. double result = ((Double)balance.get(PlayerName)).doubleValue() - Amount;
    62. balance.put(PlayerName, Double.valueOf(result));
    63. p.config.set("Accounts."+ PlayerName, Double.valueOf(result));
    64. return true;
    65. }
    66.  
    67. return false;
    68. }
    69. public static boolean hasBalance(String PlayerName, double Amount) {
    70. if (balance.containsKey(PlayerName)) {
    71. return ((Double)balance.get(PlayerName)).doubleValue() - Amount >= 0.0D;
    72. }
    73. return false;
    74. }
    75. }




    What do you mean? My accounts.yml is empty

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  5. Offline

    Compressions

    GaminForDummys There is no reason you should be using a HashMap to store a permanent currency.
     
  6. Offline

    GaminForDummys



    What should I be using then?
     
  7. Offline

    Compressions

    GaminForDummys You can easily just get the balance from the config where you should store it.
     
  8. Offline

    Warreo

    GaminForDummys Compressions It would be easier on the server to use a hashmap while the plugin is active then save the hashmap to a file onDisable then reload the data to a hashmap onEnable, editing variables is much much faster and easier than read/writing to files :)
     
  9. Offline

    Compressions

    Warreo In the long run, performance would really not be affected either way. Also, saving straight to the config would be easier to write if you're the programmer. It doesn't really matter, and doesn't make a difference though.
     
  10. Offline

    Warreo

    Compressions I was speaking in technicality, of course on a minor scale like this it wouldn't make a difference. But of course you are right read/write straight from a config is much easier (most the time haha). Comes down to preference and how comfortable the programmer is handling different objects versus read/write :)
     
  11. Offline

    Compressions

  12. Offline

    Unknowncmbk


    Actually you're wrong here.

    Time it takes to read/write to HDD: 0.01 seconds (10 milliseconds on avg.)
    Time it takes to read/write to DRAM: 1e-7 seconds (100 nanoseconds on avg.)

    Even though it seems fast to us, it's slow in computer time as a computer's processor can access the DRAM 200,000 faster than accessing the HDD.

    Convert this to human time:
    ~ 1 second to flip open a book (access RAM)
    ~27.7 hours to travel to a place by plane (access HDD).

    Of course these are just examples and the numbers are a bit off. Correct programming constructs should employ:
    - Read all your information from your files (the HDD) onEnable then store them as variables in your RAM/Cache/etc. A lot faster access time.
     
  13. Offline

    Warreo

    Unknowncmbk

    Must say I love the real world reference! :) Numbers are such a wonderful thing haha :)
     
  14. Offline

    GaminForDummys


    Well now that that is settled, anyone know why my accounts.yml isn't writing anything?
     
  15. Offline

    Warreo


    after you set the amount to the accounts.yml make sure you call save it! :)
     
  16. Offline

    Unknowncmbk

    And reload it! :p
     
  17. Offline

    JoshArgent

    Rather than caching in a hashmap why not use Bukkit's new meta data system. Save the players economy balance to their meta data and access it through there. Only save/read the file when data has changed or no data is available. I do this in all of my plugins and it is incredibly efficient.

    I also suggest that you have a scheduled task every minute to save any changed data at once. Rather than saving every single time it is changed add it to a task list and only update the file every minute asyncly. That way you're not lagging the server. A good example where I've used this method is in my plugin vaults: https://github.com/JoshArgent/Vaults/blob/master/Vaults/src/org/vanillaworld/Vaults/Backend.java
     
  18. Offline

    GaminForDummys



    What do I put in the accounts.yml?
     
  19. Offline

    Warreo

    JoshArgent Going back to the speed thing it will still be much faster to retrieve data from a HashMap than it would be to search through a player's metadata, again in the micro scale no real big difference but is technically much faster.

    GaminForDummys I assume you would want to put something like Player.Amount in your .yml
     
  20. Offline

    GaminForDummys

    Warreo Yes, I've changed from doubles to ints and I want to write like PlayerName: Amount
    Also I have another hash map that is a virtual chest that's a Player, Inventory hash map how would I go about Saving that too? To the same file as economy.

    Code:java
    1.  
    2.  
    3.  
    4. So how come my Economy isn't saving though?
    5.  
    6. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  21. Offline

    GaminForDummys

    T3h Cr33p3r

    Main Class:

    Code:java
    1. package me.mrgamingdwarf.gaminfordummys;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import me.mrgamingdwarf.gaminfordummys.cmd.*;
    6. import me.mrgamingdwarf.gaminfordummys.evts.evts;
    7. import me.mrgamingdwarf.gaminfordummys.util.ConfigurationLoader;
    8.  
    9. import org.bukkit.configuration.file.FileConfiguration;
    10. import org.bukkit.plugin.PluginManager;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class GaminForDummys extends JavaPlugin
    14. {
    15. Logger log = Logger.getLogger("Minecraft");
    16. ConfigurationLoader save;
    17. public FileConfiguration config;
    18.  
    19. public void onEnable() {
    20. save = new ConfigurationLoader(this, "accounts.yml");
    21. save.getConfig().options().copyDefaults(true);
    22. save.saveDefaultConfig();
    23. save.saveConfig();
    24. config = save.getConfig();
    25.  
    26. PluginManager pm = getServer().getPluginManager();
    27.  
    28. pm.registerEvents(new evts(), this);
    29.  
    30. getCommand("Lag").setExecutor(new Lag());
    31. getCommand("GM").setExecutor(new GM());
    32. getCommand("KillAll").setExecutor(new KillAll());
    33. getCommand("GM").setExecutor(new GM());
    34. getCommand("Money").setExecutor(new Money());
    35. getCommand("WorkBench").setExecutor(new WorkBench());
    36. getCommand("EnderChest").setExecutor(new EnderChest());
    37. getCommand("Chest").setExecutor(new Chest());
    38. getCommand("Feed").setExecutor(new Feed());
    39. getCommand("Pay").setExecutor(new Pay());
    40. getCommand("INVSee").setExecutor(new INVSee());
    41. getCommand("Chat").setExecutor(new Chat());
    42. getCommand("MSG").setExecutor(new MSG());
    43.  
    44. this.log.info("[GaminForDummys] v1.0 Enabled.");
    45. }
    46.  
    47. public void onDisable()
    48. {
    49. save.saveConfig();
    50.  
    51. this.log.info("[GaminForDummys] Disabled.");
    52. }
    53.  
    54. }



    T3h Cr33p3r

    Error line 17 of economy

    Economy Class:
    Code:java
    1. package me.mrgamingdwarf.gaminfordummys.economy;
    2.  
    3. import me.mrgamingdwarf.gaminfordummys.GaminForDummys;
    4.  
    5. public class Economy
    6. {
    7. private static int startingBalance = 1000;
    8.  
    9. static GaminForDummys plugin;
    10. public Economy(GaminForDummys plugin) {
    11. this.plugin = plugin;
    12. }
    13. public static boolean createAccount(String PlayerName)
    14. {
    15. if (!GaminForDummys.balance.containsKey(PlayerName)) {
    16. GaminForDummys.balance.put(PlayerName, startingBalance);
    17. plugin.config.set("Accounts."+ PlayerName, startingBalance);
    18. return true;
    19. }
    20. return false;
    21. }
    22.  
    23. public static boolean hasAccount(String PlayerName) {
    24. return GaminForDummys.balance.containsKey(PlayerName);
    25. }
    26.  
    27. public static boolean deleteAccount(String PlayerName) {
    28. if (GaminForDummys.balance.containsKey(PlayerName)) {
    29. GaminForDummys.balance.remove(PlayerName);
    30.  
    31. plugin.config.set("Accounts."+ PlayerName, null);
    32.  
    33. return true;
    34. }
    35. return false;
    36. }
    37.  
    38. public static double getBalance(String PlayerName) {
    39. if (GaminForDummys.balance.containsKey(PlayerName)) {
    40. return ((Integer)GaminForDummys.balance.get(PlayerName)).intValue();
    41. }
    42. return 0;
    43. }
    44. public static boolean addBalance(String PlayerName, int Amount) {
    45. if (GaminForDummys.balance.containsKey(PlayerName)) {
    46. int result = ((Integer)GaminForDummys.balance.get(PlayerName)).intValue() + Amount;
    47. GaminForDummys.balance.put(PlayerName, Integer.valueOf(result));
    48. plugin.config.set("Accounts."+ PlayerName, Double.valueOf(result));
    49. return true;
    50. }
    51. return false;
    52. }
    53. public static boolean removeBalance(String PlayerName, int Amount) {
    54. if ((GaminForDummys.balance.containsKey(PlayerName)) &&
    55. (((Integer)GaminForDummys.balance.get(PlayerName)).doubleValue() - Amount >= 0.0D)) {
    56. int result = ((Integer)GaminForDummys.balance.get(PlayerName)).intValue() - Amount;
    57. GaminForDummys.balance.put(PlayerName, Integer.valueOf(result));
    58. plugin.config.set("Accounts."+ PlayerName, Integer.valueOf(result));
    59. return true;
    60. }
    61.  
    62. return false;
    63. }
    64. public static boolean hasBalance(String PlayerName, int Amount) {
    65. if (GaminForDummys.balance.containsKey(PlayerName)) {
    66. return ((Integer)GaminForDummys.balance.get(PlayerName)).intValue() - Amount >= 0.0D;
    67. }
    68. return false;
    69. }
    70. }


    Main Class:
    Code:java
    1. package me.mrgamingdwarf.gaminfordummys;
    2.  
    3. import java.util.HashMap;
    4. import java.util.logging.Logger;
    5.  
    6. import me.mrgamingdwarf.gaminfordummys.cmd.*;
    7. import me.mrgamingdwarf.gaminfordummys.evts.evts;
    8. import me.mrgamingdwarf.gaminfordummys.util.ConfigurationLoader;
    9.  
    10. import org.bukkit.configuration.file.FileConfiguration;
    11. import org.bukkit.plugin.PluginManager;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class GaminForDummys extends JavaPlugin
    15. {
    16. Logger log = Logger.getLogger("Minecraft");
    17. ConfigurationLoader save;
    18. public FileConfiguration config;
    19.  
    20. public static HashMap<String, Integer> balance = new HashMap<String, Integer>();
    21.  
    22. public void onEnable() {
    23. save = new ConfigurationLoader(this, "accounts.yml");
    24. save.getConfig().options().copyDefaults(true);
    25. save.saveDefaultConfig();
    26. save.saveConfig();
    27. config = save.getConfig();
    28.  
    29. PluginManager pm = getServer().getPluginManager();
    30.  
    31. pm.registerEvents(new evts(), this);
    32.  
    33. getCommand("Lag").setExecutor(new Lag());
    34. getCommand("GM").setExecutor(new GM());
    35. getCommand("KillAll").setExecutor(new KillAll());
    36. getCommand("GM").setExecutor(new GM());
    37. getCommand("Money").setExecutor(new Money());
    38. getCommand("WorkBench").setExecutor(new WorkBench());
    39. getCommand("EnderChest").setExecutor(new EnderChest());
    40. getCommand("Chest").setExecutor(new Chest());
    41. getCommand("Feed").setExecutor(new Feed());
    42. getCommand("Pay").setExecutor(new Pay());
    43. getCommand("INVSee").setExecutor(new INVSee());
    44. getCommand("Chat").setExecutor(new Chat());
    45. getCommand("MSG").setExecutor(new MSG());
    46.  
    47. this.log.info("[GaminForDummys] v1.0 Enabled.");
    48. }
    49.  
    50. public void onDisable()
    51. {
    52. save.saveConfig();
    53.  
    54. this.log.info("[GaminForDummys] Disabled.");
    55. }
    56.  
    57. }


    The onPlayerJoin:
    Code:java
    1. if(!Economy.hasAccount(p.getName())) {
    2. Economy.createAccount(p.getName());
    3. }


    The Error:
    Code:java
    1. 2013-07-09 20:09:15 [SEVERE] Could not pass event PlayerJoinEvent to GaminForDummys v1.0
    2. org.bukkit.event.EventException
    3. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    4. at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    5. at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    6. at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    7. at net.minecraft.server.v1_6_R2.PlayerList.c(PlayerList.java:206)
    8. at net.minecraft.server.v1_6_R2.PlayerList.a(PlayerList.java:102)
    9. at net.minecraft.server.v1_6_R2.PendingConnection.e(PendingConnection.java:130)
    10. at net.minecraft.server.v1_6_R2.PendingConnection.d(PendingConnection.java:43)
    11. at net.minecraft.server.v1_6_R2.DedicatedServerConnectionThread.a(DedicatedServerConnectionThread.java:41)
    12. at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:29)
    13. at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:590)
    14. at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
    15. at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
    16. at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
    17. at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    18. Caused by: java.lang.NullPointerException
    19. at me.mrgamingdwarf.gaminfordummys.economy.Economy.createAccount(Economy.java:17)
    20. at me.mrgamingdwarf.gaminfordummys.evts.evts.onPlayerJoin(evts.java:34)
    21. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    22. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    23. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    24. at java.lang.reflect.Method.invoke(Unknown Source)
    25. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    26. ... 14 more






    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  22. Offline

    GaminForDummys

    T3h Cr33p3r

    Code:java
    1. package me.mrgamingdwarf.gaminfordummys.evts;
    2.  
    3. import me.mrgamingdwarf.gaminfordummys.economy.Economy;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.block.Block;
    8. import org.bukkit.entity.Entity;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.Action;
    13. import org.bukkit.event.entity.CreatureSpawnEvent;
    14. import org.bukkit.event.entity.EntityDamageEvent;
    15. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    16. import org.bukkit.event.entity.PlayerDeathEvent;
    17. import org.bukkit.event.player.PlayerInteractEvent;
    18. import org.bukkit.event.player.PlayerJoinEvent;
    19. import org.bukkit.event.player.PlayerKickEvent;
    20. import org.bukkit.event.player.PlayerQuitEvent;
    21. import org.bukkit.inventory.Inventory;
    22. import org.bukkit.inventory.ItemStack;
    23.  
    24. public class evts implements Listener {
    25.  
    26. @EventHandler
    27. public void onPlayerJoin(PlayerJoinEvent evt) {
    28. Player p = evt.getPlayer();
    29. Inventory i = p.getInventory();
    30.  
    31. if(Economy.hasAccount(p.getName())) {
    32. Economy.addBalance(p.getName(), 1000);
    33. }else{
    34. Economy.createAccount(p.getName());
    35. p.sendMessage("Created Economy Account!");
    36. }
    37.  
    38. if (p.hasPlayedBefore()) {
    39. evt.setJoinMessage(ChatColor.LIGHT_PURPLE + p.getName() + ChatColor.GRAY + " Has Connected.");
    40. p.sendMessage(ChatColor.LIGHT_PURPLE + "Welcome Back, " + ChatColor.WHITE + p.getName() + ChatColor.LIGHT_PURPLE + "!");
    41. } else {
    42. evt.setJoinMessage(ChatColor.LIGHT_PURPLE + p.getName() + ChatColor.GRAY + " Has Connected For The First Time.");
    43. p.sendMessage(ChatColor.LIGHT_PURPLE + "Welcome To " + ChatColor.WHITE + "GaminForDummys Factions PvP" + ChatColor.LIGHT_PURPLE + "!");
    44. p.sendMessage(ChatColor.LIGHT_PURPLE + "Rules, Donations, And WorkShop Are Down Stairs.");
    45. p.sendMessage(ChatColor.LIGHT_PURPLE + "Use " + ChatColor.WHITE + "/Kit Member" + ChatColor.LIGHT_PURPLE + " To Get The Member Kit");
    46. p.sendMessage(ChatColor.LIGHT_PURPLE + "Use " + ChatColor.WHITE + "/Warp Shop" + ChatColor.LIGHT_PURPLE + " To Buy Supplies");
    47. p.sendMessage(ChatColor.LIGHT_PURPLE + "Use " + ChatColor.WHITE + "/Warp Spawners" + ChatColor.LIGHT_PURPLE + " To Get Food And Money");
    48. p.sendMessage(ChatColor.LIGHT_PURPLE + "Visit " + ChatColor.WHITE + "GaminForDummys.com" + ChatColor.LIGHT_PURPLE + " For Help");
    49. i.addItem(new ItemStack(Material.IRON_SWORD, 1));
    50. i.addItem(new ItemStack(Material.BREAD, 10));
    51. p.getInventory().setBoots(new ItemStack(Material.IRON_BOOTS, 1));
    52. p.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS, 1));
    53. }
    54. }
    55.  
    56. @EventHandler
    57. public void onPlayerKick(PlayerKickEvent evt) {
    58. Player p = evt.getPlayer();
    59. evt.setLeaveMessage(ChatColor.LIGHT_PURPLE + p.getName() + ChatColor.GRAY + " Has Disconnected.");
    60. }
    61.  
    62. @EventHandler
    63. public void onPlayerLeave(PlayerQuitEvent evt) {
    64. Player p = evt.getPlayer();
    65. evt.setQuitMessage(ChatColor.LIGHT_PURPLE + p.getName() + ChatColor.GRAY + " Has Disconnected.");
    66. }
    67.  
    68. @EventHandler
    69. public void onAnimalSpawn(CreatureSpawnEvent evt) {
    70. Entity e = evt.getEntity();
    71. if (e instanceof org.bukkit.entity.Ocelot) {
    72. evt.setCancelled(true);
    73. }
    74. }
    75.  
    76. @EventHandler
    77. public void onHungerDamage(EntityDamageEvent evt) {
    78. Entity e = evt.getEntity();
    79. if (e instanceof Player) {
    80. if (((Player) e).getHealth() == 2) {
    81. if (evt.getCause().equals(DamageCause.STARVATION)) {
    82. evt.setCancelled(true);
    83. }
    84. }
    85. }
    86. }
    87.  
    88. @EventHandler
    89. public void onPlayerInteract(PlayerInteractEvent evt) {
    90. Player p = evt.getPlayer();
    91. if ((evt.getAction() == Action.RIGHT_CLICK_AIR || evt.getAction() == Action.RIGHT_CLICK_BLOCK) && p.getItemInHand().getType() == Material.MUSHROOM_SOUP) {
    92. if (p.getHealth() == p.getMaxHealth()) {
    93. if (!(p.getFoodLevel() == 20)) {
    94. if (p.getFoodLevel() >= 16) {
    95. p.setFoodLevel(20);
    96. } else {
    97. p.setFoodLevel(p.getFoodLevel() + 4);
    98. }
    99. evt.getPlayer().getItemInHand().setType(Material.BOWL);
    100. } else {
    101. evt.setCancelled(true);
    102. }
    103. } else {
    104. if (p.getHealth() >= 16) {
    105. p.setHealth(20);
    106. } else {
    107. p.setHealth(p.getHealth() + 4);
    108. }
    109. evt.getPlayer().getItemInHand().setType(Material.BOWL);
    110. }
    111. }
    112. if ((evt.getAction() == Action.RIGHT_CLICK_AIR || evt.getAction() == Action.RIGHT_CLICK_BLOCK) && p.getItemInHand().getType() == Material.BLAZE_ROD) {
    113. Block b = p.getTargetBlock(null, 50);
    114. p.getWorld().strikeLightningEffect(b.getLocation());
    115. }
    116. }
    117.  
    118. @EventHandler
    119. public void onPlayerDeath(PlayerDeathEvent evt) {
    120. Player p = evt.getEntity();
    121. Player p1 = evt.getEntity().getKiller();
    122.  
    123. if (p instanceof Player) {
    124. if (p1 instanceof Player) {
    125. evt.setDeathMessage(ChatColor.DARK_PURPLE + p.getName() + ChatColor.GRAY + " Was Killed By " + ChatColor.LIGHT_PURPLE + p1.getName() + ChatColor.GRAY + "!");
    126. Economy.addBalance(p.getName(), 100);
    127. p.sendMessage(ChatColor.LIGHT_PURPLE + "Added " + ChatColor.WHITE + " 100 " + ChatColor.LIGHT_PURPLE + "Coins");
    128. } else {
    129. evt.setDeathMessage(null);
    130. }
    131. }
    132. }
    133. }
    134.  
     
  23. Offline

    GaminForDummys

    T3h Cr33p3r Is there anything else I'm doing wrong? Its still not saving to the file...
     
  24. Offline

    GaminForDummys

    Code:java
    1. package me.mrgamingdwarf.gaminfordummys;
    2.  
    3. import java.util.HashMap;
    4. import java.util.logging.Logger;
    5.  
    6. import me.mrgamingdwarf.gaminfordummys.cmd.*;
    7. import me.mrgamingdwarf.gaminfordummys.economy.Economy;
    8. import me.mrgamingdwarf.gaminfordummys.evts.evts;
    9. import me.mrgamingdwarf.gaminfordummys.util.ConfigurationLoader;
    10.  
    11. import org.bukkit.configuration.file.FileConfiguration;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class GaminForDummys extends JavaPlugin
    15. {
    16. Logger log = Logger.getLogger("Minecraft");
    17. ConfigurationLoader save;
    18. public FileConfiguration config;
    19.  
    20. public Economy eco = new Economy(this);
    21.  
    22. public static HashMap<String, Integer> balance = new HashMap<String, Integer>();
    23.  
    24. public void onEnable() {
    25. save = new ConfigurationLoader(this, "accounts.yml");
    26. save.getConfig().options().copyDefaults(true);
    27. save.saveDefaultConfig();
    28. save.saveConfig();
    29. config = save.getConfig();
    30.  
    31. evts e = new evts(this);
    32.  
    33. getCommand("Lag").setExecutor(new Lag());
    34. getCommand("GM").setExecutor(new GM());
    35. getCommand("KillAll").setExecutor(new KillAll());
    36. getCommand("GM").setExecutor(new GM());
    37. getCommand("Money").setExecutor(new Money());
    38. getCommand("WorkBench").setExecutor(new WorkBench());
    39. getCommand("EnderChest").setExecutor(new EnderChest());
    40. getCommand("Chest").setExecutor(new Chest());
    41. getCommand("Feed").setExecutor(new Feed());
    42. getCommand("Pay").setExecutor(new Pay());
    43. getCommand("INVSee").setExecutor(new INVSee());
    44. getCommand("Chat").setExecutor(new Chat());
    45. getCommand("MSG").setExecutor(new MSG());
    46.  
    47. this.log.info("[GaminForDummys] v1.0 Enabled.");
    48. }
    49.  
    50. public void onDisable()
    51. {
    52. save.saveConfig();
    53.  
    54. this.log.info("[GaminForDummys] Disabled.");
    55. }
    56.  
    57. }
     
Thread Status:
Not open for further replies.

Share This Page