diff --git a/src/main/java/noppes/npcs/controllers/AnimationController.java b/src/main/java/noppes/npcs/controllers/AnimationController.java new file mode 100644 index 000000000..42de4b108 --- /dev/null +++ b/src/main/java/noppes/npcs/controllers/AnimationController.java @@ -0,0 +1,101 @@ +package noppes.npcs.controllers; + +import noppes.npcs.CustomNpcs; +import noppes.npcs.LogWriter; +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.*; + +public class AnimationController { + public HashMap animations = new HashMap<>(); + public static AnimationController instance; + + public AnimationController() { + instance = this; + 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(); + animation.id = Integer.parseInt(file.getName().split("_")[0]); + animation.readFromNBT(NBTJsonUtil.LoadFile(file)); + animations.put(animation.id,animation); + } catch(Exception e) { + LogWriter.error("Error loading: " + file.getAbsolutePath(), e); + } + } + } + } + + public void saveAnimations() { + Collection animationLists = animations.values(); + for (Animation animation : animationLists) { + this.saveAnimation(animation); + } + } + + public IAnimation saveAnimation(Animation animation){ + animations.put(animation.id, animation); + + File dir = this.getDir(); + if(!dir.exists()) + dir.mkdirs(); + + File file = new File(dir, animation.id + "_" + animation.name + ".json_new"); + File file2 = new File(dir, animation.id + "_" + animation.name + ".json"); + + try { + NBTJsonUtil.SaveFile(file, animation.writeToNBT()); + if(file2.exists()) + file2.delete(); + file.renameTo(file2); + } catch (Exception e) { + LogWriter.except(e); + } + return animation; + } + + private File getDir(){ + return new File(CustomNpcs.getWorldSaveDirectory(), "animations"); + } + + public void delete(int id) { + Animation animation = this.animations.get(id); + if(animation == null) + return; + File dir = this.getDir(); + for (File file : dir.listFiles()) { + if (!file.isFile() || !file.getName().endsWith(".json")) + continue; + if (file.getName().startsWith(id + "_")) { + if (!file.delete()) + return; + else break; + } + } + this.animations.remove(id); + } + + public IAnimation get(int id) { + return this.animations.get(id); + } +} diff --git a/src/main/java/noppes/npcs/controllers/data/Animation.java b/src/main/java/noppes/npcs/controllers/data/Animation.java index 74b9c4be8..4788a3dea 100644 --- a/src/main/java/noppes/npcs/controllers/data/Animation.java +++ b/src/main/java/noppes/npcs/controllers/data/Animation.java @@ -28,11 +28,13 @@ public class Animation implements IAnimation { public Animation(){} - public Animation(String name){ + public Animation(int id, String name){ + this.id = id; this.name = name; } - public Animation(String name, float speed, byte smooth){ + public Animation(int id, String name, float speed, byte smooth){ + this.id = id; this.name = name; this.speed = speed; this.smooth = smooth; diff --git a/src/main/java/noppes/npcs/scripted/NpcAPI.java b/src/main/java/noppes/npcs/scripted/NpcAPI.java index 35747b202..3a1667950 100644 --- a/src/main/java/noppes/npcs/scripted/NpcAPI.java +++ b/src/main/java/noppes/npcs/scripted/NpcAPI.java @@ -567,8 +567,12 @@ public class NpcAPI extends AbstractNpcAPI { return this.millisToTime(ticks * 50); } - public IAnimation createAnimation(String name) { - return new Animation(name); + public IAnimation createAnimation(int id, String name) { + return new Animation(id, name); + } + + public IAnimation createAnimation(int id, String name, float speed, byte smooth) { + return new Animation(id, name, speed, smooth); } public IFrame createFrame(int duration) {