/
Poses.java
121 lines (104 loc) · 3.78 KB
/
Poses.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
package net.citizensnpcs.trait;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import com.google.common.collect.Maps;
import net.citizensnpcs.api.command.exception.CommandException;
import net.citizensnpcs.api.event.SpawnReason;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.Paginator;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Pose;
import net.citizensnpcs.util.Util;
/**
* Persists named {@link Pose}s.
*/
@TraitName("poses")
public class Poses extends Trait {
private final Map<String, Pose> poses = Maps.newHashMap();
public Poses() {
super("poses");
}
/**
* Add a {@link Pose}
*
* @return whether the pose has already been added
*/
public boolean addPose(String name, Location location) {
name = name.toLowerCase();
Pose newPose = new Pose(name, location.getPitch(), location.getYaw());
if (poses.containsValue(newPose) || poses.containsKey(name))
return false;
poses.put(name, newPose);
return true;
}
private void assumePose(float yaw, float pitch) {
if (!npc.isSpawned()) {
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation(), SpawnReason.COMMAND);
}
Util.assumePose(npc.getEntity(), yaw, pitch);
}
/**
* Sets the yaw/pitch to the supplied {@link Location}.
*/
public void assumePose(Location location) {
assumePose(location.getYaw(), location.getPitch());
}
/**
* Sets the yaw/pitch to the stored pose, looked up by name.
*/
public void assumePose(String flag) {
Pose pose = poses.get(flag.toLowerCase());
assumePose(pose.getYaw(), pose.getPitch());
}
public void describe(CommandSender sender, int page) throws CommandException {
Paginator paginator = new Paginator().header("Pose");
paginator.addLine("<e>Key: <a>ID <b>Name <c>Pitch/Yaw");
int i = 0;
for (Pose pose : poses.values()) {
String line = "<a>" + i + "<b> " + pose.getName() + "<c> " + pose.getPitch() + "/" + pose.getYaw();
paginator.addLine(line);
i++;
}
if (!paginator.sendPage(sender, page))
throw new CommandException(Messages.COMMAND_PAGE_MISSING);
}
public Pose getPose(String name) {
for (Pose pose : poses.values()) {
if (pose.getName().equalsIgnoreCase(name)) {
return pose;
}
}
return null;
}
public boolean hasPose(String pose) {
return poses.containsKey(pose.toLowerCase());
}
@Override
public void load(DataKey key) throws NPCLoadException {
poses.clear();
for (DataKey sub : key.getRelative("list").getIntegerSubKeys())
try {
String[] parts = sub.getString("").split(";");
poses.put(parts[0], new Pose(parts[0], Float.valueOf(parts[1]), Float.valueOf(parts[2])));
} catch (NumberFormatException e) {
Messaging.logTr(Messages.SKIPPING_INVALID_POSE, sub.name(), e.getMessage());
}
}
public boolean removePose(String pose) {
return poses.remove(pose.toLowerCase()) != null;
}
@Override
public void save(DataKey key) {
key.removeKey("list");
int i = 0;
for (Pose pose : poses.values()) {
key.setString("list." + i, pose.stringValue());
i++;
}
}
}