-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathControllableMobs.java
More file actions
282 lines (243 loc) · 13.6 KB
/
ControllableMobs.java
File metadata and controls
282 lines (243 loc) · 13.6 KB
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
package de.ntcomputer.minecraft.controllablemobs.api;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import net.minecraft.server.v1_8_R3.EntityInsentient;
import net.minecraft.server.v1_8_R3.EntityLiving;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftLivingEntity;
import org.bukkit.entity.LivingEntity;
import de.ntcomputer.minecraft.controllablemobs.implementation.CraftControllableMob;
/**
* This is a static class which lets you retrieve instances of {@link ControllableMob}.
*
* @author Cybran
* @version v5
*
*/
public final class ControllableMobs {
private static final Map<LivingEntity,ControllableMob<?>> entities = new HashMap<LivingEntity,ControllableMob<?>>();
static {
onLoad();
}
private static void onLoad() {
Logger.getLogger("Minecraft").info("[ControllableMobsAPI] initialized by an unknown component");
}
private ControllableMobs() {
throw new AssertionError();
}
/**
* Retrieves whether the specified entity has already been assigned.
*
* @param entity to check for a {@link ControllableMob} instance
* @return whether the entity has been assigned ({@link ControllableMob} instance is available)
* @deprecated use {@link #isUnderControl(LivingEntity)} instead
*/
@Deprecated
public static boolean isAssigned(LivingEntity entity) {
return isUnderControl(entity);
}
/**
* Retrieves a {@link ControllableMob} instance for an entity that had been assigned previously.
*
* @param entity which is already under control
* @return the {@link ControllableMob} you can use to control the entity
* @throws IllegalStateException when the entity has not been assigned yet
* @deprecated use {@link #getControl(LivingEntity)} instead
*/
@Deprecated
public static <E extends LivingEntity> ControllableMob<E> get(E entity) throws IllegalStateException {
return getControl(entity);
}
/**
* Simply retrieves a {@link ControllableMob} instance if the passed entity has been assigned, or assigns the entity if not.
* Combines {@link ControllableMobs#get(LivingEntity)} and {@link ControllableMobs#assign(LivingEntity)}.
*
* @param entity which you want to control
* @return the {@link ControllableMob} you can use to control the entity
* @throws InvalidEntityException when the entity is null or can't be controlled
* @deprecated use {@link #getOrPutUnderControl(LivingEntity)} instead
*/
@Deprecated
public static <E extends LivingEntity> ControllableMob<E> getOrAssign(E entity) throws InvalidEntityException {
return getOrPutUnderControl(entity);
}
/**
* Simply retrieves a {@link ControllableMob} instance if the passed entity has been assigned, or assigns the entity if not.
* Combines {@link ControllableMobs#get(LivingEntity)} and {@link ControllableMobs#assign(LivingEntity, boolean)}
* Clearing the AI is only being performed when clearAI is true and the entity is being assigned for the first time.
*
* @param entity which you want to control
* @param clearAI a boolean indicating whether default behaviors should be removed (true) or not (false).
* @return the {@link ControllableMob} you can use to control the entity
* @throws InvalidEntityException when the entity is null or can't be controlled
* @deprecated use {@link #getOrPutUnderControl(LivingEntity, boolean)} instead
*/
@Deprecated
public static <E extends LivingEntity> ControllableMob<E> getOrAssign(E entity, boolean clearAI) throws InvalidEntityException {
return getOrPutUnderControl(entity, clearAI);
}
/**
* Simply puts the entity under your control.
* Will not change any default behaviors, the entity will continue to act normally.
*
* @param entity an instance of a subclass of LivingEntity - the entity you want to control
* @return the {@link ControllableMob} you can use to control the entity
* @throws InvalidEntityException when the entity is null or can't be controlled
* @throws IllegalStateException when the entity is already being controlled
* @deprecated use {@link #putUnderControl(LivingEntity)} instead
*/
@Deprecated
public static <E extends LivingEntity> ControllableMob<E> assign(E entity) throws IllegalStateException, InvalidEntityException {
return putUnderControl(entity);
}
/**
* Simply puts the entity under your control, optionally clearing its AI.
* If you decide to clear its AI, the entity will stop moving and attacking and stand still until you order it to execute any actions.
*
* @param entity entity an instance of a subclass of LivingEntity - the entity you want to control
* @param clearAI a boolean indicating whether default behaviors should be removed (true) or not (false)
* @return the {@link ControllableMob} you can use to control the entity
* @throws InvalidEntityException when the entity is null or can't be controlled
* @throws IllegalStateException when the entity is already being controlled
* @deprecated use {@link #putUnderControl(LivingEntity, boolean)} instead
*/
@Deprecated
public static <E extends LivingEntity> ControllableMob<E> assign(E entity, boolean clearAI) throws IllegalStateException, InvalidEntityException {
return putUnderControl(entity, clearAI);
}
/**
* Releases the entity from control and restores default behaviors.
* All actions will be stopped immediately, all custom AI behaviors will be removed and default attributes and behaviors will be restored. Frees memory.
* After having this method called, nothing will show that the entity was once controlled.
*
* @see ControllableMobs#unassign(ControllableMob, boolean)
* @param controllableMob the controller which should be unassigned
* @throws IllegalStateException when the controllableMob is already unassigned
* @deprecated use {@link #releaseControl(ControllableMob)} instead
*/
@Deprecated
public static void unassign(ControllableMob<?> controllableMob) throws IllegalStateException {
releaseControl(controllableMob);
}
/**
* Releases the entity from control and restores default behaviors.
* All actions will be stopped immediately, all custom AI behaviors will be removed and default behaviors (and attributes, if specified) will be restored. Frees memory.
* After having this method called, nothing will show that the entity was once controlled.
*
* @param controllableMob the controller which should be unassigned
* @param resetAttributes whether to also reset attributes (movement speed, attack damage, etc.) to their default values. Removes custom modifiers. Default ist true.
* @throws IllegalStateException when the controllableMob is already unassigned
* @deprecated use {@link #releaseControl(ControllableMob, boolean)} instead
*/
@Deprecated
public static void unassign(ControllableMob<?> controllableMob, boolean resetAttributes) throws IllegalStateException {
releaseControl(controllableMob, resetAttributes);
}
/**
* Retrieves whether the specified entity has already been assigned.
*
* @param entity to check for a {@link ControllableMob} instance
* @return whether the entity has been put under control ({@link ControllableMob} instance is available)
*/
public static boolean isUnderControl(LivingEntity entity) {
return entities.containsKey(entity);
}
/**
* Retrieves a {@link ControllableMob} instance for an entity that had been put under control previously.
*
* @param entity which is already under control
* @return the {@link ControllableMob} you can use to control the entity
* @throws IllegalStateException when the entity has not been put under control yet
*/
@SuppressWarnings("unchecked")
public static <E extends LivingEntity> ControllableMob<E> getControl(E entity) throws IllegalStateException {
if(!entities.containsKey(entity)) throw new IllegalStateException("entity "+entity.toString()+" is not put under control yet");
return (ControllableMob<E>) entities.get(entity);
}
/**
* Simply retrieves a {@link ControllableMob} instance if the passed entity has been put under control, or puts it under control.
* Combines {@link ControllableMobs#getControl(LivingEntity)} and {@link ControllableMobs#putUnderControl(LivingEntity)}.
*
* @param entity which you want to control
* @return the {@link ControllableMob} you can use to control the entity
* @throws InvalidEntityException when the entity is null or can't be controlled
*/
@SuppressWarnings("unchecked")
public static <E extends LivingEntity> ControllableMob<E> getOrPutUnderControl(E entity) throws InvalidEntityException {
if(entities.containsKey(entity)) return (ControllableMob<E>) entities.get(entity);
else return putUnderControl(entity);
}
/**
* Simply retrieves a {@link ControllableMob} instance if the passed entity has been put under control, or puts it under control.
* Combines {@link ControllableMobs#getControl(LivingEntity)} and {@link ControllableMobs#putUnderControl(LivingEntity, boolean)}
* Clearing the AI is only being performed when clearAI is true and the entity is being assigned for the first time.
*
* @param entity which you want to control
* @param clearAI a boolean indicating whether default behaviors should be removed (true) or not (false).
* @return the {@link ControllableMob} you can use to control the entity
* @throws InvalidEntityException when the entity is null or can't be controlled
*/
@SuppressWarnings("unchecked")
public static <E extends LivingEntity> ControllableMob<E> getOrPutUnderControl(E entity, boolean clearAI) throws InvalidEntityException {
if(entities.containsKey(entity)) return (ControllableMob<E>) entities.get(entity);
else return putUnderControl(entity, clearAI);
}
/**
* Simply puts the entity under your control.
* Will not change any default behaviors, the entity will continue to act normally.
*
* @param entity an instance of a subclass of LivingEntity - the entity you want to control
* @return the {@link ControllableMob} you can use to control the entity
* @throws InvalidEntityException when the entity is null or can't be controlled
* @throws IllegalStateException when the entity is already being controlled
*/
public static <E extends LivingEntity> ControllableMob<E> putUnderControl(E entity) throws IllegalStateException, InvalidEntityException {
return putUnderControl(entity, false);
}
/**
* Simply puts the entity under your control, optionally clearing its AI.
* If you decide to clear its AI, the entity will stop moving and attacking and stand still until you order it to execute any actions.
*
* @param entity an instance of a subclass of LivingEntity - the entity you want to control
* @param clearAI a boolean indicating whether default behaviors should be removed (true) or not (false)
* @return the {@link ControllableMob} you can use to control the entity
* @throws InvalidEntityException when the entity is null or can't be controlled
* @throws IllegalStateException when the entity is already being controlled
*/
public static <E extends LivingEntity> ControllableMob<E> putUnderControl(E entity, boolean clearAI) throws IllegalStateException, InvalidEntityException {
if(entity==null) throw new InvalidEntityException("entity must not be null",entity);
EntityLiving notchEntity = ((CraftLivingEntity) entity).getHandle();
if(!(notchEntity instanceof EntityInsentient)) throw new InvalidEntityException("the entity "+entity.toString()+" can't be controlled",entity);
if(entities.containsKey(entity)) throw new IllegalStateException("entity "+entity.toString()+" is already a controlled entity");
ControllableMob<E> controllableMob = new CraftControllableMob<E>(entity, (EntityInsentient) notchEntity);
if(clearAI) controllableMob.getAI().clear();
entities.put(entity,controllableMob);
return controllableMob;
}
/**
* Releases the entity from control and restores default behaviors.
* All actions will be stopped immediately, all custom AI behaviors will be removed and default attributes and behaviors will be restored. Frees memory.
* After having this method called, nothing will show that the entity was once controlled.
*
* @see ControllableMobs#unassign(ControllableMob, boolean)
* @param controllableMob the controller which should be unassigned
* @throws IllegalStateException when the controllableMob is already unassigned
*/
public static void releaseControl(ControllableMob<?> controllableMob) throws IllegalStateException {
releaseControl(controllableMob, true);
}
/**
* Releases the entity from control and restores default behaviors.
* All actions will be stopped immediately, all custom AI behaviors will be removed and default behaviors (and attributes, if specified) will be restored. Frees memory.
* After having this method called, nothing will show that the entity was once controlled.
*
* @param controllableMob the controller which should be unassigned
* @param resetAttributes whether to also reset attributes (movement speed, attack damage, etc.) to their default values. Removes custom modifiers. Default ist true.
* @throws IllegalStateException when the controllableMob is already unassigned
*/
public static void releaseControl(ControllableMob<?> controllableMob, boolean resetAttributes) throws IllegalStateException {
if(!entities.containsKey(controllableMob.getEntity())) throw new IllegalStateException("entity "+controllableMob.toString()+" is already released from control");
entities.remove(controllableMob.getEntity());
((CraftControllableMob<?>) controllableMob).unassign(resetAttributes);
}
}