2023-05-30 23:41:37 -05:00
|
|
|
package noppes.npcs.client;
|
|
|
|
|
|
2025-03-08 19:19:41 -05:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2023-06-02 04:22:00 -04:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
2025-03-05 22:57:24 -06:00
|
|
|
import noppes.npcs.client.controllers.MusicController;
|
2023-06-05 18:50:41 -05:00
|
|
|
import noppes.npcs.client.gui.customoverlay.OverlayCustom;
|
2025-03-08 19:19:41 -05:00
|
|
|
import noppes.npcs.client.gui.hud.ClientHudManager;
|
|
|
|
|
import noppes.npcs.client.gui.hud.EnumHudComponent;
|
|
|
|
|
import noppes.npcs.client.gui.hud.HudComponent;
|
2025-02-18 04:57:22 -05:00
|
|
|
import noppes.npcs.client.gui.select.GuiSoundSelection;
|
|
|
|
|
import noppes.npcs.client.gui.select.GuiTextureSelection;
|
2023-05-31 00:56:15 -05:00
|
|
|
import noppes.npcs.client.renderer.ImageData;
|
2023-06-03 17:23:40 -04:00
|
|
|
import noppes.npcs.config.ConfigClient;
|
2025-02-20 21:40:39 -05:00
|
|
|
import noppes.npcs.controllers.data.AnimationData;
|
|
|
|
|
import noppes.npcs.controllers.data.Party;
|
|
|
|
|
import noppes.npcs.controllers.data.PlayerData;
|
|
|
|
|
import noppes.npcs.controllers.data.SkinOverlay;
|
2025-10-19 22:06:11 -04:00
|
|
|
import kamkeel.npcs.network.enums.EnumSyncType;
|
2023-05-30 23:41:37 -05:00
|
|
|
import noppes.npcs.util.CacheHashMap;
|
|
|
|
|
|
2025-10-19 22:06:11 -04:00
|
|
|
import java.util.EnumMap;
|
2023-06-05 18:50:41 -05:00
|
|
|
import java.util.HashMap;
|
2025-10-19 22:06:11 -04:00
|
|
|
import java.util.Map;
|
2023-06-05 18:50:41 -05:00
|
|
|
import java.util.UUID;
|
|
|
|
|
|
2023-05-31 00:56:15 -05:00
|
|
|
public class ClientCacheHandler {
|
2023-06-03 17:23:40 -04:00
|
|
|
private static final CacheHashMap<String, CacheHashMap.CachedObject<ImageData>> imageDataCache = new CacheHashMap<>((long) ConfigClient.CacheLife * 60 * 1000);
|
2024-03-13 13:47:33 -04:00
|
|
|
public static PlayerData playerData = new PlayerData();
|
2023-06-05 18:50:41 -05:00
|
|
|
public static HashMap<Integer, OverlayCustom> customOverlays = new HashMap<>();
|
|
|
|
|
public static HashMap<UUID, HashMap<Integer, SkinOverlay>> skinOverlays = new HashMap<>();
|
|
|
|
|
public static HashMap<UUID, AnimationData> playerAnimations = new HashMap<>();
|
2023-05-30 23:41:37 -05:00
|
|
|
|
2025-10-19 22:06:11 -04:00
|
|
|
private static String activeServerKey = "";
|
2025-10-19 22:28:03 -04:00
|
|
|
private static String lastServerKey = "";
|
2025-10-19 22:06:11 -04:00
|
|
|
private static final Map<String, EnumMap<EnumSyncType, Integer>> clientRevisionCache = new HashMap<>();
|
|
|
|
|
|
2024-01-10 22:14:00 -06:00
|
|
|
public static Party party;
|
|
|
|
|
|
2025-02-18 22:22:40 -05:00
|
|
|
public static boolean allowProfiles = true;
|
|
|
|
|
public static boolean allowParties = true;
|
|
|
|
|
|
2025-10-19 22:06:11 -04:00
|
|
|
public static void setActiveServer(String serverKey, EnumMap<EnumSyncType, Integer> serverRevisions) {
|
|
|
|
|
String normalizedKey = serverKey == null ? "" : serverKey;
|
2025-10-19 22:28:03 -04:00
|
|
|
String previousKey = activeServerKey == null ? "" : activeServerKey;
|
|
|
|
|
lastServerKey = previousKey;
|
|
|
|
|
|
2025-10-19 22:06:11 -04:00
|
|
|
if (!normalizedKey.equals(activeServerKey)) {
|
|
|
|
|
clientRevisionCache.clear();
|
|
|
|
|
}
|
2025-10-19 22:28:03 -04:00
|
|
|
|
2025-10-19 22:06:11 -04:00
|
|
|
activeServerKey = normalizedKey;
|
|
|
|
|
|
|
|
|
|
if (activeServerKey.isEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EnumMap<EnumSyncType, Integer> cached = clientRevisionCache.computeIfAbsent(
|
|
|
|
|
activeServerKey,
|
|
|
|
|
ignored -> new EnumMap<>(EnumSyncType.class)
|
|
|
|
|
);
|
|
|
|
|
if (serverRevisions != null && !serverRevisions.isEmpty()) {
|
|
|
|
|
cached.keySet().retainAll(serverRevisions.keySet());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-19 22:28:03 -04:00
|
|
|
public static String getLastServerKey() {
|
|
|
|
|
return lastServerKey == null ? "" : lastServerKey;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-19 22:06:11 -04:00
|
|
|
public static EnumMap<EnumSyncType, Integer> getCachedRevisionsForServer(String serverKey) {
|
|
|
|
|
String key = serverKey == null ? "" : serverKey;
|
|
|
|
|
if (key.isEmpty()) {
|
|
|
|
|
return new EnumMap<>(EnumSyncType.class);
|
|
|
|
|
}
|
|
|
|
|
EnumMap<EnumSyncType, Integer> revisions = clientRevisionCache.get(key);
|
|
|
|
|
if (revisions == null) {
|
|
|
|
|
return new EnumMap<>(EnumSyncType.class);
|
|
|
|
|
}
|
|
|
|
|
return new EnumMap<>(revisions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void updateClientRevision(EnumSyncType type, int revision) {
|
|
|
|
|
if (revision < 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
String key = activeServerKey == null ? "" : activeServerKey;
|
|
|
|
|
if (key.isEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
EnumMap<EnumSyncType, Integer> revisions = clientRevisionCache.computeIfAbsent(
|
|
|
|
|
key,
|
|
|
|
|
ignored -> new EnumMap<>(EnumSyncType.class)
|
|
|
|
|
);
|
|
|
|
|
revisions.put(type, revision);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-18 22:22:40 -05:00
|
|
|
|
2023-05-30 23:41:37 -05:00
|
|
|
public static ImageData getImageData(String directory) {
|
2023-05-31 00:35:57 -05:00
|
|
|
synchronized (imageDataCache) {
|
|
|
|
|
if (!imageDataCache.containsKey(directory)) {
|
|
|
|
|
imageDataCache.put(directory, new CacheHashMap.CachedObject<>(new ImageData(directory)));
|
|
|
|
|
}
|
|
|
|
|
return imageDataCache.get(directory).getObject();
|
2023-05-30 23:41:37 -05:00
|
|
|
}
|
|
|
|
|
}
|
2023-06-02 04:22:00 -04:00
|
|
|
|
|
|
|
|
public static ImageData getNPCTexture(String directory, boolean x64, ResourceLocation resource) {
|
|
|
|
|
synchronized (imageDataCache) {
|
|
|
|
|
if (!imageDataCache.containsKey(resource.getResourcePath())) {
|
|
|
|
|
imageDataCache.put(resource.getResourcePath(), new CacheHashMap.CachedObject<>(new ImageData(directory, x64, resource)));
|
|
|
|
|
}
|
|
|
|
|
return imageDataCache.get(resource.getResourcePath()).getObject();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-05 18:50:41 -05:00
|
|
|
|
2024-02-28 23:47:24 -05:00
|
|
|
public static boolean isCachedNPC(ResourceLocation resource) {
|
|
|
|
|
synchronized (imageDataCache) {
|
|
|
|
|
return imageDataCache.containsKey(resource.getResourcePath());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-05 18:50:41 -05:00
|
|
|
public static void clearCache() {
|
|
|
|
|
ClientCacheHandler.imageDataCache.clear();
|
|
|
|
|
ClientCacheHandler.customOverlays.clear();
|
|
|
|
|
ClientCacheHandler.skinOverlays.clear();
|
|
|
|
|
ClientCacheHandler.playerAnimations.clear();
|
2025-02-18 04:57:22 -05:00
|
|
|
|
|
|
|
|
// Clear Texture Caches
|
|
|
|
|
GuiSoundSelection.cachedDomains.clear();
|
|
|
|
|
GuiTextureSelection.cachedTextures.clear();
|
2025-03-05 22:57:24 -06:00
|
|
|
|
|
|
|
|
// Clear music/bard sounds
|
|
|
|
|
MusicController.Instance.stopAllSounds();
|
2025-03-08 19:19:41 -05:00
|
|
|
|
|
|
|
|
// Clear Quest Tracker
|
|
|
|
|
HudComponent component = ClientHudManager.getInstance().getHudComponents().get(EnumHudComponent.QuestTracker);
|
2025-03-12 20:12:01 -04:00
|
|
|
if (component != null) {
|
2025-03-08 19:19:41 -05:00
|
|
|
component.loadData(new NBTTagCompound());
|
|
|
|
|
component.hasData = false;
|
|
|
|
|
}
|
2023-06-05 18:50:41 -05:00
|
|
|
}
|
2024-03-15 16:40:43 -04:00
|
|
|
|
|
|
|
|
public static void clearSkinCache() {
|
|
|
|
|
ClientCacheHandler.imageDataCache.clear();
|
|
|
|
|
ClientCacheHandler.customOverlays.clear();
|
|
|
|
|
ClientCacheHandler.skinOverlays.clear();
|
2025-02-18 04:57:22 -05:00
|
|
|
|
|
|
|
|
// Clear Texture Caches
|
|
|
|
|
GuiSoundSelection.cachedDomains.clear();
|
|
|
|
|
GuiTextureSelection.cachedTextures.clear();
|
2024-03-15 16:40:43 -04:00
|
|
|
}
|
2023-05-30 23:41:37 -05:00
|
|
|
}
|