Java void a waiting function.

Discussion in 'Plugin Development' started by SunShe, Mar 20, 2011.

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

    SunShe

    Is that frozing the craftbukkit or other plugins the time waiting with:? Because i see a pointer loop.
    Code:
    public static void waiting (int n){
    
            long t0, t1;
    
            t0 =  System.currentTimeMillis();
    
            do{
                t1 = System.currentTimeMillis();
            }
            while ((t1 - t0) < (n * 1000));
        }
     
  2. Offline

    Mixcoatl

    This is twice bad. Firstly, it blocks the calling thread. Secondly, this is called a "busy wait" which not only blocks the calling thread but also consumes as much CPU time as possible, sometimes starving other processes.

    Blocking a thread is not necessarily "wrong" as long as the thread you're blocking does not expect to return. Plug-in threads in Bukkit are generally expected to return, so blocking operations should be performed on another thread if you need them.
     
  3. Offline

    SunShe

    Yea ok, it what i had thinked. thanks.

    i gonna make a movefram, it will be better and keep free the pointer.

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

    Mixcoatl

    If you would like to block a calling thread for a configurable length of time you could use code such as this:
    Code:
    public void sleepUninterruptibly(float seconds) {
        final long waitUntil =
            System.currentTimeMillis() + (long) (seconds * 1000.0);
    
        while (true) {
            final long current = System.currentTimeMillis();
            if (current >= waitUntil)
                break;
            try {
                Thread.sleep(waitUntil - current);
            } catch (InterruptedException ex) {
                // Ignore interruptions.
            }
        }
    }
     
  5. Offline

    SunShe

    nice, thanks but i have did another way. without need a waiting void. But your exemple surely will help other developers. I have using my personal fram system using directly events. it's less dirty and more lightweight. when i say dirty, it's not about your code, it's the way to put a void in another void.

    And they have another problem with a waiting void like this, it's when the void need to be pass for 2 or 10 players in same time, it will can't support it.
    _
     
  6. Offline

    Mixcoatl

    I'm not certain what you mean by "putting a void in another void." Void is just the return type -- it means the method doesn't return any value to the caller.
    Either way, there are at least a dozen ways of doing this using Object.wait, Thread.sleep, or one or more of the classes from the java.util.concurrent package. You could even obtain similar functionality using the Bukkit scheduler or java.util.Timer classes.
    Glad you found a solution that works for you.
     
  7. Offline

    SunShe

    lol yea but now i have another problem. i can't pass my events. Craftbukkit are too long.

    [ATTENTION] SunShe moved wrongly!
    [INFO] Got position 113.5, 66.0, -121.5
    [INFO] Expected 113.5, 66.0, -90.69999998807907

    But i'm not in a wall or in a non-0 block. :mad:

    and same i have make a sub test to be sure that a free location ( blockid 0 ). but same it wont. it annoys me. That irritate me because it teleport me back to the begin for nothing.

    ahah it's like boyz, im not patient with it [​IMG]

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 12, 2016
  8. Offline

    Mixcoatl

    I'm not certain what the "Moved wrongly" error indicates. i would have to look it up :7
     
  9. Offline

    Waterwolf

    Are you trying to use event.setTo() in PLAYER_MOVED event. Or trying to teleport player inside it? Afaik changing "to" location gives that.

    Okay I managed to find a way past the Unexpected move error thing.
    I simply made a new Runnable class
    Code:
        public class teler implements Runnable {
    
            private Location teleto = null;
            private Player player = null;
            
            public teler(Player p, Location loc) {
                teleto = loc;
                player = p;
            }
            
            @Override
            public void run() {
                if (player == null || teleto == null)
                    return;
                player.teleportTo(teleto);
            }
            
        }
    
    And on PLAYER_MOVE event schedule it to next tick
    Code:
    getServer().getScheduler().scheduleSyncDelayedTask(this, new teler(player, location), 1);
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 12, 2016
  10. Offline

    SunShe

    are you know another methode, just for prevent that but without delay?

    im blocked. impossible to bypass it, i have make so many test and so many print log or (player.sendmessage) for see what's m ake problem, but all test are OK, it's impossible but the server TP me back with wrongly. amazing. why the creator are so stupid to make this function. he cant just show a simple console warning? blah. I want show to him how i gonna make a move wrongly if that continue. :mad: 2 days on this stupidity, impossible to fix it. But seriously, i think the problem come from "falling" not taked in event consideration of bukkit.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 12, 2016
  11. Offline

    Waterwolf

    The delay in code snippet I posted is 1 tick which equals 500 ms (i think) which equals almost zero. I guess you can put 0 instead of 1 in scheduler sync call too to make it execute even more instantly but doesn't matter for me.
     
  12. Offline

    SunShe

    that i have directly tested but still too long, but with your class, i have fixed 1 of problems, but for the other one impossible. it's impossible to do that instantly? because "0" are not instantly.
     
  13. Offline

    Waterwolf

    Setting delay to 0 is closest to instant you can come up with. Afaik there's no bypass to "player x moved wrongly" when player location is set within PLAYER_MOVE without hacking. I have been wrong before too though.
     
  14. Offline

    SunShe

    but if called outside and not by PLAYER_MOVE event? that can work?
     
  15. Offline

    Waterwolf

    You can schedule it from anywhere.
     
  16. Offline

    SpaceManiac

    When you event.setTo(location), just also do a player.teleportTo(location) - and if you're still having problems, try an event.setFrom(location) as well.
     
Thread Status:
Not open for further replies.

Share This Page