Checking if a player enters and area without creating lag?

Discussion in 'Plugin Development' started by WesJD, Dec 1, 2014.

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

    WesJD

    Ok, so, I'm fully aware on how to check if a player is an an area. My only problem is that how would I go about checking it without making lag(PlayerMoveEvent or a Runnable)?
     
  2. Offline

    Avygeil

    WesJD
    Checking it accurately in PlayerMoveEvent will create lag. There are some ways to reduce it though.. For example, in your event, you can check if the player's position actually changed block, with something like :
    Code:java
    1. if (event.getFrom().getBlockX() != event.getTo().getBlockX()) // same for Z, Y
    2. {
    3. // block changed, check if enters area
    4. }

    This would effectively reduce the checks by a good amount.

    If you don't need such accuracy, you could always use System.currentTimeMillis() to make cooldowns for checks.
     
  3. Offline

    PreFiXAUT

    Hmmm...well the PlayerMoveEvent or a Runnable are the once good Options tho :/

    I wouldn't suggest the PlayerMoveEvent, because the problem on this is that it would trigger on every PlayerMove on the Server, which would RIP the server when 30+ Players are moving. So I guess you should take a look in Runnables/BukkitTasks then. With a really Optimized Check it should work out. For example:
    Code:java
    1. if (condition) {
    2. if (condition2) {
    3. if (condition3) {
    4. // etc -> Takes really really long to parse, because you're using allot of nested if's
    5. }
    6. }
    7. }
    8. // "Optimized"
    9. if (!condition) return;
    10. if (!condition2) return;
    11. if (!condition3) return;
    12. // Stop the event/handling instantly instead of having tons of nested if's and stuff like that.
    13.  

    I think a 5-10 Tick Task would be enoght for this. This works even better if you only have certain Players which have to be checked because it causes less lag again. When you're doing it this way and doing some Optimazation on ifs and other stuff you should be fine :)
     
  4. Offline

    Avygeil

    PreFiXAUT Not saying you shouldn't do this (I hate nested if's), but this is more of a coding style than an optimization, and even if nested if's were slower for the JVM, the compiler takes care of such optimizations. Sure, nesting is bad style and hardly maintainable, but it's micro/premature optimization which is evil. You should look for code bottlenecks instead. I hope his server is fast enough to run 3 boolean conditions, even on a 100+ server... What you should reduce is the amount of checks ; simple example : I have 500+ regions on this world, do I have to loop through all regions for all players to check if they are in any ? Maybe checking if the player is not in a certain bigger area would be true more often, thus reducing the amount of checks. That's where the conditions order is important, always play with the least/most likely ones as your first condition. :)

    But indeed, using runnables if you don't need accuracy is a good improvement that reduces checks. Targetting certain players is another one. :)
     
    Rocoty likes this.
  5. Offline

    xize

    you could just use PlayerMoveEvent, even though ive readed alot of times that the event could make lag but that isn't really true, its all about how you implement it.

    if you would instance everytime a new instance of something at every move included the yaw movements then the implementation is not good or safe, however if you add enough checks like if the player has moved atleast 2 blocks while ignoring the yaw and pitch, then you are kinda safer (I discourage using the new keyword in any PlayerMoveEvents though just in case you made per accidental a spammy event).

    however what I would use is per chunk basis since e.getFrom().distanceSquared(e.getTo()) can't return more than 1 or 2 blocks unless they use some speed hack or are flying I geuss that still could be a bit heavy depending for what you are using it for.

    for me this psuodo works really good:
    Code:
    if(!e.getFrom().getChunk().equals(e.getTo().getChunk())) {
    //do something
    }
    
    of course you may want to check on the chunk coords and compair if they match or not, but I use this to see if a player is in a safe worldguard region or in the wilderness and almost no lag for me :)
     
  6. Offline

    Funergy

Thread Status:
Not open for further replies.

Share This Page