HashMap Error

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

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

    RRedocc

    I am fairly new to Plugin Development and am trying to learn about HashMaps. I am extremely confused as to why this does not work, and would appreciate any help. What it is supposed to do is reply to a message, finding whom to reply to in the hashmap.

    I am aware that this is messy code, but I am just trying to learn the ropes of Bukkit.

    Thanks for any help given!

    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{
    
    HashMap<Player, Player> replyList = new HashMap<Player, Player>();
    public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String args[]){
         
         
    
            if(cmd.getName().equalsIgnoreCase("m")){
                if(args.length < 2){
                    sender.sendMessage(ChatColor.RED + "Invalid Arguments!");
                    return true;
                }
                    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((Player) sender,target);
                        replyList.put(target,(Player) sender);
                        return true;
                    } 
            }
         
         
         
         
            if(cmd.getName().equalsIgnoreCase("msg")){
                if(args.length < 2){
                    sender.sendMessage(ChatColor.RED + "Invalid Arguments!");
                    return true;
                }
                    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((Player) sender,target);
                        replyList.put(target,(Player) sender);
    
                        return true;
                    } 
            }
         
         
         
         
         
         
            if(cmd.getName().equalsIgnoreCase("r")){
                Player senderHasReply = replyList.get(sender);
                if(senderHasReply != null) {
                    sender.sendMessage("Got here!");
                    Player targetName = replyList.get(sender);
                    Player target = Bukkit.getPlayer(targetName.toString());
                    if(target != null){
                        if(args.length > 0){
                         String message = StringUtils.join(args, ' ', 1, args.length);
                         target.sendMessage(ChatColor.GRAY + sender.getName().toString() + " --> §oyou§r:  " + ChatColor.RED + message);
                         sender.sendMessage(ChatColor.GRAY + "[§oyou§r --> " + target.getName().toString() + ":  " + ChatColor.RED + message);
                      
                        Player senderName = (Player) sender;
                        Player targetName1 = (Player) target;
                      
                         replyList.put(senderName, targetName1);
                         replyList.put(targetName1, senderName);
                        }
                    }
                }
            }
     
     
         
            return true;
        }
    
    
    }
    
    
     
    Last edited: Jul 12, 2016
  2. @RRedocc First, make the map private.
    Second, please use else ifs
    Third, use the ChatColors for future availability.
    Fourth, why are you putting them in twice? You can check for key and value.
    Fifth, check before casting! Do not blindly cast
     
  3. Offline

    RRedocc


    Can you tell me where I would use elseifs? And how would I make it so I don't have to cast but I can send it from console - you can't send messages to CommandSender, only Players. And what do you mean I can check for a key and a value? Do you mean I have to check the HashMap for a Key with the players name, and if one isn't found, check for a value with the players name? Thanks.
     
  4. @RRedocc
    Code:
    if (this.doesntHappen()) {
        // random code
    } else if (that.doesHappen()) {
        // more code
    } else {
        // other code
    }
    
    Also, CommandSender does have a sendMessage(String) method. In a map, two values are stored. The key and the value. And you don't need to check both the key and values, you could just use contains(Object) to see if it contains the player. If it does, you could use the get method to get the person they want to reply to.
     
    mine-care likes this.
  5. Offline

    mine-care

    Pretty sure you can
     
Thread Status:
Not open for further replies.

Share This Page