Simple Currency - Saving Hashmaps

Discussion in 'Plugin Development' started by Doooogle, Jan 11, 2014.

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

    Doooogle

    I have worked on some code set a players currency. I've setup a hashmap, and players are added to that hashmap on the PlayerJoinEvent, as shown below. I'm wondering what would be the simplest way to save this hashmap to the config file every time someone joins and when the currency is modified in anyway. Here is the code so far:
    Code:java
    1. HashMap<String, Integer> money = new HashMap<String, Integer>();
    2.  
    3. @EventHandler
    4. public void onJoin(PlayerJoinEvent event){
    5.  
    6. Player player = event.getPlayer();
    7. if(!money.containsKey(player.getName())){
    8.  
    9. money.put(player.getName(), 100);
    10.  
    11. if(money.containsKey(player.getName())){
    12.  
    13. player.sendMessage("Added to the HashMap!");
    14.  
    15. }
    16.  
    17. }
    18.  
    19. }

    ^PlayerJoinEvent, setting player to hashmap^
    Code:java
    1. @EventHandler
    2. public void onHit(EntityDamageByEntityEvent event){
    3.  
    4. Player damager = (Player)event.getEntity();
    5.  
    6. if(money.containsKey(damager.getName())){
    7.  
    8. money.put(damager.getName(), money.get(damager.getName()) + 5);
    9.  
    10. }
    11.  
    12. Player player = (Player)event.getEntity();
    13.  
    14. if(money.containsKey(player.getName())){
    15.  
    16. money.put(player.getName(), money.get(damager.getName()) - 5);
    17.  
    18. }
    19.  
    20. }

    ^Simple change in the hashmap, modifying the players currency^

    Thanks! Doooogs
     
  2. Offline

    Minecrell

    Doooogle
    You can set the map to the config. :)
    Code:java
    1. getConfig().set("money", money);

    Not sure for loading the money, but I think you get it using getConfig().get("money") and cast it to a map.
     
  3. Offline

    Doooogle

    Minecrell
    Would I put this in the playerjoinevent?
     
  4. Offline

    Minecrell

    You need to add before saving the configuration using saveConfig(), so if you want to update the configuration in the join event you need to add it there. :)
     
  5. Offline

    Doooogle

    Getting an error after putting it under the PlayerJoinEvent;
    Code:
    [SEVERE] Could not pass event PlayerJoinEvent to HungerInfinity v1.0
    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_R3.PlayerList.c(PlayerList.java:209)
        at net.minecraft.server.v1_6_R3.PlayerList.a(PlayerList.java:105)
        at net.minecraft.server.v1_6_R3.PendingConnection.e(PendingConnection.java:188)
        at net.minecraft.server.v1_6_R3.PendingConnection.d(PendingConnection.java:52)
        at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java:138)
        at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:604)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:240)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:493)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:425)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.IllegalArgumentException: File cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:192)
        at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:170)
        at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:117)
        at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:111)
        at HungerInfinity.Doooogle.me.HungerCurrency.onJoin(HungerCurrency.java:29)
        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)
        ... 14 more
    
    Dunno if the error message is any help, but before I added in the getConfig().set stuff, it showed the player a message when they were added to the hashmap, just to make sure that they were added. After I added the getConfig, it isn't working :/
     
  6. Offline

    Minecrell

    Looks like there's something wrong with your plugin class... Can you post the complete plugin class here?
     
  7. Offline

    Doooogle

    http://pastebin.com/RQwtJWzj here is the plugin class for that. The core class that has the onEnable stuff is in a different class, if you need that, ask.
     
  8. Offline

    Minecrell

    Don't make your listener / command class extend JavaPlugin. Instead pass the instance to a constructor and use it to load and save the configuration. :)
     
  9. Offline

    Doooogle

    http://pastebin.com/HW1y4haU
    HungerCore is the main class with the onEnabe; once I added the part you said to add into the HungerCurrency (current class) it gave errors to the registerEvents in the main class (HungerCore). The whole thing still isn't working, but there's no error now, I have a feeling it may be due to the lack of registering events.
     
  10. Offline

    Minecrell

    Now you need to register the events and commands in the main plugin class. (Make sure passing your plugin instance when creating the class!)
    Code:java
    1. HungerCurrency currency = new HungerCurrency(this);
    2. getServer().getPluginManager().registerEvents(currency, this);
    3. // And for the command
     
  11. Offline

    Doooogle

    Works now, registering the events and stuff. The config would be found under the plugin folder in the plugin directory, right? If so, I opened this, and there is no changes to the config file, it's still empty as usual. Thanks for your help man! Really appreciated!
     
  12. Offline

    Minecrell

    You need to save the configuration to the file system after changing it. Just call plugin.saveConfig() after setting the map and it should work. :)
     
  13. Offline

    Doooogle

    ONE last thing :) when I load the config with plugin.getconfig().get("money"), do I put that in the main class under the onEnable() or in the HungerCurrency under something else, and what is that something else if that is where you put it (PlayerJoinevent, etc.)? Thanks a ton.
     
  14. Offline

    Minecrell

    You can add it in the constructor of your listener class. Something like:
    Code:java
    1. money = (Map<String, Integer>) plugin.getConfig().get("money");

    That should work, but it would be good if you test it first because I don't know exactly.. :p
     
  15. Offline

    Doooogle

    Code:java
    1. public class HungerCurrency implements Listener, CommandExecutor{
    2.  
    3. public HungerCore plugin;
    4. public HungerCurrency(HungerCore plugin){
    5.  
    6. this.plugin = plugin;
    7.  
    8. }
    9.  
    10. HashMap<String, Integer> money = new HashMap<String, Integer>();
    11. money = (Map<String, Integer>) plugin.getConfig().get("money");

    Like this? When I do it, the ; after the HashMap part has a syntax error.
     
  16. Offline

    Minecrell

    Doooogle
    No, add it in your constructor (after you set the plugin). ^^
     
  17. Offline

    Doooogle

    Minecrell
    Code:java
    1. public HungerCurrency(HungerCore plugin){
    2.  
    3. this.plugin = plugin;
    4.  
    5. }

    If you mean in there (just new to Bukkit/java atm, still have a lot to learn, I think that's the constructor), it gives me an error when I put the money = (Map<String, Integer>) part in there.
     
  18. Offline

    Minecrell

    Doooogle
    Yes, that's the constructor. :)
    What exactly does it say? (Post something like the error message)
     
  19. Offline

    Doooogle

    Minecrell
    The error is "Map<String, Integer> cannot be resolved to a type".
     
  20. Offline

    Minecrell

    Oh, try using (HashMap<String, Integer>) instead.
     
  21. Offline

    Doooogle

    Minecrell
    Got an error when I reloaded the plugin this time. Changed it to HashMap, this is what it's like in the class: http://puu.sh/6h6at.png
    Error message on plugin reload;
    Code:
    [SEVERE] Error occurred while enabling HungerInfinity v1.0 (Is it up to date?)
    java.lang.ClassCastException: org.bukkit.configuration.MemorySection cannot be cast to java.util.HashMap
        at HungerInfinity.Doooogle.me.HungerCurrency.<init>(HungerCurrency.java:23)
        at HungerInfinity.Doooogle.me.HungerCore.onEnable(HungerCore.java:10)
        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:382)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.loadPlugin(CraftServer.java:288)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.enablePlugins(CraftServer.java:270)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.reload(CraftServer.java:618)
        at org.bukkit.Bukkit.reload(Bukkit.java:277)
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:24)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:532)
        at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:986)
        at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:897)
        at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:838)
        at net.minecraft.server.v1_6_R3.Packet3Chat.handle(Packet3Chat.java:47)
        at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:237)
        at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:117)
        at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
        at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java:131)
        at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:604)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:240)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:493)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:425)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
     
  22. Offline

    Minecrell

    Doooogle
    Hmm, too bad it's not working. I'm going to test it myself, can you post the generated configuration here?

    Doooogle
    Ok, I think I got it working. :) Replace this line:
    Code:java
    1. HashMap<String, Integer> money = new HashMap<String, Integer>();

    With this one:
    Code:java
    1. Map<String, Integer> money;

    And now load it like this:
    Code:java
    1. ConfigurationSection moneySection = this.getConfig().getConfigurationSection("money");
    2. // Create the money section in the configuration if there is none
    3. if (moneySection == null) moneySection = this.getConfig().createSection("money");
    4. // Load the map
    5. money = (Map) moneySection.getValues(false);

    Make sure importing Map, so you don't get the unknown type error again. :)
    (That's probably not the best way of doing this, but I can't think of another way at the moment..)

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

    Doooogle

    Minecrell
    Still having an issue with the "money = (Map) moneySection.getValues(false);". The error is "The expression of type Map needs unchecked conversion to conform to Map<String, Integer>".
     
  24. Offline

    Minecrell

    That should be only a warning, not a real compiler error. You can try adding the annotation before that line so it won't be displayed:
    Code:java
    1. @SupressWarnings("unchecked")
     
Thread Status:
Not open for further replies.

Share This Page