diff --git a/src/main/java/noppes/npcs/EventHooks.java b/src/main/java/noppes/npcs/EventHooks.java index 43e6d570f..7848644ec 100644 --- a/src/main/java/noppes/npcs/EventHooks.java +++ b/src/main/java/noppes/npcs/EventHooks.java @@ -15,11 +15,12 @@ import net.minecraft.world.WorldServer; import net.minecraftforge.event.entity.EntityEvent; import net.minecraftforge.event.world.WorldEvent; import noppes.npcs.api.IWorld; -import noppes.npcs.api.entity.IEntity; -import noppes.npcs.api.entity.IPlayer; -import noppes.npcs.api.entity.IProjectile; +import noppes.npcs.api.entity.*; +import noppes.npcs.api.event.IAnimationEvent; import noppes.npcs.api.gui.ICustomGui; import noppes.npcs.api.gui.IItemSlot; +import noppes.npcs.api.handler.data.IAnimation; +import noppes.npcs.api.handler.data.IFrame; import noppes.npcs.api.item.IItemCustom; import noppes.npcs.api.item.IItemStack; import noppes.npcs.constants.EnumScriptType; @@ -803,4 +804,49 @@ public class EventHooks { handler.callScript(EnumScriptType.PARTY_DISBAND, event); NpcAPI.EVENT_BUS.post(event); } + + private static boolean postAnimationEvent(IAnimationEvent event) { + IScriptHandler handler; + IAnimatable animatable = event.getAnimation().getParent().getEntity(); + if (animatable instanceof ICustomNpc) { + EntityNPCInterface npc = (EntityNPCInterface) ((ICustomNpc) animatable).getMCEntity(); + handler = npc.script; + } else { + handler = ScriptController.Instance.playerScripts; + } + + if (handler.isClient()) + return false; + + handler.callScript(event.getHookName(), (Event) event); + return NpcAPI.EVENT_BUS.post((Event) event); + } + + public static boolean onAnimationStarted(IAnimation animation) { + if (animation.getParent() == null || animation.getParent().getEntity() == null) { + return false; + } + return postAnimationEvent(new AnimationEvent.Started(animation)); + } + + public static void onAnimationEnded(IAnimation animation) { + if (animation.getParent() == null || animation.getParent().getEntity() == null) { + return; + } + postAnimationEvent(new AnimationEvent.Ended(animation)); + } + + public static void onAnimationFrameEntered(IAnimation animation, IFrame frame) { + if (frame == null || animation.getParent() == null || animation.getParent().getEntity() == null) { + return; + } + postAnimationEvent(new AnimationEvent.FrameEvent.Entered(animation, frame)); + } + + public static void onAnimationFrameExited(IAnimation animation, IFrame frame) { + if (frame == null || animation.getParent() == null || animation.getParent().getEntity() == null) { + return; + } + postAnimationEvent(new AnimationEvent.FrameEvent.Exited(animation, frame)); + } } diff --git a/src/main/java/noppes/npcs/constants/EnumScriptType.java b/src/main/java/noppes/npcs/constants/EnumScriptType.java index a8f233f47..f8991d3f5 100644 --- a/src/main/java/noppes/npcs/constants/EnumScriptType.java +++ b/src/main/java/noppes/npcs/constants/EnumScriptType.java @@ -99,7 +99,12 @@ public enum EnumScriptType { PARTY_INVITE("partyInvite"), PARTY_KICK("partyKick"), PARTY_LEAVE("partyLeave"), - PARTY_DISBAND("partyDisband"); + PARTY_DISBAND("partyDisband"), + + ANIMATION_START("animationStart"), + ANIMATION_END("animationEnd"), + ANIMATION_FRAME_ENTER("frameEnter"), + ANIMATION_FRAME_EXIT("frameExit"); public String function; diff --git a/src/main/java/noppes/npcs/scripted/event/AnimationEvent.java b/src/main/java/noppes/npcs/scripted/event/AnimationEvent.java new file mode 100644 index 000000000..1690f8e57 --- /dev/null +++ b/src/main/java/noppes/npcs/scripted/event/AnimationEvent.java @@ -0,0 +1,108 @@ +package noppes.npcs.scripted.event; + +import cpw.mods.fml.common.eventhandler.Cancelable; +import noppes.npcs.api.entity.IAnimatable; +import noppes.npcs.api.event.IAnimationEvent; +import noppes.npcs.api.handler.data.IAnimation; +import noppes.npcs.api.handler.data.IAnimationData; +import noppes.npcs.api.handler.data.IFrame; +import noppes.npcs.constants.EnumScriptType; +import noppes.npcs.controllers.data.Animation; +import noppes.npcs.controllers.data.Frame; + +public abstract class AnimationEvent extends CustomNPCsEvent implements IAnimationEvent { + protected final IAnimation animation; + protected final IAnimationData animationData; + protected final IAnimatable entity; + + public AnimationEvent(IAnimation animation) { + this.animation = animation; + this.animationData = animation.getParent(); + this.entity = this.animationData.getEntity(); + } + + @Override + public IAnimation getAnimation() { + return this.animation; + } + + @Override + public IAnimationData getAnimationData() { + return this.animationData; + } + + @Override + public IAnimatable getEntity() { + return this.entity; + } + + @Cancelable + public static class Started extends AnimationEvent { + + public Started(IAnimation animation) { + super(animation); + } + + @Override + public String getHookName() { + return EnumScriptType.ANIMATION_START.function; + } + } + + public static class Ended extends AnimationEvent { + + public Ended(IAnimation animation) { + super(animation); + } + + @Override + public String getHookName() { + return EnumScriptType.ANIMATION_END.function; + } + } + + public static abstract class FrameEvent extends AnimationEvent implements IFrameEvent { + private final IFrame frame; + private final int index; + + public FrameEvent(IAnimation animation, IFrame frame) { + super(animation); + this.frame = frame; + this.index = ((Animation)this.getAnimation()).frames.indexOf((Frame) this.frame); + } + + @Override + public int getIndex() { + return this.index; + } + + @Override + public IFrame getFrame() { + return this.frame; + } + + public static class Entered extends FrameEvent { + + public Entered(IAnimation animation, IFrame frame) { + super(animation, frame); + } + + @Override + public String getHookName() { + return EnumScriptType.ANIMATION_FRAME_ENTER.function; + } + } + + public static class Exited extends FrameEvent { + + public Exited(IAnimation animation, IFrame frame) { + super(animation, frame); + } + + @Override + public String getHookName() { + return EnumScriptType.ANIMATION_FRAME_EXIT.function; + } + } + } +}