#(tetemh) Depspawn Citizen npc

33 messages · Page 1 of 1 (latest)

nimble onyx
#

Hello, I have a problem with citizen in my plugin paper 1.21.1. I want to make a pnj of Bank which allows to deposit emeralds, I have a class Bank with a variable NPC npc in it which allows to store the npc of bank. My first solution was to kill the NPC and remove it with the onDisable of my plugin but this doesn't work, otherwise the other way was to keep it but I don't know how to do it I've tried a few things but it doesn't work. If anyone can help me, I'm very grateful.
PS: when you click on the pnj it should open an inventory made with FastInv and here I have an evet that allows you to trigger it but I don't know if it's the most opti

safe charmBOT
#

(tetemh) Depspawn Citizen npc

old raptorBOT
safe charmBOT
#

Hi I'm AutoThreadBot! Don't mind me, I'll just be adding the helper team to this thread so they can see it. A human will get to you soon.

nimble onyx
#
package fr.tetemh.skycite.custom.customclass;

@Data
public class Bank {

    private final SkyCite plugin;
    private final String name;
    private final Location location;
    private NPC npc;
    private FastInv inventory;


    public Bank (SkyCite plugin) {
        this.plugin = plugin;
        this.name = "Banquier";
        this.location = new Location(Bukkit.getWorld("world"), 0, 100, 5);

        this.genInventory();

        for (NPC npc : CitizensAPI.getNPCRegistry()) {
            if (npc.getStoredLocation().equals(this.getLocation())) {
                this.setNpc(npc);
                System.out.println("NPC déjà présent à cet emplacement.");
                return;
            }
        }

        // Créer un nouveau NPC
        this.setNpc(CitizensAPI.getNPCRegistry().createNPC(EntityType.PLAYER, "NPCName"));
    }

    public void spawn () {

        //Citizen Conf
        this.getNpc().setFlyable(true);
        this.getNpc().setUseMinecraftAI(false);
        this.getNpc().setProtected(true);

        SkinTrait skinTrait = this.getNpc().getOrAddTrait(SkinTrait.class);
        skinTrait.setSkinName("tetemhjpd");
        this.getNpc().addTrait((Trait) skinTrait);

        // Spawn Entity
        this.getNpc().spawn(this.getLocation());

        // Param on Entity
        this.getNpc().getEntity().getPersistentDataContainer().set(new NamespacedKey(this.plugin, "npc_type"), PersistentDataType.STRING, "bank");
        this.getNpc().getEntity().setGravity(false);
    }

    private void genInventory() {
        this.setInventory(new BankGui(this.getPlugin(), this));
    }

    public void disable() {
        this.getPlugin().getNpcConfig().set("npcs.bank.name", this.getName());
        this.getPlugin().getNpcConfig().set("npcs.bank.location", this.getLocation());

        this.getNpc().destroy();
    }
}
hallow reef
#

can you say it in one sentence?

nimble onyx
#

When I restart my weaner instead of having 1npc I have 2 times the same one.

nimble onyx
hallow reef
#

if (npc.getStoredLocation().equals(this.getLocation())) { this seems suspect

#

why not just check the name matches?

#

@nimble onyx

nimble onyx
hallow reef
#

@nimble onyx it is very difficult to teach how to code with language barrier

#

I think you should try debugging some more first

#

I would use ID or name instead of location

#

And I would make sure to wait for citizensenableevent before iterating through the registry

nimble onyx
#

But how do you detect by npc name?

nimble onyx
#

this works


        for (NPC npc : CitizensAPI.getNPCRegistry()) {
            if (npc.getName().equals(this.getName())) {
                this.setNpc(npc);
                return;
            }
        }

        // Créer un nouveau NPC
        this.setNpc(CitizensAPI.getNPCRegistry().createNPC(EntityType.PLAYER, this.getName()));
#
package fr.tetemh.skycite.events;

import fr.tetemh.skycite.SkyCite;
import lombok.Getter;
import net.citizensnpcs.api.event.CitizensEnableEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

@Getter
public class OnCitizenStartEvent implements Listener {

    private final SkyCite plugin;

    public OnCitizenStartEvent(SkyCite plugin) {
        this.plugin = plugin;
    }

    @EventHandler
    public void onCitizenStart(CitizensEnableEvent event) {

        // Spawn Bank
        this.getPlugin().getBank().setNPC();
        this.getPlugin().getBank().spawn();
    }
}

hallow reef
#

@nimble onyx it works?

nimble onyx
#

yes

ancient minnowBOT
#
Thread Closing Reminder

Has your issue been resolved, or your question been answered?
If so, please use the </resolved:1028673926114594866> command to close your thread.
Or </invalid:1028673926898909185> if it's not possible to resolve.

If not yet resolved, please reply below to tell us what you still need.

(Note that if there is no reply for a few days, this thread will eventually close itself.)

#

@nimble onyx

nimble onyx
#

and for spawn i use that ```java
public void spawn () {

    //Citizen Conf
    this.getNpc().setFlyable(true);
    this.getNpc().setUseMinecraftAI(false);
    this.getNpc().setProtected(true);

    SkinTrait skinTrait = this.getNpc().getOrAddTrait(SkinTrait.class);
    skinTrait.setSkinName("tetemhjpd");
    this.getNpc().addTrait((Trait) skinTrait);

    // Spawn Entity
    this.getNpc().spawn(this.getLocation());

    // Param on Entity
    this.getNpc().getEntity().getPersistentDataContainer().set(new NamespacedKey(this.plugin, "npc_type"), PersistentDataType.STRING, "bank");
    this.getNpc().getEntity().setGravity(false);
}
#

if it's utils for other user

ancient minnowBOT
#
Resolved

Thread closed as resolved.

nimble onyx
#

thx @hallow reef

ancient minnowBOT
#
Thread Reopened

Thread was manually reopened by @nimble onyx.

#
Thread Closing Reminder

Has your issue been resolved, or your question been answered?
If so, please use the </resolved:1028673926114594866> command to close your thread.
Or </invalid:1028673926898909185> if it's not possible to resolve.

If not yet resolved, please reply below to tell us what you still need.

(Note that if there is no reply for a few days, this thread will eventually close itself.)

#

@nimble onyx