Solved Execute something every x ticks

Discussion in 'Spigot Plugin Development' started by keahie, Jun 3, 2017.

Thread Status:
Not open for further replies.
  1. Hi, I'm currently working on a project, where I have to modify the regeneration time of the food level from the players. When the player walks normal, he/she should regenerate every 15 ticks a half food level, but only then. When he/she is sprinting, sneaking or standing still, the player should get another amount of food. My problem is that I didn't get it what I have to do. I made the whole thing for sprinting and it works (only the method):
    Code (Text):

    //sprintingPlayers = ArrayList<Player>
    @EventHandler
        public void onMove(PlayerMoveEvent e)
        {
            Player p = e.getPlayer();

            if (p.isSprinting())
            {
                if (!sprintingPlayers.contains(p))
                {
                    sprintingPlayers.add(p);
                    sprint = Bukkit.getScheduler().runTaskTimer(instance, new Runnable()
                    {
                       
                        @Override
                        public void run()
                        {
                            p.sendMessage(String.valueOf(p.getFoodLevel()));
                            p.setFoodLevel(p.getFoodLevel() - 1);
                            if (!p.isSprinting())
                                Bukkit.getScheduler().cancelTask(sprint.getTaskId());
                               
                        }
                    }, 0, 20L);
                }
            }
            else
            {
                if (sprintingPlayers.contains(p))
                    sprintingPlayers.remove(p);
            }
        }
    Please help me :(
     
  2. Code (Text):
    new Runnable()
                    {
                     
                        @Override
                        public void run()
                        {
                            p.sendMessage(String.valueOf(p.getFoodLevel()));
                            p.setFoodLevel(p.getFoodLevel() - 1);
                            if (!p.isSprinting())
                                Bukkit.getScheduler().cancelTask(sprint.getTaskId());
                             
                        }
                    }.runTaskTimer(plugin, 0, 20);
     
  3. I think you misunderstood me. This method for sprinting works. I have to write a method which gives every player every 15 ticks one food level but only if the player walks and don't sprint or sneak.
     
  4. then run it on playermoveevent not as a runnable. and create a timer that edits a global boolean every 15 ticks
     
  5. Ok I found a solution. I will close this thread
    Sloution:
    Code (Text):
    @EventHandler
        public void onMove(PlayerMoveEvent e)
        {
            Player p = e.getPlayer();
            if (!players.contains(p) && !p.isSprinting() && !p.isSneaking())
            {
                players.add(p);
               
                task = new BukkitRunnable()
                {
                   
                    @Override
                    public void run()
                    {
                        if (p.getFoodLevel() != 20)
                            p.setFoodLevel(p.getFoodLevel() + 1);
                        players.remove(p);
                        Bukkit.getScheduler().cancelTask(task.getTaskId());
                    }
                }.runTaskLater(instance, 20L);
            }
        }
     
Thread Status:
Not open for further replies.