Search in sources :

Example 1 with Goal

use of org.spongepowered.api.entity.ai.Goal in project SpongeCommon by SpongePowered.

the class MixinEntityAITasks method removeTask.

/**
 * @author Zidane - November 30th, 2015
 * @reason Integrate Sponge events into the AI task removal.
 *
 * @param aiBase
 */
@SuppressWarnings({ "rawtypes" })
@Overwrite
public void removeTask(EntityAIBase aiBase) {
    final Iterator iterator = this.taskEntries.iterator();
    while (iterator.hasNext()) {
        final EntityAITasks.EntityAITaskEntry entityaitaskentry = (EntityAITasks.EntityAITaskEntry) iterator.next();
        final EntityAIBase otherAiBase = entityaitaskentry.action;
        // Sponge start
        if (otherAiBase.equals(aiBase)) {
            AITaskEvent.Remove event = null;
            if (ShouldFire.AI_TASK_EVENT_REMOVE && this.owner != null && !((IMixinEntity) this.owner).isInConstructPhase()) {
                event = SpongeEventFactory.createAITaskEventRemove(Sponge.getCauseStackManager().getCurrentCause(), (Goal) this, (Agent) this.owner, (AITask) otherAiBase, entityaitaskentry.priority);
                SpongeImpl.postEvent(event);
            }
            if (event == null || !event.isCancelled()) {
                if (entityaitaskentry.using) {
                    entityaitaskentry.using = false;
                    otherAiBase.resetTask();
                    this.executingTaskEntries.remove(entityaitaskentry);
                }
                iterator.remove();
                return;
            }
        }
    // Sponge end
    }
}
Also used : Agent(org.spongepowered.api.entity.living.Agent) AITaskEvent(org.spongepowered.api.event.entity.ai.AITaskEvent) Goal(org.spongepowered.api.entity.ai.Goal) IMixinEntityAIBase(org.spongepowered.common.interfaces.ai.IMixinEntityAIBase) EntityAIBase(net.minecraft.entity.ai.EntityAIBase) Iterator(java.util.Iterator) EntityAITasks(net.minecraft.entity.ai.EntityAITasks) IMixinEntityAITasks(org.spongepowered.common.interfaces.ai.IMixinEntityAITasks) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) AITask(org.spongepowered.api.entity.ai.task.AITask) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 2 with Goal

use of org.spongepowered.api.entity.ai.Goal in project SpongeAPI by SpongePowered.

the class SpongeAITaskEventTest method testAITaskEventRemove_invalidTargetAgentAndGoalOwner.

@Test(expected = IllegalArgumentException.class)
public void testAITaskEventRemove_invalidTargetAgentAndGoalOwner() {
    Agent targetEntity = mock(Agent.class);
    Agent secondEntity = mock(Agent.class);
    Goal goal = mock(Goal.class);
    Mockito.when(goal.getOwner()).thenReturn(secondEntity);
    SpongeEventFactory.createAITaskEventRemove(Cause.of(EventContext.empty(), mock(Game.class)), goal, targetEntity, mock(AITask.class), 0);
}
Also used : Agent(org.spongepowered.api.entity.living.Agent) Goal(org.spongepowered.api.entity.ai.Goal) AITask(org.spongepowered.api.entity.ai.task.AITask) Test(org.junit.Test)

Example 3 with Goal

use of org.spongepowered.api.entity.ai.Goal in project SpongeCommon by SpongePowered.

the class MixinEntityAITasks method goal$removeTasks.

public Goal<?> goal$removeTasks(AITaskType type) {
    Iterator<EntityAITasks.EntityAITaskEntry> iterator = this.taskEntries.iterator();
    while (iterator.hasNext()) {
        final EntityAITasks.EntityAITaskEntry entityaitaskentry = iterator.next();
        final EntityAIBase otherAiBase = entityaitaskentry.action;
        final AITask<?> otherTask = (AITask<?>) otherAiBase;
        if (otherTask.getType().equals(type)) {
            if (this.executingTaskEntries.contains(entityaitaskentry)) {
                otherAiBase.resetTask();
                this.executingTaskEntries.remove(entityaitaskentry);
            }
            iterator.remove();
        }
    }
    return (Goal<?>) this;
}
Also used : Goal(org.spongepowered.api.entity.ai.Goal) IMixinEntityAIBase(org.spongepowered.common.interfaces.ai.IMixinEntityAIBase) EntityAIBase(net.minecraft.entity.ai.EntityAIBase) EntityAITasks(net.minecraft.entity.ai.EntityAITasks) IMixinEntityAITasks(org.spongepowered.common.interfaces.ai.IMixinEntityAITasks) AITask(org.spongepowered.api.entity.ai.task.AITask)

Example 4 with Goal

use of org.spongepowered.api.entity.ai.Goal in project SpongeCommon by SpongePowered.

the class GoalTypeModule method createGoalType.

private GoalType createGoalType(String combinedId, String name) {
    @SuppressWarnings("unchecked") final SpongeGoalType newType = new SpongeGoalType(combinedId, name, (Class<Goal<?>>) (Class<?>) EntityAITasks.class);
    this.goalTypes.put(combinedId, newType);
    return newType;
}
Also used : Goal(org.spongepowered.api.entity.ai.Goal) SpongeGoalType(org.spongepowered.common.entity.ai.SpongeGoalType)

Example 5 with Goal

use of org.spongepowered.api.entity.ai.Goal in project SpongeAPI by SpongePowered.

the class SpongeAITaskEventTest method testAITaskEventAdd_invalidTargetAgentAndGoalOwner.

@Test(expected = IllegalArgumentException.class)
public void testAITaskEventAdd_invalidTargetAgentAndGoalOwner() {
    Agent targetEntity = mock(Agent.class);
    Agent secondEntity = mock(Agent.class);
    Goal goal = mock(Goal.class);
    Mockito.when(goal.getOwner()).thenReturn(secondEntity);
    SpongeEventFactory.createAITaskEventAdd(Cause.of(EventContext.empty(), mock(Game.class)), 0, 0, goal, targetEntity, mock(AITask.class));
}
Also used : Agent(org.spongepowered.api.entity.living.Agent) Goal(org.spongepowered.api.entity.ai.Goal) AITask(org.spongepowered.api.entity.ai.task.AITask) Test(org.junit.Test)

Aggregations

Goal (org.spongepowered.api.entity.ai.Goal)6 AITask (org.spongepowered.api.entity.ai.task.AITask)5 Agent (org.spongepowered.api.entity.living.Agent)4 Test (org.junit.Test)3 EntityAIBase (net.minecraft.entity.ai.EntityAIBase)2 EntityAITasks (net.minecraft.entity.ai.EntityAITasks)2 IMixinEntityAIBase (org.spongepowered.common.interfaces.ai.IMixinEntityAIBase)2 IMixinEntityAITasks (org.spongepowered.common.interfaces.ai.IMixinEntityAITasks)2 Iterator (java.util.Iterator)1 AITaskEvent (org.spongepowered.api.event.entity.ai.AITaskEvent)1 Overwrite (org.spongepowered.asm.mixin.Overwrite)1 SpongeGoalType (org.spongepowered.common.entity.ai.SpongeGoalType)1 IMixinEntity (org.spongepowered.common.interfaces.entity.IMixinEntity)1