Solved BukkitRunnable not working

Discussion in 'Spigot Plugin Development' started by Waldxn, May 19, 2017.

  1. Hey so I'm trying to get a message sent to a player when their hunger is at a certain level. Nothing seems to be working though. Here is my code

    Hunger.java:
    Code (Text):
    public class Hunger extends BukkitRunnable {

        private Main plugin;

        public Hunger(Main plugin) {
            this.plugin = plugin;
        }

        int hunger;

        public void run() {
            for (Player p : Bukkit.getOnlinePlayers()) {
                hunger = p.getFoodLevel();

                if (hunger == 20) {
                    p.sendMessage("You are full!");
                } else if (hunger <= 15 && !(hunger <= 10)) {
                    p.sendMessage("You need a snack!");
                } else if (hunger <= 10 && !(hunger <= 5)) {
                    p.sendMessage("You are getting hungry!");
                } else if (hunger <= 5 && !(hunger <= 0)) {
                    p.sendMessage("You are getting VERY hungry");
                } else {
                    p.sendMessage("You are starving!");
                }
            }
        }
    }
    Main.java:
    Code (Text):
    public class Main extends JavaPlugin {

        @Override
        public void onEnable() {
            Logger logger = getLogger();
            PluginDescriptionFile pdf = getDescription();
            logger.info("[" + pdf.getName() + "]" + " has been enabled!");
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Hunger(this), 20);
        }

        @Override
        public void onDisable() {
            Logger logger = getLogger();
            PluginDescriptionFile pdf = getDescription();
            logger.info("[" + pdf.getName() + "]" + " has been disabled!");
        }
    }
     
    Why do you think it isn't working?
     
  2. Why not just listen to the FoodLevelChangeEvent?
    Also, you're just scheduling the task once. It would need to be a repeating task on a set interval.
     
    • Informative Informative x 1
  3. Must've missed that event in the Javadocs! Thank you! Will test.

    EDIT: This worked. Thanks!
     
    #3 Waldxn, May 19, 2017
    Last edited: May 19, 2017