/msg plugin

Discussion in 'Plugin Development' started by CheesyFreezy, Feb 26, 2015.

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

    CheesyFreezy

    hi hi developers,

    Thank you for trying to help me out. So, i'm making a private message plugin also with a /r (reply) command. /msg is working just fine but /r isn't working. I have no idea why

    Code:
    Code:
            if(command.equalsIgnoreCase("msg")) {
                if(sender instanceof Player) {
                    Player player = (Player) sender;
                  
                    if(args.length == 0) {
                        player.sendMessage(Plugin.prefix + ChatColor.YELLOW + "/msg [name] [message] " + ChatColor.GRAY + "- Messages a player something");
                    } else if(args.length == 1) {
                        Player target = Bukkit.getServer().getPlayer(args[0]);
                      
                        if(target instanceof Player) {
                            if(target.isValid() && target.isOnline()) {
                                player.sendMessage(Plugin.prefix + ChatColor.YELLOW + "/msg " + target.getName() + " [message] " + ChatColor.GRAY + "- Messages a player something");
                            } else {
                                player.sendMessage(Plugin.prefix + ChatColor.YELLOW + args[0] + ChatColor.GRAY + " doesn't exist or is not online!");
                            }
                        } else {
                            player.sendMessage(Plugin.prefix + ChatColor.YELLOW + args[0] + ChatColor.GRAY + " doesn't exist or is not online!");
                        }
                    } else if(args.length > 1) {
                        String msg = " ";
                      
                        Player target = Bukkit.getServer().getPlayer(args[0]);
                      
                        if(target instanceof Player) {
                            if(target.isValid() && target.isOnline()) {
                                if(player != target) {
                                    reply.put(target.getName(), player.getName());
                                  
                                    for(int i=1;i<args.length;i++) {
                                        msg = msg + args[i] + " ";
                                    }
                                  
                                    player.sendMessage(Plugin.prefix + ChatColor.BLUE + player.getName() + ChatColor.RED + "►" + ChatColor.YELLOW + target.getName() + ChatColor.BLUE + ": " + ChatColor.GRAY + msg);
                                    target.sendMessage(Plugin.prefix + ChatColor.BLUE + player.getName() + ChatColor.RED + "►" + ChatColor.YELLOW + target.getName() + ChatColor.BLUE + ": " + ChatColor.GRAY + msg);
                                  
                                    target.playSound(target.getLocation(), Sound.LEVEL_UP, 10, 1);
                                  
                                    msg = " ";
                                } else {
                                    player.sendMessage(Plugin.prefix + ChatColor.GRAY + "You can't message yourself!");
                                }
                            } else {
                                player.sendMessage(Plugin.prefix + ChatColor.YELLOW + args[0] + ChatColor.GRAY + " doesn't exist or is not online!");
                            }
                        } else {
                            player.sendMessage(Plugin.prefix + ChatColor.YELLOW + args[0] + ChatColor.GRAY + " doesn't exist or is not online!");
                        }
                    }
                } else if(sender instanceof ConsoleCommandSender) {
                    Bukkit.getConsoleSender().sendMessage(Plugin.prefix + ChatColor.RED + "Only players can use this command!");
                }
            }
            if(command.equalsIgnoreCase("r")) {
                if(sender instanceof Player) {
                    Player player = (Player) sender;
                  
                    if(args.length == 0) {
                        player.sendMessage(Plugin.prefix + ChatColor.YELLOW + "/r [message] " + ChatColor.GRAY + "- Reply to the last message");
                    } else if(args.length > 0) {
                        if(reply.containsKey(player.getName())) {
                            Player target = Bukkit.getServer().getPlayer(reply.get(player.getName()));
                          
                            String msg = " ";
                          
                            if(target instanceof Player) {
                                if(target.isValid() && target.isOnline()) {
                                    for(int i=1;i<args.length;i++) {
                                        msg = msg + args[i] + " ";
                                    }
                                  
                                    player.sendMessage(Plugin.prefix + ChatColor.BLUE + player.getName() + ChatColor.RED + "►" + ChatColor.YELLOW + target.getName() + ChatColor.BLUE + ": " + ChatColor.GRAY + msg);
                                    target.sendMessage(Plugin.prefix + ChatColor.BLUE + player.getName() + ChatColor.RED + "►" + ChatColor.YELLOW + target.getName() + ChatColor.BLUE + ": " + ChatColor.GRAY + msg);
                                  
                                    target.playSound(target.getLocation(), Sound.LEVEL_UP, 10, 1);
                                  
                                    msg = " ";
                                  
                                    reply.remove(player.getName());
                                    reply.put(target.getName(), player.getName());
                                } else {
                                    player.sendMessage(Plugin.prefix + ChatColor.YELLOW + args[0] + ChatColor.GRAY + " doesn't exist or is not online!");
                                }
                            } else {
                                player.sendMessage(Plugin.prefix + ChatColor.YELLOW + args[0] + ChatColor.GRAY + " doesn't exist or is not online!");
                            }
                        } else {
                            player.sendMessage(Plugin.prefix + ChatColor.GRAY + "You haven't received any message to reply on it!");
                        }
                    }
                } else {
                  
                }
            }
    The 'bug' is at line 60
     
  2. Offline

    teej107

    reply doesn't contain the player's name
     
  3. Offline

    CheesyFreezy

  4. Offline

    teej107

    Put in debug code
     
  5. Offline

    CheesyFreezy

  6. Offline

    teej107

    @CheesyFreezy Yes, so you
     
  7. Offline

    CheesyFreezy

  8. Offline

    teej107

    @CheesyFreezy Did you
    ?
     
  9. Offline

    CheesyFreezy

  10. Offline

    teej107

    @CheesyFreezy A piece of code (usually prints something to the console) to make sure that the code is working as intended. If it doesn't print at all or print its expected value, you know something is wrong.
     
  11. Offline

    CheesyFreezy

  12. Offline

    teej107

    @CheesyFreezy Google it.
    Printing to the console is something every developer should know.
     
  13. Offline

    BrickBoy55

    System.out.println("<your text here>");
     
  14. Offline

    CheesyFreezy

  15. @CheesyFreezy Keep putting prints such as "Step 1" and "Contains: (HashMap check)" etc
     
  16. Offline

    MajorSkillage

    Why do you need a HashMap to store messages? Can't you send the message directly to a user and use the HashMap for Player Player arguments to find who has messaged who last and if the message changes for that player it will reassign the value.
     
  17. Offline

    CheesyFreezy

    @bwfcwalshy, it stops when the code is at line 60
    @MajorSkillage, because i have to save the sender of the message and the receiever
     
  18. Offline

    Skionz

    Post the results of your debugging.
     
  19. Offline

    tomudding

  20. Offline

    MCMatters

    reply.put(target.getName(), player.getName()); under that reply.put(player.getName(), target.getName());
     
Thread Status:
Not open for further replies.

Share This Page