-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
NPCLivingAbstract.java
218 lines (185 loc) · 7.87 KB
/
NPCLivingAbstract.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
package org.inventivetalent.npclib.npc.living;
import com.google.common.base.Predicate;
import org.bukkit.Bukkit;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.inventory.ItemStack;
import org.inventivetalent.npclib.ObjectContainer;
import org.inventivetalent.npclib.Reflection;
import org.inventivetalent.npclib.SuperSwitch;
import org.inventivetalent.npclib.entity.living.NPCEntityLiving;
import org.inventivetalent.npclib.equipment.EquipmentSlot;
import org.inventivetalent.npclib.event.NPCDeathEvent;
import org.inventivetalent.npclib.event.path.NPCPathFinishEvent;
import org.inventivetalent.npclib.npc.NPCAbstract;
import org.inventivetalent.npclib.path.AStarPathfinder;
import org.inventivetalent.npclib.path.PathfinderAbstract;
import org.inventivetalent.npclib.watcher.Watch;
import org.inventivetalent.reflection.minecraft.Minecraft;
import org.inventivetalent.reflection.resolver.FieldResolver;
import org.inventivetalent.reflection.resolver.MethodResolver;
import org.inventivetalent.reflection.resolver.ResolverQuery;
import org.inventivetalent.vectors.d3.Vector3DDouble;
import javax.annotation.Nullable;
import java.lang.reflect.Constructor;
public abstract class NPCLivingAbstract<N extends NPCEntityLiving, B extends LivingEntity> extends NPCAbstract<N, B> {
// The static code gets run once, saves resources.
protected static FieldResolver entityLivingFieldResolver = new FieldResolver(Reflection.nmsClassResolver.resolveSilent("EntityLiving"));
protected static MethodResolver entityLivingMethodResolver = new MethodResolver(Reflection.nmsClassResolver.resolveSilent("EntityLiving"));
private PathfinderAbstract pathfinder;
public NPCLivingAbstract(N npcEntity) {
super(npcEntity);
}
@Watch("void die(DamageSource)")
public void onDie(ObjectContainer<Object> damageSource, SuperSwitch superSwitch) {
String damageName = Reflection.getDamageSourceName(damageSource.value);
EntityDamageEvent.DamageCause cause = Reflection.damageSourceToCause(damageName);
NPCDeathEvent event = new NPCDeathEvent(this, damageName, cause);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
superSwitch.setCancelled(true);
}
}
@Watch("void g(float,float)")
public void onHeadingMove(ObjectContainer<Float> strafe, ObjectContainer<Float> forward, SuperSwitch superSwitch) {
// System.out.println("onHeadingMove -> NPCLivingAbstract");
//TODO: NPCMoveEvent...
}
/**
* Moves the entity forward (on the current heading)
*
* @param forward forward movement amount
* @param strafe strafe movement amount
*/
public void moveForward(float forward, float strafe) {
invokeEntityLivingMethod("g", new Class[] {
float.class,
float.class }, new Object[] {
strafe,
forward });
}
public void setYaw(float yaw) {
yaw = clampYaw(yaw);
setBodyYaw(yaw);
setHeadYaw(yaw);
}
public void setBodyYaw(float yaw) {
getNpcEntity().setYaw(yaw);
if(Minecraft.VERSION.newerThan(Minecraft.Version.v1_9_R1)) {
// The method is relatively consistent between versions.
invokeEntityLivingMethod("i", new Class[] {float.class}, new Object[] {yaw});
} else {
// 1.8. The field is consistent between all 1.8 versions, however on 1.8.0 it is shared with the head value.
entityLivingFieldResolver.resolveWrapper("aI").set(getNpcEntity(), yaw);
}
}
public void setHeadYaw(float yaw) {
// Find it below getHeadRotation() in NMS class EntityLiving if it changes.
if(Minecraft.VERSION.newerThan(Minecraft.Version.v1_9_R1)) {
// The method is relatively consistent between versions.
invokeEntityLivingMethod("h", new Class[] {float.class}, new Object[] {yaw});
} else {
// 1.8
invokeEntityLivingMethod("f", new Class[] {float.class}, new Object[] {yaw});
}
}
public void setPitch(float pitch) {
getNpcEntity().setPitch(pitch);
}
protected float clampYaw(float yaw) {
while (yaw < -180.0F) {
yaw += 360.0F;
}
while (yaw >= 180.0F) {
yaw -= 360.0F;
}
return yaw;
}
public void lookAt(Vector3DDouble vector) {
double dx = vector.getX() - getBukkitEntity().getEyeLocation().getX();
double dy = vector.getY() - getBukkitEntity().getEyeLocation().getY();
double dz = vector.getZ() - getBukkitEntity().getEyeLocation().getZ();
double xzd = Math.sqrt(dx * dx + dz * dz);
double yd = Math.sqrt(xzd * xzd + dy * dy);
double yaw = Math.toDegrees(Math.atan2(dz, dx)) - 90.0D;
double pitch = -Math.toDegrees(Math.atan2(dy, yd));
setYaw((float) yaw);
setPitch((float) pitch);
}
@Override
public void tickAI() {
super.tickAI();
if (this.pathfinder != null) {
this.pathfinder.tick();
if (this.pathfinder.isFinished()) {
this.pathfinder = null;
Bukkit.getPluginManager().callEvent(new NPCPathFinishEvent(this));
}
}
}
public void pathfindTo(Vector3DDouble target, double speed) {
this.pathfinder = new AStarPathfinder(target, speed, 128);
//noinspection unchecked
this.pathfinder.setNpc(this);
this.pathfinder.find();
}
public void moveWithHeading(float strafeMotion, float forwardMotion) {
try {
entityLivingMethodResolver.resolve(new ResolverQuery("g", float.class, float.class)).invoke(getNpcEntity(), strafeMotion, forwardMotion);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
public void refreshEquipment() {
if (Minecraft.VERSION.newerThan(Minecraft.Version.v1_9_R1)) {
refreshSlot(EquipmentSlot.MAIN_HAND, getBukkitEntity().getEquipment().getItemInMainHand());
refreshSlot(EquipmentSlot.OFF_HAND, getBukkitEntity().getEquipment().getItemInOffHand());
} else {
refreshSlot(EquipmentSlot.HAND, getBukkitEntity().getEquipment().getItemInHand());
}
refreshSlot(EquipmentSlot.HEAD, getBukkitEntity().getEquipment().getHelmet());
refreshSlot(EquipmentSlot.CHEST, getBukkitEntity().getEquipment().getChestplate());
refreshSlot(EquipmentSlot.LEGS, getBukkitEntity().getEquipment().getLeggings());
refreshSlot(EquipmentSlot.FEET, getBukkitEntity().getEquipment().getBoots());
}
protected void refreshSlot(EquipmentSlot slot, ItemStack itemStack) {
try {
Object nmsItemStack = Reflection.obcClassResolver.resolve("inventory.CraftItemStack").getMethod("asNMSCopy", ItemStack.class).invoke(null, itemStack);
Object nmsSlot = slot.toNMS();
if (nmsSlot == null) { return; }
Class<?> packetClass = Reflection.nmsClassResolver.resolve("PacketPlayOutEntityEquipment");
Class<?> itemClass = Reflection.nmsClassResolver.resolve("ItemStack");
Constructor<?> constructor;
if (Minecraft.VERSION.newerThan(Minecraft.Version.v1_9_R1)) {
constructor = packetClass.getConstructor(int.class, Reflection.nmsClassResolver.resolve("EnumItemSlot"), itemClass);
} else if (Minecraft.VERSION.newerThan(Minecraft.Version.v1_8_R1)) {
constructor = packetClass.getConstructor(int.class, int.class, itemClass);
} else {
return;
}
final Object packet = constructor.newInstance(getBukkitEntity().getEntityId(), nmsSlot, nmsItemStack);
updateNearby(32, new Predicate<Player>() {
@Override
public boolean apply(@Nullable Player player) {
sendPacket(player, packet);
return false;
}
});
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public Object getEntityLivingField(String field) {
return entityLivingFieldResolver.resolveWrapper(field).get(getNpcEntity());
}
public void setEntityLivingField(String field, Object value) {
entityLivingFieldResolver.resolveWrapper(field).set(getNpcEntity(), value);
}
public Object invokeEntityLivingMethod(String method, Class<?>[] types, Object[] args) {
return entityLivingMethodResolver.resolveWrapper(new ResolverQuery(method, types)).invoke(getNpcEntity(), args);
}
public Object invokeEntityLivingMethod(String method, Object... args) {
return entityLivingMethodResolver.resolveWrapper(method).invoke(getNpcEntity(), args);
}
}