"An internal error occurred" Error When Using Command On Self

Discussion in 'Plugin Development' started by CrazyJakeLane, Sep 4, 2015.

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

    CrazyJakeLane

    I am having an issue with a simple Heal and Feed command. The Medic command works exactly as intended, however, my Feed command won't work as intended. The usage is /Medic or /Medic <Target>. I allow both possibilities. The ladder works, however, the self /Medic command does not.

    To better understand, here is my entire source code as well as my plugin.yml.

    Code:
    package me.lumpy.medic;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Medic extends JavaPlugin{
    
        public void onEnable(){
            getLogger().info("Medic enabled.");
       
        }
        public void onDisable(){
            getLogger().info("Medic disabled.");
       
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
       
            if (cmd.getName().equalsIgnoreCase("medic")) {
                Player player = (Player) sender;
             
                 if (args.length == 0) {
                         player.setHealth(20);
                         player.sendMessage(ChatColor.GREEN + "You have been healed!");
                         return true;
                 }
           
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if (target == null){
                    player.sendMessage(ChatColor.RED + "Could not find player!");
                    return true;
                }
                target.setHealth(20);
                target.sendMessage(ChatColor.GOLD + "Someone has healed you!");
                player.sendMessage(ChatColor.GOLD + "You have healed " + ChatColor.GREEN + target + ChatColor.GOLD + "!");
                return true;
            }
         
            if (cmd.getName().equalsIgnoreCase("feed")) {
                Player player = (Player) sender;
                 if (args.length == 0) {
                         player.setFoodLevel(20);
                         player.sendMessage(ChatColor.GREEN + "You have been fed!");
                         return true;
                 }
                 Player target = Bukkit.getServer().getPlayer(args[0]);
                 if (target == null) {
                         player.sendMessage(ChatColor.RED + "Could not find player!");
                         return true;
                 }
                 target.setFoodLevel(20);
                 target.sendMessage(ChatColor.GREEN + "You have been fed!");
                 player.sendMessage(ChatColor.GREEN + target.getName() + " has been fed!");
         }
       
       
            return true;
        }
    
    }
    
    Plugin.yml:

    name: Medic
    main: me.lumpy.medic.Medic
    version: 1.0
    commands:
    medic:
    description: This will heal the target to full health.
    usage: /medic
    feed:
    description: Will feed the target, restoring their food level to max.
    usage: /feed

    Please note that /feed <target> works properly. When using the command, I get the following error in game, "An internal error occurred while attempting to perform this command". Within the console I get 37 lines of errors, and well as a ". . . 15 more". I feel that the first line may be of interest as it states, "ERROR: null". I am new to coding, so I apologize if this is a simple error. All help is appreciated.

    Please note that I have also attempted the following:
    Code:
    if (args.length == 0){
    //Rest of code
    }
    if (args.length == 1){
    //Rest of code
    }
    
     
    Last edited: Sep 4, 2015
  2. Offline

    lubbs31

    Please format with:
    Code:
    [code] blah blah [/ code]
     
  3. Offline

    teej107

  4. Offline

    boomboompower

    Try
    Code:
    boolean playerFound = false;
    Player p= (Player) sender;
    
    if (playerFound == false) {
           p.sendMessage(ChatColor.RED + args[0] + " was not found!");
     }
     
    Last edited: Sep 4, 2015
  5. Offline

    caderape

    @CrazyJakeLane
    Can you show the stack trace ?
    I don't see any error in your code, except the fact you will get an error if you execute your commands from the console.
     
  6. Offline

    RoboticPlayer

    Make sure you check before casting.
     
  7. Offline

    boomboompower

    I made a healing plugin like this, I gave a sample of one of the bits ;p
     
  8. Offline

    RoboticPlayer

    @boomboompower you should still make sure that it is a player, and not assuming.
    @CrazyJakeLane You should probably deny the console to use this command (at least if args.length == 0), because the console doesn't have health.
     
  9. Offline

    boomboompower

    How to do healing:
    Code:
    for (Player playerToHeal : Bukkit.getServer().getOnlinePlayers()) {
                        if(playerToHeal.getName().equalsIgnoreCase(args[0])) {
                           //What you want to do when it finds the player
                           //
                           
                           // Messages to you & the player you are healing
                            playerToHeal.sendMessage(ChatColor.GREEN + "You have been healed by " + player.getName() + "!");
                            player.sendMessage(ChatColor.GREEN + playerToHeal.getName() + " was healed successfully");
                            playerFound = true;
                            break; // Stops the code from doing this to every online player
                        }
    Better @henderry2019 ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  10. Offline

    RoboticPlayer

    @boomboompower I don't doubt that you check before casting, but I want to make sure the OP isn't just copy-and-pasting your code and expecting it to work. And also, I don't really know how to do for statements :p, I haven't worked with them yet.
     
  11. Offline

    boomboompower

    I did test it, I could give him the whole code, but I want him to figure it out for himself. Yes I did copy & paste it from my custom plugin, but I do use that plugin on my private server.
     
  12. Offline

    RoboticPlayer

  13. Offline

    boomboompower

    aah. Good thing I put comments in it then xD
     
Thread Status:
Not open for further replies.

Share This Page