/
Trait.java
130 lines (110 loc) · 3.17 KB
/
Trait.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package net.citizensnpcs.api.trait;
import org.bukkit.event.Listener;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.RemoveReason;
/**
* Represents a Trait linked to an {@link NPC} that can be loaded and saved. This will be kept persisted inside a
* {@link NPC} across server restarts. Traits must be registered in Citizens' {@link TraitFactory}.
*
* All traits should have a default constructor with no arguments for persistence purposes.
*/
public abstract class Trait implements Listener, Runnable {
private final String name;
protected NPC npc = null;
private boolean runImplemented = true;
protected Trait(String name) {
this.name = name.toLowerCase();
}
/**
* Gets the name of this trait.
*
* @return Name of this trait
*/
public final String getName() {
return name;
}
/**
* @return The {@link NPC} this trait is attached to. May be null.
*/
public final NPC getNPC() {
return npc;
}
public boolean isRunImplemented() {
run();
return runImplemented;
}
public void linkToNPC(NPC npc) {
if (this.npc != null)
throw new IllegalArgumentException("npc may only be set once");
this.npc = npc;
onAttach();
}
/**
* Loads a trait.
*
* @param key
* DataKey to load from
* @throws NPCLoadException
* Thrown if this trait failed to load properly
*/
public void load(DataKey key) throws NPCLoadException {
}
/**
* Called when the trait has been attached to an {@link NPC}. {@link #npc} will be null until this is called.
*/
public void onAttach() {
}
/**
* Called when the trait has been newly copied to an {@link NPC}.
*/
public void onCopy() {
}
/**
* Called just before the attached {@link NPC} is despawned. {@link NPC#getEntity()} will be non-null.
*/
public void onDespawn() {
}
/**
* Called just before the attached {@link NPC} is despawned. {@link NPC#getEntity()} will be non-null.
*/
public void onDespawn(DespawnReason reason) {
onDespawn();
}
/**
* Called just before the {@link NPC} is spawned. {@link NPC#getEntity()} will return an <em>unspawned</em> entity.
*/
public void onPreSpawn() {
}
/**
* Called when a trait is removed from the attached {@link NPC}.
*/
public void onRemove() {
}
public void onRemove(RemoveReason reason) {
onRemove();
}
/**
* Called when an {@link NPC} is spawned. {@link NPC#getEntity()} will return null until this is called. This is
* also called onAttach when the NPC is already spawned.
*/
public void onSpawn() {
}
/**
* Called every tick if overridden.
*/
@Override
public void run() {
runImplemented = false;
}
/**
* Saves a trait.
*
* @param key
* DataKey to save to
*/
public void save(DataKey key) {
}
}