2023-01-25 23:18:27 -06:00
|
|
|
package noppes.npcs.controllers;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
import noppes.npcs.util.NBTJsonUtil;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
2023-01-26 14:14:13 -06:00
|
|
|
public class AnimationController implements IAnimationHandler {
|
|
|
|
|
public HashMap<String, Animation> animations;
|
2023-01-25 23:18:27 -06:00
|
|
|
public static AnimationController instance;
|
|
|
|
|
|
|
|
|
|
public AnimationController() {
|
|
|
|
|
instance = this;
|
2023-01-26 14:14:13 -06:00
|
|
|
animations = new HashMap<>();
|
2023-01-25 23:18:27 -06:00
|
|
|
load();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void load(){
|
|
|
|
|
LogWriter.info("Loading animations...");
|
|
|
|
|
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();
|
2023-01-26 14:14:13 -06:00
|
|
|
animation.name = file.getName().substring(0, file.getName().length() - 5);
|
2023-01-25 23:18:27 -06:00
|
|
|
animation.readFromNBT(NBTJsonUtil.LoadFile(file));
|
2023-01-26 14:14:13 -06:00
|
|
|
animations.put(animation.name,animation);
|
2023-01-25 23:18:27 -06:00
|
|
|
} catch(Exception e) {
|
|
|
|
|
LogWriter.error("Error loading: " + file.getAbsolutePath(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-01-26 14:14:13 -06:00
|
|
|
public IAnimation saveAnimation(IAnimation animation){
|
|
|
|
|
animations.put(animation.getName(), (Animation) animation);
|
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-01-26 14:14:13 -06:00
|
|
|
return animations.get(animation.getName());
|
2023-01-25 23:18:27 -06:00
|
|
|
}
|
|
|
|
|
|
2023-01-26 14:14:13 -06:00
|
|
|
public void delete(String name) {
|
|
|
|
|
if(!this.animations.containsKey(name))
|
2023-01-25 23:18:27 -06:00
|
|
|
return;
|
|
|
|
|
File dir = this.getDir();
|
|
|
|
|
for (File file : dir.listFiles()) {
|
|
|
|
|
if (!file.isFile() || !file.getName().endsWith(".json"))
|
|
|
|
|
continue;
|
2023-01-26 14:14:13 -06:00
|
|
|
if (file.getName().equals(name+".json")) {
|
|
|
|
|
file.delete();
|
|
|
|
|
break;
|
2023-01-25 23:18:27 -06:00
|
|
|
}
|
|
|
|
|
}
|
2023-01-26 14:14:13 -06:00
|
|
|
this.animations.remove(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IAnimation get(String name) {
|
|
|
|
|
return this.animations.get(name);
|
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
|
|
|
}
|
|
|
|
|
}
|