Detect if player is completely on ground?

Discussion in 'Spigot Plugin Development' started by Heretere, Jun 24, 2016.

Thread Status:
Not open for further replies.
  1. Hey, I created this method
    Code (Text):
        public static boolean isPlayerCompletelyOnGround(HACPlayer player) {
            double check = (player.getPlayer().getLocation().getY() - player.getPlayer().getLocation().getBlockY());
            return check % BlockUtil.getRelativeBlockHeight(LocationUtil.getActualGroundLocation(player.getGroundLocation()).getBlock().getType()) == 0 || check <= 0.002;
        }

    it checks to see if the player is completely on the ground.
    Code (Text):
        public static Double getRelativeBlockHeight(Material material) {
            switch (material) {
            case ACACIA_FENCE:
            case ACACIA_FENCE_GATE:
            case BIRCH_FENCE:
            case BIRCH_FENCE_GATE:
            case DARK_OAK_FENCE:
            case DARK_OAK_FENCE_GATE:
            case FENCE:
            case FENCE_GATE:
            case IRON_FENCE:
            case JUNGLE_FENCE:
            case JUNGLE_FENCE_GATE:
            case NETHER_FENCE:
            case SPRUCE_FENCE:
            case SPRUCE_FENCE_GATE:
            case COBBLE_WALL:
                return 0.5;
            case GRASS_PATH:
            case SOIL:
            case CACTUS:
                return 0.9375;
            case SOUL_SAND:
            case CHEST:
            case ENDER_CHEST:
            case TRAPPED_CHEST:
                return 0.875;
            case CHORUS_PLANT:
                return 0.8125;
            case ENCHANTMENT_TABLE:
                return 0.75;
            case BED_BLOCK:
                return 0.5625;
            case SKULL:
                return 0.25;
            case WATER_LILY:
                return 0.09375;
            default:
                return 0.0625;
            }
        }

    I just wanted to know if there was a better way I could accomplish this. Does anyone know a better way to do this, or is this the best way?
     
  2. Oh sht, so it is. That must be new. Or not and I just haven't noticed.
     
  3. Use Entity.isOnGround instead of Player.isOnGround maybe. The Entity.isOnGround() is not deprecated.
     
    • Optimistic Optimistic x 1
  4. it will still run player.isOnGround() even if you cast it as an entity...
    believe me, if spigot had a method for it I would use it. What I am trying to ask is if anyone knows of a more efficient method than the one above.
     
  5. Please, don't post if you have no clue.

    OP, it really isn't that complicated of a check, I'm sure your method is fine. You could also dig into the source code and find how out isOnGround is calculated.
     
  6. Lol, I tried xD ( Was bumping thread )
     
    • Funny Funny x 1
  7. See if their Y velocity is 0?
     
  8. That makes no sense at all.
     
  9. This is getting really off-topic, THE METHOD ABOVE WORKS! What I am trying to figure out is if that is as efficient as I can make it.
     
  10. Would Player.getVelocity().getY() not work?
     
  11. If Player.getVelocity().getY() works, then I feel like it would be more efficient.
     
  12. what
     
    • Agree Agree x 2
  13. No, I'm not aware of any such method. And no one has yet suggested a more efficient method.
    I'm not sure how to explain this, but no, it is very wrong. X/Y/Z values work in an entirely different way than you think.
     
  14. Never heard of OOP, have you?

    Players flying. Haven't tested, though.
     
    • Agree Agree x 1
  15. OOP? XD
     
  16. I think I understand what Hunky is trying to say here. If your velocity in the Y direction is 0, then you must be on the ground(or gravity and a velocity have exactly equaled out, making this method having a slim chance of being unreliable). This method would obviously work only while not flying.
     
    • Like Like x 1
Thread Status:
Not open for further replies.