Solved Getting Values from HashMaps - Returning Error

Discussion in 'Plugin Development' started by RRedocc, Jul 12, 2016.

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

    RRedocc

    EDIT: I solved my problem by making the HashMap static & adding the NPE catcher. Thanks!






    Hello! I am making a /message and /reply plugin, and for the /reply, I am using HashMaps to store who to reply too. It keeps clearing my HashMap!

    I am new to Bukkit Development and Hashmaps, cut me some slack. Thanks!

    Code: ( My class )

    Code:
    package me.rredocc.minestar.customessentials;
    
    import java.util.HashMap;
    
    import org.apache.commons.lang.StringUtils;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class Messaging implements CommandExecutor{
        private HashMap<String, String> replyList = new HashMap<String, String>();
    public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String args[]){
      
    
    
    
            if(cmd.getName().equalsIgnoreCase("m")){
                if(sender instanceof Player){
                Player p = (Player) sender;
      
                if(args.length < 2){
                    sender.sendMessage(ChatColor.RED + "Invalid Arguments!");
                    return true;
                }
                else{
                   Player target = Bukkit.getPlayer(args[0]);
                   String message = StringUtils.join(args, ' ', 1, args.length);
                   if (target != null) {
                       target.sendMessage(ChatColor.GRAY + sender.getName().toString() + " --> §oyou§r:  " + ChatColor.RED + message);
                       sender.sendMessage(ChatColor.GRAY + "[§oyou§r --> " + target.getName().toString() + ":  " + ChatColor.RED + message);   
                          replyList.put(p.getName().toString(),target.getName().toString());
                          p.sendMessage(replyList.values().toString());
                          p.sendMessage(replyList.keySet().toString());
                          return true;
                   }
    
                      }
                }      
            }
    
            if(cmd.getName().equalsIgnoreCase("r")){
                if(!(replyList.isEmpty())){
                if(sender instanceof Player){
                Player p = (Player) sender;
                String targeti = replyList.get(p.getName().toString());
                Player target = Bukkit.getPlayer(targeti)  ;
                    if(target != null){
                        String message = StringUtils.join(args);
                       target.sendMessage(ChatColor.GRAY + p.getName().toString() + " --> §oyou§r:  " + ChatColor.RED + message);
                       sender.sendMessage(ChatColor.GRAY + "[§oyou§r --> " + target.getName().toString() + ":  " + ChatColor.RED + message);
                            }
                    else{
                        p.sendMessage(ChatColor.RED + "You have no-one to reply toooooooo!");
                    }
                }
                }
                else{
                    sender.sendMessage(ChatColor.RED + "You have no-one to reply too!");
                }
            }
            if(cmd.getName().equalsIgnoreCase("t")){
                sender.sendMessage(replyList.values().toString());
                sender.sendMessage(replyList.keySet().toString());
    
            }
    
          
    
    
            return true;
    
        }
    }
    
    

    EDIT:

    I know the problem, but I don't know how to fix it - Look at this screenie - the hashmap isn't storing - /t shows the HashMap, I typed the commands in chat - http://prntscr.com/bs1en2 . I used separate files, do you know how to fix this? I tested it out using one file, and it worked! So, it's using separate (class) files which is causing this! Thanks so much!
     
    Last edited: Jul 12, 2016
  2. Offline

    MCMastery

    I haven't looked at it much, but you shouldn't store the player names. Store their UUIDs instead
     
  3. Offline

    blok601

    Whats the error? We can't do much without the error
     
  4. Offline

    mine-care

    In long term storage not in short term storage. In this instance there will not be any major loss if the player changes their name before the player msg's again.
     
    MCMastery likes this.
  5. Offline

    RRedocc

    Hi! Thanks for the answer! I know the problem, but I don't know how to fix it - Look at this screenie - the hashmap isn't storing - /t shows the HashMap, I typed the commands in chat - http://prntscr.com/bs1en2 . I used seperate files, do you know how to fix this?

     
  6. Offline

    mine-care

    Yes static automatically makes it a class variable. The way bukkit loads plugins makes static fields remain in RAM after reloads so they just take space without being usable.
    So make sure that you assign the map to null onDisable.
     
Thread Status:
Not open for further replies.

Share This Page