-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
SentinelWeaponHelper.java
187 lines (175 loc) · 8.22 KB
/
SentinelWeaponHelper.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
package org.mcmonkey.sentinel;
import net.citizensnpcs.api.trait.trait.Inventory;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.*;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionType;
import org.bukkit.util.Vector;
import java.util.HashMap;
/**
* Helper for weapon management, particular attacking.
*/
public class SentinelWeaponHelper extends SentinelHelperObject {
/**
* Fires a potion from the NPC at a target.
*/
public void firePotion(ItemStack potion, Location target, Vector lead) {
sentinel.stats_potionsThrown++;
HashMap.SimpleEntry<Location, Vector> start = sentinel.getLaunchDetail(target, lead);
Entity entpotion;
if (SentinelTarget.v1_9) {
entpotion = start.getKey().getWorld().spawnEntity(start.getKey(),
potion.getType() == Material.SPLASH_POTION ? EntityType.SPLASH_POTION : EntityType.LINGERING_POTION);
}
else {
entpotion = start.getKey().getWorld().spawnEntity(start.getKey(), EntityType.SPLASH_POTION);
}
((ThrownPotion) entpotion).setShooter(getLivingEntity());
((ThrownPotion) entpotion).setItem(potion);
entpotion.setVelocity(sentinel.fixForAcc(start.getValue()));
sentinel.swingWeapon();
}
/**
* Fires an arrow from the NPC at a target.
*/
public void fireArrow(ItemStack type, Location target, Vector lead) {
HashMap.SimpleEntry<Location, Vector> start = sentinel.getLaunchDetail(target, lead);
if (start == null || start.getKey() == null) {
return;
}
sentinel.stats_arrowsFired++;
Entity arrow;
if (SentinelTarget.v1_9) {
arrow = start.getKey().getWorld().spawnEntity(start.getKey(),
type.getType() == Material.SPECTRAL_ARROW ? EntityType.SPECTRAL_ARROW :
(type.getType() == Material.TIPPED_ARROW ? EntityType.TIPPED_ARROW : EntityType.ARROW));
((Projectile) arrow).setShooter(getLivingEntity());
if (arrow instanceof TippedArrow) {
PotionData data = ((PotionMeta) type.getItemMeta()).getBasePotionData();
if (data.getType() == null || data.getType() == PotionType.UNCRAFTABLE) {
// TODO: Perhaps a **single** warning?
}
else {
((TippedArrow) arrow).setBasePotionData(data);
for (PotionEffect effect : ((PotionMeta) type.getItemMeta()).getCustomEffects()) {
((TippedArrow) arrow).addCustomEffect(effect, true);
}
}
}
}
else {
arrow = start.getKey().getWorld().spawnEntity(start.getKey(), EntityType.ARROW);
((Projectile) arrow).setShooter(getLivingEntity());
}
arrow.setVelocity(sentinel.fixForAcc(start.getValue()));
if (getNPC().getTrait(Inventory.class).getContents()[0].containsEnchantment(Enchantment.ARROW_FIRE)) {
arrow.setFireTicks(10000);
}
sentinel.useItem();
}
/**
* Fires a snowball from the NPC at a target.
*/
public void fireSnowball(Location target) {
sentinel.swingWeapon();
sentinel.stats_snowballsThrown++;
sentinel.faceLocation(target);
Vector forward = getLivingEntity().getEyeLocation().getDirection();
Location spawnAt = getLivingEntity().getEyeLocation().clone().add(forward.clone().multiply(sentinel.firingMinimumRange()));
Entity ent = spawnAt.getWorld().spawnEntity(spawnAt, EntityType.SNOWBALL);
((Projectile) ent).setShooter(getLivingEntity());
ent.setVelocity(sentinel.fixForAcc(target.clone().subtract(spawnAt).toVector().normalize().multiply(2.0))); // TODO: Fiddle with '2.0'.
}
/**
* Fires an egg from the NPC at a target.
*/
public void fireEgg(Location target) {
sentinel.swingWeapon();
sentinel.stats_eggsThrown++;
sentinel.faceLocation(target);
Vector forward = getLivingEntity().getEyeLocation().getDirection();
Location spawnAt = getLivingEntity().getEyeLocation().clone().add(forward.clone().multiply(sentinel.firingMinimumRange()));
Entity ent = spawnAt.getWorld().spawnEntity(spawnAt, EntityType.EGG);
((Projectile) ent).setShooter(getLivingEntity());
ent.setVelocity(sentinel.fixForAcc(target.clone().subtract(spawnAt).toVector().normalize().multiply(2.0))); // TODO: Fiddle with '2.0'.
}
/**
* Fires a pearl from the NPC at a target.
*/
public void firePearl(LivingEntity target) {
sentinel.swingWeapon();
sentinel.faceLocation(target.getEyeLocation());
// TODO: Maybe require entity is-on-ground?
sentinel.stats_pearlsUsed++;
target.setVelocity(target.getVelocity().add(new Vector(0, sentinel.getDamage(), 0)));
}
/**
* Fires a fireballs from the NPC at a target.
*/
public void fireFireball(Location target) {
sentinel.swingWeapon();
sentinel.stats_fireballsFired++;
sentinel.faceLocation(target);
Vector forward = getLivingEntity().getEyeLocation().getDirection();
Location spawnAt = getLivingEntity().getEyeLocation().clone().add(forward.clone().multiply(sentinel.firingMinimumRange()));
Entity ent = spawnAt.getWorld().spawnEntity(spawnAt, EntityType.SMALL_FIREBALL);
((Projectile) ent).setShooter(getLivingEntity());
ent.setVelocity(sentinel.fixForAcc(target.clone().subtract(spawnAt).toVector().normalize().multiply(4))); // TODO: Fiddle with '4'.
}
/**
* Fires a skull from the NPC at a target.
*/
public void fireSkull(Location target) {
sentinel.swingWeapon();
sentinel.stats_skullsThrown++;
sentinel.faceLocation(target);
Vector forward = getLivingEntity().getEyeLocation().getDirection();
Location spawnAt = getLivingEntity().getEyeLocation().clone().add(forward.clone().multiply(sentinel.firingMinimumRange()));
Entity ent = spawnAt.getWorld().spawnEntity(spawnAt, EntityType.WITHER_SKULL);
((Projectile) ent).setShooter(getLivingEntity());
ent.setVelocity(sentinel.fixForAcc(target.clone().subtract(spawnAt).toVector().normalize().multiply(4))); // TODO: Fiddle with '4'.
}
/**
* Makes an NPC punch a target.
*/
public void punch(LivingEntity entity) {
sentinel.faceLocation(entity.getLocation());
sentinel.swingWeapon();
sentinel.stats_punches++;
if (SentinelPlugin.instance.workaroundDamage) {
if (SentinelPlugin.debugMe) {
debug("workaround damage value at " + sentinel.getDamage() + " yields "
+ ((sentinel.getDamage() * (1.0 - sentinel.getArmor(entity)))));
}
entity.damage(sentinel.getDamage() * (1.0 - sentinel.getArmor(entity)));
knockback(entity);
if (!sentinel.enemyDrops) {
sentinel.needsDropsClear.put(entity.getUniqueId(), true);
}
}
else {
if (SentinelPlugin.debugMe) {
debug("Punch/natural for " + sentinel.getDamage());
}
entity.damage(sentinel.getDamage(), getLivingEntity());
}
}
/**
* Knocks a target back from damage received (for hacked-in damage applications when required by config).
*/
public void knockback(LivingEntity entity) {
Vector relative = entity.getLocation().toVector().subtract(getLivingEntity().getLocation().toVector());
relative = relative.normalize();
relative.setY(0.75);
relative.multiply(0.5 / Math.max(1.0, entity.getVelocity().length()));
entity.setVelocity(entity.getVelocity().multiply(0.25).add(relative));
if (SentinelPlugin.debugMe) {
debug("applied knockback velocity adder of " + relative);
}
}
}