Running Cooldown Help

Discussion in 'Plugin Development' started by maxben34, Oct 26, 2013.

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

    maxben34

    I am trying to make a plugin that detects when a player is running and lowers their saturation level depending on how long they have been running. If a player hasn't been running for 5 seconds, it will fill their stamina back up to full.

    This is my code:

    Code:java
    1. package me.maxben34.runner;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.player.PlayerMoveEvent;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class runner extends JavaPlugin implements Listener{
    10. //jackson runner array... if player is in runner array then...
    11.  
    12. public void onEnable(){
    13. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    14. }
    15. //this part is supposed to see if a player is running and lower their saturation every second
    16. public void onPlayerRun(final PlayerMoveEvent e){
    17. if(e.getPlayer().getGameMode().equals(0)){
    18. if(e.getPlayer().isSprinting() && e.getPlayer().getSaturation() == 20){
    19. e.getPlayer().setSaturation(18);
    20. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    21. public void run(){
    22. if(e.getPlayer().isSprinting() && e.getPlayer().getSaturation() == 18){
    23. e.getPlayer().setSaturation(16);
    24. }
    25. }
    26. }, 20);
    27. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    28. public void run(){
    29. if(e.getPlayer().isSprinting() && e.getPlayer().getSaturation() == 16){
    30. e.getPlayer().setSaturation(14);
    31. }
    32. }
    33. }, 20);
    34. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    35. public void run(){
    36. if(e.getPlayer().isSprinting() && e.getPlayer().getSaturation() == 14){
    37. e.getPlayer().setSaturation(12);
    38. }
    39. }
    40. }, 20);
    41.  
    42. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    43. public void run(){
    44. if(e.getPlayer().isSprinting() && e.getPlayer().getSaturation() == 12){
    45. e.getPlayer().setSaturation(10);
    46. }
    47. }
    48. }, 20);
    49. }
    50. }
    51. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    52. public void run(){
    53. if(e.getPlayer().isSprinting() && e.getPlayer().getSaturation() == 10){
    54. e.getPlayer().setSaturation(8);
    55. }
    56. }
    57. }, 20);
    58.  
    59. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    60. public void run(){
    61. if(e.getPlayer().isSprinting() && e.getPlayer().getSaturation() == 8){
    62. e.getPlayer().setSaturation(6);
    63. }
    64. }
    65. }, 20);
    66.  
    67. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    68. public void run(){
    69. if(e.getPlayer().isSprinting() && e.getPlayer().getSaturation() == 6){
    70. e.getPlayer().setSaturation(4);
    71. }
    72. }
    73. }, 20);
    74. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    75. public void run(){
    76. if(e.getPlayer().isSprinting() && e.getPlayer().getSaturation() == 4){
    77. e.getPlayer().setSaturation(2);
    78. }
    79. }
    80. }, 20);
    81.  
    82. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    83. public void run(){
    84. if(!(e.getPlayer().isSprinting()) && e.getPlayer().getSaturation() != 20){
    85. e.getPlayer().setSaturation(20);}}}
    86. , 5*20); // after 5 seconds of not sprinting the player gets their saturation back to 20
    87. }
    88. }
    89.  
    90.  


    Nothing happens when I run the code, and i'm not getting any stack-traces. Hopefully somebody can help me with why this isn't properly working.
     
  2. Offline

    Trevor1134

    maxben34 Real quickly I see, you need @EventHandler above public void onPlayerRun
     
  3. Offline

    maxben34

    lul I is noob.

    Trevor1134
    I added that but it's still not working :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  4. Offline

    Trevor1134

    maxben34 Just brainstorming here: Try a AsyncRepeatingTask? p.setHealth(p.getHealth() - 2); Instead of a ton of delayed?
     
  5. Offline

    maxben34

    Trevor1134
    So this?
    Code:java
    1. package me.maxben34.runner;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.player.PlayerMoveEvent;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class runner extends JavaPlugin implements Listener{
    11. //jackson runner array... if player is in runner array then...
    12.  
    13. public void onEnable(){
    14. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    15. }
    16. //this part is supposed to see if a player is running and lower their saturation every second
    17. @SuppressWarnings("deprecation")
    18. @EventHandler
    19. public void onPlayerRun(final PlayerMoveEvent e){
    20. if(e.getPlayer().getGameMode().equals(0)){
    21. if(e.getPlayer().isSprinting() && e.getPlayer().getSaturation() >= 20){
    22. e.getPlayer().setSaturation(18);
    23. Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){
    24. public void run(){
    25. if(e.getPlayer().isSprinting() && e.getPlayer().getSaturation() == 18){
    26. e.getPlayer().setSaturation(e.getPlayer().getSaturation() -2);
    27. }
    28. }
    29. }, 20, 20*8);
    30.  
    31.  
    32. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    33. public void run(){
    34. if(!(e.getPlayer().isSprinting()) && e.getPlayer().getSaturation() != 20){
    35. e.getPlayer().setSaturation(20);}}}
    36. , 5*20); // after 5 seconds of not sprinting the player gets their saturation back to 20
    37. }
    38. }
    39. }
    40. }
    41.  


    IT wasn't working. Maybe I did it wrong?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  6. Offline

    unforgiven5232

    maxben34
    Theres already a plugin like this. If anything you can take the source code from that. Anyways this is extremely easy to do, might i suggest learning Java first? Then move onto more advanced APIs such as Bukkit.
     
  7. Offline

    maxben34

    I do know Java. Let's try to be friendly and helpful instead of accusing me that I don't know what i'm doing.
     
    jimuskin and MineDoubleSpace like this.
  8. Offline

    unforgiven5232

    maxben34
    If you DO know what you're doing then you wouldn't be here.
     
  9. Offline

    xTrollxDudex

    I usually know what I'm doing and I post threads all the time...
     
    PogoStick29 likes this.
  10. Offline

    xigsag

    What maxben34 is saying is that, he knows Java, but is not familiar with the Bukkit API.

    What unforgiven5232 is saying, is that maxben34 doesn't know Java at all, and should probably learn that first before making plugins with the Bukkit API.

    There must be some sort of misunderstanding here.
    Why don't we get back to the problem at hand, or is it solved?

    I feel like this forum is getting more and more hostile nowadays.

    minor edit: sorry, mis-tahged trolldude.
     
    Drkmaster83 and jimuskin like this.
  11. Offline

    maxben34

    xigsag
    The problem isn't solved at the moment. Can we work on a solution please?
     
  12. Offline

    xigsag

    maxben34 Well, in fact this thread might be abit of help to me too :p . I tried doing this once, and I couldn't. Heh.

    More like, the need for me to figure it out was no more, and I simply gave up to do other things. Since this thread exists, I might as well look at people's suggestions, than figure it out myself.

    Lazy me, tehee! :p
     
  13. Offline

    maxben34

    Lol. I'm going to tag xTrollxDudex then... maybe he knows how to help us both xigsag
     
  14. Offline

    unforgiven5232

    maxben34
    Or you can do as i suggested before. I've actually have the code, if you are willing to wait for me to get home then i will grab it and give it to you.
     
  15. Offline

    PogoStick29

  16. Offline

    unforgiven5232

    xigsag I'm usually not hostile, but to this one, I am. But doesn't mean i'm not willing to help people who ask.
     
  17. Offline

    maxben34

    I am aware but this isn't the kinda cooldown that I want to use. I used all your tutorials to learn bukkit. By cooldown I meant that the hungerbars go down to 1 hunger depending on how much you run and then they come back to 20 after a few seconds without running.
     
  18. Offline

    xTrollxDudex

    maxben34
    Get game mode returns GameMode not int
     
  19. Offline

    maxben34

    xTrollxDudex
    Oh, I thought that gamemode was an int like 0 or 1 which is extremely idiotic and stupid of me. Whoops. Thanks ...
    It's working partly now. I just need to figure out a way to get the loop to continue on through until the hunger is at 2
    Code:java
    1. package me.maxben34.runner;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.player.PlayerMoveEvent;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class runner extends JavaPlugin implements Listener{
    11. //jackson runner array... if player is in runner array then...
    12.  
    13. public void onEnable(){
    14. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    15. }
    16. //this part is supposed to see if a player is running and lower their saturation every second
    17. @SuppressWarnings("deprecation")
    18. @EventHandler
    19. public void onPlayerRun(final PlayerMoveEvent e){
    20. if(e.getPlayer().isSprinting() && e.getPlayer().getFoodLevel() >= 20){
    21. e.getPlayer().setFoodLevel(18);
    22. Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){
    23. public void run(){
    24. if(e.getPlayer().isSprinting() && e.getPlayer().getFoodLevel() == 18){
    25. e.getPlayer().setFoodLevel(e.getPlayer().getFoodLevel() -2);
    26. }
    27. }
    28. }, 20, 20*8);
    29.  
    30.  
    31. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    32. public void run(){
    33. if(!(e.getPlayer().isSprinting()) && e.getPlayer().getFoodLevel() != 20){
    34. e.getPlayer().setFoodLevel(20);}}}
    35. , 5*20); // after 5 seconds of not sprinting the player gets their saturation back to 20
    36. }
    37. }
    38. }
    39.  
     
  20. Offline

    PogoStick29


    Code:java
    1. if(e.getPlayer().isSprinting() && e.getPlayer().getFoodLevel() == 18){
    2. e.getPlayer().setFoodLevel(e.getPlayer().getFoodLevel() -2);
    3. }


    The if statement will only be true if the player is sprinting and their food level is exactly 18. I think you mean <= 18?
     
  21. Offline

    maxben34

    That is what I mean. I guess once again I was being stupid haha.
     
  22. Offline

    PogoStick29

    Did that solve the problem?
     
  23. Offline

    maxben34

    It didn't solve the problem actually.
     
  24. Offline

    PogoStick29

    Any stack traces?
     
  25. Offline

    PogoStick29

    Look at your logic. Make sure your if statements do exactly what you want them to do.
     
  26. Offline

    maxben34

    PogoStick29
    Well that's the thing. My code is posted above. That's what I have... the thing is that i'm using a scheduler and I don't know anything past your scheduling tutorial and a bit more than that.
     
  27. Offline

    PogoStick29

    You're going to want to do some debugging. Figure out which of your methods isn't working, then put tons of System.out.println()s in there to make sure that if statements are working when they should and values are being set appropriately.
     
  28. Offline

    maxben34

    PogoStick29
    I think my methods are fine I think that it's just my scheduler. Can you look at my code and tell me if my scheduler logic makes sense?
     
  29. Offline

    PogoStick29

    I'm really tired, but if you go through and debug, it should help.
     
  30. Offline

    Drkmaster83

    Just an idea here, may or may not be right, didn't read the whole thread.
    Code:
    int i = 0;
    Bukkit.getScheduler().runTaskTimer(this, new Runnable()
    {
      @Override
      public void run()
      {
        if(i >= 3)
        {
          player.setFoodLevel(player.getFoodLevel() + 1);
        }
        if(player.isSprinting())
        {
          i = 0;
          player.setFoodLevel(player.getFoodLevel() - 1);
        }
        else if(!(player.isSprinting()))
        {
          i++;
        }
      }
    }, 0L, 20L);
    
    I believe you could create a separate instance of a class for each player online if you create a separate class, however that's much more complicated than doing HashMap work.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 4, 2018
Thread Status:
Not open for further replies.

Share This Page