代码之家  ›  专栏  ›  技术社区  ›  Marvin Klar

使用bukkit的非实心块获取两个位置之间的距离

  •  0
  • Marvin Klar  · 技术社区  · 4 年前

    我的目标是实现我的bukkit服务器上的玩家只有在没有墙的情况下才能听到对方的声音。因此,我的想法是使用(飞行)Pathfinder(Goal)来获取AsyncPlayerChatEvent上消息的发送者和接收者(getRecipients())之间的距离,这样就有了通往另一个玩家的路径(通过空中)。如果没有办法或路径太长,我会将收件人从列表中删除。

    到目前为止我所拥有的:

        @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?
            }
        }
    

    我已经把功能提高了三倍 public int a(PathPoint[] var0, PathPoint var1) { 从…起 PathfinderFlying 但当 var0 是 发件人的位置以及 var1 是收件人的位置。

    我用的是bukkit 1.17.1。

    0 回复  |  直到 4 年前
        1
  •  2
  •   Elikill58    4 年前

    我正在研究如何在特定的地点找到最佳的出行方式。所以,我做了 BaritoneCalculator .

    你应该这样做才能使用它:

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

    现在,它将计算出更好的路径。

    要获取路径,请使用以下方法:

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

    或者,您也可以使用一个事件: PathCalculatedEvent .

    计算所有头寸的完整代码(即您的问题):

    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
    }
    

    有关的更多信息(和更新) readme .