我正在使用citizensAPI来生成和操纵NPC。代码如下。
我正在尝试查找半径为20的最近的JUNGLE_WOOD类型的块。
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沿着路径移动到该区块?
发布于 2020-10-14 21:04:41
首先,您应该检查NPC是否存在:NPC.isSpawned() - boolean
然后,您应该在NPCSpawnEvent (https://jd.citizensnpcs.co/net/citizensnpcs/api/event/NPCSpawnEvent.html)之后触发此部分:
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/
我发现这个真的很有用。
https://stackoverflow.com/questions/64228979
复制相似问题