首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NPC未移动到目标位置

NPC未移动到目标位置
EN

Stack Overflow用户
提问于 2020-10-06 23:27:33
回答 1查看 593关注 0票数 0

我正在使用citizensAPI来生成和操纵NPC。代码如下。

我正在尝试查找半径为20的最近的JUNGLE_WOOD类型的块。

代码语言:javascript
复制
package org.mineacademy.orion.npctest;

import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.ai.PathStrategy;
import net.citizensnpcs.api.hpastar.Tile;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.ai.AStarNavigationStrategy;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.mineacademy.fo.command.SimpleCommand;

public class BlockFinder extends SimpleCommand {

    public BlockFinder(){
        super("finder");
    }

    public void onCommand() {
        checkConsole();

        NPC npc= CitizensAPI.getNPCRegistry().createNPC(EntityType.VILLAGER,"cutter");
        Player player=getPlayer();
        npc.spawn(player.getTargetBlock(null, 5).getLocation().add(3, 3, 0));

        //add location and setTarget to that location.

        Location finalLoc=newLocation(npc,player);
        npc.setProtected(false);
        npc.faceLocation(finalLoc);
        npc.getNavigator().setTarget(finalLoc);


    }

    public Location newLocation(NPC npc, Player player) {
        double r =25;
        double x=npc.getEntity().getLocation().getX();
        double y=npc.getEntity().getLocation().getY();
        double z=npc.getEntity().getLocation().getZ();
        int minx=(int)(x-r);
        int miny=(int)(y-r);
        int minz=(int)(z-r);
        int maxx=(int)(x+r);
        int maxy=(int)(y+r);
        int maxz=(int)(z+r);
        Location nloc=new Location(npc.getEntity().getWorld(),x,y,z);
        for(int rx=minx;rx<=maxx;rx++) {
            for (int ry = miny; ry <= maxy; ry++) {
                for (int rz = minz; rz <= maxz; rz++) {
                    Block block = player.getWorld().getBlockAt(rx, ry, rz);
                    if (block.getType() == Material.JUNGLE_WOOD) {
                        nloc.setX(rx);
                        nloc.setY(ry);
                        nloc.setZ(rz);
                        return nloc;
                    }
                }
            }
        }
        return nloc;
    }

}

NPC可以正常产卵,但有时它根本不会移动,否则它会旋转或只是移动几米并停止。有没有人知道任何修复方法,以便我能够将NPC沿着路径移动到该区块?

EN

回答 1

Stack Overflow用户

发布于 2020-10-14 21:04:41

首先,您应该检查NPC是否存在:NPC.isSpawned() - boolean

然后,您应该在NPCSpawnEvent (https://jd.citizensnpcs.co/net/citizensnpcs/api/event/NPCSpawnEvent.html)之后触发此部分:

代码语言:javascript
复制
        Location finalLoc=newLocation(npc,player);
        npc.setProtected(false);
        npc.faceLocation(finalLoc);
        npc.getNavigator().setTarget(finalLoc);

顺便说一下,你可以在谷歌上搜索一下:https://www.spigotmc.org/threads/citizens-api-problem-walking-npc.365712/

我发现这个真的很有用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64228979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档