Low Gravity

Discussion in 'Plugin Development' started by SE_Original, Aug 19, 2014.

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

    SE_Original

    Hello,
    I'm pretty new with bukkit and tried to make a low gravity plugin Here's the code:
    Code:java
    1. public void Gravity(final double x, final double y, final double z, final double x1, final double y1, final double z1) {
    2. this.reloadConfig();
    3. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    4. public void run() {
    5. for(Player gravSend : Bukkit.getServer().getOnlinePlayers())
    6. {
    7. if(!gravSend.isFlying()) {
    8. if(gravity) {
    9. if(!gravSend.isOnGround()) {
    10. if(isInRect(gravSend, new Location(gravSend.getWorld(),x,y,z), new Location(gravSend.getWorld(),x1,y1,z1))) {
    11. double currentVecX = gravSend.getVelocity().getX();
    12. double currentVecZ = gravSend.getVelocity().getZ();
    13. double currentVecY = gravSend.getVelocity().getY();
    14. gravSend.setVelocity(new Vector(currentVecX * 1.1 ,currentVecY + 0.04,currentVecZ * 1.1));
    15.  
    16.  
    17.  
    18. }
    19. }
    20. } else {
    21. return;
    22. }
    23. }
    24. }
    25. }
    26. }, 0L, 1L);
    27. }


    The code is pretty simple. It just checks for a player in the region and applies a velocity. It works perfectly on my localhost test-server, but it's really laggy on a real servers! I don't know if there's a way to get rid of the jiggle but if there is please let me know!
     
  2. Offline

    The Fancy Whale

    New to bukkit API and first thing you try is velocities? That's a hard task. Also, it is laggy because you are running a repeating task every tick.
     
    yewtree8 likes this.
  3. Offline

    Wingzzz

    Eh, new to Bukkit- he never said to math ;)

    SE_Original
    Like The Fancy Whale said, you're running a task every tick (there's 20 ticks in a second) that can do some pretty hefty calculations. You're checking for each player on the server (the more players the longer it takes) to see if they're within a rectangle (larger the rectangle the longer it takes), you're making multiple Location objects each tick (granted they're temporary), and then you're calculating velocities to be applied to the player.

    You may want to increase the time between intervals. Also, question: you only call Gravity(double, double, double, double, double, double) once right? If not, you may have multiple tasks running this which would most definitely be a huge performance hit.
     
  4. Offline

    SE_Original

    Thanks for the help! I tried removing the isInRect part and replaced it with isInWorld, again works perfectly on localhost! And I think you understood it wrong, by jiggle and lag I meant the player movement! I didn't test it on the server yet because the one I test it on is not mine. And yeah i only call gravity once!
     
  5. Offline

    Wingzzz

    If it's not a performance hit with lag, and the player just looks "laggy", then it's due to the velocity calculations changing so quickly. Also, I'd recommend renaming that method to be using lowerCamelCase as per the Oracle's Java coding conventions (not an up to date document by the way, but this particular section has not changed as far as I know).
     
  6. Offline

    ChipDev

    New to 'Bukkit' That doesn't mean new to Java. But yes... It is hard anyways ^^Also, Apply velocity (Upwards +) on Jump event.
     
  7. Offline

    _Filip

    ChipDev There is no jump event.....
     
  8. Offline

    ChipDev

    Oh, Stupid me.....
    [​IMG]
    on move event, check if y is the same o.o
     
  9. Offline

    AoH_Ruthless

    ChipDev
    Not quite, you would need to check if the to y is higher than the from y. This is more specific because just checking if y isn't same means a falling player could be considered jumping.
     
  10. Offline

    ChipDev

    Yes.. Thats what I meant.
    Your'e J.R Tolkien
    I'm Hemingway.
    (Saying I give less detail.)
    But also.. How is that bad if the player is falling?
    He wants low gravity .-.
     
  11. Offline

    AoH_Ruthless

    ChipDev
    Perhaps, but you are contradicting yourself...

    While these two statements are not mutually exclusive, they are still not concordant phrases. Your first suggestion was to try and detect a player jumping (raise in y), and saying that there is nothing wrong with fall detection (lowering in y) makes no sense. I was pointing out that your statements don't exactly agree with each other.
     
  12. Offline

    ChipDev

    AoH_Ruthless Oh well. Another fraise by AoH that makes me look stupid.. Lol! So yes,
    Apply upwards on jump, Apply slowness on fall.
    But also...:
    Like I said, I don't like to give so much detail, If you add + on jump, Then well... its obvious to - on fall.
     
  13. Offline

    ZachBora

    reminds me of that plugin SethBling made
    Edit: this
     
Thread Status:
Not open for further replies.

Share This Page