Skip to content

Commit 2611295

Browse files
committed
added AI component handlers (adjusting attributes based on added AI
behaviors)
1 parent 4acd9fe commit 2611295

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package de.ntcomputer.minecraft.controllablemobs.implementation.ai;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import net.minecraft.server.v1_6_R3.PathfinderGoal;
7+
import net.minecraft.server.v1_6_R3.PathfinderGoalBreakDoor;
8+
import net.minecraft.server.v1_6_R3.PathfinderGoalDoorInteract;
9+
import net.minecraft.server.v1_6_R3.PathfinderGoalOpenDoor;
10+
import de.ntcomputer.minecraft.controllablemobs.api.ai.AIType;
11+
import de.ntcomputer.minecraft.controllablemobs.implementation.CraftControllableMob;
12+
import de.ntcomputer.minecraft.controllablemobs.implementation.nativeinterfaces.NativeInterfaces;
13+
14+
public final class AIComponentHandlers {
15+
private static final Map<Class<? extends PathfinderGoal>, AIComponentListener<?>> handlerMap = new HashMap<Class<? extends PathfinderGoal>, AIComponentListener<?>>();
16+
17+
private AIComponentHandlers() {
18+
throw new AssertionError();
19+
}
20+
21+
private static <T extends PathfinderGoal> void add(Class<T> goalClass, AIComponentListener<? super T> listener) {
22+
handlerMap.put(goalClass, listener);
23+
}
24+
25+
static {
26+
AIComponentListener<PathfinderGoalDoorInteract> closedDoorListener = new AIComponentListener<PathfinderGoalDoorInteract>() {
27+
@Override
28+
public void onAdd(CraftControllableMob<?> mob, PathfinderGoalDoorInteract goal) {
29+
NativeInterfaces.NAVIGATION.FIELD_USECLOSEDDOOR.set(mob.nmsEntity.getNavigation(), true);
30+
}
31+
@Override
32+
public void onRemoved(CraftControllableMob<?> mob, PathfinderGoalDoorInteract goal) {
33+
if(!mob.getAI().hasBehavior(AIType.ACTION_DOOROPEN, AIType.ACTION_DOORBREAK)) {
34+
NativeInterfaces.NAVIGATION.FIELD_USECLOSEDDOOR.set(mob.nmsEntity.getNavigation(), false);
35+
}
36+
}
37+
};
38+
add(PathfinderGoalOpenDoor.class, closedDoorListener);
39+
add(PathfinderGoalBreakDoor.class, closedDoorListener);
40+
}
41+
42+
@SuppressWarnings("unchecked")
43+
static <T extends PathfinderGoal> void handleAdd(CraftControllableMob<?> mob, T goal) {
44+
AIComponentListener<T> listener = (AIComponentListener<T>) handlerMap.get(goal.getClass());
45+
if(listener!=null) {
46+
listener.onAdd(mob, goal);
47+
}
48+
}
49+
50+
@SuppressWarnings("unchecked")
51+
static <T extends PathfinderGoal> void handleRemoved(CraftControllableMob<?> mob, T goal) {
52+
AIComponentListener<T> listener = (AIComponentListener<T>) handlerMap.get(goal.getClass());
53+
if(listener!=null) {
54+
listener.onRemoved(mob, goal);
55+
}
56+
}
57+
58+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package de.ntcomputer.minecraft.controllablemobs.implementation.ai;
2+
3+
import net.minecraft.server.v1_6_R3.PathfinderGoal;
4+
import de.ntcomputer.minecraft.controllablemobs.implementation.CraftControllableMob;
5+
6+
public interface AIComponentListener<T extends PathfinderGoal> {
7+
8+
public void onAdd(CraftControllableMob<?> mob, T goal);
9+
public void onRemoved(CraftControllableMob<?> mob, T goal);
10+
11+
}

src/java/de/ntcomputer/minecraft/controllablemobs/implementation/ai/AIController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ <B extends AIBehavior<? super E>> CraftAIPart<E,B> add(B behavior) {
154154
}
155155

156156
private void attach(CraftAIPart<E,?> part) {
157+
AIComponentHandlers.handleAdd(mob, part.goal);
157158
this.addGoal(part.priority, part.goal);
158159
this.attachedParts.add(part);
159160
this.goalPartMap.put(part.goal, part);
@@ -171,6 +172,7 @@ void unattach(CraftAIPart<E,?> part) {
171172
this.attachedParts.remove(part);
172173
this.goalPartMap.remove(part.goal);
173174
part.setState(AIState.UNATTACHED);
175+
AIComponentHandlers.handleRemoved(mob, part.goal);
174176
}
175177

176178
void remove(Set<AIType> typeSet, boolean remove) {

0 commit comments

Comments
 (0)