Repeat a line of code forever?

Discussion in 'Plugin Development' started by Wolfram1, Aug 23, 2013.

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

    Wolfram1

    I have tried making a line of code repeat itself forever, but I failed.
    I would like to somehow repeat this line:
    Code:java
    1. player.setVelocity(player.getVelocity().setY(1));
     
  2. Offline

    Stoux

    Mind explaining a bit more what you want to do?
    If you repeat a line forever your code will die, because it will be stuck on that line.
    Are you trying to do that line for multiple players, for one user every second/tick or something?
     
  3. Offline

    Tirelessly

    Do it on PlayerMoveEvent.
     
  4. Offline

    Wolfram1

    I'm trying to make the player "fly", so repeating the velocity change process would make it easier.

    I'm doing it on PlayerMoveEvent, I just cannot find the way to repeat it :)

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

    Stoux

    In theory, every time the players moves that event gets fired, which should set the velocity to 1 again, causing an infinite loop. But judging by your reply, I guess that doesn't work? Full code of the the event?
     
  6. you can use for (i = 1; i > 0; i++){
    //do your stuff forevaah
    }
    because this ^ is an infinite loop (since i will always be bigger than 0, and if i is bigger than 0, it wil add something to i)
     
  7. Offline

    Tirelessly

    Which will crash the server
     
    werter318 likes this.
  8. :) (but it is infinite :) )
     
  9. Offline

    dark navi


    Why wouldn't you just while(true) then?

    First off, if you are just trying to make someone fly normally, please be aware there is the setFlying member function which toggles vanilla flying.

    If you wanted to set a player's velocity to 1 over and over again, you could put it in the PlayerMoveEvent, or you could schedule a repeated task to do it every x ticks. If you put an infinite loop in your code in the main thread, which you can assume you'll be in, it will lock up the entire server because when it hits your loop, it will never stop. So please don't try to 'forever loop' something.
     
    evilmidget38 likes this.
  10. Offline

    Samthelord1

    Wolfram1 wouldnt the players Y always be 1?
     
  11. Offline

    GaaTavares

    Make a repeating task and set for 1 second?
     
  12. Offline

    evilmidget38

    Samthelord1 It's the velocity, not their Location.
     
    lukegb likes this.
  13. Offline

    Samthelord1

    evilmidget38 ah, I'm learning velocity I don't really understand it, I'm working with it atm in my thread :p
     
  14. Offline

    naorpeled

    boolean forever = true;
    while(forever = true){
    //This will reapet forever :D
    }
     
  15. Wolfram1
    Use a "while loop". This video might help you:

    Basically what you do is this:

    Code:java
    1. while (x < 1000000){ //you can set x to be less than whatever you want
    2. //do stuff here
    3. }


    What naorpeled suggested will work for you.
     
  16. Offline

    TheKomputerKing

    Just do this:

    Code:java
    1. boolean youAreOverweight = true
    2. while (youAreOverweight = true){
    3. loseSomeWeight();
    4. }


    Just replace the weight bits with anything xD.

    (Also loseSomeWeight(); is looped forever)
     
    Wolfram1 likes this.
  17. Offline

    Dippoakabob

    Just a note, a while loop will prevent your plugin from doing anything else. So you may want to go with some of the scheduling options that are provided by Bukkit. Such as the SyncRepeatingTask as follows:
    Code:
    Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin , new Runnable() { //for the "plugin" variable, use "this" if you're running this from the main class
                public void run(){
                    if(player.getGameMode().equals(GameMode.CREATIVE)){ //Check to see if the player is in Creative mode
                        player.setVelocity(new Vector(0, 0.5, 0)); //Set the Velocity
                    }
                }
            }, 0, 2); //The first number is the amount of ticks before the loop is started. And the second number is the number of ticks before it repeats. (there are usually ~20 ticks in a second)
     
    _Dashy likes this.
  18. Offline

    epicfacecreeper

    Everybody stop!
    PluginMan here to help with your bukkit-related questions

    If you are going to suggest using while loop, at least do it correctly and use
    Code:java
    1. while (true) {
    2. doSomething();
    3. }
    !

    BUT DO NOT DO THIS!

    This will lock up the server and crash it, because bukkit calls plugins in sync!!

    Instead, use the BukkitScheduler and make an anonymous Runnable inline!
    This will be called every X ticks, so try not to do complicated logic and/or tons of code!

    PluginMan away!
     
  19. Offline

    Cirno

    Why are a lot of people suggesting a while(true) loop? That's kinda stupid, since it locks the main thread (aka, freeze entire server). If you want to truly do a while(true) loop, try to multithread, due note that you have to make everything thread-safe and multithreading will degrade performance if used incorrectly.

    Other then that, just use Bukkit's scheduler system.
     
  20. Offline

    Wolfram1

    It's almost impossible to use these loops while changing player's velocity, every single one crashes the server :p
     
  21. Offline

    Techy4198

    Wolfram1 yep. that's what everyone has been saying. do NOT use loops. use schedulers.
     
  22. Offline

    Tirelessly

    Techy4198 Or just use PlayerMoveEvent ffs. Setting their velocity will move them, which will set their velocity, which will move them, etc
     
  23. Offline

    Techy4198

    Tirelessly uh nope... player move event is only triggered by teleportation, when the vehicle a player is riding moves them, or the player manually moving. since velocity isn't any of those, it doesn't work. (this information may be untrue as it is entirely based off my personal experience, I tried it and it does not work)
     
  24. Offline

    Stoux

    So many people trying to break servers here :eek:.
     
  25. Offline

    Tirelessly

    PlayerMoveEvent is fired when a player moves.
     
  26. For God's sake people this really isn't that hard...
    Wolfram1 This should work.....
    Code:java
    1. Integer id = pluginExample.getServer().getScheduler().scheduleSyncRepeatingTask(pluginExample, new Runnable() {
    2.  
    3. @Override
    4. public void run() {
    5. for(Player player: flyingPlayerList)//uses list of players that you want to have fly
    6. player.setVelocity(player.getVelocity().setY(1));
    7. }
    8. }, 10L, 10L);//how often it repeats
     
  27. Offline

    yttriuszzerbus

    What if i overflows: either an overflow error and a crash, or it resets to 0 or a large negative number if it's a signed int. Whichever way that goes, things will be nasty.
     
  28. Offline

    Techy4198

    the moment you start ANY loop in the main thread, the server becomes useless anyway, so please just use BorrisTheTerrible's code!
     
  29. Offline

    zeddio

    GaaTavares Dippoakabob BorisTheTerrible
    In your examples I saw that it is either 1 or 10 tick delay, but what happends if you set the delay value to zero?
    Will that task run like a normal while(true) loop without delay or will it be a failue with the Scheduler so it never will run it at all? :oops:
     
  30. Offline

    Dippoakabob

    1 tick is around 1 twentieth of a second. the repeating loop is basically as close to a while look as you're going to find. if there was no delay, it would break the game. Stick with the repeating task at a 1-20 tick delay because that's still less than a second.
     
    _Dashy likes this.
Thread Status:
Not open for further replies.

Share This Page