-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
DenizenEntityType.java
136 lines (118 loc) · 4.62 KB
/
DenizenEntityType.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
package net.aufdemrand.denizen.utilities.entity;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.objects.Mechanism;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class DenizenEntityType {
private static final Map<String, DenizenEntityType> registeredTypes = new HashMap<String, DenizenEntityType>();
private final EntityType bukkitEntityType;
private final String name;
private final String lowercaseName;
private final double gravity;
private final Method createMethod;
static {
for (EntityType entityType : EntityType.values()) {
registeredTypes.put(entityType.name(), new DenizenEntityType(entityType));
}
}
private DenizenEntityType(EntityType entityType) {
this.bukkitEntityType = entityType;
this.name = entityType.name();
this.lowercaseName = name.toLowerCase();
this.gravity = Gravity.getGravity(entityType);
this.createMethod = null;
}
private DenizenEntityType(String name, Class<? extends DenizenCustomEntity> entityType) {
this(name, entityType, 0.115);
}
private DenizenEntityType(String name, Class<? extends DenizenCustomEntity> entityType, double gravity) {
EntityType bukkitEntityType = EntityType.UNKNOWN;
if (entityType != null) {
for (EntityType type : EntityType.values()) {
Class<? extends Entity> clazz = type.getEntityClass();
if (clazz != null && clazz.isAssignableFrom(entityType)) {
bukkitEntityType = type;
break;
}
}
}
this.bukkitEntityType = bukkitEntityType;
this.name = name.toUpperCase();
this.lowercaseName = name.toLowerCase();
this.gravity = gravity;
Method finalMethod = null;
if (entityType != null) {
for (Method method : entityType.getDeclaredMethods()) {
if (method.isAnnotationPresent(DenizenCustomEntity.CreateEntity.class)) {
finalMethod = method;
break;
}
}
}
this.createMethod = finalMethod;
}
public Entity spawnNewEntity(Location location, ArrayList<Mechanism> mechanisms) {
try {
if (name.equals("DROPPED_ITEM")) {
ItemStack itemStack = new ItemStack(Material.STONE);
for (Mechanism mechanism : mechanisms) {
if (mechanism.matches("item") && mechanism.requireObject(dItem.class)) {
itemStack = mechanism.getValue().asType(dItem.class).getItemStack();
break;
}
}
return location.getWorld().dropItem(location, itemStack);
}
else if (!isCustom()) {
return location.getWorld().spawnEntity(location, bukkitEntityType);
}
else {
return (Entity) createMethod.invoke(null, location, mechanisms);
}
}
catch (Exception e) {
dB.echoError(e);
}
return null;
}
public String getName() {
return this.name;
}
public String getLowercaseName() {
return this.lowercaseName;
}
public double getGravity() {
return this.gravity;
}
public EntityType getBukkitEntityType() {
return bukkitEntityType;
}
public static void registerEntityType(String name, Class<? extends DenizenCustomEntity> entityType) {
registeredTypes.put(name.toUpperCase(), new DenizenEntityType(name, entityType));
}
public static boolean isRegistered(String name) {
return registeredTypes.containsKey(name.toUpperCase());
}
public static DenizenEntityType getByName(String name) {
return registeredTypes.get(name.toUpperCase());
}
public static DenizenEntityType getByEntity(Entity entity) {
if (entity instanceof DenizenCustomEntity) {
return getByName(((DenizenCustomEntity) entity).getEntityTypeName());
}
else {
return getByName(entity.getType().name());
}
}
public boolean isCustom() {
return createMethod != null;
}
}