-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
NicknameTrait.java
87 lines (75 loc) · 2.75 KB
/
NicknameTrait.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package net.aufdemrand.denizen.npc.traits;
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import org.bukkit.ChatColor;
import org.bukkit.event.Listener;
/**
* <p>Adds the ability to 'nickname' an NPC. This is meant to extend the NPCs real
* name to perhaps add more description. Similar to a Player's 'Display Name', but better.
* Inside Denizen, Nicknames can be utilized containing Replaceable TAGs. Outside Denizen,
* the methods contained in this Trait can be used to get, set, and remove nicknames.</p>
*
* <p>Nicknames should not used as a static reference to an NPC because of the
* dynamic nature of the Trait. Each time the trait is asked for a nickname, tags are
* replaced. This allows for nicknames to use FLAGs and other dynamically changing
* TAGs and have the linked information updated live.</p>
*
* <p>Though not in this Trait class, Denizen also provides some Replaceable TAGs
* for getting a NPCs nickname. Use <</p>
*
*/
public class NicknameTrait extends Trait implements Listener {
@Persist("")
private String nickname = null;
public NicknameTrait() {
super("nickname");
}
/**
* Sets the nickname of this NPC. When setting, dScript TAGS
* can be used. This included dScript color codes.
*
* @param nickName the new nickname for this NPC
*
*/
public void setNickname(String nickName) {
this.nickname = nickName;
}
/**
* Gets the current nickname of this NPC. This may include color codes.
* Note: To strip color codes, use {@link #getUncoloredNickname()}. If
* this NPC does not have a nickname, its NPC name is returned instead.
*
* @return the nickname for this NPC
*
*/
public String getNickname() {
if (nickname == null || nickname.equals("")) return npc.getName();
else return TagManager.tag(null, DenizenAPI.getDenizenNPC(npc), nickname, false);
}
/**
* Gets the current nickname of this NPC and strips out all text colors.
* To get the colored nickname use {@link #getNickname()}.
*
* @return The uncolored nickname for this NPC
*/
public String getUncoloredNickname() {
return ChatColor.stripColor( getNickname() );
}
/**
* Removes the current nickname from the NPC.
*
*/
public void removeNickname() {
nickname = null;
}
/**
* Checks if the NPC has a nickname set.
*
* @return true if NPC has a nickname
*/
public boolean hasNickname() {
return (nickname != null);
}
}