mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-16 11:54:47 +00:00
Implemented animation caching
This commit is contained in:
1 parent
7820ad171b
commit
a67604e22d
7 files changed
+99
-24
No files matched your search
@@ -1,17 +1,22 @@
|
||||
package noppes.npcs.controllers.data;
|
||||
|
||||
|
||||
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.DataDisplay;
|
||||
import noppes.npcs.Server;
|
||||
import noppes.npcs.api.handler.data.IAnimationData;
|
||||
import noppes.npcs.api.handler.data.IAnimation;
|
||||
import noppes.npcs.constants.EnumPacketClient;
|
||||
import noppes.npcs.controllers.data.Animation;
|
||||
import noppes.npcs.controllers.data.PlayerData;
|
||||
import noppes.npcs.controllers.PlayerDataController;
|
||||
import noppes.npcs.entity.EntityNPCInterface;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
public class AnimationData implements IAnimationData {
|
||||
//Server-side: DataDisplay, PlayerData
|
||||
//Client-side: DataDisplay, EntityPlayer
|
||||
@@ -20,19 +25,29 @@ public class AnimationData implements IAnimationData {
|
||||
public Animation animation;
|
||||
public boolean allowAnimation = false;
|
||||
|
||||
private final HashSet<Integer> cachedAnimationIDs = new HashSet<>();
|
||||
|
||||
public AnimationData(Object parent){
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public void updateClient() {
|
||||
NBTTagCompound compound = new NBTTagCompound();
|
||||
compound = this.writeToNBT(compound);
|
||||
compound.setTag("Animation",this.animation == null ? new NBTTagCompound() : this.animation.writeToNBT());
|
||||
if (parent instanceof PlayerData) {
|
||||
Server.sendToAll(EnumPacketClient.UPDATE_ANIMATIONS, compound, ((PlayerData) parent).player.getCommandSenderName());
|
||||
} else if (parent instanceof DataDisplay) {
|
||||
compound.setInteger("EntityId",((DataDisplay) parent).npc.getEntityId());
|
||||
Server.sendAssociatedData(((DataDisplay) parent).npc, EnumPacketClient.UPDATE_ANIMATIONS, compound);
|
||||
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(
|
||||
sendingEntity.posX - range, sendingEntity.posY - range, sendingEntity.posZ - range,
|
||||
sendingEntity.posX + range, sendingEntity.posY + range, sendingEntity.posZ + range));
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +77,33 @@ public class AnimationData implements IAnimationData {
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
|
||||
compound.setBoolean("AllowAnimation", allowAnimation);
|
||||
return compound;
|
||||
|
||||
Reference in new issue
Block a user