-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
SentinelAttackHelper.java
340 lines (330 loc) · 12 KB
/
SentinelAttackHelper.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
package org.mcmonkey.sentinel;
import net.citizensnpcs.api.ai.TargetType;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.entity.LivingEntity;
import org.bukkit.inventory.ItemStack;
import org.mcmonkey.sentinel.events.SentinelAttackEvent;
import org.mcmonkey.sentinel.targeting.SentinelTarget;
/**
* Helper for Sentinel NPCs attacking targets.
*/
public class SentinelAttackHelper extends SentinelHelperObject {
/**
* Causes the NPC to chase a target.
*/
public void chase(LivingEntity entity) {
if (getNPC().getNavigator().getTargetType() == TargetType.LOCATION
&& getNPC().getNavigator().getTargetAsLocation() != null
&& ((getNPC().getNavigator().getTargetAsLocation().getWorld().equals(entity.getWorld())
&& getNPC().getNavigator().getTargetAsLocation().distanceSquared(entity.getLocation()) < 2 * 2))) {
return;
}
sentinel.cleverTicks = 0;
sentinel.chasing = entity;
sentinel.chased = true;
sentinel.needsSafeReturn = true;
if (getNPC().getNavigator().getTargetType() == TargetType.ENTITY
&& SentinelUtilities.getTargetFor(getNPC().getNavigator().getEntityTarget()).getUniqueId().equals(entity.getUniqueId())) {
return;
}
/*
Location goal = entity.getLocation().clone().add(entity.getVelocity().clone());
npc.getNavigator().setTarget(goal);
bunny_goal = goal;
*/
if (SentinelPlugin.instance.workaroundEntityChasePathfinder) {
final Location entityEyeLoc = entity.getEyeLocation();
getNPC().getNavigator().setTarget(entity.getLocation());
getNPC().getNavigator().getLocalParameters().lookAtFunction(n -> entityEyeLoc);
}
else {
getNPC().getNavigator().setTarget(entity, false);
}
getNPC().getNavigator().getLocalParameters().stuckAction(null);
sentinel.autoSpeedModifier();
}
/**
* Repeats the last chase instruction (to ensure the NPC keeps going for a target).
*/
public void rechase() {
if (sentinel.chasing != null) {
chase(sentinel.chasing);
}
}
/**
* Returns a boolean indicating whether the NPC sees a threat from the input entity (for use with shield blocking).
*/
public boolean seesThreatFrom(LivingEntity entity) {
if (!targetingHelper.canSee(entity)) {
return false;
}
if (!SentinelUtilities.isLookingTowards(getLivingEntity().getEyeLocation(), entity.getLocation(), 60, 60)) {
return false;
}
double dist = getLivingEntity().getEyeLocation().distanceSquared(entity.getEyeLocation());
if (dist < 5 * 5) {
return true;
}
else if (SentinelTarget.isRangedWeapon(SentinelUtilities.getHeldItem(entity))) {
return true;
}
else {
return false;
}
}
/**
* Causes the entity to prepare to defend against a dangerous opponent (if it sees the threat).
* This will raise the NPC's shield (if it has one).
*/
public void tryDefendFrom(LivingEntity entity) {
if (!itemHelper.hasShield()) {
return;
}
if (seesThreatFrom(entity)) {
sentinel.startBlocking();
}
else {
sentinel.stopBlocking();
}
}
/**
* Causes the NPC to attempt an attack on a target.
* Returns whether any attack occurred.
*/
public boolean tryAttack(LivingEntity target) {
if (tryAttackInternal(target)) {
return true;
}
LivingEntity quickTarget = targetingHelper.findQuickMeleeTarget();
if (quickTarget != null) {
if (itemHelper.isRanged()) {
if (!sentinel.autoswitch) {
return false;
}
itemHelper.swapToMelee();
if (itemHelper.isRanged()) {
return false;
}
}
if (tryAttackInternal(quickTarget)) {
chase(target);
return true;
}
chase(target);
}
return false;
}
/**
* Pre-calculation for ranged attacks.
* Returns 'true' when the attack should be cancelled.
*/
public boolean rangedPreCalculation(LivingEntity entity) {
double dist = getLivingEntity().getEyeLocation().distanceSquared(entity.getEyeLocation());
if (dist < sentinel.projectileRange * sentinel.projectileRange && targetingHelper.canSee(entity)) {
if (sentinel.timeSinceAttack < sentinel.attackRateRanged) {
if (SentinelPlugin.debugMe) {
debug("tryAttack refused, timeSinceAttack");
}
if (sentinel.rangedChase) {
rechase();
}
return true;
}
sentinel.timeSinceAttack = 0;
return false;
}
else if (sentinel.rangedChase) {
if (SentinelPlugin.debugMe) {
debug("tryAttack refused, range or visibility");
}
chase(entity);
return true;
}
return true;
}
/**
* Post-calculation for ammo handling.
*/
public void rangedAmmoCalculation() {
if (sentinel.needsAmmo) {
itemHelper.takeOne();
itemHelper.grabNextItem();
}
}
/**
* Internal attack attempt logic.
*/
public boolean tryAttackInternal(LivingEntity entity) {
if (!entity.getWorld().equals(getLivingEntity().getWorld())) {
return false;
}
if (!getLivingEntity().hasLineOfSight(entity)) {
return false;
}
// TODO: Simplify this code!
sentinel.stats_attackAttempts++;
double dist = getLivingEntity().getEyeLocation().distanceSquared(entity.getEyeLocation());
if (SentinelPlugin.debugMe) {
debug("tryAttack at range " + (((int) (Math.sqrt(dist) * 0.1)) * 10));
}
if (sentinel.autoswitch && dist > sentinel.reach * sentinel.reach) {
itemHelper.swapToRanged();
}
else if (sentinel.autoswitch && dist < sentinel.reach * sentinel.reach) {
itemHelper.swapToMelee();
}
sentinel.chasing = entity;
SentinelAttackEvent sat = new SentinelAttackEvent(getNPC());
Bukkit.getPluginManager().callEvent(sat);
if (sat.isCancelled()) {
if (SentinelPlugin.debugMe) {
debug("tryAttack refused, event cancellation");
}
return false;
}
targetingHelper.addTarget(entity.getUniqueId());
for (SentinelIntegration si : SentinelPlugin.integrations) {
if (si.tryAttack(sentinel, entity)) {
return true;
}
}
if (itemHelper.usesBow()) {
if (rangedPreCalculation(entity)) {
return false;
}
ItemStack item = itemHelper.getArrow();
if (item != null) {
weaponHelper.fireArrow(item, entity.getEyeLocation(), entity.getVelocity());
if (sentinel.needsAmmo) {
itemHelper.reduceDurability();
itemHelper.takeArrow();
itemHelper.grabNextItem();
}
return true;
}
}
else if (itemHelper.usesSnowball()) {
if (rangedPreCalculation(entity)) {
return false;
}
weaponHelper.fireSnowball(entity.getEyeLocation());
rangedAmmoCalculation();
return true;
}
else if (itemHelper.usesTrident()) {
if (rangedPreCalculation(entity)) {
return false;
}
weaponHelper.fireTrident(entity.getEyeLocation());
rangedAmmoCalculation();
return true;
}
else if (itemHelper.usesPotion()) {
if (rangedPreCalculation(entity)) {
return false;
}
weaponHelper.firePotion(SentinelUtilities.getHeldItem(getLivingEntity()), entity.getEyeLocation(), entity.getVelocity());
rangedAmmoCalculation();
return true;
}
else if (itemHelper.usesEgg()) {
if (rangedPreCalculation(entity)) {
return false;
}
weaponHelper.fireEgg(entity.getEyeLocation());
rangedAmmoCalculation();
return true;
}
else if (itemHelper.usesPearl()) {
if (rangedPreCalculation(entity)) {
return false;
}
weaponHelper.firePearl(entity);
rangedAmmoCalculation();
return true;
}
else if (itemHelper.usesWitherSkull()) {
if (rangedPreCalculation(entity)) {
return false;
}
weaponHelper.fireSkull(entity.getEyeLocation());
rangedAmmoCalculation();
return true;
}
else if (itemHelper.usesFireball()) {
if (rangedPreCalculation(entity)) {
return false;
}
weaponHelper.fireFireball(entity.getEyeLocation());
rangedAmmoCalculation();
return true;
}
else if (itemHelper.usesLightning()) {
if (rangedPreCalculation(entity)) {
return false;
}
sentinel.swingWeapon();
entity.getWorld().strikeLightningEffect(entity.getLocation());
if (SentinelPlugin.debugMe) {
debug("Lightning hits for " + sentinel.getDamage(false));
}
entity.damage(sentinel.getDamage(false));
rangedAmmoCalculation();
return true;
}
else if (itemHelper.usesSpectral()) {
if (rangedPreCalculation(entity)) {
return false;
}
if (!entity.isGlowing()) {
sentinel.swingWeapon();
try {
Sound snd = SentinelPlugin.instance.spectralSound;
if (snd != null) {
entity.getWorld().playSound(entity.getLocation(), snd, 1f, 1f);
}
}
catch (Exception e) {
// Do nothing!
}
entity.setGlowing(true);
}
rangedAmmoCalculation();
return true;
}
else {
if (dist < sentinel.reach * sentinel.reach) {
if (sentinel.timeSinceAttack < sentinel.attackRate) {
if (SentinelPlugin.debugMe) {
debug("tryAttack refused, timeSinceAttack");
}
if (sentinel.closeChase) {
rechase();
}
return false;
}
sentinel.timeSinceAttack = 0;
// TODO: Damage sword if needed!
if (SentinelPlugin.debugMe) {
debug("tryAttack passed!");
}
weaponHelper.punch(entity);
if (sentinel.needsAmmo && itemHelper.shouldTakeDura()) {
itemHelper.reduceDurability();
itemHelper.grabNextItem();
}
return true;
}
else if (sentinel.closeChase) {
if (SentinelPlugin.debugMe) {
debug("tryAttack refused, range");
}
chase(entity);
return false;
}
}
return false;
}
}