Player isn't being added into the hashmap on login

Discussion in 'Plugin Development' started by TheRCPanda, Jul 23, 2014.

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

    TheRCPanda

    So basically, I am working on some code for a network, and right now I am working on the points plugin. And basically, correct me if I am wrong, but I believe the most efficient way to find out what players need to be given points is to add any player that's ever been logged on into a hashmap by name. And then I just hook into the stats plugin and give them a point hourly. The problem is when the player logs in they are not being added into the hashmap nor are the messages they are supposed to be sent showing up. Here is all the code in the plugin that relates to this hashmap:

    Code:java
    1. package net.TheRCPanda.GGNPoints;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import nl.lolmewn.stats.api.StatsAPI;
    6.  
    7. import org.bukkit.block.Block;
    8. import org.bukkit.block.Sign;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.block.SignChangeEvent;
    15. import org.bukkit.event.player.PlayerLoginEvent;
    16. import org.bukkit.plugin.RegisteredServiceProvider;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public class GGNPoints extends JavaPlugin implements Listener {
    20.  
    21. //Hashmaps for storing players and points data
    22.  
    23. HashMap<Integer, String> playerHist = new HashMap<Integer, String>();
    24. HashMap<String, Integer> playerPoints = new HashMap<String, Integer>();
    25.  
    26. //Method for hooking into the Stats API
    27.  
    28. private StatsAPI statsAPI;
    29.  
    30. private boolean setupStatsAPI(){
    31. RegisteredServiceProvider<StatsAPI> stats = getServer().getServicesManager().getRegistration(nl.lolmewn.stats.api.StatsAPI.class);
    32. if (stats!= null) {
    33. statsAPI = stats.getProvider();
    34. }
    35. return (statsAPI != null);
    36. }
    37.  
    38. public void onEnable() {
    39.  
    40. //Registering all the events in the class
    41.  
    42. getServer().getPluginManager().registerEvents(this, this);
    43.  
    44. //Checking if the Stats API is present then hooking into it
    45.  
    46. if(getServer().getPluginManager().getPlugin("Stats") != null) {
    47. setupStatsAPI();
    48. } else {
    49. getLogger().severe("The Stats plugin was not found in the plugins directory. Download the plugin here: [url]http://dev.bukkit.org/server-mods/lolmewnstats[/url]");
    50. return;
    51. }
    52.  
    53. getLogger().info("GGNPoints plugin is active!");
    54.  
    55. }
    56.  
    57. public void onDisable() {
    58.  
    59. getLogger().info("GGNPoints plugin is no longer active!");
    60.  
    61. }
    62.  
    63. //Adding new players to the playerHist hashmap
    64.  
    65. @EventHandler
    66. public void onPLayerLogin(PlayerLoginEvent event) {
    67.  
    68. Player player = event.getPlayer();
    69. String playerName = player.getName();
    70.  
    71. if(playerHist.containsValue(playerName)) {
    72. player.sendMessage("Welcome to the server!");
    73. playerHist.put(playerHist.size()+1, playerName);
    74. } else {
    75. player.sendMessage("Welcome back, " + playerName + "!");
    76. player.sendMessage("You have" + playerPoints.get(playerName) + "points!");
    77. }
    78.  
    79. }


    Please leave some answers and share your ideas with me if you think there is a more efficient way to do this. And P.S, they are only supposed to get points if they are online :)
     
  2. Offline

    Flegyas

    I don't think you are correctly using HashMaps. For me, the best way to do that is using a DB or any other external data manager because values in HashMaps aren't permanent, every time the plugin is started you'll have an empty HashMap.
    Also I don't understand what you're doing with 2 HashMaps, isn't better the use of only one with UUIDs as key and Points as value?
     
Thread Status:
Not open for further replies.

Share This Page