/
Trait.java
107 lines (91 loc) · 2.34 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
package net.citizensnpcs.api.trait;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.DataKey;
import org.bukkit.event.Listener;
/**
* Represents a Trait that can be loaded and saved.
*/
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;
}
/**
* 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 when a trait is removed from the attached {@link NPC}.
*/
public void 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() {
}
@Override
public void run() {
runImplemented = false;
}
/**
* Saves a trait.
*
* @param key
* DataKey to save to
*/
public void save(DataKey key) {
}
}