From ae2edfd79f893e9f6b937796e4fcf0c62990015c Mon Sep 17 00:00:00 2001 From: luisc Date: Fri, 5 Apr 2024 13:14:37 -0500 Subject: [PATCH] Fixed animations not ticking correctly sometimes. --- src/main/java/noppes/npcs/CommonProxy.java | 60 +++++++++---------- .../npcs/controllers/data/Animation.java | 8 +++ .../npcs/controllers/data/AnimationData.java | 24 ++++++-- 3 files changed, 55 insertions(+), 37 deletions(-) diff --git a/src/main/java/noppes/npcs/CommonProxy.java b/src/main/java/noppes/npcs/CommonProxy.java index 5bfd6b54f..80bf89d78 100644 --- a/src/main/java/noppes/npcs/CommonProxy.java +++ b/src/main/java/noppes/npcs/CommonProxy.java @@ -19,6 +19,7 @@ import noppes.npcs.blocks.tiles.TileNpcContainer; import noppes.npcs.constants.EnumGuiType; import noppes.npcs.containers.*; import noppes.npcs.controllers.data.Animation; +import noppes.npcs.controllers.data.AnimationData; import noppes.npcs.controllers.data.PlayerData; import noppes.npcs.entity.EntityNPCInterface; import noppes.npcs.util.MillisTimer; @@ -26,8 +27,8 @@ import noppes.npcs.util.MillisTimer; import java.util.HashSet; public class CommonProxy implements IGuiHandler { - public final static HashSet clientPlayingAnimations = new HashSet<>(); - public final static HashSet serverPlayingAnimations = new HashSet<>(); + public final static HashSet clientPlayingAnimations = new HashSet<>(); + public final static HashSet serverPlayingAnimations = new HashSet<>(); protected MillisTimer animationTimer = new MillisTimer(1000); protected static long totalServerTicks; protected static long totalClientTicks; @@ -48,18 +49,22 @@ public class CommonProxy implements IGuiHandler { } for (int i = 0; i < animationTimer.elapsedTicks; ++i) { - for (Animation animation : clientPlayingAnimations) { - int tickDuration = animation.currentFrame().tickDuration(); - if (updateAnimation(animation) && totalClientTicks % tickDuration == 0) { - animation.increaseTime(); + for (AnimationData data : clientPlayingAnimations) { + if (data.animation != null) { + int tickDuration = data.animation.currentFrame().tickDuration(); + if (totalClientTicks % tickDuration == 0) { + data.increaseTime(); + } } } totalClientTicks++; - for (Animation animation : serverPlayingAnimations) { - int tickDuration = animation.currentFrame().tickDuration(); - if (updateAnimation(animation) && totalServerTicks % tickDuration == 0) { - animation.increaseTime(); + for (AnimationData data : serverPlayingAnimations) { + if (data.animation != null) { + int tickDuration = data.animation.currentFrame().tickDuration(); + if (totalServerTicks % tickDuration == 0) { + data.increaseTime(); + } } } totalServerTicks++; @@ -83,44 +88,33 @@ public class CommonProxy implements IGuiHandler { thread.start(); } - private boolean updateAnimation(Animation animation) { - return animation.parent.isActive() - || animation.parent.animation == null - && animation.parent.isActive(animation.parent.currentClientAnimation); - } - - private boolean removeAnimation(Animation animation) { - if (animation.currentFrame() == null || animation.parent == null) { - return true; - } - + private boolean removeAnimation(AnimationData data) { Entity entity = null; - if (animation.parent.parent instanceof DataDisplay) { - entity = ((DataDisplay)animation.parent.parent).npc; + if (data.parent instanceof DataDisplay) { + entity = ((DataDisplay)data.parent).npc; } else { - if (animation.parent.parent instanceof PlayerData) { - entity = ((PlayerData) animation.parent.parent).player; - } else if (animation.parent.parent instanceof EntityPlayer) { - entity = (EntityPlayer) animation.parent.parent; + if (data.parent instanceof PlayerData) { + entity = ((PlayerData) data.parent).player; + } else if (data.parent instanceof EntityPlayer) { + entity = (EntityPlayer) data.parent; } } if (entity == null) return true; if (entity.worldObj != null && entity.worldObj.isRemote) { - return animation.parent.animation != animation || clientRemoveAnimation(entity); + return clientRemoveAnimation(entity); } else { - return !animation.parent.isClientAnimating() - || animation.parent.animation != animation && animation.parent.currentClientAnimation != animation - || entity.worldObj == null || !entity.worldObj.loadedEntityList.contains(entity) + return entity.worldObj == null || !entity.worldObj.loadedEntityList.contains(entity) || CustomNpcs.getServer().isServerStopped(); } } @SideOnly(Side.CLIENT) private boolean clientRemoveAnimation(Entity entity) { - return Minecraft.getMinecraft().theWorld == null - || Minecraft.getMinecraft().currentScreen == null && !Minecraft.getMinecraft().theWorld.loadedEntityList.contains(entity); + Minecraft mc = Minecraft.getMinecraft(); + return mc.theWorld == null + || mc.currentScreen == null && entity != mc.thePlayer && !mc.theWorld.loadedEntityList.contains(entity); } public PlayerData getPlayerData(EntityPlayer player) { diff --git a/src/main/java/noppes/npcs/controllers/data/Animation.java b/src/main/java/noppes/npcs/controllers/data/Animation.java index 5de123d88..41f05c971 100644 --- a/src/main/java/noppes/npcs/controllers/data/Animation.java +++ b/src/main/java/noppes/npcs/controllers/data/Animation.java @@ -179,6 +179,14 @@ public class Animation implements IAnimation { id = newID; } + public long getTotalTime() { + long time = 0; + for (Frame frame : this.frames) { + time += (long) frame.duration * (frame.customized ? frame.tickDuration : this.tickDuration); + } + return time; + } + public void readFromNBT(NBTTagCompound compound){ if(compound.hasKey("ID")){ id = compound.getInteger("ID"); diff --git a/src/main/java/noppes/npcs/controllers/data/AnimationData.java b/src/main/java/noppes/npcs/controllers/data/AnimationData.java index ba7fde579..966789d9a 100644 --- a/src/main/java/noppes/npcs/controllers/data/AnimationData.java +++ b/src/main/java/noppes/npcs/controllers/data/AnimationData.java @@ -32,6 +32,7 @@ public class AnimationData implements IAnimationData { public Animation animation; public boolean allowAnimation = false; + public long animatingTime = 0; public Animation currentClientAnimation; private boolean isClientAnimating; @@ -93,6 +94,7 @@ public class AnimationData implements IAnimationData { EventHooks.onAnimationEnded(this.currentClientAnimation); } if (this.isClientAnimating) { + this.animatingTime = 0; this.currentClientAnimation = this.animation; } @@ -105,7 +107,7 @@ public class AnimationData implements IAnimationData { synchronized (CommonProxy.serverPlayingAnimations) { if (this.isClientAnimating && this.getMCEntity() != null && !this.getMCEntity().worldObj.isRemote && this.animation != null) { - CommonProxy.serverPlayingAnimations.add(this.animation); + CommonProxy.serverPlayingAnimations.add(this); } } @@ -160,6 +162,14 @@ public class AnimationData implements IAnimationData { } } + public void increaseTime() { + if (this.isActive()) { + this.animation.increaseTime(); + } else if (this.isActive(animation.parent.currentClientAnimation)) { + this.currentClientAnimation.increaseTime(); + } + } + public boolean isCached(int id) { return this.cachedAnimationIDs.contains(id); } @@ -259,9 +269,11 @@ public class AnimationData implements IAnimationData { newAnim.parent = this; } - synchronized (CommonProxy.clientPlayingAnimations) { - if (this.getMCEntity() != null && this.getMCEntity().worldObj.isRemote && newAnim != null) { - CommonProxy.clientPlayingAnimations.add(newAnim); + + if (this.getMCEntity() != null && this.getMCEntity().worldObj.isRemote && newAnim != null) { + this.animatingTime = 0; + synchronized (CommonProxy.clientPlayingAnimations) { + CommonProxy.clientPlayingAnimations.add(this); } } @@ -290,4 +302,8 @@ public class AnimationData implements IAnimationData { public IAnimation getAnimation() { return animation; } + + public long getAnimatingTime() { + return this.animatingTime; + } }