Save Hashmap to config.yml

Discussion in 'Plugin Development' started by CBaer4848, Jul 30, 2014.

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

    CBaer4848

    So I'm making an xray detector plugin where it stores the amount of emeralds, diamonds, gold, and iron the player has mined. Everytime I reload / restart the data resets in the hashmap. Then when I type the command in game " /xraycheck (name)" it says some data is null. I need some help, thanks anyway!
     
  2. Offline

    TheMcScavenger

    Config:
    Code:java
    1. <playername>:
    2. <item name>:<item amount>
    3. <item name>:<item amount>
    4. <playername>:
    5. <item name>:<item amount>
    6. <item name>:<item amount>


    Code:
    Code:java
    1. getConfig().set(player.getUniqueID().toString() + "." + materialName, amount);


    Show your code!
     
  3. Offline

    valon750

    CBaer4848
    During the onDisable method, make sure to loop through the HashMap, saving the "keySet" to a file.

    Then, during the onEnable, simply loop through the keys of the config, storing them in a HashMap. :3
     
  4. Offline

    CBaer4848

  5. Offline

    TheMcScavenger

    I already posted the code, it's not that hard.
     
  6. Offline

    CBaer4848

    How do I make the config.yml like that, that's the part I don't understand TheMcScavenger
     
  7. Offline

    mythbusterma

    CBaer4848
    This:
     
  8. Offline

    CBaer4848

  9. Offline

    mythbusterma

    CBaer4848

    You put that whenever you need to save it, don't forget to follow it with saveConfig() (I think that's what it is...).
     
  10. Offline

    CBaer4848

  11. Offline

    TheMcScavenger

    The configuration file is automatically generated on "saveConfig()" when it doesn't exist.
     
  12. Offline

    CBaer4848

    Also here is my code, if you needed that to help me...

    Code:
    import java.util.HashMap;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin implements Listener {
       
        Permission alertPerm = new Permission("xray.alert");
       
        public HashMap<String, Integer> diamonds = new HashMap<String, Integer>();
        public HashMap<String, Integer> emeralds = new HashMap<String, Integer>();
        public HashMap<String, Integer> gold = new HashMap<String, Integer>();
        public HashMap<String, Integer> iron = new HashMap<String, Integer>();
        public HashMap<String, Integer> spawner = new HashMap<String, Integer>();
       
       
        @Override
        public void onEnable() {
            System.out.println("XrayDetector enabled.");
            PluginManager pm = Bukkit.getPluginManager();
            pm.registerEvents(this, this);
     
        }
       
        @Override
        public void onDisable() {
            System.out.println("XrayDetector disabled.");
           
        }
       
        public void addEmerald(Player player, int amount) {
            emeralds.put(player.getName(), Integer.valueOf(emeralds.get(player.getName()) == null ? amount : ((Integer) emeralds.get(player.getName()).intValue() + amount)));
        }
       
        public void addDiamond(Player player, int amount) {
            diamonds.put(player.getName(), Integer.valueOf(diamonds.get(player.getName()) == null ? amount : ((Integer) diamonds.get(player.getName()).intValue() + amount)));
        }
       
        public void addGold(Player player, int amount) {
            gold.put(player.getName(), Integer.valueOf(gold.get(player.getName()) == null ? amount : ((Integer) gold.get(player.getName()).intValue() + amount)));
        }
       
        public void addIron(Player player, int amount) {
            iron.put(player.getName(), Integer.valueOf(iron.get(player.getName()) == null ? amount : ((Integer) iron.get(player.getName()).intValue() + amount)));
        }
       
        public void addSpawner(Player player, int amount) {
            spawner.put(player.getName(), Integer.valueOf(spawner.get(player.getName()) == null ? amount : ((Integer) spawner.get(player.getName()).intValue() + amount)));
        }
       
        public void sendAlertXrayMessage(String message) {
            for(Player online : Bukkit.getOnlinePlayers()) {
                if(online.hasPermission(alertPerm) || online.isOp()) {
                    online.sendMessage(message);
                }
                   
                }
            }
           
        @EventHandler
        public void onPlayerBreak(BlockBreakEvent event) {
            Player player = event.getPlayer();
            Block block = event.getBlock();
            if(block.getType() == Material.EMERALD_ORE) {
                addEmerald(player, 1);
                sendAlertXrayMessage(player.getDisplayName() + ChatColor.GRAY + " uncovered emerald (" + ChatColor.RED + emeralds.get(player.getName()) + ChatColor.GRAY + ")");
            }
            if(block.getType() == Material.DIAMOND_ORE) {
                addDiamond(player, 1);
                sendAlertXrayMessage(player.getDisplayName() + ChatColor.GRAY + " uncovered diamond (" + ChatColor.RED + diamonds.get(player.getName()) + ChatColor.GRAY + ")");
            }
            if(block.getType() == Material.GOLD_ORE) {
                addGold(player, 1);
                sendAlertXrayMessage(player.getDisplayName() + ChatColor.GRAY + " uncovered gold (" + ChatColor.RED + gold.get(player.getName()) + ChatColor.GRAY + ")");
            }
            if(block.getType() == Material.IRON_ORE) {
                addIron(player, 1);
                sendAlertXrayMessage(player.getDisplayName() + ChatColor.GRAY + " uncovered iron (" + ChatColor.RED + iron.get(player.getName()) + ChatColor.GRAY + ")");
            }
            if(block.getType() == Material.MOB_SPAWNER) {
                addSpawner(player, 1);
                sendAlertXrayMessage(player.getDisplayName() + ChatColor.GRAY + " uncovered a spawner (" + ChatColor.RED + spawner.get(player.getName()) + ChatColor.GRAY + ")");
            }
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String tag, String[] args) {
            if(!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "Player command only!");
                return true;
            }
            Player player = (Player) sender;
            if(tag.equalsIgnoreCase("xraycheck")) {
                if(args.length == 0) {
                    player.sendMessage(ChatColor.GREEN + "Correct usage - /xraycheck (username)");
                    return true;
                }
                if(args.length == 1) {
                    Player online = Bukkit.getPlayer(args[0]);
                    if(online == null) {
                        player.sendMessage(ChatColor.RED + "Player not found!");
                    } else {
                        if(emeralds.get(player.getName()) == null) {
                            player.sendMessage(ChatColor.GREEN + "Emeralds mined: " + ChatColor.WHITE + "0");
                        } else {
                            player.sendMessage(ChatColor.DARK_GRAY + "Emeralds Mined: " + ChatColor.WHITE + emeralds.get(online.getName()));
                        }
                        if(diamonds.get(player.getName()) == null) {
                            player.sendMessage(ChatColor.AQUA + "Diamonds mined: " + ChatColor.WHITE + "0");
                        } else {
                            player.sendMessage(ChatColor.DARK_GRAY + "Diamonds Mined: " + ChatColor.WHITE + diamonds.get(online.getName()));
                        }
                        if(gold.get(player.getName()) == null) {
                            player.sendMessage(ChatColor.GOLD + "Gold mined: " + ChatColor.WHITE + "0");
                        } else {
                            player.sendMessage(ChatColor.DARK_GRAY + "Gold Mined: " + ChatColor.WHITE + gold.get(online.getName()));
                        }
                        if(iron.get(player.getName()) == null) {
                            player.sendMessage(ChatColor.GRAY + "Iron mined: " + ChatColor.WHITE + "0");
                        } else {
                            player.sendMessage(ChatColor.DARK_GRAY + "Iron Mined: " + ChatColor.WHITE + iron.get(online.getName()));
                        }
                        if(spawner.get(player.getName()) == null) {
                            player.sendMessage(ChatColor.DARK_GREEN + "Spawners mined: " + ChatColor.WHITE + "0");
                        } else {
                            player.sendMessage(ChatColor.DARK_GRAY + "Spawners Mined: " + ChatColor.WHITE + spawner.get(online.getName()));
                        }
                    }
                }
            }
            return false;
        }
     
    }
    
    So I would put the code you said on the onDisable, cause that's where you save it?

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

    TheMcScavenger

    That depends when you want to save it lol.
     
  14. Offline

    mythbusterma

    CBaer4848

    Also when a Player logs off and you free the map entry. Also, you should probably not be using the Player's name to store the associated values, as the switch to a UUID-based system is imminent.
     
  15. Offline

    CBaer4848

    I have no idea what I'm doing right now, cause I'm really confused...

    mythbusterma

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

    mythbusterma

    CBaer4848

    I don't really know what to tell you, we've explained how to do this about as well as we can.
     
  17. Offline

    CBaer4848

    Could you take my code, and add it where you would? mythbusterma

    Bump

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

    Maved

    CBaer4848 mythbusterma He's not going to spoon feed, 2 or more people have it explained it clearly to you now.
     
  19. Offline

    CBaer4848

    I've tried the code, and I get an error when I try to apply it. I'm just asking him / her where they would put it. Maved

    So I've figured out how to make it work with the config, but it doesn't update in game with the command. Any help?

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

Share This Page