Class TaskTriggerer<E extends LivingEntity,M>

java.lang.Object
net.minecraft.entity.ai.brain.task.TaskTriggerer<E,M>
All Implemented Interfaces:
com.mojang.datafixers.kinds.App<TaskTriggerer.K1<E>,M>
Direct Known Subclasses:
TaskTriggerer.QueryMemory, TaskTriggerer.Supply, TaskTriggerer.Trigger

public class TaskTriggerer<E extends LivingEntity,M> extends Object implements com.mojang.datafixers.kinds.App<TaskTriggerer.K1<E>,M>
A lambda-based alternative to subclassing Task.

To use this,

  1. Make a static method with all configurable parameters (such as speed, range, etc).
  2. If there is a value that needs to be tracked between ticks (such as cooldown), declare a mutable variable (such as MutableInt).
  3. Return the result of task(java.util.function.Function<net.minecraft.entity.ai.brain.task.TaskTriggerer.TaskContext<E>, ? extends com.mojang.datafixers.kinds.App<net.minecraft.entity.ai.brain.task.TaskTriggerer.K1<E>, net.minecraft.entity.ai.brain.task.TaskRunnable<E>>>). This method creates a new SingleTickTask.

A lambda is passed to the task method. This takes a TaskTriggerer.TaskContext. You can either call TaskTriggerer.TaskContext.point(A) to set the actual task function, or declare a dependency on memory queries. The task function is a function that takes the server world, entity, and the current time, and returns a boolean indicating whether a task successfully ran.

Example of memory-independent task


 public static Task<LivingEntity> createHealTask() {
     return TaskTriggerer.task(context -> context.point((world, entity, time) -> {
         entity.heal(1.0f);
         return true;
     }));
 }
 

Memory-dependent task

Memory-dependent tasks (i.e. one that queries, remembers, or forgets a memory) first should call TaskContext.group with results of Context.queryMemory methods for all the dependent memories. Then, call apply with context and a lambda function taking the results of the query. This function returns the task function seen earlier.

If any of the query is not successful (e.g. because a value was not present), the task will not run. If all succeed, then the task runs, and the query result can be obtained via TaskTriggerer.TaskContext.getValue(net.minecraft.entity.ai.brain.MemoryQueryResult<com.mojang.datafixers.kinds.IdF.Mu, Value>).

There are three query types:

For example, the following code queries MemoryModuleType.MEETING_POINT and forgets it:


 public static Task<LivingEntity> createForgetMeetingPointTask() {
     return TaskTriggerer.task(context -> context.group(context.queryMemoryValue(MemoryModuleType.MEETING_POINT)).apply(context, meetingPoint -> (world, entity, time) -> {
         GlobalPos pos = (GlobalPos)context.getValue(meetingPoint);
         // Do something with meetingPoint or pos
         // For example, forget the meeting point:
         meetingPoint.forget();
         return true;
     }));
 }
 
Mappings:
Namespace Name
official bld
intermediary net/minecraft/class_7898
named net/minecraft/entity/ai/brain/task/TaskTriggerer
  • Field Details

    • function

      private final TaskTriggerer.TaskFunction<E extends LivingEntity,M> function
      Mappings:
      Namespace Name Mixin selector
      official a Lbld;a:Lbld$e;
      intermediary field_40998 Lnet/minecraft/class_7898;field_40998:Lnet/minecraft/class_7898$class_7904;
      named function Lnet/minecraft/entity/ai/brain/task/TaskTriggerer;function:Lnet/minecraft/entity/ai/brain/task/TaskTriggerer$TaskFunction;
  • Constructor Details

    • TaskTriggerer

      TaskTriggerer(TaskTriggerer.TaskFunction<E,M> function)
      Mappings:
      Namespace Name Mixin selector
      official <init> Lbld;<init>(Lbld$e;)V
      intermediary <init> Lnet/minecraft/class_7898;<init>(Lnet/minecraft/class_7898$class_7904;)V
      named <init> Lnet/minecraft/entity/ai/brain/task/TaskTriggerer;<init>(Lnet/minecraft/entity/ai/brain/task/TaskTriggerer$TaskFunction;)V
  • Method Details

    • cast

      public static <E extends LivingEntity, M> TaskTriggerer<E,M> cast(com.mojang.datafixers.kinds.App<TaskTriggerer.K1<E>,M> app)
      Mappings:
      Namespace Name Mixin selector
      official a Lbld;a(Lcom/mojang/datafixers/kinds/App;)Lbld;
      intermediary method_47220 Lnet/minecraft/class_7898;method_47220(Lcom/mojang/datafixers/kinds/App;)Lnet/minecraft/class_7898;
      named cast Lnet/minecraft/entity/ai/brain/task/TaskTriggerer;cast(Lcom/mojang/datafixers/kinds/App;)Lnet/minecraft/entity/ai/brain/task/TaskTriggerer;
    • newContext

      public static <E extends LivingEntity> TaskTriggerer.TaskContext<E> newContext()
      Mappings:
      Namespace Name Mixin selector
      official a Lbld;a()Lbld$b;
      intermediary method_47215 Lnet/minecraft/class_7898;method_47215()Lnet/minecraft/class_7898$class_7900;
      named newContext Lnet/minecraft/entity/ai/brain/task/TaskTriggerer;newContext()Lnet/minecraft/entity/ai/brain/task/TaskTriggerer$TaskContext;
    • task

      public static <E extends LivingEntity> SingleTickTask<E> task(Function<TaskTriggerer.TaskContext<E>,? extends com.mojang.datafixers.kinds.App<TaskTriggerer.K1<E>,TaskRunnable<E>>> creator)
      Mappings:
      Namespace Name Mixin selector
      official a Lbld;a(Ljava/util/function/Function;)Lbjb;
      intermediary method_47224 Lnet/minecraft/class_7898;method_47224(Ljava/util/function/Function;)Lnet/minecraft/class_7894;
      named task Lnet/minecraft/entity/ai/brain/task/TaskTriggerer;task(Ljava/util/function/Function;)Lnet/minecraft/entity/ai/brain/task/SingleTickTask;
    • runIf

      public static <E extends LivingEntity> SingleTickTask<E> runIf(TaskRunnable<? super E> predicate, TaskRunnable<? super E> task)
      Mappings:
      Namespace Name Mixin selector
      official a Lbld;a(Lblg;Lblg;)Lbjb;
      intermediary method_47217 Lnet/minecraft/class_7898;method_47217(Lnet/minecraft/class_7911;Lnet/minecraft/class_7911;)Lnet/minecraft/class_7894;
      named runIf Lnet/minecraft/entity/ai/brain/task/TaskTriggerer;runIf(Lnet/minecraft/entity/ai/brain/task/TaskRunnable;Lnet/minecraft/entity/ai/brain/task/TaskRunnable;)Lnet/minecraft/entity/ai/brain/task/SingleTickTask;
    • runIf

      public static <E extends LivingEntity> SingleTickTask<E> runIf(Predicate<E> predicate, SingleTickTask<? super E> task)
      Mappings:
      Namespace Name Mixin selector
      official a Lbld;a(Ljava/util/function/Predicate;Lbjb;)Lbjb;
      intermediary method_47227 Lnet/minecraft/class_7898;method_47227(Ljava/util/function/Predicate;Lnet/minecraft/class_7894;)Lnet/minecraft/class_7894;
      named runIf Lnet/minecraft/entity/ai/brain/task/TaskTriggerer;runIf(Ljava/util/function/Predicate;Lnet/minecraft/entity/ai/brain/task/SingleTickTask;)Lnet/minecraft/entity/ai/brain/task/SingleTickTask;
    • predicate

      public static <E extends LivingEntity> SingleTickTask<E> predicate(Predicate<E> predicate)
      Mappings:
      Namespace Name Mixin selector
      official a Lbld;a(Ljava/util/function/Predicate;)Lbjb;
      intermediary method_47225 Lnet/minecraft/class_7898;method_47225(Ljava/util/function/Predicate;)Lnet/minecraft/class_7894;
      named predicate Lnet/minecraft/entity/ai/brain/task/TaskTriggerer;predicate(Ljava/util/function/Predicate;)Lnet/minecraft/entity/ai/brain/task/SingleTickTask;
    • predicate

      public static <E extends LivingEntity> SingleTickTask<E> predicate(BiPredicate<ServerWorld,E> predicate)
      Mappings:
      Namespace Name Mixin selector
      official a Lbld;a(Ljava/util/function/BiPredicate;)Lbjb;
      intermediary method_47221 Lnet/minecraft/class_7898;method_47221(Ljava/util/function/BiPredicate;)Lnet/minecraft/class_7894;
      named predicate Lnet/minecraft/entity/ai/brain/task/TaskTriggerer;predicate(Ljava/util/function/BiPredicate;)Lnet/minecraft/entity/ai/brain/task/SingleTickTask;
    • getFunction

      static <E extends LivingEntity, M> TaskTriggerer.TaskFunction<E,M> getFunction(com.mojang.datafixers.kinds.App<TaskTriggerer.K1<E>,M> app)
      Mappings:
      Namespace Name Mixin selector
      official b Lbld;b(Lcom/mojang/datafixers/kinds/App;)Lbld$e;
      intermediary method_47229 Lnet/minecraft/class_7898;method_47229(Lcom/mojang/datafixers/kinds/App;)Lnet/minecraft/class_7898$class_7904;
      named getFunction Lnet/minecraft/entity/ai/brain/task/TaskTriggerer;getFunction(Lcom/mojang/datafixers/kinds/App;)Lnet/minecraft/entity/ai/brain/task/TaskTriggerer$TaskFunction;
    • of

      static <E extends LivingEntity, M> TaskTriggerer<E,M> of(TaskTriggerer.TaskFunction<E,M> function)
      Mappings:
      Namespace Name Mixin selector
      official a Lbld;a(Lbld$e;)Lbld;
      intermediary method_47216 Lnet/minecraft/class_7898;method_47216(Lnet/minecraft/class_7898$class_7904;)Lnet/minecraft/class_7898;
      named of Lnet/minecraft/entity/ai/brain/task/TaskTriggerer;of(Lnet/minecraft/entity/ai/brain/task/TaskTriggerer$TaskFunction;)Lnet/minecraft/entity/ai/brain/task/TaskTriggerer;