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
#(tetemh) Depspawn Citizen npc
33 messages · Page 1 of 1 (latest)
(tetemh) Depspawn Citizen npc
ASCII Name Rule Enforcement
Non-ASCII nickname for @nimble onyx changed to base username. Please only use a readable+typable US-English ASCII nickname.
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.
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();
}
}
I don't understand what the problem is
can you say it in one sentence?
When I restart my weaner instead of having 1npc I have 2 times the same one.
My English is bad but I use a translator so it doesn't help to make myself understood.
if (npc.getStoredLocation().equals(this.getLocation())) { this seems suspect
why not just check the name matches?
@nimble onyx
it's just another try i've already tried with name and id
@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
and so instead of launching my NPC's spawn in the server's onEnable, I register Citizen's start event and launch it there, if I've understood correctly.
But how do you detect by npc name?
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();
}
}
@nimble onyx it works?
yes
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
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
Resolved
Thread closed as resolved.
thx @hallow reef
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