2023-01-25 23:18:27 -06:00
|
|
|
package noppes.npcs.controllers;
|
|
|
|
|
|
2023-11-08 14:16:01 -05:00
|
|
|
import net.minecraft.nbt.CompressedStreamTools;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
2023-01-25 23:18:27 -06:00
|
|
|
import noppes.npcs.CustomNpcs;
|
|
|
|
|
import noppes.npcs.LogWriter;
|
2023-01-26 14:14:13 -06:00
|
|
|
import noppes.npcs.api.handler.IAnimationHandler;
|
2023-01-25 23:18:27 -06:00
|
|
|
import noppes.npcs.api.handler.data.IAnimation;
|
|
|
|
|
import noppes.npcs.controllers.data.Animation;
|
2023-12-13 14:32:09 -06:00
|
|
|
import noppes.npcs.controllers.data.PlayerData;
|
2023-01-25 23:18:27 -06:00
|
|
|
import noppes.npcs.util.NBTJsonUtil;
|
|
|
|
|
|
2023-11-08 14:16:01 -05:00
|
|
|
import java.io.*;
|
2023-01-25 23:18:27 -06:00
|
|
|
import java.util.*;
|
2023-11-08 14:16:01 -05:00
|
|
|
import java.util.zip.GZIPInputStream;
|
2023-01-25 23:18:27 -06:00
|
|
|
|
2023-01-26 14:14:13 -06:00
|
|
|
public class AnimationController implements IAnimationHandler {
|
2023-11-01 21:39:08 -04:00
|
|
|
public HashMap<Integer, Animation> animations;
|
2023-11-08 14:16:01 -05:00
|
|
|
private HashMap<Integer, String> bootOrder;
|
|
|
|
|
|
2023-06-05 22:12:48 -04:00
|
|
|
public static AnimationController Instance;
|
2023-11-01 21:39:08 -04:00
|
|
|
private int lastUsedID = 0;
|
2023-01-25 23:18:27 -06:00
|
|
|
|
|
|
|
|
public AnimationController() {
|
2023-06-05 22:12:48 -04:00
|
|
|
Instance = this;
|
2023-11-08 14:16:01 -05:00
|
|
|
bootOrder = new HashMap<>();
|
2023-01-26 14:14:13 -06:00
|
|
|
animations = new HashMap<>();
|
2023-01-25 23:18:27 -06:00
|
|
|
load();
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-21 19:34:24 -04:00
|
|
|
public static AnimationController getInstance(){
|
2023-06-05 22:12:48 -04:00
|
|
|
return Instance;
|
2023-03-21 19:34:24 -04:00
|
|
|
}
|
|
|
|
|
|
2023-01-25 23:18:27 -06:00
|
|
|
public void load(){
|
|
|
|
|
LogWriter.info("Loading animations...");
|
2023-11-08 14:16:01 -05:00
|
|
|
readAnimationMap();
|
2023-01-25 23:18:27 -06:00
|
|
|
loadAnimations();
|
|
|
|
|
LogWriter.info("Done loading animations.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void loadAnimations(){
|
|
|
|
|
animations.clear();
|
|
|
|
|
|
|
|
|
|
File dir = getDir();
|
|
|
|
|
if (!dir.exists()) {
|
|
|
|
|
dir.mkdir();
|
|
|
|
|
} else {
|
|
|
|
|
for (File file : dir.listFiles()) {
|
|
|
|
|
if (!file.isFile() || !file.getName().endsWith(".json"))
|
|
|
|
|
continue;
|
|
|
|
|
try {
|
|
|
|
|
Animation animation = new Animation();
|
|
|
|
|
animation.readFromNBT(NBTJsonUtil.LoadFile(file));
|
2023-11-08 14:16:01 -05:00
|
|
|
animation.name = file.getName().substring(0, file.getName().length() - 5);
|
|
|
|
|
|
|
|
|
|
if(animation.id == -1){
|
|
|
|
|
animation.id = getUnusedId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int originalID = animation.id;
|
|
|
|
|
int setID = animation.id;
|
|
|
|
|
while (bootOrder.containsKey(setID) || animations.containsKey(setID)){
|
|
|
|
|
if(bootOrder.containsKey(setID))
|
|
|
|
|
if(bootOrder.get(setID).equals(animation.name))
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
setID++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
animation.id = setID;
|
|
|
|
|
if(originalID != setID){
|
|
|
|
|
LogWriter.info("Found Animation ID Mismatch: " + animation.name + ", New ID: " + setID);
|
|
|
|
|
animation.save();
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-01 21:39:08 -04:00
|
|
|
animations.put(animation.id, animation);
|
2023-01-25 23:18:27 -06:00
|
|
|
} catch(Exception e) {
|
|
|
|
|
LogWriter.error("Error loading: " + file.getAbsolutePath(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-08 14:16:01 -05:00
|
|
|
|
|
|
|
|
saveAnimationMap();
|
2023-01-25 23:18:27 -06:00
|
|
|
}
|
|
|
|
|
|
2023-01-26 14:14:13 -06:00
|
|
|
private File getDir() {
|
|
|
|
|
return new File(CustomNpcs.getWorldSaveDirectory(), "animations");
|
2023-01-25 23:18:27 -06:00
|
|
|
}
|
|
|
|
|
|
2023-11-01 21:39:08 -04:00
|
|
|
public int getUnusedId(){
|
|
|
|
|
if(lastUsedID == 0){
|
|
|
|
|
for(int catid : animations.keySet()){
|
|
|
|
|
if(catid > lastUsedID)
|
|
|
|
|
lastUsedID = catid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
lastUsedID++;
|
|
|
|
|
return lastUsedID;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-26 14:14:13 -06:00
|
|
|
public IAnimation saveAnimation(IAnimation animation){
|
2023-11-01 21:39:08 -04:00
|
|
|
if(animation.getID() < 0){
|
|
|
|
|
animation.setID(getUnusedId());
|
|
|
|
|
while(hasName(animation.getName()))
|
|
|
|
|
animation.setName(animation.getName() + "_");
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
Animation existing = animations.get(animation.getID());
|
|
|
|
|
if(existing != null && !existing.name.equals(animation.getName()))
|
|
|
|
|
while(hasName(animation.getName()))
|
|
|
|
|
animation.setName(animation.getName() + "_");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
animations.remove(animation.getID());
|
|
|
|
|
animations.put(animation.getID(), (Animation) animation);
|
2023-01-25 23:18:27 -06:00
|
|
|
|
2023-12-13 14:32:09 -06:00
|
|
|
for (PlayerData playerData : PlayerDataController.Instance.getAllPlayerData()) {
|
|
|
|
|
playerData.animationData.uncacheAnimation(animation.getID());
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 14:16:01 -05:00
|
|
|
saveAnimationMap();
|
|
|
|
|
|
2023-11-01 21:39:08 -04:00
|
|
|
// Save Animation File
|
2023-01-25 23:18:27 -06:00
|
|
|
File dir = this.getDir();
|
|
|
|
|
if(!dir.exists())
|
|
|
|
|
dir.mkdirs();
|
|
|
|
|
|
2023-01-26 14:14:13 -06:00
|
|
|
File file = new File(dir, animation.getName() + ".json_new");
|
|
|
|
|
File file2 = new File(dir, animation.getName() + ".json");
|
2023-01-25 23:18:27 -06:00
|
|
|
|
|
|
|
|
try {
|
2023-01-26 14:14:13 -06:00
|
|
|
NBTJsonUtil.SaveFile(file, ((Animation)animation).writeToNBT());
|
2023-01-25 23:18:27 -06:00
|
|
|
if(file2.exists())
|
|
|
|
|
file2.delete();
|
|
|
|
|
file.renameTo(file2);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
LogWriter.except(e);
|
|
|
|
|
}
|
2023-11-01 21:39:08 -04:00
|
|
|
return animations.get(animation.getID());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean hasName(String newName) {
|
|
|
|
|
if(newName.trim().isEmpty())
|
|
|
|
|
return true;
|
|
|
|
|
for(Animation animation : animations.values())
|
|
|
|
|
if(animation.name.equals(newName))
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
2023-01-25 23:18:27 -06:00
|
|
|
}
|
|
|
|
|
|
2023-01-26 14:14:13 -06:00
|
|
|
public void delete(String name) {
|
2023-11-01 21:39:08 -04:00
|
|
|
Animation delete = getAnimationFromName(name);
|
|
|
|
|
if(delete != null){
|
|
|
|
|
Animation foundAnimation = this.animations.remove(delete.getID());
|
|
|
|
|
if(foundAnimation != null && foundAnimation.name != null){
|
|
|
|
|
File dir = this.getDir();
|
|
|
|
|
for (File file : dir.listFiles()) {
|
|
|
|
|
if (!file.isFile() || !file.getName().endsWith(".json"))
|
|
|
|
|
continue;
|
|
|
|
|
if (file.getName().equals(foundAnimation.name+".json")) {
|
|
|
|
|
file.delete();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-08 14:16:01 -05:00
|
|
|
|
|
|
|
|
saveAnimationMap();
|
2023-11-01 21:39:08 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void delete(int id) {
|
|
|
|
|
if(!this.animations.containsKey(id))
|
2023-01-25 23:18:27 -06:00
|
|
|
return;
|
2023-11-01 21:39:08 -04:00
|
|
|
|
|
|
|
|
Animation foundAnimation = this.animations.remove(id);
|
|
|
|
|
if(foundAnimation != null && foundAnimation.name != null){
|
|
|
|
|
File dir = this.getDir();
|
|
|
|
|
for (File file : dir.listFiles()) {
|
|
|
|
|
if (!file.isFile() || !file.getName().endsWith(".json"))
|
|
|
|
|
continue;
|
|
|
|
|
if (file.getName().equals(foundAnimation.name+".json")) {
|
|
|
|
|
file.delete();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-01-25 23:18:27 -06:00
|
|
|
}
|
2023-11-08 14:16:01 -05:00
|
|
|
|
|
|
|
|
saveAnimationMap();
|
2023-01-25 23:18:27 -06:00
|
|
|
}
|
2023-01-26 14:14:13 -06:00
|
|
|
}
|
|
|
|
|
|
2023-01-28 01:19:57 -06:00
|
|
|
public boolean has(String name) {
|
2023-11-01 21:39:08 -04:00
|
|
|
return getAnimationFromName(name) != null;
|
2023-01-28 01:19:57 -06:00
|
|
|
}
|
|
|
|
|
|
2023-01-26 14:14:13 -06:00
|
|
|
public IAnimation get(String name) {
|
2023-11-01 21:39:08 -04:00
|
|
|
return getAnimationFromName(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IAnimation get(int id) {
|
|
|
|
|
return this.animations.get(id);
|
2023-01-25 23:18:27 -06:00
|
|
|
}
|
|
|
|
|
|
2023-01-26 14:14:13 -06:00
|
|
|
public IAnimation[] getAnimations() {
|
|
|
|
|
ArrayList<IAnimation> animations = new ArrayList<>(this.animations.values());
|
|
|
|
|
return animations.toArray(new IAnimation[0]);
|
2023-01-25 23:18:27 -06:00
|
|
|
}
|
2023-03-21 19:34:24 -04:00
|
|
|
|
|
|
|
|
public Animation getAnimationFromName(String animation){
|
2023-11-01 21:39:08 -04:00
|
|
|
for (Map.Entry<Integer,Animation> entryAnimation : AnimationController.getInstance().animations.entrySet()){
|
2023-03-21 19:34:24 -04:00
|
|
|
if (entryAnimation.getValue().name.equalsIgnoreCase(animation)){
|
|
|
|
|
return entryAnimation.getValue();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String[] getNames() {
|
|
|
|
|
String[] names = new String[animations.size()];
|
|
|
|
|
int i = 0;
|
|
|
|
|
for(Animation animation : animations.values()){
|
|
|
|
|
names[i] = animation.name.toLowerCase();
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
return names;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 14:16:01 -05:00
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
// ANIMATION MAP
|
|
|
|
|
|
|
|
|
|
public File getMapDir(){
|
|
|
|
|
File dir = CustomNpcs.getWorldSaveDirectory();
|
|
|
|
|
if(!dir.exists())
|
|
|
|
|
dir.mkdir();
|
|
|
|
|
return dir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void readAnimationMap(){
|
|
|
|
|
bootOrder.clear();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
File file = new File(getMapDir(), "animations.dat");
|
|
|
|
|
if(file.exists()){
|
|
|
|
|
loadAnimationMapFile(file);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
try {
|
|
|
|
|
File file = new File(getMapDir(), "animations.dat_old");
|
|
|
|
|
if(file.exists()){
|
|
|
|
|
loadAnimationMapFile(file);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception ignored) {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NBTTagCompound writeMapNBT(){
|
|
|
|
|
NBTTagCompound nbt = new NBTTagCompound();
|
|
|
|
|
NBTTagList animationList = new NBTTagList();
|
|
|
|
|
for(Integer key: animations.keySet()){
|
|
|
|
|
Animation animation = animations.get(key);
|
|
|
|
|
if(!animation.getName().isEmpty()){
|
|
|
|
|
NBTTagCompound animationCompound = new NBTTagCompound();
|
|
|
|
|
animationCompound.setString("Name", animation.getName());
|
|
|
|
|
animationCompound.setInteger("ID", key);
|
|
|
|
|
|
|
|
|
|
animationList.appendTag(animationCompound);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
nbt.setTag("Animations", animationList);
|
|
|
|
|
return nbt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void readMapNBT(NBTTagCompound compound){
|
|
|
|
|
NBTTagList list = compound.getTagList("Animations", 10);
|
|
|
|
|
if(list != null){
|
|
|
|
|
for(int i = 0; i < list.tagCount(); i++)
|
|
|
|
|
{
|
|
|
|
|
NBTTagCompound nbttagcompound = list.getCompoundTagAt(i);
|
|
|
|
|
String animationName = nbttagcompound.getString("Name");
|
|
|
|
|
Integer key = nbttagcompound.getInteger("ID");
|
|
|
|
|
bootOrder.put(key, animationName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void loadAnimationMapFile(File file) throws IOException {
|
|
|
|
|
DataInputStream var1 = new DataInputStream(new BufferedInputStream(new GZIPInputStream(new FileInputStream(file))));
|
|
|
|
|
readAnimationMap(var1);
|
|
|
|
|
var1.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void readAnimationMap(DataInputStream stream) throws IOException{
|
|
|
|
|
NBTTagCompound nbtCompound = CompressedStreamTools.read(stream);
|
|
|
|
|
this.readMapNBT(nbtCompound);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void saveAnimationMap(){
|
|
|
|
|
try {
|
|
|
|
|
File saveDir = getMapDir();
|
|
|
|
|
File file = new File(saveDir, "animations.dat_new");
|
|
|
|
|
File file1 = new File(saveDir, "animations.dat_old");
|
|
|
|
|
File file2 = new File(saveDir, "animations.dat");
|
|
|
|
|
CompressedStreamTools.writeCompressed(this.writeMapNBT(), new FileOutputStream(file));
|
|
|
|
|
if(file1.exists())
|
|
|
|
|
{
|
|
|
|
|
file1.delete();
|
|
|
|
|
}
|
|
|
|
|
file2.renameTo(file1);
|
|
|
|
|
if(file2.exists())
|
|
|
|
|
{
|
|
|
|
|
file2.delete();
|
|
|
|
|
}
|
|
|
|
|
file.renameTo(file2);
|
|
|
|
|
if(file.exists())
|
|
|
|
|
{
|
|
|
|
|
file.delete();
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
LogWriter.except(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
////////////////////////////////////////////////////////
|
2023-01-25 23:18:27 -06:00
|
|
|
}
|