Loads of changes from the animation branch

This commit is contained in:
Luis C.M. committed 2025-01-22 18:54:00 -06:00
1 parent 990419a411
commit 572077b7e5
13 files changed
+1091 -692

No files matched your search

@@ -3,19 +3,25 @@ package noppes.npcs.controllers.data;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import net.minecraft.entity.Entity;
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.DataDisplay;
import noppes.npcs.EventHooks;
import noppes.npcs.Server;
import noppes.npcs.api.entity.IAnimatable;
import noppes.npcs.api.entity.IEntity;
import noppes.npcs.api.handler.data.IAnimation;
import noppes.npcs.api.handler.data.IAnimationData;
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;
@@ -29,20 +35,47 @@ public class AnimationData implements IAnimationData {
public Animation animation;
public boolean allowAnimation = false;
public long animatingTime = 0;
public Animation currentClientAnimation;
private boolean isClientAnimating;
private final HashSet<Integer> cachedAnimationIDs = new HashSet<>();
//Client-side values
public EntityLivingBase animationEntity;
public int finishedTime = -1;
public int finishedFrame = -1;
public AnimationData(Object parent){
this.parent = parent;
if (parent instanceof DataDisplay) {
this.animationEntity = ((DataDisplay) parent).npc;
} else if (parent instanceof EntityPlayer) {
this.animationEntity = (EntityLivingBase) parent;
}
public static AnimationData getData(Entity entity) {
if (entity instanceof EntityPlayerMP) {
return PlayerData.get((EntityPlayer) entity).animationData;
} else if (entity instanceof EntityNPCInterface) {
return ((EntityNPCInterface)entity).display.animationData;
} else {
return null;
}
}
public IAnimatable getEntity() {
IEntity<?> entity = NpcAPI.Instance().getIEntity(this.getMCEntity());
if (entity instanceof IAnimatable) {
return (IAnimatable) entity;
}
return null;
}
public EntityLivingBase getMCEntity() {
if (this.parent instanceof DataDisplay) {
return ((DataDisplay) this.parent).npc;
} else {
if (this.parent instanceof PlayerData) {
return ((PlayerData) this.parent).player;
} else {
return (EntityPlayer) this.parent;
}
}
}
@@ -57,6 +90,30 @@ public class AnimationData implements IAnimationData {
if(sendingEntity.dimension != sendingEntity.worldObj.provider.dimensionId)
sendingEntity.dimension = sendingEntity.worldObj.provider.dimensionId;
this.animatingTime = 0;
boolean prevIsClientAnimating = this.isClientAnimating && this.currentClientAnimation.currentFrame() != null;
this.isClientAnimating = this.allowAnimation && this.animation != null;
if (prevIsClientAnimating && (!this.isClientAnimating || this.animation != this.currentClientAnimation)) {
EventHooks.onAnimationEnded(this.currentClientAnimation);
}
if (this.isClientAnimating) {
this.currentClientAnimation = this.animation;
}
if (this.animation != null && this.allowAnimation) {
if (EventHooks.onAnimationStarted(this.animation))
return;
EventHooks.onAnimationFrameEntered(this.animation, this.animation.currentFrame());
}
synchronized (CommonProxy.serverPlayingAnimations) {
if (this.isClientAnimating && this.getMCEntity() != null &&
!this.getMCEntity().worldObj.isRemote && this.animation != null) {
CommonProxy.serverPlayingAnimations.add(this);
}
}
List<EntityPlayer> entities = sendingEntity.worldObj.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getBoundingBox(
sendingEntity.posX - range, sendingEntity.posY - range, sendingEntity.posZ - range,
sendingEntity.posX + range, sendingEntity.posY + range, sendingEntity.posZ + range));
@@ -69,13 +126,21 @@ public class AnimationData implements IAnimationData {
if (animationNBT == null && this.animation != null && !animationData.isCached(this.animation.getID())) {
animationNBT = this.animation.writeToNBT();
}
animationData.viewAnimation(this.animation, sendingEntity, this, animationNBT);
animationData.viewAnimation(this.animation, this, animationNBT);
}
}
}
public boolean isClientAnimating() {
return this.isClientAnimating;
}
public boolean isActive() {
if (!this.allowAnimation || this.animation == null || this.animation.currentFrame == this.animation.frames.size() || animation.currentFrame() == null)
return this.isActive(this.animation);
}
public boolean isActive(Animation animation) {
if (!this.allowAnimation || animation == null || animation.currentFrame == animation.frames.size() || animation.currentFrame() == null)
return false;
if (this.parent instanceof DataDisplay) {
@@ -100,6 +165,25 @@ public class AnimationData implements IAnimationData {
}
}
public void increaseTime() {
Animation updateAnimation = null;
if (this.isActive(animation.parent.currentClientAnimation)) {
updateAnimation = this.currentClientAnimation;
} else {
this.isClientAnimating = false;
if (this.isActive()) {
updateAnimation = this.animation;
}
}
if (updateAnimation != null && updateAnimation.increaseTime()) {
Frame frame = (Frame) updateAnimation.currentFrame();
if (frame != null) {
this.animatingTime += frame.customized ? frame.tickDuration : updateAnimation.tickDuration;
}
}
}
public boolean isCached(int id) {
return this.cachedAnimationIDs.contains(id);
}
@@ -116,27 +200,77 @@ public class AnimationData implements IAnimationData {
this.cachedAnimationIDs.clear();
}
public void viewAnimation(Animation animation, EntityLivingBase entity, AnimationData animationData, NBTTagCompound animationNBT) {
NBTTagCompound data = animationData.writeToNBT(new NBTTagCompound());
public void viewAnimation(Animation animation, AnimationData animationData, NBTTagCompound animationNBT) {
this.viewAnimation(animation, animationData, animationNBT, animationData.allowAnimation, -1, -1);
}
public boolean viewAnimation(Animation animation, AnimationData animationData, NBTTagCompound animationNBT, boolean enabled, int currentFrame, int time) {
if (animation != null
&& (currentFrame >= animation.frames.size()
|| currentFrame >= 0 && animation.frames.get(currentFrame).getDuration() < time)) {
return false;
}
boolean prevEnabled = animationData.allowAnimation;
animationData.allowAnimation = enabled;
NBTTagCompound data = animationData.viewWriteNBT(new NBTTagCompound());
animationData.allowAnimation = prevEnabled;
if (animation != null && currentFrame >= 0 && currentFrame < animation.frames.size()) {
data.setInteger("Frame", currentFrame);
data.setInteger("Time", time);
}
if (animation != null) {
if (this.cachedAnimationIDs.contains(animation.getID())) {
if (this.cachedAnimationIDs.contains(animation.getID()) && !data.hasKey("Frame")) {
data.setInteger("AnimationID", animation.getID());
} else {
data.setTag("Animation", animationNBT);
}
}
IAnimatable animatable = animationData.getEntity();
Entity entity = ((IEntity<?>) animatable).getMCEntity();
if (!(entity instanceof EntityPlayer)) {
data.setInteger("EntityId", entity.getEntityId());
}
Server.sendData((EntityPlayerMP) ((PlayerData) parent).player, EnumPacketClient.UPDATE_ANIMATIONS, data, entity.getCommandSenderName());
return true;
}
public NBTTagCompound viewWriteNBT(NBTTagCompound compound) {
compound.setBoolean("AllowAnimation", allowAnimation);
return compound;
}
public void viewReadFromNBT(NBTTagCompound compound) {
this.setEnabled(compound.getBoolean("AllowAnimation"));
}
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
if (this.currentClientAnimation != null) {
compound.setBoolean("IsClientAnimating", isClientAnimating);
if (this.isClientAnimating) {
compound.setTag("CurrentAnimation", currentClientAnimation.writeToNBT());
}
}
compound.setBoolean("AllowAnimation", allowAnimation);
return compound;
}
public void readFromNBT(NBTTagCompound compound) {
Entity entity = this.getMCEntity();
boolean isServer = entity != null && !entity.worldObj.isRemote;
if (compound.hasKey("IsClientAnimating") && isServer) {
this.isClientAnimating = compound.getBoolean("IsClientAnimating");
if (this.isClientAnimating) {
this.currentClientAnimation = new Animation();
this.currentClientAnimation.parent = this;
this.currentClientAnimation.readFromNBT(compound.getCompoundTag("CurrentAnimation"));
this.animation = this.currentClientAnimation;
}
}
this.setEnabled(compound.getBoolean("AllowAnimation"));
}
@@ -154,32 +288,41 @@ public class AnimationData implements IAnimationData {
}
public void setAnimation(IAnimation animation) {
if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER) {
this.animation = animation != null ? (Animation) animation : null;
} else {
if (animation != null) {
Animation newAnim = new Animation();
newAnim.readFromNBT(((Animation) animation).writeToNBT());
if (this.isActive() && !newAnim.frames.isEmpty()) {
Frame frame = (Frame) this.animation.currentFrame();
if (frame != null) {
Frame firstFrame = newAnim.frames.get(0);
for (Map.Entry<EnumAnimationPart, FramePart> entry : frame.frameParts.entrySet()) {
if (firstFrame.frameParts.containsKey(entry.getKey())) {
FramePart prevFramePart = entry.getValue();
FramePart newFramePart = firstFrame.frameParts.get(entry.getKey());
for (int i = 0; i < 3; i++) {
newFramePart.prevPivots[i] = prevFramePart.prevPivots[i];
newFramePart.prevRotations[i] = prevFramePart.prevRotations[i];
}
}
Animation newAnim = null;
if (animation != null) {
newAnim = new Animation();
newAnim.readFromNBT(((Animation) animation).writeToNBT());
newAnim.currentFrame = 0;
newAnim.currentFrameTime = 0;
newAnim.parent = this;
}
if (this.getMCEntity() != null && this.getMCEntity().worldObj.isRemote && newAnim != null) {
this.animatingTime = 0;
synchronized (CommonProxy.clientPlayingAnimations) {
CommonProxy.clientPlayingAnimations.add(this);
}
}
Animation prevAnim = this.animation;
this.animation = newAnim;
if (this.getMCEntity() != null && this.getMCEntity().worldObj.isRemote &&
this.isActive() && prevAnim != null && newAnim != null && !newAnim.frames.isEmpty()) {
Frame frame = (Frame) prevAnim.currentFrame();
if (frame != null) {
Frame firstFrame = newAnim.frames.get(0);
for (Map.Entry<EnumAnimationPart, FramePart> entry : frame.frameParts.entrySet()) {
if (firstFrame.frameParts.containsKey(entry.getKey())) {
FramePart prevFramePart = entry.getValue();
FramePart newFramePart = firstFrame.frameParts.get(entry.getKey());
for (int i = 0; i < 3; i++) {
newFramePart.prevPivots[i] = prevFramePart.prevPivots[i];
newFramePart.prevRotations[i] = prevFramePart.prevRotations[i];
}
}
}
this.animation = newAnim;
newAnim.parent = this;
} else {
this.animation = null;
}
}
}
@@ -187,4 +330,8 @@ public class AnimationData implements IAnimationData {
public IAnimation getAnimation() {
return animation;
}
public long getAnimatingTime() {
return this.animatingTime;
}
}