Skip to content

Commit

Permalink
Add NPCRenameEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Dec 20, 2022
1 parent eb96177 commit 3ce4c54
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
39 changes: 39 additions & 0 deletions src/main/java/net/citizensnpcs/api/event/NPCRenameEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package net.citizensnpcs.api.event;

import org.bukkit.event.HandlerList;

import net.citizensnpcs.api.npc.NPC;

public class NPCRenameEvent extends NPCEvent {
private String newName;
private final String oldName;

public NPCRenameEvent(NPC npc, String oldName, String newName) {
super(npc);
this.oldName = oldName;
this.setNewName(newName);
}

@Override
public HandlerList getHandlers() {
return handlers;
}

public String getNewName() {
return newName;
}

public String getOldName() {
return oldName;
}

public void setNewName(String newName) {
this.newName = newName;
}

public static HandlerList getHandlerList() {
return handlers;
}

private static final HandlerList handlers = new HandlerList();
}
7 changes: 5 additions & 2 deletions src/main/java/net/citizensnpcs/api/npc/AbstractNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import net.citizensnpcs.api.event.NPCRemoveByCommandSenderEvent;
import net.citizensnpcs.api.event.NPCRemoveEvent;
import net.citizensnpcs.api.event.NPCRemoveTraitEvent;
import net.citizensnpcs.api.event.NPCRenameEvent;
import net.citizensnpcs.api.event.NPCTeleportEvent;
import net.citizensnpcs.api.persistence.PersistenceLoader;
import net.citizensnpcs.api.trait.Trait;
Expand Down Expand Up @@ -472,8 +473,10 @@ public void setName(String name) {
if (name.equals(this.name))
return;

this.name = name;
minecraftComponentCache = Messaging.minecraftComponentFromRawMessage(name);
NPCRenameEvent event = new NPCRenameEvent(this, this.name, name);
Bukkit.getPluginManager().callEvent(event);
this.name = event.getNewName();
minecraftComponentCache = Messaging.minecraftComponentFromRawMessage(this.name);

if (!isSpawned())
return;
Expand Down

0 comments on commit 3ce4c54

Please sign in to comment.