Implemented animation caching

This commit is contained in:
Luis C.M committed 2023-12-13 14:32:09 -06:00
1 parent 7820ad171b
commit a67604e22d
7 files changed
+99 -24

No files matched your search

@@ -108,6 +108,9 @@ public class PacketHandlerServer{
}
compound.setTag("TagNames",tagList);
Server.sendData(player, EnumPacketClient.GUI_DATA, compound);
} else if (type == EnumPacketServer.CacheAnimation) {
PlayerDataController.Instance.getPlayerData(player).animationData.cacheAnimation(buffer.readInt());
return;
}
if(type.needsNpc && npc == null){
@@ -1,6 +1,7 @@
package noppes.npcs.client;
import net.minecraft.util.ResourceLocation;
import noppes.npcs.controllers.data.Animation;
import noppes.npcs.controllers.data.AnimationData;
import noppes.npcs.client.gui.OverlayQuestTracking;
import noppes.npcs.client.gui.customoverlay.OverlayCustom;
@@ -14,6 +15,8 @@ import java.util.UUID;
public class ClientCacheHandler {
private static final CacheHashMap<String, CacheHashMap.CachedObject<ImageData>> imageDataCache = new CacheHashMap<>((long) ConfigClient.CacheLife * 60 * 1000);
public static final HashMap<Integer, Animation> animationCache = new HashMap<>();
public static OverlayQuestTracking questTrackingOverlay = null;
public static HashMap<Integer, OverlayCustom> customOverlays = new HashMap<>();
public static HashMap<UUID, HashMap<Integer, SkinOverlay>> skinOverlays = new HashMap<>();
@@ -39,6 +42,7 @@ public class ClientCacheHandler {
public static void clearCache() {
ClientCacheHandler.imageDataCache.clear();
ClientCacheHandler.animationCache.clear();
ClientCacheHandler.questTrackingOverlay = null;
ClientCacheHandler.customOverlays.clear();
ClientCacheHandler.skinOverlays.clear();
@@ -30,6 +30,7 @@ import noppes.npcs.client.gui.util.*;
import noppes.npcs.config.ConfigClient;
import noppes.npcs.constants.EnumGuiType;
import noppes.npcs.constants.EnumPacketClient;
import noppes.npcs.constants.EnumPacketServer;
import noppes.npcs.controllers.RecipeController;
import noppes.npcs.controllers.data.Animation;
import noppes.npcs.controllers.data.AnimationData;
@@ -299,24 +300,37 @@ public class PacketHandlerClient extends PacketHandlerServer{
}
else if(type == EnumPacketClient.UPDATE_ANIMATIONS) {
NBTTagCompound compound = Server.readNBT(buffer);
AnimationData animationData = null;
if (compound.hasKey("EntityId")) {
Entity entity = Minecraft.getMinecraft().theWorld.getEntityByID(compound.getInteger("EntityId"));
if (entity instanceof EntityNPCInterface) {
AnimationData data = ((EntityNPCInterface) entity).display.animationData;
data.readFromNBT(compound);
data.animation = new Animation();
data.animation.readFromNBT(compound.getCompoundTag("Animation"));
animationData = ((EntityNPCInterface) entity).display.animationData;
}
} else {
EntityPlayer sendingPlayer = Minecraft.getMinecraft().theWorld.getPlayerEntityByName(Server.readString(buffer));
if (sendingPlayer != null) {
AnimationData data = new AnimationData(sendingPlayer);
data.readFromNBT(compound);
data.animation = new Animation();
data.animation.readFromNBT(compound.getCompoundTag("Animation"));
ClientCacheHandler.playerAnimations.put(sendingPlayer.getUniqueID(), data);
ClientCacheHandler.playerAnimations.put(sendingPlayer.getUniqueID(), new AnimationData(sendingPlayer));
animationData = ClientCacheHandler.playerAnimations.get(sendingPlayer.getUniqueID());
}
}
if (animationData != null) {
animationData.readFromNBT(compound);
int animationId;
if (compound.hasKey("AnimationID")) {
animationId = compound.getInteger("AnimationID");
} else {
Animation animation = new Animation();
animation.readFromNBT(compound.getCompoundTag("Animation"));
ClientCacheHandler.animationCache.put(animation.getID(), animation);
animationId = animation.getID();
}
Animation animation = new Animation();
animation.readFromNBT(ClientCacheHandler.animationCache.get(animationId).writeToNBT());
animationData.animation = animation;
Client.sendData(EnumPacketServer.CacheAnimation, animationId);
}
}
else if(type == EnumPacketClient.DISABLE_MOUSE_INPUT) {
long length = buffer.readLong();
@@ -91,7 +91,7 @@ public enum EnumPacketServer {
RemoteTpToNpc,QuestGet, QuestCategoryGet,
SaveTileEntity,
NaturalSpawnGetAll, MailOpenSetup,
DimensionsGet, DimensionTeleport, GetTileEntity, Gui, IsGuiOpen,
DimensionsGet, DimensionTeleport, GetTileEntity, Gui, IsGuiOpen, CacheAnimation,
CustomGuiButton,CustomGuiScrollClick,CustomGuiClose,CustomGuiUnfocused,
@@ -3,15 +3,12 @@ package noppes.npcs.controllers;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import noppes.npcs.api.handler.IAnimationHandler;
import noppes.npcs.api.handler.data.IAnimation;
import noppes.npcs.api.handler.data.IFaction;
import noppes.npcs.controllers.data.Animation;
import noppes.npcs.controllers.data.Faction;
import noppes.npcs.controllers.data.TagMap;
import noppes.npcs.controllers.data.PlayerData;
import noppes.npcs.util.NBTJsonUtil;
import java.io.*;
@@ -120,6 +117,10 @@ public class AnimationController implements IAnimationHandler {
animations.remove(animation.getID());
animations.put(animation.getID(), (Animation) animation);
for (PlayerData playerData : PlayerDataController.Instance.getAllPlayerData()) {
playerData.animationData.uncacheAnimation(animation.getID());
}
saveAnimationMap();
// Save Animation File
@@ -281,6 +281,17 @@ public class PlayerDataController {
return data;
}
public ArrayList<PlayerData> getAllPlayerData() {
ArrayList<PlayerData> playerDataList = new ArrayList<>();
List<?> list = MinecraftServer.getServer().getConfigurationManager().playerEntityList;
for (Object o : list) {
if (o instanceof EntityPlayer) {
playerDataList.add(this.getPlayerData((EntityPlayer)o));
}
}
return playerDataList;
}
public PlayerData getPlayerData(EntityPlayer player){
PlayerData data = getPlayerDataCache(player.getUniqueID().toString());
if(data != null){
@@ -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;