/
ShootCommand.java
191 lines (148 loc) · 7.04 KB
/
ShootCommand.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package net.aufdemrand.denizen.scripts.commands.core;
import java.util.HashMap;
import java.util.Map;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.scripts.containers.core.TaskScriptContainer;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.arguments.Duration;
import net.aufdemrand.denizen.utilities.arguments.dLocation;
import net.aufdemrand.denizen.utilities.arguments.Script;
import net.aufdemrand.denizen.utilities.arguments.aH;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Projectile;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
/**
* Makes the NPC shoot entities towards a location.
* If no location is chosen, the NPC shoots entities in the direction it is facing.
*
* @author David Cernat
*/
public class ShootCommand extends AbstractCommand {
private enum ShooterType { NPC, PLAYER }
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Initialize necessary fields
ShooterType shooterType = ShooterType.NPC;
LivingEntity shooter = null;
EntityType entityType = null;
//Integer qty = null;
dLocation location = null;
Script newScript = null;
Boolean ride = false;
Boolean burn = false;
// Set some defaults
if (scriptEntry.getPlayer() != null) {
location = new dLocation(scriptEntry.getPlayer().getLocation());
}
if (location == null && scriptEntry.getNPC() != null) {
location = new dLocation(scriptEntry.getNPC().getLocation());
}
for (String arg : scriptEntry.getArguments()) {
if (aH.matchesEntityType(arg)) {
entityType = aH.getEntityFrom(arg);
dB.echoDebug("...projectile set to '%s'.", arg);
} else if (aH.matchesLocation(arg)) {
location = aH.getLocationFrom(arg);
dB.echoDebug("...location set to '%s'.", arg);
} else if (aH.matchesArg("PLAYER", arg)) {
shooterType = ShooterType.PLAYER;
dB.echoDebug("... will be shot by the player!");
} else if (aH.matchesScript(arg)) {
newScript = aH.getScriptFrom(arg);
dB.echoDebug(Messages.DEBUG_SET_SCRIPT, arg);
} else if (aH.matchesArg("ride, mount", arg)) {
ride = true;
dB.echoDebug("...will be mounted.");
} else if (aH.matchesArg("burn, burning", arg)) {
burn = true;
dB.echoDebug("...will burn.");
} else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
}
if (shooterType.name().matches("NPC")) {
shooter = scriptEntry.getNPC().getEntity();
}
else if (shooterType.name().matches("PLAYER")) {
shooter = scriptEntry.getPlayer();
}
if (entityType == null) throw new InvalidArgumentsException(Messages.ERROR_INVALID_ENTITY);
// Stash objects
scriptEntry.addObject("entityType", entityType);
scriptEntry.addObject("location", location);
scriptEntry.addObject("shooter", shooter);
scriptEntry.addObject("script", newScript);
scriptEntry.addObject("ride", ride);
scriptEntry.addObject("burn", burn);
}
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects
final dLocation location = scriptEntry.hasObject("location") ?
(dLocation) scriptEntry.getObject("location") :
(dLocation) scriptEntry.getNPC().getEyeLocation().getDirection().
multiply(4).toLocation(scriptEntry.getNPC().getWorld());
EntityType entityType = (EntityType) scriptEntry.getObject("entityType");
LivingEntity shooter = (LivingEntity) scriptEntry.getObject("shooter");
Boolean ride = (Boolean) scriptEntry.getObject("ride");
Boolean burn = (Boolean) scriptEntry.getObject("burn");
if (location != null) {
Utilities.faceLocation(shooter, location);
}
final Entity entity = shooter.getWorld().spawnEntity(
shooter.getEyeLocation().add(
shooter.getEyeLocation().getDirection())
.subtract(0, 0.4, 0),
entityType);
Utilities.faceLocation(entity, location);
if (ride == true) {
entity.setPassenger(scriptEntry.getPlayer());
}
if (burn == true) {
entity.setFireTicks(500);
}
if (entity instanceof Projectile) {
((Projectile) entity).setShooter(shooter);
}
BukkitRunnable task = new BukkitRunnable()
{
int runs = 0;
public void run() {
if (runs < 40 && entity.isValid())
{
//dB.echoDebug(entity.getType().name() + " flying time " + getRuns() + " in task " + getId());
Vector v1 = entity.getLocation().toVector().clone();
Vector v2 = location.toVector().clone();
Vector v3 = v2.clone().subtract(v1).normalize().multiply(1.5);
entity.setVelocity(v3);
runs++;
if (Math.abs(v2.getX() - v1.getX()) < 2 && Math.abs(v2.getY() - v1.getY()) < 2
&& Math.abs(v2.getZ() - v1.getZ()) < 2)
{
runs = 40;
}
}
else
{
this.cancel();
runs = 0;
if (scriptEntry.getObject("script") != null)
{
Map<String, String> context = new HashMap<String, String>();
context.put("1", entity.getLocation().getX() + "," + entity.getLocation().getY() + "," + entity.getLocation().getZ() + "," + entity.getLocation().getWorld().getName());
((TaskScriptContainer) ((Script) scriptEntry.getObject("script")).
getContainer()).setSpeed(new Duration(0))
.runTaskScript(scriptEntry.getPlayer(), scriptEntry.getNPC(), context);
}
}
}
};
task.runTaskTimer(denizen, 0, 2);
}
}