Search code examples
javapluginsminecraftbukkit

How could I spawn an NPC with Citizens API?


I am making a housing plugin(Bukkit 1.6.4) in java for the mod company that I am working for. I need to use NPCS for it. I tried to use the Cizizens API but when I try to create it always gives me an error:

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)

Plugins.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");

    }

}

I also included CitizensAPI.setImplementation(new CitizensPlugin()) {...} in my main.java file because it would give me an illegalargumentexception if I wouldn't include this line.

Would someone know how I could fix this?

Thanks


Solution

  • Not quite sure about all the details of the Citizens API but from the java error I would conclude that the variable registry (The NPCRegistry object) is null.

    Further I remember from my time with Mincraft that you need to add the API which you want to depend your plugin on to the plugin.yml. Which means in your case that you have to change the first line of your plugin.yml to this:

    depend: [Citizens, Multiverse-Core]
    

    If you do this you should normally be able to execute your code as it is. You can find further information here (Under "Hooking Into Citizens): https://wiki.citizensnpcs.co/API

    Cobra_8