gpt4 book ai didi

java - 我如何使用 Citizens API 生成 NPC?

转载 作者:行者123 更新时间:2023-12-02 10:08:25 27 4
gpt4 key购买 nike

我正在用java为我工作的mod公司制作一个住房插件(Bukkit 1.6.4)。我需要使用NPCS。我尝试使用 Cizizens API,但当我尝试创建它时总是出现错误:

java.lang.NullPointerException
at com.packagenamehere.npc.HouseShopNPC.<init>(HouseShopNPC.java:23)
at com.packagenamehere.houses.House.createNPCS(House.java:88)
at com.packagenamehere.data.HouseDataLoader.loadData(HouseDataLoader.java:80)
at com.packagenamehere.data.HouseDataLoader.<init>(HouseDataLoader.java:20)
at com.packagenamehere.data.DataSetUp.checkFirstRun(DataSetUp.java:24)
at com.packagenamehere.data.DataSetUp.<init>(DataSetUp.java:16)
at com.packagenamehere.main.Main.onEnable(Main.java:205)
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
at org.bukkit.craftbukkit.v1_6_R3.CraftServer.loadPlugin(CraftServer.java:284)
at org.bukkit.craftbukkit.v1_6_R3.CraftServer.enablePlugins(CraftServer.java:266)
at net.minecraft.server.v1_6_R3.MinecraftServer.l(MinecraftServer.java:315)
at net.minecraft.server.v1_6_R3.MinecraftServer.f(MinecraftServer.java:292)
at net.minecraft.server.v1_6_R3.MinecraftServer.a(MinecraftServer.java:252)
at net.minecraft.server.v1_6_R3.DedicatedServer.init(DedicatedServer.java:152)
at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:393)
at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)

插件.yml:

depend: [Multiverse-Core]
name: Housing
description: This is a plugin for housing.
version: 1.0
main: com.packagenamehere.main.Main
Authors: MaxR

commands:
h:
usage: /<command>
description: type /house help for a list of cmds

HouseShopNPC:

package com.packagenamehere.npc;

import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.NPCClickEvent;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

public class HouseShopNPC implements Listener{

public HouseShopNPC(Location loc){

World hWorld = Bukkit.getWorld("HouseWorld");

NPCRegistry registry = CitizensAPI.getNPCRegistry();

NPC hNpc = registry.createNPC(EntityType.PLAYER, "Shop");
hNpc.spawn(Bukkit.getWorlds().get(0).getSpawnLocation());

}

@EventHandler
public void onClickEvent(NPCClickEvent e){

Player plr = e.getClicker();

plr.sendMessage("sdfsdf");

}

}

我还在我的 main.java 文件中包含了 CitizensAPI.setImplementation(new CitizensPlugin()) {...} ,因为如果我不包含这一行,它会给我一个非法参数异常。

有人知道我该如何解决这个问题吗?

谢谢

最佳答案

不太确定 Citizens API 的所有细节,但从 java 错误中我可以得出结论,变量注册表(NPCRegistry 对象)为 null。

此外,我记得在使用 Mincraft 时,您需要将插件依赖的 API 添加到 plugin.yml 中。这意味着在您的情况下,您必须将 plugin.yml 的第一行更改为:

depend: [Citizens, Multiverse-Core]

如果您这样做,您通常应该能够按原样执行代码。您可以在这里找到更多信息(在“吸引公民”下): https://wiki.citizensnpcs.co/API

Cobra_8

关于java - 我如何使用 Citizens API 生成 NPC?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55172082/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com