-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
SentinelTrait.java
424 lines (371 loc) · 14.4 KB
/
SentinelTrait.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package org.mcmonkey.sentinel;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.util.PlayerAnimation;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.craftbukkit.v1_9_R1.Overridden;
import org.bukkit.entity.*;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
public class SentinelTrait extends Trait {
public SentinelTrait() {
super("sentinel");
}
@Persist("stats_ticksSpawned")
public long stats_ticksSpawned = 0;
@Persist("stats_timesSpawned")
public long stats_timesSpawned = 0;
@Persist("stats_arrowsFired")
public long stats_arrowsFired = 0;
@Persist("stats_potionsThrow")
public long stats_potionsThrown = 0;
@Persist("stats_fireballsFired")
public long stats_fireballsFired = 0;
@Persist("stats_punches")
public long stats_punches = 0;
@Persist("stats_attackAttempts")
public long stats_attackAttempts = 0;
@Persist("targets")
public HashSet<SentinelTarget> targets = new HashSet<SentinelTarget>();
@Persist("range")
public double range = 20;
@Persist("damage")
public double damage = -1;
@Persist("armor")
public double armor = -1;
@Persist("health")
public double health = 20;
@Persist("ignores")
public HashSet<SentinelTarget> ignores = new HashSet<SentinelTarget>();
@Persist("ranged_chase")
public boolean rangedChase = false;
@Persist("close_chase")
public boolean closeChase = true;
@Persist("invincible")
public boolean invincible = false;
@Persist("attackRate")
public int attackRate = 30;
@Override
public void onAttach() {
FileConfiguration config = SentinelPlugin.instance.getConfig();
attackRate = config.getInt("sentinel defaults.attack rate", 30);
rangedChase = config.getBoolean("sentinel defaults.ranged chase target", false);
closeChase = config.getBoolean("sentinel defaults.close chase target", true);
armor = config.getDouble("sentinel defaults.armor", -1);
damage = config.getDouble("sentinel defaults.damage", -1);
health = config.getDouble("sentinel defaults.health", 20);
if (npc.isSpawned()) {
getLivingEntity().setMaxHealth(health);
getLivingEntity().setHealth(health);
}
invincible = config.getBoolean("defaults.invincible", false);
npc.setProtected(invincible);
}
public void useItem() {
if (npc.isSpawned() && getLivingEntity() instanceof Player) {
PlayerAnimation.START_USE_MAINHAND_ITEM.play((Player) getLivingEntity());
BukkitRunnable runner = new BukkitRunnable() {
@Override
public void run() {
if (npc.isSpawned() && getLivingEntity() instanceof Player) {
PlayerAnimation.STOP_USE_ITEM.play((Player) getLivingEntity());
}
}
};
runner.runTaskLater(SentinelPlugin.instance, 10);
}
}
public void swingWeapon() {
if (npc.isSpawned() && getLivingEntity() instanceof Player) {
PlayerAnimation.ARM_SWING.play((Player) getLivingEntity());
}
}
public HashMap.SimpleEntry<Location, Vector> getLaunchDetail(Location target, Vector lead) {
double speed;
npc.faceLocation(target);
double angt = -1;
Location start = getLivingEntity().getEyeLocation().clone().add(getLivingEntity().getEyeLocation().getDirection());
for (speed = 20; speed <= 45; speed += 5) {
angt = SentinelUtilities.getArrowAngle(start, target, speed, 20);
if (!Double.isInfinite(angt)) {
break;
}
}
if (Double.isInfinite(angt)) {
return null;
}
double hangT = SentinelUtilities.hangtime(angt, speed, target.getY() - start.getY(), 20);
Location to = target.clone().add(lead.clone().multiply(hangT));
Vector relative = to.clone().subtract(start.toVector()).toVector();
double deltaXZ = Math.sqrt(relative.getX() * relative.getX() + relative.getZ() * relative.getZ());
if (deltaXZ == 0) {
deltaXZ = 0.1;
}
for (speed = 20; speed < 45; speed += 5) {
angt = SentinelUtilities.getArrowAngle(start, to, speed, 20);
if (!Double.isInfinite(angt)) {
break;
}
}
if (Double.isInfinite(angt)) {
return null;
}
relative.setY(Math.tan(angt) * deltaXZ);
relative = relative.normalize();
Vector normrel = relative.clone();
speed = speed + (1.188 * hangT * hangT);
relative = relative.multiply(speed / 20.0);
start.setDirection(normrel);
return new HashMap.SimpleEntry<Location, Vector>(start, relative);
}
public void firePotion(ItemStack potion, Location target, Vector lead) {
stats_potionsThrown++;
HashMap.SimpleEntry<Location, Vector> start = getLaunchDetail(target, lead);
Entity entpotion = start.getKey().getWorld().spawnEntity(start.getKey(),
potion.getType() == Material.SPLASH_POTION ? EntityType.SPLASH_POTION: EntityType.LINGERING_POTION);
((ThrownPotion) entpotion).setShooter(getLivingEntity());
((ThrownPotion) entpotion).setItem(potion);
entpotion.setVelocity(start.getValue());
swingWeapon();
}
public void fireArrow(Location target, Vector lead) {
stats_arrowsFired++;
HashMap.SimpleEntry<Location, Vector> start = getLaunchDetail(target, lead);
Entity arrow = start.getKey().getWorld().spawnEntity(start.getKey(), EntityType.ARROW);
((Projectile)arrow).setShooter(getLivingEntity());
arrow.setVelocity(start.getValue());
// TODO: Apply damage amount!
// TODO: Prevent pick up if needed!
useItem();
}
public void fireFireball(Location target) {
swingWeapon();
stats_fireballsFired++;
npc.faceLocation(target);
Vector forward = getLivingEntity().getEyeLocation().getDirection();
Location spawnAt = getLivingEntity().getEyeLocation().clone().add(forward.clone().multiply(2));
Entity ent = spawnAt.getWorld().spawnEntity(spawnAt, EntityType.SMALL_FIREBALL);
ent.setVelocity(target.clone().subtract(spawnAt).toVector().normalize().multiply(4)); // TODO: Fiddle with '4'.
// TODO: Apply damage amount!
}
public double getDamage() {
if (damage < 0) {
ItemStack weapon = getLivingEntity().getEquipment().getItemInMainHand();
double baseDamage = 1;
// TODO: Calculate damage!
return baseDamage;
}
return damage;
}
// TODO: use this value!
public double getArmor() {
if (armor < 0) {
double baseArmor = 0;
// TODO: Calculate armor!
return baseArmor;
}
return armor;
}
public void punch(LivingEntity entity) {
npc.faceLocation(entity.getLocation());
swingWeapon();
stats_punches++;
entity.damage(getDamage(), getLivingEntity());
}
public void chase(LivingEntity entity) {
npc.getNavigator().setTarget(entity.getLocation());
}
public void tryAttack(LivingEntity entity) {
stats_attackAttempts++;
if (usesBow()) {
if (canSee(entity)) {
if (timeSinceAttack < attackRate) {
return;
}
timeSinceAttack = 0;
// TODO: Consume ammo if needed!
fireArrow(entity.getEyeLocation(), entity.getVelocity());
}
else if (rangedChase) {
chase(entity);
}
}
else if (usesPotion()) {
double distsq = entity.getLocation().distanceSquared(getLivingEntity().getLocation());
if (canSee(entity)) {
if (timeSinceAttack < attackRate) {
return;
}
timeSinceAttack = 0;
// TODO: Consume ammo if needed!
firePotion(getLivingEntity().getEquipment().getItemInMainHand(),
entity.getEyeLocation(), entity.getVelocity());
}
else if (rangedChase) {
chase(entity);
}
}
else if (usesFireball()) {
if (canSee(entity)) {
if (timeSinceAttack < attackRate) {
return;
}
timeSinceAttack = 0;
// TODO: Consume ammo if needed!
fireFireball(entity.getEyeLocation());
}
else if (rangedChase) {
chase(entity);
}
}
else if (usesLightning()) {
if (canSee(entity)) {
if (timeSinceAttack < attackRate) {
return;
}
timeSinceAttack = 0;
// TODO: Consume ammo if needed!
swingWeapon();
entity.getWorld().strikeLightning(entity.getLocation()); // TODO: Audio?
}
else if (rangedChase) {
chase(entity);
}
}
else {
double distsq = entity.getLocation().distanceSquared(getLivingEntity().getLocation());
if (distsq < 3 * 3) {
if (timeSinceAttack < attackRate) {
return;
}
timeSinceAttack = 0;
// TODO: Damage sword if needed!
punch(entity);
}
else {
chase(entity);
}
}
}
public boolean canSee(LivingEntity entity) {
return getLivingEntity().hasLineOfSight(entity);
}
public LivingEntity getLivingEntity() {
// Not a good idea to turn a non-living NPC into a Sentinel for now.
return (LivingEntity) npc.getEntity();
}
public boolean isRanged() {
return usesBow() || usesFireball();
}
public boolean usesBow() {
return getLivingEntity().getEquipment().getItemInMainHand().getType() == Material.BOW;
}
public boolean usesFireball() {
return getLivingEntity().getEquipment().getItemInMainHand().getType() == Material.BLAZE_ROD;
}
public boolean usesLightning() {
return getLivingEntity().getEquipment().getItemInMainHand().getType() == Material.NETHER_STAR;
}
public boolean usesPotion() {
Material type = getLivingEntity().getEquipment().getItemInMainHand().getType();
return type == Material.SPLASH_POTION || type == Material.LINGERING_POTION;
}
public boolean shouldTarget(LivingEntity entity) {
return isTargeted(entity) && !isIgnored(entity);
}
public boolean isIgnored(LivingEntity entity) {
HashSet<SentinelTarget> possible = SentinelPlugin.entityToTargets.get(entity.getType());
for (SentinelTarget poss: possible) {
if (ignores.contains(poss)) {
return true;
}
}
return false;
}
public boolean isTargeted(LivingEntity entity) {
HashSet<SentinelTarget> possible = SentinelPlugin.entityToTargets.get(entity.getType());
for (SentinelTarget poss: possible) {
if (targets.contains(poss)) {
return true;
}
}
return false;
}
public int cTick = 0;
/**
* This method searches for the nearest targetable entity with direct line-of-sight.
* Failing a direct line of sight, the nearest entity in range at all will be chosen.
*/
public LivingEntity findBestTarget() {
double rangesquared = range * range;
Location pos = getLivingEntity().getEyeLocation();
LivingEntity closest = null;
for (LivingEntity ent: getLivingEntity().getWorld().getLivingEntities()) {
double dist = ent.getEyeLocation().distanceSquared(pos);
if (dist < rangesquared && shouldTarget(ent) && canSee(ent)) {
rangesquared = dist;
closest = ent;
}
}
if (closest == null) {
// NOTE: Possibly can be optimized by retrieving a list from the above logic?
for (LivingEntity ent: getLivingEntity().getWorld().getLivingEntities()) {
double dist = ent.getEyeLocation().distanceSquared(pos);
if (dist < rangesquared && shouldTarget(ent)) {
rangesquared = dist;
closest = ent;
}
}
}
return closest;
}
public long timeSinceAttack = 0;
public long timeSinceHeal = 0;
public void runUpdate() {
timeSinceAttack += SentinelPlugin.instance.tickRate;
timeSinceHeal += SentinelPlugin.instance.tickRate;
// TODO: HealRate
if (timeSinceHeal > 20 && getLivingEntity().getHealth() < health) {
getLivingEntity().setHealth(getLivingEntity().getHealth() + 0.5);
}
LivingEntity target = findBestTarget();
if (target == null) {
return;
}
tryAttack(target);
}
@Override
public void run() {
if (!npc.isSpawned()) {
return;
}
stats_ticksSpawned++;
cTick++;
if (cTick >= SentinelPlugin.instance.tickRate) {
cTick = 0;
runUpdate();
}
}
@Override
public void onSpawn() {
stats_timesSpawned++;
setHealth(health);
setInvincible(invincible);
}
public void setHealth(double heal) {
health = heal;
if (npc.isSpawned()) {
getLivingEntity().setMaxHealth(health);
}
}
public void setInvincible(boolean inv) {
invincible = inv;
npc.setProtected(invincible);
}
}