From e13c6762f39d5f48c7f2333fc203765f876dbfa3 Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Mon, 3 Mar 2025 23:59:48 +0200 Subject: [PATCH] Added Scheduled Tasks system for NPC scripting. Allows for scripting the scheduling of runnable methods in sequence within the NPC's AI An example sequence of tasks: 1. Dashes towards player (might take 3-5 seconds, doesn't matter, task durations are dynamic so it can take as long as it needs). 2. Punches player away 3. Waits 10 ticks - Fires a ki blast 4. Teleports behind player 5. Knocks player out --- java/noppes/npcs/api/IAction.java | 69 +++++++++++++++++++++ java/noppes/npcs/api/IActionManager.java | 51 +++++++++++++++ java/noppes/npcs/api/entity/ICustomNpc.java | 7 +++ 3 files changed, 127 insertions(+) create mode 100644 java/noppes/npcs/api/IAction.java create mode 100644 java/noppes/npcs/api/IActionManager.java diff --git a/java/noppes/npcs/api/IAction.java b/java/noppes/npcs/api/IAction.java new file mode 100644 index 0000000..14acd2e --- /dev/null +++ b/java/noppes/npcs/api/IAction.java @@ -0,0 +1,69 @@ +package noppes.npcs.api; + +import java.util.function.Consumer; + +public interface IAction { + + /** + * @return number of times the function passed has been called upon + */ + int getCount(); + + /** + * @return number of ticks since action starting + */ + int getDuration(); + + String getName(); + + int getMaxDuration(); + + /** + * @param done set to true to end action + */ + void setDone(boolean done); + + boolean isDone(); + + /** + * Allows the storage of objects that might be needed within the action. + * Equivalent to npc.getTempData(). + * All data deleted on action ending + * + * @param key + * @return + */ + void addData(String key, Object value); + + Object getData(String key); + + int getUpdateEveryXTick(); + + /** + * @param X calls upon the function passed every X ticks + */ + void setUpdateEveryXTick(int X); + + /** + * @return number of ticks to elapse before starting action + */ + int getStartAfterTicks(); + + /** + * Creates a new IAction. allows for instant daisy-chaining without having to go through npc.getActionManager() again + * + * @param name + * @param maxDuration + * @param startAfterTicks + * @param action + * @return + */ + IAction create(String name, int maxDuration, int startAfterTicks, Consumer action); + + /** + * Schedules newAction right after this one, skipping the line of everything after + * + * @param newAction + */ + void scheduleAfter(IAction newAction); +} diff --git a/java/noppes/npcs/api/IActionManager.java b/java/noppes/npcs/api/IActionManager.java new file mode 100644 index 0000000..3adef83 --- /dev/null +++ b/java/noppes/npcs/api/IActionManager.java @@ -0,0 +1,51 @@ +package noppes.npcs.api; + +import java.util.Queue; +import java.util.function.Consumer; + +public interface IActionManager { + /** + * @param name + * @param maxDuration once maxDuration is reached the action ends + * @param action function to invoke as long as this action is active + * @param startAfterTicks number of ticks to elapse before starting action + * @return + */ + IAction createAction(String name, int maxDuration, int startAfterTicks, Consumer action); + + /** + * Starts ActionManager (paused by default) + */ + void start(); + + /** + * stops ActionManager + */ + void stop(); + + void addScheduledAction(IAction action); + + void addScheduledAction(String name, int maxDuration, int startAfterTicks, Consumer action); + + /** + * @param index to add action at in the actionQueue + * @param action + */ + void addScheduledActionAt(int index, IAction action); + + /** + * @param after The action to add toSchedule after + * @param toSchedule + */ + void scheduleActionAfter(IAction after, IAction toSchedule); + + /** + * @param action + * @return action's index in the actionQueue + */ + int getActionIndex(IAction action); + + IAction getCurrentAction(); + + Queue getActionQueue(); +} diff --git a/java/noppes/npcs/api/entity/ICustomNpc.java b/java/noppes/npcs/api/entity/ICustomNpc.java index 8f5ab6f..0253508 100644 --- a/java/noppes/npcs/api/entity/ICustomNpc.java +++ b/java/noppes/npcs/api/entity/ICustomNpc.java @@ -1,6 +1,7 @@ package noppes.npcs.api.entity; import net.minecraft.entity.EntityCreature; +import noppes.npcs.api.IActionManager; import noppes.npcs.api.IPos; import noppes.npcs.api.ITimers; import noppes.npcs.api.entity.data.IModelData; @@ -933,4 +934,10 @@ public interface ICustomNpc extends IEntityLiving, void updateClient(); void updateAI(); + + /** + * + * @return the action manager responsible for scheduling actions to the NPC's AI + */ + IActionManager getActionManager(); }