Hashmap Problems, Keep getting nulls[Economy-like Plugin]

Discussion in 'Plugin Development' started by shinobuattack, Jul 21, 2013.

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

    shinobuattack

    Trying to make an economy-like plugin. Basically whenever a new player logs into the server, they are start out with $100, but for some reason when I do /balance, I am getting a null?
    Pay special close attention to onPlayerJoin?
    This is mainly for practice and I'm not using to make an actually useful plugin, just practice
    Code:
    package me.shinobuattack.mobmoney;
     
    import java.util.HashMap;
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class MobMoney extends JavaPlugin{
    private static final Logger log = Logger.getLogger("Minecraft");
    public final HashMap<String, Integer> playerMoney = new HashMap<String, Integer>();
     
    @Override
    public void onDisable(){
    log.info("[MobMoney] has been disabled.");
    }
     
    @Override
    public void onEnable(){
    log.info("[MobMoney] has been enabled.");
    }
     
    public boolean onCommand(CommandSender sender, Command cmd, String commandLbl, String[] arg){
    Player player = (Player)sender;
    if(commandLbl.equalsIgnoreCase("balance")){
    if(arg.length == 0){
    player.sendMessage("Balance: \n$" + playerMoney.get(player.getName()));
    }else if(arg.length == 1){
    Player targetPlayer = player.getServer().getPlayer(arg[0]);
    player.sendMessage("Balance of " + ChatColor.GREEN + targetPlayer.getName() + ChatColor.YELLOW + ": $" + playerMoney.get(targetPlayer.getName()));
    }
    }
    return false;
    }
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event)
    {
    if(!event.getPlayer().hasPlayedBefore()){
    event.getPlayer().sendMessage(ChatColor.DARK_BLUE + "Welcome, " + ChatColor.AQUA + "First timer, " + ChatColor.GREEN + event.getPlayer().getDisplayName());
    playerMoney.put(event.getPlayer().getName(), 100);
    }else{
    event.getPlayer().sendMessage(ChatColor.DARK_BLUE + "Welcome, " + event.getPlayer().getDisplayName() + ChatColor.DARK_AQUA + "to the Server!");
    }
    }
    }
    
     
  2. Offline

    Lolmewn

    Register the events and implement listener
     
  3. Offline

    shinobuattack

    Lolmewn thank you
    Hmm.. not sure if I did it right. Registering events is something that is always done in the onEnable, right? Also, is this an effective way of starting first time players out with money in an economy server? Not perfect, just if it gets the job done?
    Code:
    public class MobMoney extends JavaPlugin implements Listener{
        private static final Logger log = Logger.getLogger("Minecraft");
        public final HashMap<String, Integer> playerMoney = new HashMap<String, Integer>();
     
        @Override
        public void onDisable(){
            log.info("[MobMoney] has been disabled.");
        }
     
        @Override
        public void onEnable(){
            log.info("[MobMoney] has been enabled.");
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents((Listener)this, this);
        }
    Oh, I think that it is a problem of saving the data in the hashmap, I'll do some research
     
  4. Offline

    Lolmewn

    shinobuattack yep, on enable. Also, instead of using hasPlayedBefore I suggest you use your config to see if he already has a balance. If not, set it to default.
     
  5. Offline

    shinobuattack

    Would MySQL DataBase be better? Which would be easiest, or better yet, which is more efficient MySQL DataBase or using the config. Also for some reason when I try to put information on my config nothing happens, it just remains a blank yml

    Lolmewn
    EDIT: I don't think I am ready for MySQL so could you explain to me how to store the player balance to config.yml?

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

    Lolmewn

    shinobuattack check out the wiki, there's a great article on how to use the config.
     
  7. Offline

    shinobuattack

    Lolmewn First, I'd like to thank you for taking the time to answer my problems, and second
    I am aware of that wiki. It has useful information but for some reason I can't set any values in it but I am able to check my config.yml but... I am able to use,for example:
    Code:java
    1. if(this.getConfig().contains(player.getName())){
    2. bal += 100.0;
    3. playerMoney.put(player.getName(), bal);

    That works fine. but when I try to set a String, it can't access it or something. Not sure what is wrong. Ex:
    Code:java
    1. @Override
    2. public void onDisable(){
    3. log.info("[Economy] has been disabled.");
    4. this.getConfig().set("message", "some string");
    5. }

    I've tried doing this in many points of my code but it just doesn't write anything in the config :/

    Here's the full code:
    Code:java
    1. package me.shinobuattack.econ;
    2.  
    3. import java.util.HashMap;
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.player.PlayerJoinEvent;
    13. import org.bukkit.plugin.PluginManager;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class Economy extends JavaPlugin implements Listener{
    17. Double bal = 1000.0;
    18. private static final Logger log = Logger.getLogger("Minecraft");
    19. public final HashMap<String, Double> playerMoney = new HashMap<String, Double>();
    20.  
    21. @Override
    22. public void onDisable(){
    23. log.info("[Economy] has been disabled.");
    24. this.getConfig().set("message", "some string");
    25. }
    26.  
    27. @Override
    28. public void onEnable(){
    29. this.reloadConfig();
    30. log.info("[Economy] has been enabled.");
    31. PluginManager pm = getServer().getPluginManager();
    32. pm.registerEvents(this, this);
    33. }
    34.  
    35. //public boolean
    36.  
    37. public boolean onCommand(CommandSender sender, Command cmd, String commandLbl, String[] arg){
    38. Player player = (Player)sender;
    39. if(commandLbl.equalsIgnoreCase("balance")){
    40. if(arg.length == 0){
    41. player.sendMessage("Balance:\n $ " + getPlayerBal(player.getName()));
    42. }else if(arg.length == 1){
    43. Player targetPlayer = player.getServer().getPlayer(arg[0]);
    44. player.sendMessage(ChatColor.GOLD + "Balance of " + ChatColor.GREEN + targetPlayer.getName() + ChatColor.WHITE + ": $ " + getPlayerBal((targetPlayer.getName())));
    45. }
    46. }else if(commandLbl.equalsIgnoreCase("add")){
    47. if(this.getConfig().contains(player.getName())){
    48. bal += 100.0;
    49. playerMoney.put(player.getName(), bal);
    50. }else
    51. player.sendMessage(ChatColor.RED + "You must be registered on this server to add to your balance!");
    52. }
    53. return false;
    54. }
    55. @EventHandler
    56. public void onPlayerJoin(PlayerJoinEvent event)
    57. {
    58. if(this.getConfig().contains(event.getPlayer().getName())){
    59. event.getPlayer().sendMessage(ChatColor.DARK_BLUE + "Welcome, " + ChatColor.WHITE + event.getPlayer().getDisplayName() + ChatColor.DARK_GREEN + " to the Server!");
    60. }else{
    61. playerMoney.put(event.getPlayer().getName(), bal);
    62. event.getPlayer().sendMessage(ChatColor.DARK_BLUE + "Welcome, " + ChatColor.AQUA + "First timer, " + ChatColor.GREEN + event.getPlayer().getDisplayName());
    63. this.getConfig().set("playerMoney", playerMoney); // <--- Is this the right way of storing a HashMap??
    64. }
    65. }
    66.  
    67. public Double getPlayerBal(String playerName){
    68. if(playerMoney.containsKey(playerName)){
    69. return playerMoney.get(playerName);
    70. }
    71. return 0D;
    72. }
    73.  
    74.  
    75. }
    76.  
     
  8. Offline

    collielimabean

  9. Offline

    Lolmewn

    shinobuattack Try using saveDefaultConfig(), this saves the config.yml which is in the root directory of your jar.
    Also, don't forget to saveConfig() after modifying it.
     
  10. Offline

    shinobuattack

    I get the weird feeling that this is about to work :D

    Lolmewn
    Yep, it writes in the config now :)

    Now it seems that I am unable to check if a player name is in the config (which it now is) and everytime I log into my localserver, it says that I am a new player (I obviously am not)
    Here's the code:
    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(PlayerJoinEvent event)
    3. {
    4. if(getConfig().contains(event.getPlayer().getName())){ // This line
    5. event.getPlayer().sendMessage(ChatColor.DARK_BLUE + "Welcome, " + ChatColor.WHITE + event.getPlayer().getDisplayName() + ChatColor.DARK_GREEN + " to the Server!");
    6. }else{
    7. playerMoney.put(event.getPlayer().getName(), bal);
    8. event.getPlayer().sendMessage(ChatColor.DARK_BLUE + "Welcome, " + ChatColor.AQUA + "First timer, " + ChatColor.GREEN + event.getPlayer().getDisplayName());
    9. getConfig().set("playerMoney", playerMoney);
    10. }
    11. }

    Also:
    Code:java
    1. else if(commandLbl.equalsIgnoreCase("add")){
    2. if(getConfig().contains(player.getName())){ // This line too
    3. bal += 100.0;
    4. playerMoney.put(player.getName(), bal);
    5. }else
    6. player.sendMessage(ChatColor.RED + "You must be registered on this server to add to your balance!");
    7. }

    Is that the correct way of checking a config for a String?

    Okay for the /add command that I made, in that if statement, instead of
    Code:java
    1. player.sendMessage(ChatColor.RED + "You must be registered on this server to add to your balance!");

    I put:
    Code:java
    1. player.sendMessage(ChatColor.RED + getConfig().getString(player.getName()));
    and I got back a null for what should have been a String so does that mean that the String is there ( I know it is ) and I am accessing config incorrectly?

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

    Lolmewn

  12. Offline

    shinobuattack

    Lolmewn
    The only thing I put in my config was the HashMap:

    playerMoney: Jaycalli50: 1000.0
     
  13. Offline

    Lolmewn

    shinobuattack Then you should use 'playerMoney.Jaycalli50' for the path instead of only your name.
     
  14. Offline

    shinobuattack

    Lolmewn Thank you! It worked :)

    Lolmewn
    I'm really sorry, I have to ask another question... No rush, whenever you feel like it just because I keep pestering you, really sorry. How do I retrieve the balance from the config if it is a potentially changeable value? I've been trying to rap my head around this for the past hour and a half to no avail :(

    This is what I have so far (and btw, it returns null again >.<)
    Code:java
    1. public Double getPlayerBal(String player){
    2. playerMoney.put(getConfig().getString("playerMoney." + player), getConfig().getDouble("playerMoney." + player + "." + bal));
    3. return playerMoney.get(player);
    4. }
    5. }
    6.  


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

    Lolmewn

    shinobuattack I'd simply either load the configuration on start and save the data every... Say 10 minutes, or only use the configuration and no sets or maps
     
  16. Offline

    shinobuattack

    Lolmewn I'm not sure if that is my problem.. I can't access the balance because it is a changeable double so I was thinking about looping the HashMap with the config so it would be like this: (HashMap --> config <--> HashMap) so it would start out as playerMoney (the hashmap) then transfer to config then I'd have to access config again in order to get the balance because there is no other way to get the balance after the player logs off the server than the config, right?
     
  17. Offline

    Lolmewn

    shinobuattack unless you keep them in your hashmap, yes. I suggest you try and take a look at how simply economy plugins do this, actually. Try looking one up on dbo, and look for its source.
     
  18. Offline

    shinobuattack

    Yeah, I thought it was weird that my head was spinning round n round from just trying to get access a value I already had.. Idk what is wrong with me but I'll give what you said a try, thanks for the suggestion.
     
Thread Status:
Not open for further replies.

Share This Page