Looping through a hashmap

Discussion in 'Plugin Development' started by TopTobster5, Aug 19, 2014.

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

    TopTobster5

    Hi.

    I am making a plugin which allows users to have multiple nicknames which cycle around. In order to store this, I am using a Hashmap with <String, ArrayList<String>>. This works fine when the plugin is running, but When I come to save it, I run into problems.

    After some Googling, I found out you can loop through a hashmap using
    Code:java
    1. for (Entry<String, ArrayList<String>>playernames : playernicknames.entrySet())

    Where playernicknames is my current hashmap.

    This is my entire for loop:

    Code:java
    1. for (Entry<String, ArrayList<String>>playernames : playernicknames.entrySet()) {
    2. tempnames.clear();
    3. getLogger().info(playernames.getKey());
    4. getLogger().info(String.valueOf(playernames.getValue().size()));
    5. for (String s : playernames.getValue()) {
    6. tempnames.add(s);
    7. }
    8. getLogger().info(String.valueOf(tempnames.size()));
    9. getNicknames().set("Nicknames." + playernames.getKey(), tempnames);
    10. }


    As you can see, I have alot of debug messages.

    The first time I run the code, it does so perfectly. No matter what i do to it. (I have tried colorCodes, multiple nicknames, multiple players). However, the second time I reload the server, It all breaks.

    Here is what happens when I try and run the code the second time:
    Code:
    [11:52:48] [Server thread/INFO]: [MultiNick] Disabling MultiNick v1.0
    [11:52:48] [Server thread/INFO]: [MultiNick] 7e9cbb5e-dda4-40a9-ad4e-40e17466bfa6
    [11:52:48] [Server thread/INFO]: [MultiNick] 0
    [11:52:48] [Server thread/INFO]: [MultiNick] 0
    [11:52:48] [Server thread/INFO]: [MultiNick] MultiNick has been disabled!
    Here is what the yml file should look like:
    Nicknames:
    7e9cbb5e-dda4-40a9-ad4e-40e17466bfa6:
    - Toby
    - Top
    And here is what it looks like after the code breaks it: (Not sure if you need this, though I should put it in anyway)
    Nicknames:
    7e9cbb5e-dda4-40a9-ad4e-40e17466bfa6: []

    The UUID is my UUID, so I know it was actually looping. The zeroes are the size of the loop, both 'tempnames' and 'playernames.getKey()'

    However, it should be 2. So why is it doing this?

    Thanks for any help received. I understand this is a long post, but I was trying to give you all the relevant information I could.
     
  2. Offline

    Rocoty

    Well, it depends on the rest of your code. We cannot possibly help you with what we have now.
     
  3. Offline

    br456

    TopTobster5
    Are you putting the names into the hashmap when the plugin loads?
     
  4. Offline

    fireblast709

    TopTobster5 why are you adding all names to a temp List. Just save the List :3
     
  5. Offline

    TopTobster5

    fireblast709 I tried that, I was just fiddling about trying to get something to work.
    br456 I am using Player uuids

    Rocoty Here is the rest of my code:
    Code:java
    1. package me.toptobster5.multinick;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.io.InputStream;
    6. import java.util.ArrayList;
    7. import java.util.HashMap;
    8. import java.util.List;
    9. import java.util.Map.Entry;
    10. import java.util.logging.Level;
    11.  
    12. import org.bukkit.Bukkit;
    13. import org.bukkit.ChatColor;
    14. import org.bukkit.command.Command;
    15. import org.bukkit.command.CommandSender;
    16. import org.bukkit.configuration.file.FileConfiguration;
    17. import org.bukkit.configuration.file.YamlConfiguration;
    18. import org.bukkit.entity.Player;
    19. import org.bukkit.event.Listener;
    20. import org.bukkit.plugin.java.JavaPlugin;
    21.  
    22. public class Multinick extends JavaPlugin implements Listener {
    23.  
    24. public File nicknamesfile;
    25. public FileConfiguration nicknames;
    26.  
    27. public String prefix = ChatColor.BLUE + "[MultiNick] " + ChatColor.DARK_BLUE;
    28.  
    29. public HashMap<String, ArrayList<String>>playernicknames = new HashMap<String, ArrayList<String>>();
    30. public HashMap<String, String>playernicknamescurrent = new HashMap<String, String>();
    31. public List<String> tempnames = new ArrayList<String>();
    32.  
    33. public void reloadNicknames() {
    34. if (nicknamesfile == null) {
    35. nicknamesfile = new File(getDataFolder(), "nicknames.yml");
    36. }
    37. nicknames = YamlConfiguration.loadConfiguration(nicknamesfile);
    38.  
    39. InputStream defConfigStream = this.getResource("nicknames.yml");
    40. if (defConfigStream != null) {
    41. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    42. nicknames.setDefaults(defConfig);
    43. }
    44. }
    45.  
    46. public FileConfiguration getNicknames() {
    47. if (nicknames == null) {
    48. reloadNicknames();
    49. }
    50. return nicknames;
    51. }
    52.  
    53. public void saveNicknames() {
    54. if (nicknames == null || nicknamesfile == null) {
    55. return;
    56. }
    57. try {
    58. getNicknames().save(nicknamesfile);
    59. } catch (IOException ex) {
    60. getLogger().log(Level.SEVERE, "Could not save config to " + nicknamesfile, ex);
    61. }
    62. }
    63.  
    64. public void saveDefaultNicknames() {
    65. if (nicknamesfile == null) {
    66. nicknamesfile = new File(getDataFolder(), "nicknames.yml");
    67. }
    68. if (!nicknamesfile.exists()) {
    69. getLogger().info("nicknames.yml does not exist, creating a new one!");
    70. this.saveResource("nicknames.yml", false);
    71. }
    72. }
    73. @Override
    74. public void onEnable() {
    75. saveDefaultNicknames();
    76. getServer().getPluginManager().registerEvents(this, this);
    77. try {
    78. for (String key : getNicknames().getConfigurationSection("Nicknames").getKeys(false)) {
    79. tempnames = getNicknames().getStringList("Nicknames." + key);
    80. playernicknames.put(key, (ArrayList<String>) tempnames);
    81. }
    82. } catch (NullPointerException e) {
    83. }
    84. getLogger().info("MultiNick has been enabled!");
    85. }
    86.  
    87. @Override
    88. public void onDisable() {
    89. getNicknames().set("Nicknames", null);
    90. for (Entry<String, ArrayList<String>>playernames : playernicknames.entrySet()) {
    91. tempnames.clear();
    92. getLogger().info(playernames.getKey());
    93. getLogger().info(String.valueOf(playernames.getValue().size()));
    94. for (String s : playernames.getValue()) {
    95. tempnames.add(s);
    96. }
    97. getLogger().info(String.valueOf(tempnames.size()));
    98. getNicknames().set("Nicknames." + playernames.getKey(), tempnames);
    99. }
    100. saveNicknames();
    101. getLogger().info("MultiNick has been disabled!");
    102. }
    103.  
    104. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    105. if (label.equalsIgnoreCase("nick")) {
    106. if (args.length > 0) {
    107. if (args[0].equalsIgnoreCase("list")) {
    108. if (args.length == 1) {
    109. if (sender instanceof Player) {
    110. String uuid = ((Player) sender).getUniqueId().toString();
    111. ArrayList<String>n = playernicknames.get(uuid);
    112. sender.sendMessage(prefix + "Your current nicknames:");
    113. try {
    114. for (String s : n) {
    115. sender.sendMessage(s);
    116. }
    117. } catch (NullPointerException e) {
    118. }
    119. return true;
    120. } else {
    121. sender.sendMessage(prefix + "You must be a player to do that!");
    122. return true;
    123. }
    124. } else if (args.length == 2) {
    125. if (sender.hasPermission("multinick.mod") || sender.isOp()) {
    126. for (Player player : Bukkit.getOnlinePlayers()) {
    127. if (player.getName().equalsIgnoreCase(args[1])) {
    128. sender.sendMessage(prefix + player.getName() + " nicknames:");
    129. for (String s : playernicknames.get(player.getUniqueId().toString())) {
    130. sender.sendMessage(s);
    131. }
    132. return true;
    133. }
    134. sender.sendMessage(prefix + "That player is not online!");
    135. return true;
    136. }
    137. } else {
    138. sender.sendMessage(prefix + "You don't have permission to do that!");
    139. return true;
    140. }
    141. } else {
    142. sender.sendMessage(prefix + "Unknown command!");
    143. return true;
    144. }
    145. } else if (args[0].equalsIgnoreCase("add")) {
    146. if (args.length == 2) {
    147. if (sender instanceof Player) {
    148. if (sender.hasPermission("multinick.add") || sender.isOp()) {
    149. String nick = ChatColor.translateAlternateColorCodes('&', args[1]);
    150. try {
    151. playernicknames.get(((Player) sender).getUniqueId().toString()).add(nick);
    152. } catch (NullPointerException e) {
    153. ArrayList<String>m = new ArrayList<String>();
    154. m.add(nick);
    155. playernicknames.put(((Player) sender).getUniqueId().toString(), m);
    156. }
    157. sender.sendMessage(prefix + "Nickname added!");
    158. return true;
    159. } else {
    160. sender.sendMessage(prefix + "You don't have permission to do that!");
    161. return true;
    162. }
    163. } else {
    164. sender.sendMessage(prefix + "You must be a player to do that!");
    165. return true;
    166. }
    167. }
    168. }
    169. } else {
    170. sender.sendMessage(prefix + "Unknown command!");
    171. return true;
    172. }
    173. }
    174. return false;
    175. }
    176.  
    177.  
    178. }
    179.  
     
  6. Offline

    fireblast709

    TopTobster5 I laughed a little. After you put the List in the Map, the next iteration that tempnames still points to the List in the Map, and you clear it. Just removing the tempnames.clear() from the onEnable loop should load them properly.
     
  7. Offline

    TopTobster5

    fireblast709 My problem is in the saving part, although I recognize what I did. I have fixed that part, but the plugin still won't save it.
     
  8. Offline

    fireblast709

    TopTobster5 Most likely the same issue. Try it without the use of templist and .clear().
     
Thread Status:
Not open for further replies.

Share This Page