Merge branch 'animation-events' of https://github.com/KAMKEEL/CustomNPC-Plus into animation-events

This commit is contained in:
Kam committed 2024-03-24 19:13:54 -04:00
commit cee52ec9e0
18 files changed
+254 -46

No files matched your search

+49 -20
View File
@@ -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;
@@ -119,7 +120,6 @@ public class EventHooks {
return;
noppes.npcs.scripted.event.NpcEvent.InitEvent event = new noppes.npcs.scripted.event.NpcEvent.InitEvent(npc.wrappedNPC);
ScriptController.Instance.globalNpcScripts.callScript(EnumScriptType.INIT, event);
npc.script.callScript(EnumScriptType.INIT, event);
NpcAPI.EVENT_BUS.post(event);
npc.advanced.soulStoneInit = false;
@@ -130,7 +130,6 @@ public class EventHooks {
return;
noppes.npcs.scripted.event.NpcEvent.UpdateEvent event = new noppes.npcs.scripted.event.NpcEvent.UpdateEvent(npc.wrappedNPC);
ScriptController.Instance.globalNpcScripts.callScript(EnumScriptType.TICK, event);
npc.script.callScript(EnumScriptType.TICK, event);
NpcAPI.EVENT_BUS.post(event);
}
@@ -140,7 +139,6 @@ public class EventHooks {
return false;
NpcEvent.DialogEvent event = new NpcEvent.DialogEvent(npc.wrappedNPC, player, dialogId, optionId, dialog);
ScriptController.Instance.globalNpcScripts.callScript(EnumScriptType.DIALOG, event);
npc.script.callScript(EnumScriptType.DIALOG, event, "player", event.getPlayer(), "dialog", event.getDialogId(), "option", event.getOptionId(), "dialogObj", event.getDialog());
return NpcAPI.EVENT_BUS.post(event);
@@ -151,7 +149,6 @@ public class EventHooks {
return;
NpcEvent.DialogClosedEvent event = new NpcEvent.DialogClosedEvent(npc.wrappedNPC, player, dialogId, optionId, dialog);
ScriptController.Instance.globalNpcScripts.callScript(EnumScriptType.DIALOG_CLOSE, event);
npc.script.callScript(EnumScriptType.DIALOG_CLOSE, event, "player", event.getPlayer(), "dialog", event.getDialogId(), "option", event.getOptionId(), "dialogObj", event.getDialog());
NpcAPI.EVENT_BUS.post(event);
@@ -162,7 +159,6 @@ public class EventHooks {
return false;
NpcEvent.InteractEvent event = new NpcEvent.InteractEvent(npc.wrappedNPC,player);
ScriptController.Instance.globalNpcScripts.callScript(EnumScriptType.INTERACT, event);
boolean result = npc.script.callScript(EnumScriptType.INTERACT, event, "player", player);
NpcAPI.EVENT_BUS.post(event);
return result;
@@ -172,7 +168,6 @@ public class EventHooks {
if(npc == null || npc.wrappedNPC == null)
return false;
ScriptController.Instance.globalNpcScripts.callScript(EnumScriptType.ATTACK_MELEE, event);
npc.script.callScript(EnumScriptType.ATTACK_MELEE, event, "target", event.target);
npc.script.callScript(EnumScriptType.ATTACK, event, "target", event.target);
return NpcAPI.EVENT_BUS.post(event);
@@ -182,7 +177,6 @@ public class EventHooks {
if(npc == null || npc.wrappedNPC == null)
return false;
ScriptController.Instance.globalNpcScripts.callScript(EnumScriptType.ATTACK_SWING, event);
npc.script.callScript(EnumScriptType.ATTACK_SWING, event);
return NpcAPI.EVENT_BUS.post(event);
}
@@ -191,7 +185,6 @@ public class EventHooks {
if(npc == null || npc.wrappedNPC == null)
return false;
ScriptController.Instance.globalNpcScripts.callScript(EnumScriptType.RANGED_LAUNCHED, event);
npc.script.callScript(EnumScriptType.RANGED_LAUNCHED, event, "target", event.target);
npc.script.callScript(EnumScriptType.ATTACK, event, "target", event.target);
return NpcAPI.EVENT_BUS.post(event);
@@ -202,7 +195,6 @@ public class EventHooks {
return;
NpcEvent.KilledEntityEvent event = new NpcEvent.KilledEntityEvent(npc.wrappedNPC,entity);
ScriptController.Instance.globalNpcScripts.callScript(EnumScriptType.KILLS, event);
npc.script.callScript(EnumScriptType.KILLS, event, "target", entity);
NpcAPI.EVENT_BUS.post(event);
}
@@ -211,7 +203,6 @@ public class EventHooks {
if(npc == null || npc.wrappedNPC == null)
return false;
ScriptController.Instance.globalNpcScripts.callScript(EnumScriptType.TARGET, event);
npc.script.callScript(EnumScriptType.TARGET, event);
return NpcAPI.EVENT_BUS.post(event);
}
@@ -221,7 +212,6 @@ public class EventHooks {
return false;
NpcEvent.TargetLostEvent event = new NpcEvent.TargetLostEvent(npc.wrappedNPC, prevtarget, newTarget);
ScriptController.Instance.globalNpcScripts.callScript(EnumScriptType.TARGET_LOST, event);
npc.script.callScript(EnumScriptType.TARGET_LOST, event);
return NpcAPI.EVENT_BUS.post(event);
}
@@ -231,7 +221,6 @@ public class EventHooks {
return;
NpcEvent.CollideEvent event = new NpcEvent.CollideEvent(npc.wrappedNPC,entity);
ScriptController.Instance.globalNpcScripts.callScript(EnumScriptType.COLLIDE, event);
npc.script.callScript(EnumScriptType.COLLIDE, event, "entity", entity);
NpcAPI.EVENT_BUS.post(event);
}
@@ -240,7 +229,6 @@ public class EventHooks {
if(npc == null || npc.wrappedNPC == null)
return false;
ScriptController.Instance.globalNpcScripts.callScript(EnumScriptType.DAMAGED, event);
npc.script.callScript(EnumScriptType.DAMAGED, event);
return NpcAPI.EVENT_BUS.post(event);
}
@@ -249,7 +237,6 @@ public class EventHooks {
if(npc == null || npc.wrappedNPC == null)
return false;
ScriptController.Instance.globalNpcScripts.callScript(EnumScriptType.KILLED, event);
npc.script.callScript(EnumScriptType.KILLED, event);
return NpcAPI.EVENT_BUS.post(event);
}
@@ -259,14 +246,12 @@ public class EventHooks {
return;
NpcEvent.TimerEvent event = new NpcEvent.TimerEvent(npc.wrappedNPC, id);
ScriptController.Instance.globalNpcScripts.callScript(EnumScriptType.TIMER, event);
npc.script.callScript(EnumScriptType.TIMER, event);
NpcAPI.EVENT_BUS.post(event);
}
public static void onProjectileTick(EntityProjectile projectile) {
ProjectileEvent.UpdateEvent event = new ProjectileEvent.UpdateEvent((IProjectile) NpcAPI.Instance().getIEntity(projectile));
ScriptController.Instance.globalNpcScripts.callScript(EnumScriptType.PROJECTILE_TICK, event);
for(ScriptContainer script : projectile.scripts) {
if(script.isValid()) {
script.run(EnumScriptType.PROJECTILE_TICK, event);
@@ -284,7 +269,6 @@ public class EventHooks {
}
public static void onProjectileImpact(EntityProjectile projectile, ProjectileEvent.ImpactEvent event) {
ScriptController.Instance.globalNpcScripts.callScript(EnumScriptType.PROJECTILE_IMPACT, event);
for(ScriptContainer script : projectile.scripts) {
if(script.isValid()) {
script.run(EnumScriptType.PROJECTILE_IMPACT, event);
@@ -805,4 +789,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));
}
}
@@ -99,13 +99,27 @@ 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;
public static EnumScriptType valueOfIgnoreCase(String channelName) {
channelName = channelName.toUpperCase();
return valueOf(channelName);
try {
return valueOf(channelName);
} catch (IllegalArgumentException exception) {
for (EnumScriptType value : EnumScriptType.values()) {
if (value.function.toUpperCase().equals(channelName)) {
return value;
}
}
throw exception;
}
}
private EnumScriptType(String function) {
@@ -1,10 +1,13 @@
package noppes.npcs.controllers.data;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import noppes.npcs.EventHooks;
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.EnumAnimationPart;
import noppes.npcs.controllers.AnimationController;
@@ -48,6 +51,10 @@ public class Animation implements IAnimation {
this.id = id;
}
public IAnimationData getParent() {
return this.parent;
}
public IFrame currentFrame() {
return currentFrame < frames.size() ? frames.get(currentFrame) : null;
}
@@ -237,6 +244,8 @@ public class Animation implements IAnimation {
this.currentFrameTime++;
if (this.currentFrameTime == this.currentFrame().getDuration()) {
EventHooks.onAnimationFrameExited(this, this.currentFrame());
Frame prevFrame = (Frame) this.currentFrame();
Frame nextFrame = null;
this.currentFrameTime = 0;
@@ -246,6 +255,13 @@ public class Animation implements IAnimation {
} else if (this.loop >= 0 && this.loop < this.frames.size()) {
this.currentFrame = this.loop;
}
if (this.currentFrame() != null) {
EventHooks.onAnimationFrameEntered(this, this.currentFrame());
} else {
EventHooks.onAnimationEnded(this);
}
if (nextFrame != null) {
for (EnumAnimationPart part : EnumAnimationPart.values()) {
if (prevFrame.frameParts.containsKey(part) && nextFrame.frameParts.containsKey(part)) {
@@ -2,24 +2,22 @@ package noppes.npcs.controllers.data;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import noppes.npcs.CommonProxy;
import noppes.npcs.CustomNpcs;
import noppes.npcs.DataDisplay;
import noppes.npcs.Server;
import noppes.npcs.*;
import noppes.npcs.api.entity.IAnimatable;
import noppes.npcs.api.entity.ICustomNpc;
import noppes.npcs.api.entity.IPlayer;
import noppes.npcs.api.handler.data.IAnimation;
import noppes.npcs.api.handler.data.IAnimationData;
import noppes.npcs.client.ClientCacheHandler;
import noppes.npcs.constants.EnumAnimationPart;
import noppes.npcs.constants.EnumPacketClient;
import noppes.npcs.controllers.PlayerDataController;
import noppes.npcs.entity.EntityNPCInterface;
import noppes.npcs.scripted.NpcAPI;
import java.util.Arrays;
import java.util.HashSet;
@@ -34,6 +32,9 @@ public class AnimationData implements IAnimationData {
public Animation animation;
public boolean allowAnimation = false;
private Animation sentAnimation;
private boolean isClientAnimating;
private final HashSet<Integer> cachedAnimationIDs = new HashSet<>();
//Client-side values
@@ -50,6 +51,21 @@ public class AnimationData implements IAnimationData {
}
}
public IAnimatable getEntity() {
if (this.parent instanceof DataDisplay) {
EntityNPCInterface npc = ((DataDisplay)this.parent).npc;
return (ICustomNpc<?>) NpcAPI.Instance().getIEntity(npc);
} else {
EntityPlayer player;
if (this.parent instanceof PlayerData) {
player = ((PlayerData) this.parent).player;
} else {
player = (EntityPlayer) this.parent;
}
return (IPlayer<?>) NpcAPI.Instance().getIEntity(player);
}
}
public void updateClient() {
this.updateClient(new EntityPlayer[0]);
}
@@ -58,6 +74,21 @@ public class AnimationData implements IAnimationData {
EntityLivingBase sendingEntity = parent instanceof PlayerData ? ((PlayerData) parent).player : parent instanceof DataDisplay ? ((DataDisplay) parent).npc : null;
float range = parent instanceof PlayerData ? 160 : 60;
if (sendingEntity != null) {
boolean prevIsClientAnimating = this.isClientAnimating && this.sentAnimation.currentFrame() != null;
this.isClientAnimating = this.allowAnimation && this.animation != null;
if (prevIsClientAnimating && (!this.isClientAnimating || this.animation != this.sentAnimation)) {
EventHooks.onAnimationEnded(this.sentAnimation);
}
if (this.isClientAnimating) {
this.sentAnimation = this.animation;
}
if (this.animation != null && this.allowAnimation) {
if (EventHooks.onAnimationStarted(this.animation))
return;
EventHooks.onAnimationFrameEntered(this.animation, this.animation.currentFrame());
}
if (!CustomNpcs.proxy.hasClient()) {
CommonProxy.serverPlayingAnimations.add(this.animation);
}
@@ -236,13 +236,14 @@ public class DataScript implements INpcScriptHandler {
@Override
public void callScript(EnumScriptType type, Event event) {
ScriptController.Instance.globalNpcScripts.callScript(type, event);
callScript(type, event, "$$IGNORED$$", null);
}
@Override
public void callScript(String hookName, Event event) {
try {
EnumScriptType enumScriptType = EnumScriptType.valueOf(hookName);
EnumScriptType enumScriptType = EnumScriptType.valueOfIgnoreCase(hookName);
this.callScript(enumScriptType, event);
} catch (IllegalArgumentException ignored) {}
}
@@ -305,4 +306,4 @@ public class DataScript implements INpcScriptHandler {
public Collection<ScriptContainer> getNPCScripts() {
return this.scripts.values();
}
}
}
@@ -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;
}
}
}
}
@@ -11,7 +11,7 @@ import noppes.npcs.api.event.IBlockEvent;
import noppes.npcs.constants.EnumScriptType;
import noppes.npcs.scripted.NpcAPI;
public class BlockEvent extends CustomNPCsEvent implements IBlockEvent {
public abstract class BlockEvent extends CustomNPCsEvent implements IBlockEvent {
public IBlock block;
public BlockEvent(IBlock block){
this.block = block;
@@ -10,7 +10,7 @@ import noppes.npcs.api.item.IItemStack;
import noppes.npcs.constants.EnumScriptType;
import noppes.npcs.scripted.NpcAPI;
public class CustomGuiEvent extends CustomNPCsEvent implements ICustomGuiEvent {
public abstract class CustomGuiEvent extends CustomNPCsEvent implements ICustomGuiEvent {
public final IPlayer player;
public final ICustomGui gui;
@@ -9,12 +9,10 @@ import noppes.npcs.api.event.ICustomNPCsEvent;
import noppes.npcs.api.handler.data.INaturalSpawn;
import noppes.npcs.constants.EnumScriptType;
public class CustomNPCsEvent extends Event implements ICustomNPCsEvent {
public abstract class CustomNPCsEvent extends Event implements ICustomNPCsEvent {
public final AbstractNpcAPI API = AbstractNpcAPI.Instance();
public String getHookName() {
return "CNPCEvent";
}
public abstract String getHookName();
public CustomNPCsEvent() {
}
@@ -6,7 +6,7 @@ import noppes.npcs.api.event.IDialogEvent;
import noppes.npcs.api.handler.data.IDialog;
import noppes.npcs.constants.EnumScriptType;
public class DialogEvent extends CustomNPCsEvent implements IDialogEvent {
public abstract class DialogEvent extends CustomNPCsEvent implements IDialogEvent {
public final IDialog dialog;
public final IPlayer player;
@@ -6,7 +6,7 @@ import noppes.npcs.api.event.IFactionEvent;
import noppes.npcs.api.handler.data.IFaction;
import noppes.npcs.constants.EnumScriptType;
public class FactionEvent extends CustomNPCsEvent implements IFactionEvent {
public abstract class FactionEvent extends CustomNPCsEvent implements IFactionEvent {
public final IFaction faction;
public final IPlayer player;
@@ -7,7 +7,7 @@ import noppes.npcs.api.event.IItemEvent;
import noppes.npcs.api.item.IItemCustom;
import noppes.npcs.constants.EnumScriptType;
public class ItemEvent extends CustomNPCsEvent implements IItemEvent {
public abstract class ItemEvent extends CustomNPCsEvent implements IItemEvent {
public final IItemCustom item;
public ItemEvent(IItemCustom item) {
@@ -20,7 +20,7 @@ import noppes.npcs.scripted.NpcAPI;
import java.util.ArrayList;
public class NpcEvent extends CustomNPCsEvent implements INpcEvent {
public abstract class NpcEvent extends CustomNPCsEvent implements INpcEvent {
public final ICustomNpc npc;
public NpcEvent(ICustomNpc npc) {
@@ -7,7 +7,7 @@ import noppes.npcs.api.handler.data.IParty;
import noppes.npcs.api.handler.data.IQuest;
import noppes.npcs.constants.EnumScriptType;
public class PartyEvent extends CustomNPCsEvent implements IPartyEvent {
public abstract class PartyEvent extends CustomNPCsEvent implements IPartyEvent {
public final IQuest quest;
public final IParty party;
@@ -18,7 +18,7 @@ import noppes.npcs.api.item.IItemStack;
import noppes.npcs.constants.EnumScriptType;
import noppes.npcs.scripted.NpcAPI;
public class PlayerEvent extends CustomNPCsEvent implements IPlayerEvent {
public abstract class PlayerEvent extends CustomNPCsEvent implements IPlayerEvent {
public final IPlayer player;
public PlayerEvent(IPlayer player) {
@@ -4,8 +4,9 @@ import noppes.npcs.api.IBlock;
import noppes.npcs.api.entity.IEntity;
import noppes.npcs.api.entity.IProjectile;
import noppes.npcs.api.event.IProjectileEvent;
import noppes.npcs.constants.EnumScriptType;
public class ProjectileEvent extends CustomNPCsEvent implements IProjectileEvent {
public abstract class ProjectileEvent extends CustomNPCsEvent implements IProjectileEvent {
public IProjectile projectile;
public IEntity source;
@@ -29,6 +30,11 @@ public class ProjectileEvent extends CustomNPCsEvent implements IProjectileEvent
public UpdateEvent(IProjectile projectile) {
super(projectile);
}
@Override
public String getHookName() {
return EnumScriptType.PROJECTILE_TICK.function;
}
}
/**
@@ -62,5 +68,10 @@ public class ProjectileEvent extends CustomNPCsEvent implements IProjectileEvent
return (IBlock) target;
return null;
}
@Override
public String getHookName() {
return EnumScriptType.PROJECTILE_IMPACT.function;
}
}
}
@@ -7,7 +7,7 @@ import noppes.npcs.api.handler.data.IQuest;
import noppes.npcs.api.item.IItemStack;
import noppes.npcs.constants.EnumScriptType;
public class QuestEvent extends CustomNPCsEvent implements IQuestEvent {
public abstract class QuestEvent extends CustomNPCsEvent implements IQuestEvent {
public final IQuest quest;
public final IPlayer player;