1.14.4 How to get check the distance between a player and a block?

Discussion in 'Spigot Plugin Development' started by ComfortableAndy, Apr 1, 2020.

  1. Code (Text):
    player.getEyeLocation().distance(block.getLocation())
    or
    Code (Text):
    player.getLocation().distance(block.getLocation())
    It won't work cause it need double.
    help plz!
     
  2. These are both the ways to do it what isn't working about it? But if for some reason you wanted to you could calculate it yourself.
     
  3. Code (Text):
    org.bukkit.event.EventException: null
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:529) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:514) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at org.bukkit.craftbukkit.v1_14_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:429) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at org.bukkit.craftbukkit.v1_14_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:396) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at org.bukkit.craftbukkit.v1_14_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:392) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:1679) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.PacketPlayInArmAnimation.a(SourceFile:32) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.PacketPlayInArmAnimation.a(SourceFile:9) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:910) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:903) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeAll(SourceFile:103) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:886) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:820) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.NullPointerException
        at andy.guns.Double.onPlayerInteract(Double.java:82) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_241]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_241]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:316) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        ... 20 more
    Code (Text):
     double distance = player.getLocation().distance(block.getLocation());
    emmm
     
  4. I just tried using the following code for myself and it worked flawlessly:
    Code (Text):
    double distance = player.getLocation().distance(event.getBlock().getLocation());
    Where are you calling the distance double?
     
  5. :unsure: Are you using it in 1.14.4(player interact)?
     
  6. Well since we don't have all your code those errors are pointing to, I'd assume either one of the locations is null or something completely different is going wrong.
     
  7. Maybe I should try to recode it:unsure:
     
  8. Make sure none of your variables are null, such as player, or the block location you're referencing
     
  9. Sorry for waiting that long, still not working,
    my code
    Code (Java):
    @EventHandler

        public void onPlayerInteract(PlayerInteractEvent event) {

            Player player = event.getPlayer();

            Block block = event.getClickedBlock();

            Action action = event.getAction();

            double distance = player.getLocation().distance(block.getLocation());

            if(action.equals(action.RIGHT_CLICK_BLOCK)&&distance<=1) {

                if (cd.checkCd(player.getPlayer())) {

                    player.setVelocity(player.getLocation().getDirection().multiply(1).setY(1.5));

                    if (player.isSprinting()) {

                        player.setVelocity(player.getLocation().getDirection().multiply(-1).setY(1.5));

                        player.getLocation().setYaw(180);

                    }

                }else player.sendMessage("[Server] Please Wait For" + cd.getCd(player.getPlayer()));

            }

        }
    crash-report
    Code (Text):
    [Server thread/ERROR]: Could not pass event PlayerInteractEvent to Guns v3
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:529) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:514) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:429) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:396) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:392) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:1679) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PacketPlayInArmAnimation.a(SourceFile:32) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PacketPlayInArmAnimation.a(SourceFile:9) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:910) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:903) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:887) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:820) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.NullPointerException
            at andy.guns.o.onPlayerInteract(o.java:23) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_241]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_241]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:316) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            ... 20 more
     
  10. First you should calculate the distance after you check the player action

    Then do distance <= 1

    Also you are assuming that the player clicked on a block, what if he clicked on air? You shoulf check if the clicked block is null before using it
     
  11. so you mean put the distance =whatever after check the action?

    And isn't click on air another action?
     
    #11 ComfortableAndy, Apr 5, 2020
    Last edited: Apr 5, 2020