Looping Timers, Freezing and No Move.

Discussion in 'Plugin Development' started by qlimax5000, Dec 11, 2013.

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

    qlimax5000

    Hello.

    I need help with these 3 problems.

    I want a for loop with a delay like:

    > Loop 10 times
    > Each loop wait 2 seconds
    > Perform code
    > Return to looping

    Also.

    When a player moves, if he's in a list return him to the original block

    And last.

    A timer that waits and only runs when the player hasn't moved in 2 seconds (Pitch and Yaw can be changed)

    Thanks
    - Ben
     
  2. Offline

    L33m4n123

    And at what part are you stuck? Did you tried anything yourself yet?
     
  3. Offline

    qlimax5000

    For the looping timer i've tried:
    Code:java
    1. for (int i = 0; i < 20; i++) {
    2. getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    3. public void run() {
    4. Bukkit.broadcastMessage("This line is here to resemble code");
    5. }
    6. }, 40);
    7. }


    For freezing i've tried:

    Code:java
    1. @EventHandler
    2. public void onPlayerMove(PlayerMoveEvent event) {
    3. if (frozen.contains(event.getPlayer().getName())) {
    4. event.setCancelled(true);
    5. }
    6. }


    For the no move i have a pretty good idea on how to do this but i don't know how to check if they only move their feet
     
  4. Offline

    L33m4n123


    The best way to achieve this would be probably listening for the PlayerMoveEvent and check if their X-Value or Z-Value changes and only then cancel it.

    So. Since you got some code that looks ok by having a fast look. Where are you stuck?
     
  5. Offline

    qlimax5000

    L33m4n123
    When doing the the loop the for loop is java, the delay is bukkit, meaning that the for loop runs 20 times in 0.01 seconds and launching each delay with 0.002 seconds or so instead of launching the delay waiting till the delay happends then restarting the loop.
     
  6. Offline

    L33m4n123


    Oh.

    You probably would want to use a RepeatingTask and within the task itself a counter. If it has been running for x times then cancel it within the Task
     
  7. Offline

    qlimax5000

  8. Offline

    Developing

    qlimax5000

    Note: 20L / 20 Ticks = 1 Second.

    1st Part

    Define a counter.
    Code:java
    1. public int counter = 1;

    Now add this.
    Code:java
    1. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    2.  
    3. @Override
    4. public void run() {
    5. if(!(counter > 10)){
    6. //perform code
    7. counter++;
    8. }
    9.  
    10. }
    11.  
    12. }, 40L, 0);


    2nd Part

    Define an arraylist
    Code:java
    1. List<String> frozen = new ArrayList<String>();

    Now add this.
    Code:java
    1. @EventHandler
    2. public void onPlayerMove(PlayerMoveEvent e){
    3. if(frozen.contains(e.getPlayer().getName()){
    4. e.setCancelled(true);
    5. }
    6.  
    7. }


    3rd Part

    Define an arraylist
    Code:java
    1. List<String> moved = new ArrayList<String>();

    If they are not in the moved list when they move , add them to the list. Then , use a scheduler to remove them after 2 seconds. If they are in the moved list , cancel the move event.
    Code:java
    1. @EventHandler
    2. public void onPlayerMove(PlayerMoveEvent e){
    3. if(!moved.contains(e.getPlayer().getName()){
    4. moved.add(e.getPlayer().getName());
    5. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    6. @Override
    7. public void run(){
    8. moved.remove(e.getPlayer().getName());
    9. }
    10. }, 40L);
    11. }else{
    12. e.setCancelled(true);
    13. }
    14.  
    15. }
     
  9. Offline

    L33m4n123

    I'd like to say rather not to do it this way since this will keep running the Task just with no code

    Code:java
    1. int taskID; //so we cancel it from inside
    2. taskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    3. @Override
    4. public void run() {
    5. if(!(counter > 10)){
    6. //perform code
    7. counter++;
    8. } else {
    9. Bukkit.cancelTask(taskID); // will cancel the task once the counter hits 10
    10. }
    11. }
    12. }, 40L, 0);
     
Thread Status:
Not open for further replies.

Share This Page