HashMaps and ArrayLists not working

Discussion in 'Plugin Development' started by PikaThieme, Jun 25, 2017.

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

    PikaThieme

    Hey everyone,
    I was making a 1.12 plugin but I figured out that arraylists en hashmaps doesn't work. I tried difference methods but they keep returning null. Well, it actually works, but only for 1 part of a code. For example:
    @EventHandler
    (code here)
    HashMap.put(bla,bla);
    (later on in code)
    HashMap.get(bla); //works fine

    @EventHandler
    HashMap.get(bla); // returns null even tho in the other event it returns bla

    Here is the code:
    Code:java
    1.  
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11.  
    12. public class Message implements CommandExecutor {
    13.  
    14. HashMap<String,String> hash = new HashMap<String,String>();
    15.  
    16. @Override
    17. public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
    18. Player p = (Player) sender;
    19. if (command.getName().equalsIgnoreCase("msg")) {
    20. if (args.length == 0 || args.length == 1) {
    21. sender.sendMessage("§cPlease use: /" + command.getName() + " <Player> <Message>");
    22. } else {
    23. if (sender instanceof Player) {
    24. String player = args[0];
    25. if (Bukkit.getPlayer(player) != null) {
    26. Player receiver = Bukkit.getPlayer(player);
    27. if (receiver == p) {
    28. p.sendMessage("§cYou can't send a message to yourself ._.");
    29. } else {
    30. String msg = "";
    31. for (int i = 0; i < args.length - 1; i++) {
    32. int a = i + 1;
    33. msg = msg + args[a] + " ";
    34. }
    35. p.sendMessage("§7[§3Me§7] §8➪ " + ChatColor.GRAY + "[§3" + receiver.getDisplayName() + "§7] §8» §7" + msg);
    36. receiver.sendMessage("§7[§3" + p.getDisplayName() + "§7] §8➪ " + ChatColor.GRAY + "[§3Me§7] §8» §7" + msg);
    37. hash.put(p.getName(), receiver.getName());
    38. hash.put(receiver.getName(), p.getName());
    39. }
    40. } else {
    41. p.sendMessage("§cPlayer not found!");
    42. }
    43. }
    44. }
    45.  
    46. }
    47. if(command.getName().equalsIgnoreCase("react")) {
    48. if(args.length == 0) {
    49. p.sendMessage("§cPlease use: /react <Message>");
    50. } else {
    51. if(hash.get(p) == null) {
    52. p.sendMessage("§cThere is no one to react to");
    53. } else {
    54. String msg = "";
    55. for (int i = 0; i < args.length; i++) {
    56. int a = i;
    57. msg = msg + args[a] + " ";
    58. }
    59. Player receiver = Bukkit.getPlayer(hash.get(p.getName()));
    60. p.sendMessage("§7[§3Me§7] §8➪ " + ChatColor.GRAY + "[§3" + receiver.getDisplayName() + "§7] §8» §7" + msg);
    61. receiver.sendMessage("§7[§3" + p.getDisplayName() + "§7] §8➪ " + ChatColor.GRAY + "[§3Me§7] §8» §7" + msg);
    62. }
    63. }
    64. }
    65.  
    66. return true;
    67. }
    68.  
    69.  
    70. }
     
  2. @PikaThieme
    Well, I'm not sure which lines are problematic, but the one I highlighted above probably doesn't work as you expect it to, since you're storing the players names as keys in the map, but you're trying to get the value using the player instance. Try sticking .getName() on the end of the p variable and it should work.

    Also, don't store names in the Maps, names can change! If you store UUIDs instead, you're absolutely sure that you're always referencing the same player.
     
  3. Offline

    PikaThieme

    Well, because it's just a msg plugin, using UUID wouldn't make a different. And yes, I tried in first intention just the hashmap with players (HashMap<Player,Player>) and with the Bukkit.getPlayer the HashMap.get(p).getName() but it still returns null... I think this is a problem with the 1.12 spigot or something :/
     
  4. Online

    timtower Administrator Administrator Moderator

    @PikaThieme Then put this on an older server. Highly doubt it though that they are changing base classes.
    And the other classes probably have their own hashmap, make 1 in the main class with a getter for it.
     
  5. Offline

    Caderape2

    @PikaThieme
    you still have to modify this line
    Or it will always be null.

    There'e no bug with arraylist and hashmap, it'ts just your logic the problem
     
  6. Offline

    PikaThieme

    Tried, gave the null exception on this line:
    plugin.hash.put(p, receiver);

    private Main plugin;

    public Message(Main plugin) {
    this.plugin = plugin;

    }
     
  7. Online

    timtower Administrator Administrator Moderator

    @PikaThieme Then post the main class.
    And use getters, don't use public modifiers on fields please.
     
  8. Offline

    PikaThieme

    There isn't something special in main. Just above the onEnable is this:
    HashMap<Player,Player> hash = new HashMap<Player,Player>();
     
  9. Online

    timtower Administrator Administrator Moderator

    @PikaThieme Don't use Player objects, they can get invalid, use UUID's.
     
  10. Offline

    PikaThieme

    Could it be that I screwed something up in my eclipse settings?
     
  11. Online

    timtower Administrator Administrator Moderator

  12. Offline

    PikaThieme

    Maybe the server? Or otherwise I have no clue what's wrong...
     
  13. Online

    timtower Administrator Administrator Moderator

    @PikaThieme Post your main class. Post all classes that use that hashmap.
    The code is wrong, not the server.
     
    Caderape2 likes this.
  14. Offline

    Horsey

    Last edited: Jun 25, 2017
Thread Status:
Not open for further replies.

Share This Page