-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
SentinelItemHelper.java
461 lines (426 loc) · 15.2 KB
/
SentinelItemHelper.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package org.mcmonkey.sentinel;
import net.citizensnpcs.api.trait.trait.Inventory;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.mcmonkey.sentinel.targeting.SentinelTarget;
import org.mcmonkey.sentinel.utilities.SentinelVersionCompat;
/**
* Helper for item management.
*/
public class SentinelItemHelper extends SentinelHelperObject {
/**
* Gets the correct ArrowItem type for the NPC based on inventory items (can be null if the NPC needs ammo but has none).
*/
public ItemStack getArrow() {
if (!getNPC().hasTrait(Inventory.class)) {
return sentinel.needsAmmo ? null : new ItemStack(Material.ARROW, 1);
}
Inventory inv = getNPC().getOrAddTrait(Inventory.class);
ItemStack[] items = inv.getContents();
for (ItemStack item : items) {
if (item != null) {
Material mat = item.getType();
if (mat == Material.ARROW
|| (SentinelVersionCompat.v1_9 && (mat == Material.TIPPED_ARROW || mat == Material.SPECTRAL_ARROW))
|| (SentinelVersionCompat.v1_14 && mat == Material.FIREWORK_ROCKET)) {
return item.clone();
}
}
}
return sentinel.needsAmmo ? null : new ItemStack(Material.ARROW, 1);
}
/**
* Reduces the durability of the NPC's held item.
*/
public void reduceDurability() {
ItemStack toSet;
ItemStack item = getHeldItem();
if (item != null && item.getType() != Material.AIR) {
if (item.getDurability() >= item.getType().getMaxDurability() - 1) {
toSet = null;
}
else {
item.setDurability((short) (item.getDurability() + 1));
toSet = item;
}
if (getLivingEntity().getEquipment() == null) {
return;
}
if (SentinelVersionCompat.v1_9) {
getLivingEntity().getEquipment().setItemInMainHand(toSet);
}
else {
getLivingEntity().getEquipment().setItemInHand(toSet);
}
}
}
/**
* Takes an arrow from the NPC's inventory.
*/
public void takeArrow() {
if (!getNPC().hasTrait(Inventory.class)) {
return;
}
Inventory inv = getNPC().getOrAddTrait(Inventory.class);
ItemStack[] items = inv.getContents();
for (int i = 0; i < items.length; i++) {
ItemStack item = items[i];
if (item != null) {
Material mat = item.getType();
if (mat == Material.ARROW
|| (SentinelVersionCompat.v1_9 && (mat == Material.TIPPED_ARROW || mat == Material.SPECTRAL_ARROW))
|| (SentinelVersionCompat.v1_14 && mat == Material.FIREWORK_ROCKET)) {
if (item.getAmount() > 1) {
item.setAmount(item.getAmount() - 1);
items[i] = item;
inv.setContents(items);
return;
}
else {
items[i] = null;
inv.setContents(items);
return;
}
}
}
}
}
/**
* Takes one item from the NPC's held items (for consumables).
*/
public void takeOne() {
ItemStack toSet;
ItemStack item = getHeldItem();
if (item != null && item.getType() != Material.AIR) {
if (item.getAmount() > 1) {
item.setAmount(item.getAmount() - 1);
toSet = item;
}
else {
toSet = null;
}
if (getLivingEntity().getEquipment() == null) {
return;
}
if (SentinelVersionCompat.v1_9) {
getLivingEntity().getEquipment().setItemInMainHand(toSet);
}
else {
getLivingEntity().getEquipment().setItemInHand(toSet);
}
}
}
/**
* Grabs the next item for an NPC to use and moves it into the NPC's hand.
*/
public void grabNextItem() {
if (!getNPC().hasTrait(Inventory.class)) {
return;
}
Inventory inv = getNPC().getOrAddTrait(Inventory.class);
ItemStack[] items = inv.getContents();
ItemStack held = items[0];
if (held != null && held.getType() != Material.AIR) {
return;
}
for (int i = 0; i < items.length; i++) {
ItemStack item = items[i];
if (item != null) {
item = item.clone();
Material mat = item.getType();
if (SentinelVersionCompat.isWeapon(mat)) {
if (item.getAmount() > 1) {
item.setAmount(item.getAmount() - 1);
items[i] = item;
items[0] = item.clone();
items[0].setAmount(1);
inv.setContents(items);
item = item.clone();
item.setAmount(1);
return;
}
else {
items[i] = new ItemStack(Material.AIR);
items[0] = item.clone();
inv.setContents(items);
return;
}
}
}
}
}
/**
* Swaps the NPC to a ranged weapon if possible.
*/
public void swapToRanged() {
if (!getNPC().isSpawned() || !getNPC().hasTrait(Inventory.class)) {
return;
}
Inventory inv = getNPC().getOrAddTrait(Inventory.class);
ItemStack[] items = inv.getContents();
ItemStack held = items[0] == null ? null : items[0].clone();
if (isRanged(held)) {
return;
}
for (int i = 0; i < items.length; i++) {
if (sentinel.getLivingEntity() instanceof Player && i >= 36 && i <= 39) {
// Patch for armor, which is "in the inventory" but not really tracked through it
continue;
}
if (items[i] != null && items[i].getType() != Material.AIR && isRanged(items[i])) {
items[0] = items[i].clone();
items[i] = held == null ? null : held.clone();
inv.setContents(items);
if (sentinel.getLivingEntity() instanceof Player && i == 40) {
// Patch for offhand, which is "in the inventory" but not really tracked through it
sentinel.getLivingEntity().getEquipment().setItemInOffHand(items[i]);
}
return;
}
}
}
/**
* Swaps the NPC to a melee weapon if possible.
*/
public void swapToMelee() {
if (!getNPC().isSpawned() || !getNPC().hasTrait(Inventory.class)) {
return;
}
Inventory inv = getNPC().getOrAddTrait(Inventory.class);
ItemStack[] items = inv.getContents();
ItemStack held = items[0] == null ? null : items[0].clone();
if (!isRanged(held)) {
return;
}
for (int i = 0; i < items.length; i++) {
if (sentinel.getLivingEntity() instanceof Player && i >= 36 && i <= 39) {
// Patch for armor, which is "in the inventory" but not really tracked through it
continue;
}
if (items[i] != null && items[i].getType() != Material.AIR && !isRanged(items[i])) {
items[0] = items[i].clone();
items[i] = held == null ? null : held.clone();
inv.setContents(items);
if (sentinel.getLivingEntity() instanceof Player && i == 40) {
// Patch for offhand, which is "in the inventory" but not really tracked through it
sentinel.getLivingEntity().getEquipment().setItemInOffHand(items[i]);
}
return;
}
}
}
/**
* Returns whether the NPC is holding a shield in its offhand.
*/
public boolean hasShield() {
if (SentinelVersionCompat.v1_9) {
ItemStack item = SentinelUtilities.getOffhandItem(sentinel.getLivingEntity());
return item != null && item.getType() == Material.SHIELD;
}
return false;
}
/**
* Returns whether the NPC is holding a ranged weapon.
*/
public boolean isRanged() {
return isRanged(getHeldItem());
}
/**
* Returns whether the NPC is holding a ranged weapon.
*/
public boolean isRanged(ItemStack item) {
return usesBow(item)
|| usesFireball(item)
|| usesSnowball(item)
|| usesLightning(item)
|| usesEgg(item)
|| usesPearl(item)
|| usesWitherSkull(item)
|| usesTrident(item)
|| usesSpectral(item)
|| usesPotion(item);
}
/**
* Processes weapon redirection for an item, returning the redirected item (or an unchanged one).
*/
public ItemStack autoRedirect(ItemStack stack) {
if (stack == null) {
return null;
}
String redirect = sentinel.weaponRedirects.get(stack.getType().name().toLowerCase());
if (redirect == null) {
return stack;
}
Material mat = Material.valueOf(redirect.toUpperCase());
ItemStack newStack = stack.clone();
newStack.setType(mat);
return newStack;
}
/**
* Returns the item held by an NPC.
*/
public ItemStack getHeldItem() {
if (!getNPC().isSpawned()) {
return null;
}
ItemStack stack = SentinelUtilities.getHeldItem(getLivingEntity());
if (stack != null && stack.getType() != Material.AIR) {
return autoRedirect(stack);
}
if (getNPC().hasTrait(Inventory.class)) {
// Note: this allows entities that don't normally have equipment to still 'hold' weapons (eg a cow can hold a bow)
return autoRedirect(getNPC().getOrAddTrait(Inventory.class).getContents()[0]);
}
return null;
}
/**
* Returns whether the NPC is using a bow item.
*/
public boolean usesBow() {
return usesBow(getHeldItem());
}
/**
* Returns whether the NPC is using a bow item.
*/
public boolean usesBow(ItemStack it) {
if (it == null) {
return false;
}
if (SentinelVersionCompat.v1_14) {
if (it.getType() == Material.CROSSBOW && getArrow() != null) {
return true;
}
}
return it.getType() == Material.BOW && getArrow() != null;
}
/**
* Returns whether the NPC is using a fireball item.
*/
public boolean usesFireball() {
return usesFireball(getHeldItem());
}
/**
* Returns whether the NPC is using a fireball item.
*/
public boolean usesFireball(ItemStack it) {
return it != null && it.getType() == SentinelVersionCompat.MATERIAL_BLAZE_ROD;
}
/**
* Returns whether the NPC is using a snowball item.
*/
public boolean usesSnowball() {
return usesSnowball(getHeldItem());
}
/**
* Returns whether the NPC is using a snowball item.
*/
public boolean usesSnowball(ItemStack it) {
return it != null && it.getType() == SentinelVersionCompat.MATERIAL_SNOW_BALL;
}
/**
* Returns whether the NPC is using a lightning-attack item.
*/
public boolean usesLightning() {
return usesLightning(getHeldItem());
}
/**
* Returns whether the NPC is using a lightning-attack item.
*/
public boolean usesLightning(ItemStack it) {
return it != null && it.getType() == SentinelVersionCompat.MATERIAL_NETHER_STAR;
}
/**
* Returns whether the NPC is using an egg item.
*/
public boolean usesEgg() {
return usesEgg(getHeldItem());
}
/**
* Returns whether the NPC is using an egg item.
*/
public boolean usesEgg(ItemStack it) {
return it != null && it.getType() == Material.EGG;
}
/**
* Returns whether the NPC is using a pearl item.
*/
public boolean usesPearl() {
return usesPearl(getHeldItem());
}
/**
* Returns whether the NPC is using a pearl item.
*/
public boolean usesPearl(ItemStack it) {
return it != null && it.getType() == Material.ENDER_PEARL;
}
/**
* Returns whether the NPC is using a wither-skull item.
*/
public boolean usesWitherSkull() {
return usesWitherSkull(getHeldItem());
}
/**
* Returns whether the NPC is using a wither-skull item.
*/
public boolean usesWitherSkull(ItemStack it) {
if (!SentinelPlugin.instance.canUseSkull) {
return false;
}
return it != null && SentinelVersionCompat.SKULL_MATERIALS.contains(it.getType());
}
/**
* Returns whether the NPC is using a trident item.
*/
public boolean usesTrident() {
return usesTrident(getHeldItem());
}
/**
* Returns whether the NPC is using a trident item.
*/
public boolean usesTrident(ItemStack it) {
if (!SentinelVersionCompat.v1_13) {
return false;
}
return it != null && it.getType() == Material.TRIDENT;
}
/**
* Returns whether the NPC is using a spectral-effect-attack item.
*/
public boolean usesSpectral() {
return usesSpectral(getHeldItem());
}
/**
* Returns whether the NPC is using a spectral-effect-attack item.
*/
public boolean usesSpectral(ItemStack it) {
if (!SentinelVersionCompat.v1_10) {
return false;
}
return it != null && it.getType() == Material.SPECTRAL_ARROW;
}
/**
* Returns whether the NPC is using a potion item.
*/
public boolean usesPotion() {
return usesPotion(getHeldItem());
}
/**
* Returns whether the NPC is using a potion item.
*/
public boolean usesPotion(ItemStack it) {
return it != null && SentinelVersionCompat.POTION_MATERIALS.contains(it.getType());
}
/**
* Returns whether the NPC can take durability from the held item.
*/
public boolean shouldTakeDura() {
ItemStack it = getHeldItem();
if (it == null) {
return false;
}
Material type = it.getType();
return SentinelVersionCompat.BOW_MATERIALS.contains(type) || SentinelVersionCompat.SWORD_MATERIALS.contains(type)
|| SentinelVersionCompat.PICKAXE_MATERIALS.contains(type) || SentinelVersionCompat.AXE_MATERIALS.contains(type);
}
}