|
| 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 | +} |
0 commit comments