Set player hunger

Discussion in 'Plugin Development' started by AlpacaKyle, Mar 3, 2020.

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

    AlpacaKyle

    Hello! I am wondering if there is a way to set a players hunger. I have looked at other posts on this, but all I have found id p.setFoodLevel(); or event.setFoodLevel(); I understand the setFoodLevel function is to be used but I don't understand the p. or event. while creating this syntax. If I try to use them, it throws an error at me. Can someone please help?!
     
  2. Offline

    yPedx

    @AlpacaKyle
    The "p" used in the example is the player object. You need to get your player from somewhere, and then call setFoodLevel on that player.
     
    Last edited: Mar 3, 2020
  3. Offline

    Tango_

    Show us your code so far so we can guide you
     
  4. Offline

    AlpacaKyle

    package main;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.plugin.java.JavaPlugin;

    public class MainClass extends JavaPlugin {
    @Override
    public void onEnable() {
    }
    @Override
    public void onDisable() {
    }
    @Override
    @EventHandler
    public boolean onCommand(CommandSender sender,
    Command command,
    String label,
    String[] args) {
    if (command.getName().equalsIgnoreCase("mycommand")) {
    sender.sendMessage("You ran /mycommand!");
    return true;
    }
    if (command.getName().equalsIgnoreCase("feed")) {
    p.setFoodLevel(20);
    sender.sendMessage("Your Appetite Has Been Sated.");
    return true;
    }


    return false;
    }

    }

    Thats my code so far.
     
  5. Offline

    Machine Maker

    In the future, please put your code inside CODE tags or upload it to https://pastebin.com.

    1. You don't need an @EventHandler annotation before your onCommand method.

    2. You haven't actually registered your command. You have to have your class implement CommandExecutor and then in the onEnable method, actually register your command. Here is a quick resource to learn about creating commands.
     
  6. Offline

    bowlerguy66

    To get your p variable to work you have to cast the sender variable as a player, like this:
    Code:
    Player p = (Player) sender;
    Keep in mind that you haven't checked if the sender is actually a player, make sure to add a check before you cast to make sure you don't get any problems.
     
  7. Offline

    yPedx

    JavaPlugin already inherits the CommandExecutor implementation, so there's no need to implement it again.
     
  8. Offline

    Machine Maker

    Oh does it really? I haven't written an actual command is so long. I just use ACF for all commands. Its pretty amazing.

    But either way, you do still have to register the command I believe.
     
  9. Offline

    timtower Administrator Administrator Moderator

    Only when not using the main class.
    You do need to still put then in the plugin.yml of course.
     
  10. Offline

    AlpacaKyle

    Thanks! This worked.
     
    bowlerguy66 likes this.
Thread Status:
Not open for further replies.

Share This Page