"An internal error occurred while attempting this command"

Discussion in 'Plugin Development' started by NoLife, Feb 25, 2015.

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

    NoLife

    Hi again ;)

    I have make private message plugin(i'm new to bukkit api), but then i use the command it say "An internal error occured while attempting this command". I don't know what i having do wrong

    plugin.yml:
    Code:
    name: WaterEssentials
    main: io.github.Waterman2707.WE.Start
    version: 1.0
    
    commands:
      pm:
        description: Send private message.
        usage: /<command> [player] [message]
        permission: we.msg
        permission-message: You don't have permission for this command
    Start/main:
    Code:
    package io.github.Waterman2707.WE;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Start extends JavaPlugin {
    
        @Override
        public void onEnable() {
            this.getCommand("pm").setExecutor(new WECommandExecutor(this));
        }
    
        @Override
        public void onDisable() {
        
        }
    
    
    }
    
    CommandExecutor:
    Code:
    package io.github.Waterman2707.WE;
    
    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 WECommandExecutor implements CommandExecutor{
        private final Start plugin;
    
        public WECommandExecutor(Start plugin) {
            this.plugin = plugin;
        }
    
    public boolean onCommand(CommandSender sender, Command cmd, String label,String[] args) {
            if (cmd.getName().equalsIgnoreCase("pm")) {
                if (!(sender instanceof Player)) {
                    return false;
                }
                if (sender.hasPermission("WE.msg")) {
                
                Player p = (Player) sender;
                String pn = p.getName();
    
                if(Bukkit.getServer().getPlayer(args[0]) == null) {
                    sender.sendMessage(ChatColor.DARK_RED + Bukkit.getServer().getPlayer(args[0]).getName() + "is not online");
                }
            
                Player t = Bukkit.getServer().getPlayer(args[0]);
                String tn = t.getName();
            
                String message = "";
            
                for (int i = 1; i <= args.length; i++) {
                    message += " " + args[i];
                }
            
                t.sendMessage(ChatColor.DARK_GREEN + "[" + pn + "]--->" + ChatColor.DARK_PURPLE + "[me]" + ChatColor.WHITE + message);
                p.sendMessage(ChatColor.DARK_GREEN + "[Me]--->" + ChatColor.DARK_PURPLE + "[" + tn + "]" + ChatColor.WHITE + message);
                }
    }
            return true;
    }
    }
    
    And what is the typical reason the error come? because i have seen it earlier then i was setting servers up.

    //NoLife
     
  2. Offline

    mine-care

    A stacktrace would help a ton, but ok.
    Hmm:
    You are trying to get args[0] without knowing if there are command args. (Line27)
    Same line, you send the error message to the sender and you are not stoping code execution there, code continues on.
     
  3. Offline

    NoLife

    @mine-care
    Thanks for the help, but now come the error then i try to use the command with an "args".

    // NoLife

    and what is a stacktrace?

    my change is this
    Code:
                if (args.length == 0) {
                    return false;
                }
                if (Bukkit.getServer().getPlayer(args[0]) == null) {
                    sender.sendMessage(ChatColor.DARK_RED + Bukkit.getServer().getPlayer(args[0]).getName() + "is not online");
                    return true;
                }
     
  4. Offline

    TGRHavoc

  5. Offline

    NoLife

    @TGRHavoc
    Thank you. I have always know to that feature, but not know the name of it, and how i could read it :S
     
    Last edited: Feb 25, 2015
Thread Status:
Not open for further replies.

Share This Page