Load hashmap on user login

Discussion in 'Plugin Development' started by ratedam, Feb 3, 2014.

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

    ratedam

    Evening, so i'm trying to load the user hashmap on login and save on loggout.

    Here's my main class:
    Code:
    package com.angelo.armazena;
     
    import java.util.HashMap;
    import java.util.List;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    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.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class armazena extends JavaPlugin implements Listener {
    public final Logger logger = Logger.getLogger("Minecraft");
    public static armazena plugin; // para mencionar o nome no futuro
    private HashMap<Player, Integer> map = new HashMap<Player, Integer>();
    private int times = 0;
     
    public void onDisable() {
     
    PluginDescriptionFile pdfFile = this.getDescription(); // Atualizar
    // descrição
    List<String> s = getConfig().getStringList("scores");
    for (Player player : map.keySet()) {
    s.add(player.getName() + ":" + map.get(player));
    }
    getConfig().set("scores", s);
    saveConfig();
     
    this.logger.info(pdfFile.getName() + " has been disabled"); // vai
    // buscar o
    // nome
    }
     
    public void onEnable() {
    PluginDescriptionFile pdfFile = this.getDescription(); // Atualizar
    // descrição
    saveDefaultConfig();
    List<String> s = getConfig().getStringList("scores");
    for (String str : s) {
    String[] words = str.split(":");
    map.put(Bukkit.getServer().getPlayer(words[0]),
    Integer.parseInt(words[1]));
     
    }
    Bukkit.getPluginManager().registerEvents(new WelcomeListener(), this);
    this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion()
    + " has been enabled"); // vai buscar o nome
    }
     
    public boolean onCommand(CommandSender sender, Command cmd,
    String commandLabel, String[] args) {
    Player player = (Player) sender;
    if (commandLabel.equalsIgnoreCase("sendme")) {
    if (map.get(player) == null) {
    map.put(player, 1);
    times = map.get(player);
    player.sendMessage("não existia e " + ChatColor.BLUE + "Sent "
    + times + " times");
    } else {
    times = map.get(player) + 1;
    map.put(player, times);
    player.sendMessage(ChatColor.BLUE + "Sent " + times + " times");
    }
     
    }
    return false;
    }
     
    }
    And my listener
    Code:
    package com.angelo.armazena;
     
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
     
    public class WelcomeListener implements Listener {
    @EventHandler
       public void onPlayerJoin(PlayerJoinEvent event) {
           Player player = event.getPlayer();
           player.sendMessage(ChatColor.GREEN + "Welcome ");
           
       }
    }
    How can i load now? I tried to create a private method in the main an access it through the class but that's impossible to do due to java restrictions...

    Any help on this? Cheers.

    I'd really appreciate if someone could help me with this. Thank you

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

    Staartvin

    You can either make your hashmap public and use hashmap.put() and hashmap.get(), or you can keep the hashmap private but make public methods to load or save a value.

    Be careful though, using a player variable in a hashmap can cause memory leaks when not handled properly. It is best to use the name of the player and get the player object after you got the name of the player.
     
  3. Offline

    xize

    ratedam
    if your event is not inside the same class make variable map static.
    then do in PlayerJoinEvent yourplugin.map.put(e.getPlayer(), number); and in PlayerQuitEvent and PlayerKickEvent you do yourplugin.map.remove(e.getPlayer().getName());

    make always sure you check the HashMap with containsKey() before removing players or getting players.
     
  4. Offline

    ratedam

    I now have in my main class
    Code:java
    1. public boolean load(){
    2. saveDefaultConfig();
    3. List<String> s = getConfig().getStringList("scores");
    4. for (String str : s) {
    5. String[] words = str.split(":");
    6. map.put(Bukkit.getServer().getPlayer(words[0]),
    7. Integer.parseInt(words[1]));
    8. }
    9. return false;
    10. }


    and in the WelcomeListener.java inside the onPlayerjoin the armazena.load() and it says that i must put the load has static but when i do that i can't access the saveDefaultConfig() ,getConfig() nor make reference to the hashmap (it says that i must turn him to static).

    How can i overcome this? Thank you

    xize won't that delete the values of the player on loggout?

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

    xize

    ratedam
    for HashMaps, ArrayLists, HashSets its very important you remove players or player names from the HashMap when the player quits or gets kicked else your server ends up with a memory leak.

    if that whas your awnser to your question else I don't understand.
     
  6. Offline

    1Rogue

    Don't use static, and you should even be careful about exposing the map entirely. My advice would really be keeping the map in a class with a package-private accessor method, or even keeping the map in the same class.
     
  7. Offline

    ratedam

    xize well this might seem a dumb question from myself, when i remove the player or the player names from the hashmap, when the user relogs on the server he won't have their details right? Maybe if i explain you my problem it'd be better.

    I want that everytime the users send the message "sendme" it adds +1 to the sent var. Whenever the user uses the "sendme" it prints "You have sent it X times". Currently with what i have when i reload the server it saves the current state of that X but when the player logs out the X value if lost.

    Thank you
     
Thread Status:
Not open for further replies.

Share This Page