Loop [Need Help]

Discussion in 'Plugin Development' started by Josh014, Nov 26, 2013.

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

    Josh014

    Hey,
    So I have a code to loop something but it gives or broadcast it 6 times in a row. How can I make it that it only goes once?
    Code:java
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent event){
    3. plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    4. public void run() {
    5. for (Player player : Bukkit.getServer().getOnlinePlayers()) {
    6.  
    7. int ChargeFood = player.getFoodLevel() + 1;
    8.  
    9. if(player.getFoodLevel() != 20)
    10. player.setFoodLevel(ChargeFood);
    11. }
    12. }
    13. }, 0L, (2 * 20));
    14. }
     
  2. Offline

    GusGold

    Josh014
    You are never cancelling the repeating task, also, are you meaning to feed everyone on the whole server each time someone joins?
    Heal everyone on the server (open)
    Code:text
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent event){
    3. plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    4. public void run() {
    5. int fullFeed = 0;
    6. for (Player player : Bukkit.getServer().getOnlinePlayers()) {
    7. if(player.getFoodLevel() == 20){
    8. fullFeed++;
    9. } else {
    10. player.setFoodLevel(player.getFoodLevel() + 1);
    11. }
    12. }
    13. if (fullFeed >= Bukkit.getServer().getOnlinePlayers().size()){
    14. this.cancel();
    15. }
    16. }
    17. }, 0L, (2 * 20));
    18. }
    Heal only the player that joined (open)
    Code:text
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent event){
    3. final player = e.getPlayer()
    4. plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    5. public void run() {
    6. if (player.getFoodLevel > 20){
    7. player.setFoodLevel(player.getFoodLevel() + 1);
    8. } else {
    9. this.cancel();
    10. }
    11. }
    12. }, 0L, (2 * 20));
    13.  
    14. }
    Haven't checked it but it should work :)
     
  3. Offline

    Josh014

    GusGold
    I have that now but I need to make a Method for the this.cancel(); thing.

    My code:
    Code:java
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent event){
    3. final Player player = event.getPlayer();
    4.  
    5. final int FoodCharge = player.getFoodLevel() + 1;
    6.  
    7. plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    8. public void run() {
    9. if (player.getFoodLevel() > 20){
    10. player.setFoodLevel(FoodCharge);
    11. } else {
    12. this.cancel();
    13. }
    14. }
    15. }, 0L, (2 * 20));
    16.  
    17. }
     
  4. Offline

    GusGold

    Josh014
    Sorry, I forgot something, set it out like so:
    Code:java
    1. int taskId;
    2. @EventHandler
    3. public void onJoin(PlayerJoinEvent event){
    4. final Player player = event.getPlayer();
    5.  
    6. final int FoodCharge = player.getFoodLevel() + 1;
    7.  
    8. taskId = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    9. public void run() {
    10. if (player.getFoodLevel() > 20){
    11. player.setFoodLevel(FoodCharge);
    12. } else {
    13. plugin.getServer().getScheduler().cancelTask(taskId);
    14. }
    15. }
    16. }, 0L, (2 * 20));
    17. }
     
  5. Offline

    Josh014

    GusGold
    Alright, it kinda works but it still spams it 6 times at once instead of giving me half hungerbar every 2 seconds -.-. And every time if I get the half of my food it gives me full food level.
     
  6. Offline

    Josh014

  7. Offline

    Josh014

    Bump ^^
     
  8. Offline

    GaaTavares

    Use just 1 scheduler, whenever the onEnable() method is fired, start a scheduler. You don't really need to start a new scheduler whenever the onJoin is fired..
     
  9. Offline

    Josh014

    GaaTavares I want to have each player his own scheduler, because if you use in onEnable() then it will continious loop and it will give quite some lagg.
     
  10. Offline

    Josh014

  11. Offline

    Garris0n

    What exactly are you trying to do? Give every player health every two seconds constantly?
     
  12. Offline

    Josh014

    Garris0n, Not health but Hunger and only when their hunger is lower then the full hungerbar (20).
     
  13. Offline

    Garris0n

    Just keep one repeating task going, it's not going to cause any noticeable lag. Check every player's hunger, and, if it's low, heal it up a bit.
     
  14. Offline

    Sagacious_Zed Bukkit Docs

Thread Status:
Not open for further replies.

Share This Page