-
-
Notifications
You must be signed in to change notification settings - Fork 308
/
MobEntityController.java
60 lines (49 loc) · 2.09 KB
/
MobEntityController.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
package net.citizensnpcs.npc;
import java.lang.reflect.Constructor;
import java.util.Map;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.util.NMS;
import net.minecraft.server.v1_7_R1.World;
import org.bukkit.Location;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_7_R1.CraftWorld;
import org.bukkit.entity.Entity;
import com.google.common.collect.Maps;
public abstract class MobEntityController extends AbstractEntityController {
private final Constructor<?> constructor;
protected MobEntityController(Class<?> clazz) {
this.constructor = getConstructor(clazz);
NMS.registerEntityClass(clazz);
}
@Override
protected Entity createEntity(Location at, NPC npc) {
net.minecraft.server.v1_7_R1.Entity entity = createEntityFromClass(((CraftWorld) at.getWorld()).getHandle(),
npc);
entity.setPositionRotation(at.getX(), at.getY(), at.getZ(), at.getYaw(), at.getPitch());
// entity.onGround isn't updated right away - we approximate here so
// that things like pathfinding still work *immediately* after spawn.
org.bukkit.Material beneath = at.getBlock().getRelative(BlockFace.DOWN).getType();
if (beneath.isBlock())
entity.onGround = true;
return entity.getBukkitEntity();
}
private net.minecraft.server.v1_7_R1.Entity createEntityFromClass(Object... args) {
try {
return (net.minecraft.server.v1_7_R1.Entity) constructor.newInstance(args);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
private static Constructor<?> getConstructor(Class<?> clazz) {
Constructor<?> constructor = CONSTRUCTOR_CACHE.get(clazz);
if (constructor != null)
return constructor;
try {
return clazz.getConstructor(World.class, NPC.class);
} catch (Exception ex) {
throw new IllegalStateException("unable to find an entity constructor");
}
}
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = Maps.newHashMap();
}