Someone help with saving HashMap!

Discussion in 'Plugin Development' started by lordbobby104, Sep 19, 2013.

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

    lordbobby104

    I have been coding this plugin for a little while but I can't seem to figure out how to save the hashmap in this code:

    Code:
    public class Possible extends JavaPlugin {
     
        private HashMap<String, Integer> report = new HashMap<String, Integer>();
        private static final Logger log = Logger.getLogger("Minecraft");
     
        @Override
        public void onEnable() {
     
        }
     
        @Override
        public void onDisable() {
         
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player player = (Player) sender;
         
            if(commandLabel.equalsIgnoreCase("report"))
            {
                Player target = Bukkit.getPlayer(args[0]);
                if(target instanceof Player) {
                    player.sendMessage(ChatColor.RED + "You must report a player!");
                }
             
                if(target == null)
                {
                    player.sendMessage(ChatColor.RED + "That player isn't online!");
                }
             
                if(player.isOp() || player.hasPermission("admin.report"))
                {
                    player.sendMessage(ChatColor.RED + target.getName() + ChatColor.GREEN + " has been reported!");
                    log.info("==========Hacker Reported==========");
                    log.info("");
                    log.info(target.getName() + " was reported of hacks!");
                    log.info("Reported by " + player.getName() + "!");
                    log.info("");
                    log.info("===================================");
                    if(!report.containsKey(target)) {
                        report.put(target.getName(), 1);
                    } else {
                        report.put(target.getName(), report.get(target) + 1);             
                    }
                }
                else
                {
                    player.sendMessage(ChatColor.RED + "You can't do that!");
                }
                return true;
            }
            if(commandLabel.equalsIgnoreCase("check")) {
                Player target = Bukkit.getPlayer(args[0]);
                if(player.isOp() || player.hasPermission("admin.check")) {
                    if(!report.containsKey(target)) {
                        player.sendMessage(ChatColor.GREEN + "That player hasn't been reported!");
                    } else {
                        player.sendMessage(ChatColor.GREEN + "That player has been reported " + report.get(target) + " times!");
                    }
                }
                return true;
            }
            return false;
        }
     
     
    }
     
  2. Offline

    chasechocolate

    What's the problem? Is there an error? Also, you have to check the length of the arguments before trying to access an index of an array which could not exist. For example, if you want to get args[0], you would need to check if args.length == 1.
     
  3. Offline

    lordbobby104

    chasechocolate
    I'm trying to save the hashmap when the server closes and then reload it when the server starts again. I don't know how to though. :p
     
  4. Offline

    chasechocolate

  5. Offline

    turqmelon

    You could save each value in a list in a string, separated by a special character in your config.

    So, say you have your key and value and you want to use @ as the special character, make a String "key@value", then save it to a list. When the server loads, iterate through your list, adding each part to your hashmap as it was before shutdown.

    lordbobby104
     
  6. Offline

    lordbobby104

    turqmelon
    Could you give an example code of how to do this?
     
Thread Status:
Not open for further replies.

Share This Page