Files
CustomNPC-Plus/src/main/java/noppes/npcs/controllers/AnimationController.java
T

128 lines
3.8 KiB
Java
Raw Normal View History

2023-01-25 23:18:27 -06:00
package noppes.npcs.controllers;
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
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-03-21 19:34:24 -04:00
import noppes.npcs.controllers.data.Faction;
2023-01-25 23:18:27 -06:00
import noppes.npcs.util.NBTJsonUtil;
import java.io.File;
import java.util.*;
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;
animations = new HashMap<>();
2023-01-25 23:18:27 -06:00
load();
}
2023-03-21 19:34:24 -04:00
public static AnimationController getInstance(){
return instance;
}
2023-01-25 23:18:27 -06:00
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();
animation.name = file.getName().substring(0, file.getName().length() - 5);
2023-01-25 23:18:27 -06:00
animation.readFromNBT(NBTJsonUtil.LoadFile(file));
animations.put(animation.name,animation);
2023-01-25 23:18:27 -06:00
} catch(Exception e) {
LogWriter.error("Error loading: " + file.getAbsolutePath(), e);
}
}
}
}
private File getDir() {
return new File(CustomNpcs.getWorldSaveDirectory(), "animations");
2023-01-25 23:18:27 -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();
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 {
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);
}
return animations.get(animation.getName());
2023-01-25 23:18:27 -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;
if (file.getName().equals(name+".json")) {
file.delete();
break;
2023-01-25 23:18:27 -06:00
}
}
this.animations.remove(name);
}
2023-01-28 01:19:57 -06:00
public boolean has(String name) {
return this.animations.containsKey(name);
}
public IAnimation get(String name) {
return this.animations.get(name);
2023-01-25 23:18:27 -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){
for (Map.Entry<String,Animation> entryAnimation : AnimationController.getInstance().animations.entrySet()){
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-01-25 23:18:27 -06:00
}