Solved msg plugin

Discussion in 'Plugin Development' started by ImFr0zen, Jan 7, 2016.

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

    ImFr0zen

    I tried to write an msg plugin, everything works fine if you enter the text, but If you enter only /msg
    It keeps saying "An internal error occured while performing this command".
    Code:
    package de.imfrozen.cmd;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class msg implements CommandExecutor {
        public boolean onCommand(CommandSender sender, Command cmd, String string, String[] args){
            if(sender instanceof Player){
            Player p = (Player) sender;
            @SuppressWarnings("deprecation")
            Player ps = Bukkit.getPlayer(args[0]);
            String message = null;
          
            if (args.length < 2){
                p.sendMessage(ChatColor.RED + "You have to enter '/msg [PLAYER] [MESSAGE]'!");
            }
          
    
            message = args[1];
            for (int ii = 2; ii != args.length; ii++) {
              message = message + " " + args[ii];
            }
          
    
            if(p.isOp()){  
              
                if(ps.isOp()){
                p.sendMessage(ChatColor.DARK_RED + p.getName() + ChatColor.DARK_GREEN + " >> " + ChatColor.DARK_RED + ps.getName() + ChatColor.GRAY + ": " + message);
              
                } else {
                    p.sendMessage(ChatColor.DARK_RED + p.getName() + ChatColor.DARK_GREEN + " >> " + ChatColor.BLUE + ps.getName() + ChatColor.GRAY + ": " + message);
                }
              
            } else {
              
                if(ps.isOp()){
                    p.sendMessage(ChatColor.BLUE + p.getName() + ChatColor.DARK_GREEN + " >> " + ChatColor.DARK_RED + ps.getName() + ChatColor.GRAY + ": " + message);
              
                } else {
                    p.sendMessage(ChatColor.BLUE + p.getName() + ChatColor.DARK_GREEN + " >> " + ChatColor.BLUE + ps.getName() + ChatColor.GRAY + ": " + message);
                }
            }
            }
          
            return false;
        }
    }
    
     
  2. Offline

    mine-care

    you never stop the code from executing at this point... if it has less than two arguments, it will send the message to the player and it will continue the code execution.

    Follow naming conventions

    This is kind of pointless code... Let me represent the algorythm.

    if player is op:
    ---if plater is op send a message
    ---if player is not op send another message
    if player is not op:
    ---if plater is op send a message
    ---if player is not op send another message

    The inner if's are pointless since one of their conditionds will never fire.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 7, 2016
  3. Offline

    Gorbit99

    You don't the domain, imfrozen.de, so you shouldn't use it in your package name, because you can get fined for that, change it to something like me.imfrozen, create a github page, or use your email address
     
  4. Offline

    ImFr0zen

    I won't upload that plugin, it is just for testing

    I tried to get the problem shown above working, but I failed...
    Code:
    package imfrozen.cmd;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class msg implements CommandExecutor {
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String string, String[] args){
            if(sender instanceof Player){
            Player p = (Player) sender;
            Player ps = Bukkit.getPlayer(args[0]);
            String message = null;
           
            message = args[1];
            for (int ii = 2; ii != args.length; ii++) {
              message = message + " " + args[ii];
            }
           
            if (args.length > 1){
               
                if (ps.isOnline()){
                    ps.sendMessage(Bukkit.getScoreboardManager().getMainScoreboard().getPlayerTeam(p).getPrefix() + ChatColor.UNDERLINE +  p.getDisplayName() + ChatColor.DARK_GREEN + " >> " + ChatColor.RESET + Bukkit.getScoreboardManager().getMainScoreboard().getPlayerTeam(ps).getPrefix() + ps.getName() + ChatColor.GRAY + ": " + message);
                    p.sendMessage(Bukkit.getScoreboardManager().getMainScoreboard().getPlayerTeam(p).getPrefix() + p.getDisplayName() + ChatColor.DARK_GREEN + " >> " + ChatColor.RESET + Bukkit.getScoreboardManager().getMainScoreboard().getPlayerTeam(ps).getPrefix() + ChatColor.UNDERLINE + ps.getName() + ChatColor.GRAY + ": " + message);
                } else {
                    p.sendMessage(ChatColor.RED + "This player is not online!");
                }
            } else {
                p.sendMessage(ChatColor.RED + "You have to enter '/msg [PLAYER] [MESSAGE]' !");
            }
            }
            return false;
        }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 8, 2016
  5. Offline

    mine-care

    first of all, this returns a player, not an offline player. If the player is not online, it returns null so your check at line 25, will either be always true or it will throw an exception.
    Keep reading:
    You are not checking if there are arguments in the command.... It will probably throw an ArrayIndexOutOfBoundsException.

    Also you did not fix what i recomended :(
     
  6. Offline

    ImFr0zen

    Ok thanks, now everything works fine ;)
     
  7. Offline

    Gorbit99

    Okay, if you're not going to upload it, it's "fine", but you should most certainly change the package name, so you eont learn bad practices
    Also, mark the thread as solved, if it's solved
     
Thread Status:
Not open for further replies.

Share This Page