2023-07-27 17:19:15 -05:00
|
|
|
package noppes.npcs.controllers.data;
|
2023-01-12 03:13:39 -05:00
|
|
|
|
2023-01-16 13:53:15 -06:00
|
|
|
|
2024-01-30 19:17:55 -06:00
|
|
|
import cpw.mods.fml.common.FMLCommonHandler;
|
|
|
|
|
import cpw.mods.fml.relauncher.Side;
|
2023-12-13 14:32:09 -06:00
|
|
|
import net.minecraft.entity.EntityLivingBase;
|
2023-01-16 13:53:15 -06:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2023-12-13 14:32:09 -06:00
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
2023-01-12 03:13:39 -05:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2023-12-13 14:32:09 -06:00
|
|
|
import net.minecraft.util.AxisAlignedBB;
|
2023-07-27 17:19:15 -05:00
|
|
|
import noppes.npcs.DataDisplay;
|
|
|
|
|
import noppes.npcs.Server;
|
2023-01-16 13:53:15 -06:00
|
|
|
import noppes.npcs.api.handler.data.IAnimation;
|
2024-02-15 17:54:28 -05:00
|
|
|
import noppes.npcs.api.handler.data.IAnimationData;
|
2024-01-30 19:17:55 -06:00
|
|
|
import noppes.npcs.constants.EnumAnimationPart;
|
2023-01-12 03:13:39 -05:00
|
|
|
import noppes.npcs.constants.EnumPacketClient;
|
2023-12-13 14:32:09 -06:00
|
|
|
import noppes.npcs.controllers.PlayerDataController;
|
2023-01-12 03:13:39 -05:00
|
|
|
import noppes.npcs.entity.EntityNPCInterface;
|
|
|
|
|
|
2023-12-22 17:25:40 -06:00
|
|
|
import java.util.Arrays;
|
2023-12-13 14:32:09 -06:00
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.List;
|
2024-01-30 19:17:55 -06:00
|
|
|
import java.util.Map;
|
2023-12-13 14:32:09 -06:00
|
|
|
|
2023-01-16 13:53:15 -06:00
|
|
|
public class AnimationData implements IAnimationData {
|
|
|
|
|
//Server-side: DataDisplay, PlayerData
|
|
|
|
|
//Client-side: DataDisplay, EntityPlayer
|
|
|
|
|
public Object parent;
|
2023-01-12 03:13:39 -05:00
|
|
|
|
2023-01-16 13:53:15 -06:00
|
|
|
public Animation animation;
|
|
|
|
|
public boolean allowAnimation = false;
|
2023-01-12 03:13:39 -05:00
|
|
|
|
2023-12-13 14:32:09 -06:00
|
|
|
private final HashSet<Integer> cachedAnimationIDs = new HashSet<>();
|
|
|
|
|
|
2023-12-22 17:25:40 -06:00
|
|
|
//Client-side values
|
|
|
|
|
public EntityLivingBase animationEntity;
|
|
|
|
|
public int finishedTime = -1;
|
|
|
|
|
public int finishedFrame = -1;
|
|
|
|
|
|
2023-01-16 13:53:15 -06:00
|
|
|
public AnimationData(Object parent){
|
|
|
|
|
this.parent = parent;
|
2023-12-22 17:25:40 -06:00
|
|
|
if (parent instanceof DataDisplay) {
|
|
|
|
|
this.animationEntity = ((DataDisplay) parent).npc;
|
|
|
|
|
} else if (parent instanceof EntityPlayer) {
|
|
|
|
|
this.animationEntity = (EntityLivingBase) parent;
|
|
|
|
|
}
|
2023-01-12 03:13:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void updateClient() {
|
2023-12-22 17:25:40 -06:00
|
|
|
this.updateClient(new EntityPlayer[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void updateClient(EntityPlayer... excludedPlayers) {
|
2023-12-13 14:32:09 -06:00
|
|
|
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) {
|
|
|
|
|
List<EntityPlayer> entities = sendingEntity.worldObj.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getBoundingBox(
|
2024-02-19 19:23:43 -05:00
|
|
|
sendingEntity.posX - range, sendingEntity.posY - range, sendingEntity.posZ - range,
|
|
|
|
|
sendingEntity.posX + range, sendingEntity.posY + range, sendingEntity.posZ + range));
|
2023-12-13 14:32:09 -06:00
|
|
|
|
2023-12-22 17:25:40 -06:00
|
|
|
entities.removeIf(player -> Arrays.stream(excludedPlayers).anyMatch(exp -> player == exp));
|
|
|
|
|
|
2023-12-13 14:32:09 -06:00
|
|
|
NBTTagCompound animationNBT = null;
|
|
|
|
|
for (EntityPlayer player : entities) {
|
|
|
|
|
AnimationData animationData = PlayerDataController.Instance.getPlayerData(player).animationData;
|
|
|
|
|
if (animationNBT == null && this.animation != null && !animationData.isCached(this.animation.getID())) {
|
|
|
|
|
animationNBT = this.animation.writeToNBT();
|
|
|
|
|
}
|
|
|
|
|
animationData.viewAnimation(this.animation, sendingEntity, this, animationNBT);
|
|
|
|
|
}
|
2023-01-16 13:53:15 -06:00
|
|
|
}
|
2023-01-12 03:13:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isActive() {
|
2023-02-06 23:17:05 -06:00
|
|
|
if (!this.allowAnimation || this.animation == null || this.animation.currentFrame == this.animation.frames.size() || animation.currentFrame() == null)
|
2023-01-12 03:13:39 -05:00
|
|
|
return false;
|
|
|
|
|
|
2023-01-16 13:53:15 -06:00
|
|
|
if (this.parent instanceof DataDisplay) {
|
|
|
|
|
EntityNPCInterface npc = ((DataDisplay)this.parent).npc;
|
|
|
|
|
if (!npc.isEntityAlive())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return animation.whileAttacking && npc.isAttacking() || animation.whileMoving && npc.isWalking() || animation.whileStanding && !npc.isWalking();
|
|
|
|
|
} else {
|
|
|
|
|
EntityPlayer player;
|
|
|
|
|
if (this.parent instanceof PlayerData) {
|
|
|
|
|
player = ((PlayerData) this.parent).player;
|
|
|
|
|
} else {
|
|
|
|
|
player = (EntityPlayer) this.parent;
|
|
|
|
|
}
|
|
|
|
|
if (!player.isEntityAlive())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
boolean moving = Math.sqrt(player.motionX*player.motionX + player.motionY*player.motionY + player.motionZ*player.motionZ) != 0.0D;
|
|
|
|
|
|
|
|
|
|
return animation.whileAttacking && player.getLastAttackerTime() - player.ticksExisted < 20 || animation.whileMoving && moving || animation.whileStanding && !moving;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-13 14:32:09 -06:00
|
|
|
public boolean isCached(int id) {
|
|
|
|
|
return this.cachedAnimationIDs.contains(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void cacheAnimation(int id) {
|
|
|
|
|
this.cachedAnimationIDs.add(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void uncacheAnimation(int id) {
|
|
|
|
|
this.cachedAnimationIDs.remove(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void viewAnimation(Animation animation, EntityLivingBase entity, AnimationData animationData, NBTTagCompound animationNBT) {
|
|
|
|
|
NBTTagCompound data = animationData.writeToNBT(new NBTTagCompound());
|
|
|
|
|
if (animation != null) {
|
|
|
|
|
if (this.cachedAnimationIDs.contains(animation.getID())) {
|
|
|
|
|
data.setInteger("AnimationID", animation.getID());
|
|
|
|
|
} else {
|
|
|
|
|
data.setTag("Animation", animationNBT);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!(entity instanceof EntityPlayer)) {
|
|
|
|
|
data.setInteger("EntityId", entity.getEntityId());
|
|
|
|
|
}
|
|
|
|
|
Server.sendData((EntityPlayerMP) ((PlayerData) parent).player, EnumPacketClient.UPDATE_ANIMATIONS, data, entity.getCommandSenderName());
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 13:53:15 -06:00
|
|
|
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
|
2023-03-02 23:45:38 -05:00
|
|
|
compound.setBoolean("AllowAnimation", allowAnimation);
|
2023-01-16 13:53:15 -06:00
|
|
|
return compound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void readFromNBT(NBTTagCompound compound) {
|
2023-12-22 17:25:40 -06:00
|
|
|
this.setEnabled(compound.getBoolean("AllowAnimation"));
|
2023-01-16 13:53:15 -06:00
|
|
|
}
|
2023-01-12 03:13:39 -05:00
|
|
|
|
2023-01-16 13:53:15 -06:00
|
|
|
public void setEnabled(boolean enabled) {
|
2023-12-22 17:25:40 -06:00
|
|
|
if (this.allowAnimation != enabled) {
|
|
|
|
|
this.allowAnimation = enabled;
|
|
|
|
|
if (this.parent instanceof EntityPlayer) {
|
|
|
|
|
this.finishedTime = enabled ? -1 : ((EntityPlayer) this.parent).getAge();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-16 13:53:15 -06:00
|
|
|
}
|
2023-01-12 03:13:39 -05:00
|
|
|
|
2023-01-16 13:53:15 -06:00
|
|
|
public boolean enabled() {
|
|
|
|
|
return this.allowAnimation;
|
2023-01-12 03:13:39 -05:00
|
|
|
}
|
|
|
|
|
|
2023-01-16 13:53:15 -06:00
|
|
|
public void setAnimation(IAnimation animation) {
|
2024-01-30 19:17:55 -06:00
|
|
|
if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER) {
|
|
|
|
|
this.animation = (Animation) animation;
|
|
|
|
|
} else {
|
|
|
|
|
Animation newAnim = new Animation();
|
|
|
|
|
newAnim.readFromNBT(((Animation) animation).writeToNBT());
|
2024-02-19 18:24:39 -06:00
|
|
|
if (this.isActive() && !newAnim.frames.isEmpty()) {
|
2024-01-30 19:17:55 -06:00
|
|
|
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];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.animation = newAnim;
|
|
|
|
|
newAnim.parent = this;
|
|
|
|
|
}
|
2023-01-16 13:53:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IAnimation getAnimation() {
|
|
|
|
|
return animation;
|
|
|
|
|
}
|
2023-01-12 03:13:39 -05:00
|
|
|
}
|