-
-
Notifications
You must be signed in to change notification settings - Fork 294
/
Age.java
98 lines (85 loc) · 2.55 KB
/
Age.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
package net.citizensnpcs.trait;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Ageable;
import org.bukkit.entity.Tadpole;
import org.bukkit.entity.Zombie;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.util.Messages;
/**
* Persists the Minecraft age variable for {@link Ageable}s. Will also set baby for {@link Zombie}s.
*
* @see Ageable
*/
@TraitName("age")
public class Age extends Trait implements Toggleable {
@Persist
private int age = 0;
private Ageable ageable;
@Persist
private boolean locked = true;
public Age() {
super("age");
}
/**
* Send a brief description of the current state of the trait to the supplied {@link CommandSender}.
*/
public void describe(CommandSender sender) {
Messaging.sendTr(sender, Messages.AGE_TRAIT_DESCRIPTION, npc.getName(), age, locked);
}
public int getAge() {
return age;
}
private boolean isAgeable() {
return ageable != null;
}
@Override
public void onSpawn() {
if (npc.getEntity() instanceof Ageable) {
Ageable entity = (Ageable) npc.getEntity();
entity.setAge(age);
entity.setAgeLock(locked);
ageable = entity;
} else {
if (npc.getEntity() instanceof Zombie) {
((Zombie) npc.getEntity()).setBaby(age < 0);
} else if (npc.isSpawned() && npc.getEntity().getType().name().equals("TADPOLE")) {
((Tadpole) npc.getEntity()).setAge(age);
}
ageable = null;
}
}
@Override
public void run() {
if (!locked && isAgeable()) {
age = ageable.getAge();
}
}
public void setAge(int age) {
this.age = age;
onSpawn();
}
public void setLocked(boolean locked) {
this.locked = locked;
if (isAgeable()) {
ageable.setAgeLock(locked);
}
}
/**
* Toggles the age lock variable and returns whether the age is currently locked.
*/
@Override
public boolean toggle() {
locked = !locked;
if (isAgeable()) {
ageable.setAgeLock(locked);
}
return locked;
}
@Override
public String toString() {
return "Age{age=" + age + ",locked=" + locked + "}";
}
}