Solved Respawn event not setting foodlevel

Discussion in 'Spigot Plugin Development' started by NielsAdventure, Jan 23, 2020.

  1. Hi, i currently have a weird issue with my RespawnEvent and my FoodLevelChangeEvent. When i respawn it does not set my foodlevel to 18 also if my foodlevel changes and it is more then 18 it doesnt set it to 18.

    Code (Java):
        @EventHandler
        public void onFoodLevelChange(FoodLevelChangeEvent event) {
            Entity entity = event.getEntity();
                Player player = (Player) entity;
                int foodLevel = player.getFoodLevel();
                if (foodLevel > 18) {
                    player.setFoodLevel(18);
                }
        }
    and my respawn event

    Code (Java):
        @EventHandler
        public void onRespawn(PlayerRespawnEvent event) {
            Player player = event.getPlayer();
            player.getInventory().clear();
            player.setHealth(20);
            player.setFoodLevel(18);
        }
     
  2. Are you registering your listener(s) in your main class?
     
  3. Try doing some debugging, print to the console or send a message to player when the events are supposed to fire. If you get a message then we’ll move on the solving it, if not then the events aren’t firing for some reason
     
  4. I made this code at now, and it's work for me :)

    You need to register "foodChange" method in your main class, like
    Code (Java):

    PluginManager pm = Bukkit.getPluginManager();
        @Override
        public void onEnable() {
             for(Player player :Bukkit.getOnlinePlayers()) {
                PlayerFoodUpEvent.foodChange(player);
            }
           pm.registerEvents(new PlayerFoodUpEvent(), this);
        }
     


    Code (Java):
    package com.fighter.Listeners;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.FoodLevelChangeEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.scheduler.BukkitScheduler;
    import org.bukkit.scheduler.BukkitTask;

    import com.fighter.bukkit.main;

    public class PlayerFoodUpEvent implements Listener {

        private static BukkitTask task;
        private static BukkitScheduler scheduler = Bukkit.getScheduler();
        private static main plugin = main.getPlugin(main.class);

        public static void foodChange(Player player) {

            task = scheduler.runTaskTimer(plugin, new Runnable() {

                @Override
                public void run() {
                    if (player.getFoodLevel() > 18) {
                        player.setFoodLevel(18);
                    }
                }

            }, 0, 10);

        }

        @EventHandler
        public void onFoodChange(FoodLevelChangeEvent event) {
            Entity entity = event.getEntity();
            Player player = (Player) entity;
            if (entity.getType() == EntityType.PLAYER) {
                if (player.getFoodLevel() > 18) {
                    foodChange(player);
                }
            }

        }

        @EventHandler
        public void onJoin(PlayerJoinEvent event) {
            if (task == null) {

            } else {
                foodChange(event.getPlayer());
            }
        }

        @EventHandler
        public void onRespawn(PlayerRespawnEvent event) {

            Player player = event.getPlayer();

            player.getInventory().clear();
            player.setHealth(20);
            if (player.getFoodLevel() >= 18) {
                foodChange(player);
            }
        }
    }
     
     
    • Like Like x 1
  5. I Tried debugging and the console messages say it gets through the if statement but it doesn't set it so ill try the code from fighter and then ill check
     
  6. FrostedSnowman

    Resource Staff

    before you try his code, just wait 1 tick before setting the health or hunger during the respawn event
     
  7. Well actually his code worked and i am gonna stick with that :)
     
  8. FrostedSnowman

    Resource Staff

    Well there's static abuse and some other redundancies occurring. I'd look over that if you plan to use it.
     
  9. Might indeed plan on recoding since it isn't made in proper java