Running Cooldown Help

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

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

    maxben34

    Drkmaster83
    I implemented that into my code like 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.  
    17. //this part is supposed to see if a player is running and lower their saturation every second
    18. @EventHandler
    19. public void onPlayerMove(final PlayerMoveEvent e){
    20. Bukkit.getScheduler().runTaskTimer(this, new Runnable(){
    21. @Override
    22. public void run(){
    23. int i = 0;
    24. Player player = (Player) e.getPlayer();
    25. if(i >= 3){
    26. player.setFoodLevel(player.getFoodLevel() + 1);
    27. }
    28. if(player.isSprinting()){
    29. i = 0;
    30. player.setFoodLevel(player.getFoodLevel() - 1);
    31. }
    32. else if(!(player.isSprinting())){
    33. i++;
    34. }
    35. }
    36. }, 0L, 20L);
    37. }
    38. }
    39.  

    When I go into survival now, it sets my hunger automatically at 0 and never regens

    PogoStick29
    No. But it only takes hunger away until there is 16 hungerlevel then it stops taking it and doesn't refill the hunger either. Hopefully that is kinda helpful

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 4, 2018
  2. Offline

    Drkmaster83

    Because every time you run it (every second), it sets i to 0 and 0 is not greater than or equal to 3, so it exits out. There's a reason I put the "i" outside the timer. You need to have a number for each player specifically, so you might have to do some HashMap work.

    You're scheduling a running task every time a player moves. Within seconds of moving, you may have maybe 20 timers running at once. Not good. Run that from start up (onEnable()), and do some HashMap work.
     
  3. Offline

    maxben34

    Drkmaster83
    That's true. I'm wondering if there's another way of going about this, I'm trying not to use hash maps, as I have like 20 already within the plugin and it's getting confusing.

    PogoStick29
    Okay :)

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

    maxben34

    Once again true. I think this is similar to making an xp timer... I might b able to use a for loop with Bukkit.getonlineplayers or something like that and set a variable that works with it. Then BukkitRunnable for scheduling?
     
  5. Offline

    Wingzzz

    Code:java
    1. public class GameplayListener {
    2. private final Plugin plugin;
    3.  
    4. public GameplayListener(Plugin plugin) {
    5. this.plugin = plugin;
    6. Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new BukkitRunnable() {
    7. @Override
    8. public void run() {
    9. for(Player player : Bukkit.getOnlinePlayers()) {
    10. if(!player.isSprinting()) {
    11. if(player.getFoodLevel() != 20) {
    12. player.setFoodLevel(player.getFoodLevel() + 1);
    13. }
    14. }
    15. }
    16. }
    17. }, 0L, 20L);
    18. }
    19.  
    20. @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
    21. public void onPlayerMovement(PlayerMoveEvent event) {
    22. final Player player = (Player) event.getPlayer();
    23. if(player.isSprinting()) {
    24. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new BukkitRunnable() {
    25. @Override
    26. public void run() {
    27. if(player.getFoodLevel() != 0) {
    28. player.setFoodLevel(player.getFoodLevel() - 1);
    29. }
    30. }
    31. }, 20L);
    32. }
    33. }
    34.  
    35. }


    This should create a basic run cooldown. Although I would of course recommend changing it to suit your needs more so. This should help provide a working example, albeit this is very different from other ways of doing it. Since this is movement based it's simple to just check if someone is sprinting; if you were to instead do skill cooldowns or command cooldowns you could do something involving collection(s). If you would like an example of that please let me know.

    Also, if you have any questions at all about the code above please don't hesitate to ask/question. This was written quickly, as well as not tested. I can comment and explain all reasoning if needed.

    Make sure to tag me if you would like me to see, comment, or reply to a post.

    Resources (Bukkit Related)
    Scheduler Programming
    Event API Reference
    Bukkit JavaDocs/Doxygen
     
  6. Offline

    maxben34

    Wingzzz
    I have some code here that seems to be a good option on how to do this

    Code:java
    1. @EventHandler
    2. public void onPlayerMove(final PlayerMoveEvent e){
    3. new BukkitRunnable()
    4. {
    5. int timer = 360;
    6. public void run()
    7. {
    8. if((e.getPlayer().isSprinting())){
    9. for (Player p : Bukkit.getOnlinePlayers()){
    10. p.setFoodLevel(p.getFoodLevel() - 1); // increase all levels by 1
    11. }
    12. timer--;
    13. }
    14.  
    15. if (timer >= 0 && !e.getPlayer().isSprinting()){
    16. for(Player p : Bukkit.getOnlinePlayers()){
    17. p.setFoodLevel(p.getFoodLevel() + 1);
    18.  
    19. }
    20. timer --;
    21. }
    22. }
    23. public void cancel()
    24. {
    25. }
    26.  
    27. // timer has reached 0, do stuff here
    28.  
    29.  
    30. }.runTaskTimer(this, 0L, 20L);
    31. }
    32. }
    33.  

    Right now though, nothing happens when a player is sprinting.

    I changed my code drastically now. It isn't completely working and it does some weird things.

    1. Only works for one player since it sets the other players hunger at 0
    2. hunger levels come in instantly... not one at a time after a 5 second delay.

    Code:java
    1. package me.maxben34.runner;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Sound;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerMoveEvent;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13. import org.bukkit.scheduler.BukkitRunnable;
    14.  
    15. public class runner extends JavaPlugin implements Listener{
    16. //jackson runner array... if player is in runner array then...
    17. List <Player> running = new ArrayList<Player>();
    18. List <Player> notrunning = new ArrayList<Player>();
    19. public void onEnable(){
    20. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    21. }
    22.  
    23. //this part is supposed to see if a player is running and lower their saturation every second
    24. @EventHandler
    25. public void onPlayerMove(final PlayerMoveEvent e){
    26. new BukkitRunnable(){
    27. int timer = 20;
    28. public void run() {
    29. if(e.getPlayer().isSprinting()){
    30. running.add(e.getPlayer());
    31. notrunning.remove(e.getPlayer());
    32. for (Player p : running){
    33. p.setFoodLevel(p.getFoodLevel() - 1); // increase all levels by 1
    34.  
    35. }
    36. timer--;
    37. if (timer == 0){
    38.  
    39. }
    40. cancel();
    41. }
    42. }
    43. // timer has reached 0, do stuff here
    44.  
    45.  
    46. }.runTaskTimer(this, 0L, 100L);
    47.  
    48. new BukkitRunnable(){
    49. int timer1 = 20;
    50. public void run(){
    51. if(!(e.getPlayer().isSprinting())){
    52. notrunning.add(e.getPlayer());
    53. running.remove(e.getPlayer());
    54. for(Player p : Bukkit.getOnlinePlayers()){
    55. p.setFoodLevel(p.getFoodLevel() + 1);
    56.  
    57. }
    58. timer1--;
    59. if (timer1 == 0){
    60. }
    61. cancel();
    62. }
    63. }
    64.  
    65. }.runTaskTimer(this, 0L, 100L);
    66. }
    67. }
    68.  


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

    jackson30007

    line 38 has nothing inside the {}. That's about all I can do to help :p
     
  8. Offline

    maxben34

    Fixed that but the problem still occurs (I knew that wouldn't fix it :p)
     
Thread Status:
Not open for further replies.

Share This Page