diff --git a/src/api b/src/api index 3cfe8be05..e13c6762f 160000 --- a/src/api +++ b/src/api @@ -1 +1 @@ -Subproject commit 3cfe8be0562243595015c76cbbcff6fc15a73cfb +Subproject commit e13c6762f39d5f48c7f2333fc203765f876dbfa3 diff --git a/src/main/java/noppes/npcs/entity/EntityNPCInterface.java b/src/main/java/noppes/npcs/entity/EntityNPCInterface.java index 4443b1dc5..c50a62125 100644 --- a/src/main/java/noppes/npcs/entity/EntityNPCInterface.java +++ b/src/main/java/noppes/npcs/entity/EntityNPCInterface.java @@ -74,6 +74,7 @@ import noppes.npcs.controllers.data.*; import noppes.npcs.entity.data.DataTimers; import noppes.npcs.roles.*; import noppes.npcs.scripted.NpcAPI; +import noppes.npcs.scripted.ScriptedActionManager; import noppes.npcs.scripted.entity.ScriptNpc; import noppes.npcs.scripted.event.NpcEvent; import noppes.npcs.util.GameProfileAlt; @@ -96,7 +97,7 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti public DataTimers timers; public CombatHandler combatHandler = new CombatHandler(this); - //public ScriptedActionManager actionManager = new ScriptedActionManager(); + public ScriptedActionManager actionManager = new ScriptedActionManager(); public String linkedName = ""; public long linkedLast = 0; @@ -362,7 +363,7 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti combatHandler.update(); onCollide(); - // actionManager.tick(ticksExisted); + actionManager.tick(ticksExisted); } if (wasKilled != isKilled() && wasKilled) { @@ -1150,7 +1151,7 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti setHealth(getMaxHealth()); dataWatcher.updateObject(14, 0); // animation Normal dataWatcher.updateObject(15, 0); - //actionManager.clear(); + actionManager.clear(); combatHandler.reset(); this.setAttackTarget(null); this.setRevengeTarget(null); diff --git a/src/main/java/noppes/npcs/scripted/ScriptedActionManager.java b/src/main/java/noppes/npcs/scripted/ScriptedActionManager.java new file mode 100644 index 000000000..426497c37 --- /dev/null +++ b/src/main/java/noppes/npcs/scripted/ScriptedActionManager.java @@ -0,0 +1,188 @@ +package noppes.npcs.scripted; + +import noppes.npcs.api.IAction; +import noppes.npcs.api.IActionManager; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Queue; +import java.util.function.Consumer; + +public class ScriptedActionManager implements IActionManager { + private boolean isWorking; + private Queue actionQueue = new LinkedList<>(); + + @Override + public IAction createAction(String name, int maxDuration, int startAfterTicks, Consumer action) { + return new CustomAction(name, maxDuration, startAfterTicks, action); + } + + @Override + public void start() { + isWorking = true; + } + + @Override + public void stop() { + isWorking = false; + } + + @Override + public void addScheduledAction(IAction action) { + actionQueue.add(action); + } + + @Override + public void addScheduledAction(String name, int maxDuration, int startAfterTicks, Consumer action) { + addScheduledAction(createAction(name, maxDuration, startAfterTicks, action)); + } + + @Override + public void addScheduledActionAt(int index, IAction action) { + ((LinkedList) actionQueue).add(index, action); + } + + @Override + public void scheduleActionAfter(IAction after, IAction toSchedule) { + int index = getActionIndex(after); + if (index != -1) + addScheduledActionAt(index + 1, toSchedule); + } + + @Override + public int getActionIndex(IAction action) { + if (action == null) + return -1; + return ((LinkedList) actionQueue).indexOf(action); + } + + @Override + public IAction getCurrentAction() { + return actionQueue.peek(); + } + + @Override + public Queue getActionQueue() { + return actionQueue; + } + + public void clear(){ + actionQueue.clear(); + } + public void tick(int ticksExisted) { + if (isWorking) { + CustomAction current = (CustomAction) getCurrentAction(); + if (current != null) { + current.tick(ticksExisted); + if (current.isDone || current.duration >= current.maxDuration) { + current.destroy(); + actionQueue.poll(); + } + } + } + } + + //Nested not static to access outer class instance + public class CustomAction implements IAction { + Consumer action; + private HashMap data = new HashMap<>(); + + private String name; + private int startAfterTicks; //number of ticks to start action after + private int count; //number of times action ran + private int duration, maxDuration; //duration since action began + private int updateEveryXTick = 5; + + private boolean isDone; + + public CustomAction(String name, int maxDuration, int startAfterTicks, Consumer task) { + this.name = name; + this.maxDuration = maxDuration; + this.startAfterTicks = startAfterTicks; + this.action = task; + } + + private void tick(int ticksExisted) { + if (startAfterTicks <= 0) { + if (ticksExisted % updateEveryXTick == 0) { + action.accept(this); + count++; + } + duration++; + } else + startAfterTicks--; + } + + private void destroy() { + data.clear(); + } + + @Override + public int getCount() { + return count; + } + + @Override + public int getDuration() { + return duration; + } + + @Override + public String getName() { + return name; + } + + @Override + public int getMaxDuration() { + return maxDuration; + } + + @Override + public void setDone(boolean done) { + isDone = done; + } + + @Override + public boolean isDone() { + return isDone; + } + + @Override + public Object getData(String key) { + return data.get(key); + } + + @Override + public void addData(String key, Object value) { + data.put(key, value); + } + + @Override + public int getUpdateEveryXTick() { + return updateEveryXTick; + } + + @Override + public void setUpdateEveryXTick(int X) { + this.updateEveryXTick = X; + } + + @Override + public int getStartAfterTicks() { + return startAfterTicks; + } + + @Override + public IAction create(String name, int maxDuration, int startAfterTicks, Consumer action) { + return createAction(name, maxDuration, startAfterTicks, action); + } + + @Override + public void scheduleAfter(IAction action) { + scheduleActionAfter(this, action); + } + } +} + + + diff --git a/src/main/java/noppes/npcs/scripted/entity/ScriptNpc.java b/src/main/java/noppes/npcs/scripted/entity/ScriptNpc.java index 45694f845..ed6bff1db 100644 --- a/src/main/java/noppes/npcs/scripted/entity/ScriptNpc.java +++ b/src/main/java/noppes/npcs/scripted/entity/ScriptNpc.java @@ -3,6 +3,7 @@ package noppes.npcs.scripted.entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import noppes.npcs.NoppesUtilServer; +import noppes.npcs.api.IActionManager; import noppes.npcs.api.IPos; import noppes.npcs.api.ITimers; import noppes.npcs.api.entity.ICustomNpc; @@ -1379,4 +1380,9 @@ public class ScriptNpc extends ScriptLiving imp public void updateClient() { this.npc.updateClient(); } public void updateAI() { this.npc.updateTasks(); } + + @Override + public IActionManager getActionManager() { + return npc.actionManager; + } }