1

My goal is to achieve that players on my bukkit server only hear each other, when there are no walls between them. So my idea was to get the distance between the sender of a message on the AsyncPlayerChatEvent and the recipient (getRecipients()) using a (Flying)Pathfinder(Goal), so that there is a path (throught the air) to the other player. If there is no way or the path is too long, I would remove the recipient from the list.

What I have so far:

    @EventHandler
    public void onAsyncPlayerChat(AsyncPlayerChatEvent e) {
        Player p = e.getPlayer();
        Location start = p.getLocation();
        if (start.getWorld() == null) {
            return;
        }
        PathfinderFlying pathfinder = new PathfinderFlying();
        World world = ((CraftPlayer) p).getHandle().getWorld();
        ChunkCache chunkCache = new ChunkCache(world,
                new BlockPosition(start.getX() - 500, 0, start.getZ() - 500),
                new BlockPosition(start.getX() + 500, 0, start.getZ() + 500)); //Dunno if this is correct
        // EntityInsentientImplementation is basically: EntityInsentientImplementation extends EntityInsentient with default constructor
        pathfinder.a(chunkCache, new EntityInsentientImplementation(EntityTypes.am, world));
        for (Player target : e.getRecipients()) {
            Location dest = target.getLocation();
            // How do I get the distance?
        }
    }

I already thried the function public int a(PathPoint[] var0, PathPoint var1) { from PathfinderFlying but this seems to return a static value (26) when var0 is the location of th sender and var1 is the location of the recipient.

I'm using bukkit 1.17.1.

Marvin Klar
  • 1,869
  • 3
  • 14
  • 32
  • Just to clarify, you're after the distance that the pathfinder took rather than the A->B distance of the two players? – Lucan Nov 08 '21 at 09:20
  • @Lucan, yes, I want to know the distance that the pathfinder took. The distance between the two players is easy to determine. – Marvin Klar Nov 10 '21 at 11:10
  • So you want to know the distance if a player would like to walk from the current loc to another player's loc ? – Elikill58 Nov 23 '21 at 10:03
  • Nearly, instead of walking, I want to know the distance through air to the given player. So flying instead of walking. – Marvin Klar Nov 23 '21 at 12:22
  • @MarvinKlar Ok, I think I have something that can help you, but it's with lot of class and I will not be able to show everything in SO, it's not important if there is a github link to a new repository ? – Elikill58 Nov 23 '21 at 18:36
  • Also, it's important if it's NOT a pathfinder ? Because it seems to don't have pathfinder that do what you want – Elikill58 Nov 23 '21 at 19:19
  • You can just send/post the link to the repo. It's fine for me. Your soluion must NOT include pathfinding. It waas just an idea of solving this. If we can solve the problem using something else, thats also fine! – Marvin Klar Nov 23 '21 at 19:45

1 Answers1

2

I was working on how to find the best way to go at specific location. So, I made BaritoneCalculator.

You should do like that to use it :

GoalBlock goal = new GoalBlock(x, y, z); // create direction goal
BaritoneAPI.getProvider().getNewBaritone(p).getPathingBehavior().startGoal(goal);

Now, it will calculate the better path.

To get the path, use this :

BaritoneAPI.getProvider().getBaritone(p).getPathingBehavior().getPath();

Or, you can also use an event: PathCalculatedEvent.

Full code to count all positions to pass through (i.e. your issue) :

PathingBehavior pathing = BaritoneAPI.getProvider().getNewBaritone(p).getPathingBehavior();
GoalBlock goal = new GoalBlock(x, y, z); // create direction goal
pathing.startGoal(goal); // start goal and calculate
Optional<IPath> optPath = pathing.getPath();
if(optPath.isPresent()) { // can be not found yet, use event to be sure
   int distance = pathing.get().positions().size() -1; // -1 to don't count current player position
   // now you have the distance
} else {
   // failed to find way
}

More informations (and updated) on readme.

Elikill58
  • 4,050
  • 24
  • 23
  • 45
  • Sometimes I get the following error: https://pastebin.com/ze4NSCZR – Marvin Klar Nov 24 '21 at 18:04
  • When I increase the distance between 2 players, the calulated distance sometimes gets smaller. And sometimes, it doesn't find a way, even if I'm on a flat map seeing the other player a few blocks away – Marvin Klar Nov 24 '21 at 18:06
  • Oh oups, it's because it don't find a NMS method. You can find `PlayerContext` class. Really ? Can you precise more ? That's seems strange. Can you show me, do you want to talk on github on discord ? – Elikill58 Nov 24 '21 at 18:07
  • If the distance is too low (less than 5 blocks) it doesn't calculate – Elikill58 Nov 24 '21 at 18:08