1.8.8 Help me, please!

Discussion in 'Spigot Plugin Development' started by Raqooon, Jan 28, 2020.

  1. My plugin doesnt work :(
    Its a simple HealCommand Plugin but I always have an error, when I try to start up my server.
    I hope you can help me.

    -HealCommand.java-

    package de.raqooon.test.commands;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class HealCommand implements CommandExecutor{

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if(sender instanceof Player) {
    Player player = (Player) sender;
    player.setHealth(20);
    player.setFoodLevel(20);
    player.sendMessage("§aDu wurdest geheilt!");
    } else
    sender.sendMessage("Du bist kein Spieler!");
    return false;
    }


    -Main.java-
    package de.raqooon.test.main;

    import org.bukkit.plugin.java.JavaPlugin;

    import de.raqooon.test.commands.HealCommand;

    public class Main extends JavaPlugin {

    public void onEnable() {
    getCommand("heal").setExecutor(new HealCommand());


    }
    }



    -plugin.yml-
    name: Test
    version: 1.0
    main: de.raqooon.test.main.Main

    commands:
    heal:
    description: Dieses Kommando heilt dich!
     
  2. 1) Please use a code block when posting code, that is nearly impossible to read
    2) Your command is return false regardless if it should or not:
    Code (Java):
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if(sender instanceof Player) {
            Player player = (Player) sender;
            player.setHealth(20);
            player.setFoodLevel(20);
            player.sendMessage("§aDu wurdest geheilt!");
        } else
            sender.sendMessage("Du bist kein Spieler!");
        return false; //returns false regardless of result
    }
    We're not mind readers... what's the error?
     
    • Agree Agree x 1
  3. This should work fine.

    Code (Java):
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if(sender instanceof Player) {
            Player player = (Player) sender;
            player.setHealth(player.getHealthScale());
            player.setFoodLevel(20);
            player.sendMessage("§aDu wurdest geheilt!");
        } else
            sender.sendMessage("Du bist kein Spieler!");
        return false;
    }
     
  4. Strahan

    Benefactor

    Personally, I'd do:
    Code (Text):
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage("Du bist kein Spieler!");
            return true;
        }

        Player player = (Player) sender;
        player.setHealth(player.getHealthScale());
        player.setFoodLevel(20);
        player.sendMessage(ChatColor.GREEN + "Du wurdest geheilt!");
        return true;
    }
    Embedding the color character is not good practices and I prefer to always return true and handle sending the command help myself and do negative checking.
     
    • Winner Winner x 1

  5. Wow that looks pretty clean. Wd