-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathRemoveRandomMovementAction.java
More file actions
69 lines (59 loc) · 2.68 KB
/
RemoveRandomMovementAction.java
File metadata and controls
69 lines (59 loc) · 2.68 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
package com.froobworld.farmcontrol.controller.action;
import com.froobworld.farmcontrol.utils.NmsUtils;
import com.google.common.collect.Sets;
import org.bukkit.entity.Mob;
import java.util.*;
import static org.joor.Reflect.on;
public class RemoveRandomMovementAction extends Action {
private final static Map<Mob, Set<Object>> entityRemovedGoalsMap = new WeakHashMap<>();
private final static Set<Class<?>> randomMovementGoals = new HashSet<>();
static {
try {
randomMovementGoals.add(Class.forName(NmsUtils.getFullyQualifiedClassName("PathfinderGoalRandomFly", "world.entity.ai.goal")));
randomMovementGoals.add(Class.forName(NmsUtils.getFullyQualifiedClassName("PathfinderGoalRandomStroll", "world.entity.ai.goal")));
randomMovementGoals.add(Class.forName(NmsUtils.getFullyQualifiedClassName("PathfinderGoalRandomStrollLand", "world.entity.ai.goal")));
randomMovementGoals.add(Class.forName(NmsUtils.getFullyQualifiedClassName("PathfinderGoalRandomSwim", "world.entity.ai.goal")));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void cleanUp() {
entityRemovedGoalsMap.entrySet().removeIf(entry -> !entry.getKey().isValid());
}
public RemoveRandomMovementAction() {
super("remove-random-movement", false, false);
}
@Override
public void doAction(Mob mob) {
Object entityObject = on(mob).call("getHandle").get();
Set<?> wrappedGoals = on(entityObject)
.field(NmsUtils.GoalSelectorHelper.getGoalSelectorFieldName())
.field("d")
.as(Set.class);
Iterator<?> goalIterator = wrappedGoals.iterator();
Set<Object> removedGoals = new HashSet<>();
while (goalIterator.hasNext()) {
Object next = goalIterator.next();
Class<?> nextClass = on(next)
.field("a")
.get().getClass();
if (randomMovementGoals.contains(nextClass)) {
goalIterator.remove();
removedGoals.add(next);
}
}
entityRemovedGoalsMap.compute(mob, (k, v) -> v == null ? removedGoals : Sets.union(removedGoals, v));
}
@Override
public void undoAction(Mob mob) {
Object entityObject = on(mob).call("getHandle").get();
Set wrappedGoals = on(entityObject)
.field(NmsUtils.GoalSelectorHelper.getGoalSelectorFieldName())
.field("d")
.as(Set.class);
Set<Object> removedGoals = entityRemovedGoalsMap.remove(mob);
if (removedGoals != null) {
wrappedGoals.addAll(removedGoals);
}
}
}