Require food while flying

Discussion in 'Plugin Development' started by ChrisStarr32, Aug 7, 2014.

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

    ChrisStarr32

    Wondering how I could remove 1 food from players food bar for every 10 blocks they flown.
     
  2. Offline

    AoH_Ruthless

    ChrisStarr32
    In PlayerMoveEvent. Check if they are flying, then check their location and store it in some sort of Mapping. In the same event, check if their distanceSquared to the stored location is > 100, and remove 1 food from their hunger bar.
     
  3. Offline

    ChrisStarr32

    AoH_Ruthless
    I kind of understand what your saying im just not exactly sure on the squaring part.
    Could you explain more in depth? :)
     
  4. Offline

    AoH_Ruthless

    ChrisStarr32
    Player#getLocation()#distance(Location)

    Let's say your stored location is 'l'.
    In your PlayerMoveEvent, you will want to check if the player is 10 blocks+ away from l. To do this, we would use

    Code:java
    1. if (Player.getLocation().distance(l) >= 10) {
    2. // code
    3. }


    However, .distance() uses square roots which is very intensive (comparatively). Especially in a PlayerMoveEvent which can be called every single tick. So, you will want to use the counterpart .distanceSquared. However, to accomodate you will have to square 10 also (10^2=100).

    New code:
    Code:java
    1. if (Player.getLocation().distanceSquared(l) >= 100) {
    2. // code
    3. }
     
  5. Offline

    Deleted user

    ChrisStarr32
    Code:java
    1. double distanceSquared = location1.distanceSquared(location2);
     
  6. Offline

    ChrisStarr32

    Joiner AoH_Ruthless
    Ahh. Okey. Im starting to see.

    I may sound like a noob but how do I get location 1 and location 2 correctly?
    Ik Location location1 = player.getLocation(); but when?

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

    Deleted user

     
  8. Offline

    AoH_Ruthless

    ChrisStarr32
     
  9. Offline

    ChrisStarr32

    AoH_Ruthless
    Right. I see that. Mapping you mean array right? :confused:
     
  10. Offline

    Deleted user

    No, you can't use an ArrayList, you have to use an HashMap.
     
Thread Status:
Not open for further replies.

Share This Page