/
PoseCommand.java
164 lines (137 loc) · 6.09 KB
/
PoseCommand.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package net.aufdemrand.denizen.scripts.commands.npc;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import net.citizensnpcs.trait.Poses;
import org.bukkit.Location;
import org.bukkit.entity.Player;
public class PoseCommand extends AbstractCommand {
// <--[command]
// @Name Pose
// @Syntax pose (add/remove/{assume}) [id:<name>] (player/{npc}) (<location>)
// @Required 1
// @Plugin Citizens
// @Short Rotates the player or NPC to match a pose, or adds/removes an NPC's poses.
// @Group npc
//
// @Description
// Makes a player or NPC assume the position of a pose saved on an NPC, removes a
// pose with a specified ID from the current linked NPC, or adds a pose to the NPC
// with an ID and a location, although the only thing that matters in the location
// is the pitch and yaw.
//
// @Tags
// <n@npc.has_pose[<name>]>
// <n@npc.pose[<name>]>
//
// @Usage
// Make an NPC assume a pose.
// - pose id:MyPose1
//
// @Usage
// Add a pose to an NPC. (Note that only the last 2 numbers matter)
// - pose add id:MyPose2 l@0,0,0,-2.3,5.4,world
//
// @Usage
// Remove a pose from an NPC.
// - pose remove id:MyPose1
// -->
private enum TargetType {NPC, PLAYER}
private enum Action {ADD, REMOVE, ASSUME}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Parse Arguments
for (aH.Argument arg : aH.interpretArguments(scriptEntry.aHArgs)) {
if (arg.matches("add", "assume", "remove")) {
scriptEntry.addObject("action", Action.valueOf(arg.getValue().toUpperCase()));
}
else if (arg.matchesPrefix("id")) {
scriptEntry.addObject("pose_id", arg.getValue());
}
else if (arg.matches("player")) {
scriptEntry.addObject("target", TargetType.PLAYER);
}
else if (arg.matchesArgumentType(dLocation.class)) {
scriptEntry.addObject("pose_loc", arg.asType(dLocation.class));
}
}
// Even if the target is a player, this command requires an NPC to get the pose from.
if (!Utilities.entryHasNPC(scriptEntry)) {
throw new InvalidArgumentsException("This command requires an NPC!");
}
// It also requires a pose ID
if (!scriptEntry.hasObject("pose_id")) {
throw new InvalidArgumentsException("No ID specified!");
}
// Set default objects
scriptEntry.defaultObject("target", TargetType.NPC);
scriptEntry.defaultObject("action", Action.ASSUME);
// If the target is a player, it needs a player! However, you can't ADD/REMOVE poses
// from players, so only allow ASSUME.
if (scriptEntry.getObject("target") == TargetType.PLAYER) {
if (scriptEntry.getObject("action") != Action.ASSUME) {
throw new InvalidArgumentsException("You cannot add or remove poses from a player.");
}
else if (!Utilities.entryHasPlayer(scriptEntry)) {
throw new InvalidArgumentsException("This command requires a linked player!");
}
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
// Get objects
TargetType target = (TargetType) scriptEntry.getObject("target");
dNPC npc = Utilities.getEntryNPC(scriptEntry);
Action action = (Action) scriptEntry.getObject("action");
String id = (String) scriptEntry.getObject("pose_id");
dLocation pose_loc = (dLocation) scriptEntry.getObject("pose_loc");
// Report to dB
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(),
aH.debugObj("Target", target.toString())
+ (target == TargetType.PLAYER ? Utilities.getEntryPlayer(scriptEntry).debug() : "")
+ npc.debug()
+ aH.debugObj("Action", action.toString())
+ aH.debugObj("Id", id)
+ (pose_loc != null ? pose_loc.debug() : ""));
}
if (!npc.getCitizen().hasTrait(Poses.class)) {
npc.getCitizen().addTrait(Poses.class);
}
Poses poses = npc.getCitizen().getTrait(Poses.class);
switch (action) {
case ASSUME:
if (!poses.hasPose(id)) {
dB.echoError("Pose \"" + id + "\" doesn't exist for " + npc.toString());
}
if (target.name().equals("NPC")) {
poses.assumePose(id);
}
else {
Player player = Utilities.getEntryPlayer(scriptEntry).getPlayerEntity();
Location location = player.getLocation();
location.setYaw(poses.getPose(id).getYaw());
location.setPitch(poses.getPose(id).getPitch());
// The only way to change a player's yaw and pitch in Bukkit
// is to use teleport on him/her
player.teleport(location);
}
break;
case ADD:
if (!poses.addPose(id, pose_loc)) {
dB.echoError(npc.toString() + " already has that pose!");
}
break;
case REMOVE:
if (!poses.removePose(id)) {
dB.echoError(npc.toString() + " does not have that pose!");
}
break;
}
}
}