diff --git a/src/main/java/noppes/npcs/CustomNpcs.java b/src/main/java/noppes/npcs/CustomNpcs.java index cc7ea3699..d6de279ec 100644 --- a/src/main/java/noppes/npcs/CustomNpcs.java +++ b/src/main/java/noppes/npcs/CustomNpcs.java @@ -198,6 +198,7 @@ public class CustomNpcs { new GlobalDataController(); new SpawnController(); new LinkedNpcController(); + new AnimationController(); ScriptController.Instance.loadStoredData(); ScriptController.Instance.loadForgeScripts(); ScriptController.Instance.loadNPCScripts(); diff --git a/src/main/java/noppes/npcs/controllers/AnimationController.java b/src/main/java/noppes/npcs/controllers/AnimationController.java index 42de4b108..3b755e5a6 100644 --- a/src/main/java/noppes/npcs/controllers/AnimationController.java +++ b/src/main/java/noppes/npcs/controllers/AnimationController.java @@ -2,6 +2,7 @@ package noppes.npcs.controllers; import noppes.npcs.CustomNpcs; import noppes.npcs.LogWriter; +import noppes.npcs.api.handler.IAnimationHandler; import noppes.npcs.api.handler.data.IAnimation; import noppes.npcs.controllers.data.Animation; import noppes.npcs.util.NBTJsonUtil; @@ -9,12 +10,13 @@ import noppes.npcs.util.NBTJsonUtil; import java.io.File; import java.util.*; -public class AnimationController { - public HashMap animations = new HashMap<>(); +public class AnimationController implements IAnimationHandler { + public HashMap animations; public static AnimationController instance; public AnimationController() { instance = this; + animations = new HashMap<>(); load(); } @@ -36,9 +38,9 @@ public class AnimationController { continue; try { Animation animation = new Animation(); - animation.id = Integer.parseInt(file.getName().split("_")[0]); + animation.name = file.getName().substring(0, file.getName().length() - 5); animation.readFromNBT(NBTJsonUtil.LoadFile(file)); - animations.put(animation.id,animation); + animations.put(animation.name,animation); } catch(Exception e) { LogWriter.error("Error loading: " + file.getAbsolutePath(), e); } @@ -46,56 +48,52 @@ public class AnimationController { } } - public void saveAnimations() { - Collection animationLists = animations.values(); - for (Animation animation : animationLists) { - this.saveAnimation(animation); - } + private File getDir() { + return new File(CustomNpcs.getWorldSaveDirectory(), "animations"); } - public IAnimation saveAnimation(Animation animation){ - animations.put(animation.id, animation); + public IAnimation saveAnimation(IAnimation animation){ + animations.put(animation.getName(), (Animation) 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"); + File file = new File(dir, animation.getName() + ".json_new"); + File file2 = new File(dir, animation.getName() + ".json"); try { - NBTJsonUtil.SaveFile(file, animation.writeToNBT()); + NBTJsonUtil.SaveFile(file, ((Animation)animation).writeToNBT()); if(file2.exists()) file2.delete(); file.renameTo(file2); } catch (Exception e) { LogWriter.except(e); } - return animation; + return animations.get(animation.getName()); } - private File getDir(){ - return new File(CustomNpcs.getWorldSaveDirectory(), "animations"); - } - - public void delete(int id) { - Animation animation = this.animations.get(id); - if(animation == null) + public void delete(String name) { + if(!this.animations.containsKey(name)) 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; + if (file.getName().equals(name+".json")) { + file.delete(); + break; } } - this.animations.remove(id); + this.animations.remove(name); } - public IAnimation get(int id) { - return this.animations.get(id); + public IAnimation get(String name) { + return this.animations.get(name); + } + + public IAnimation[] getAnimations() { + ArrayList animations = new ArrayList<>(this.animations.values()); + return animations.toArray(new IAnimation[0]); } } diff --git a/src/main/java/noppes/npcs/controllers/data/Animation.java b/src/main/java/noppes/npcs/controllers/data/Animation.java index 4788a3dea..71e29216a 100644 --- a/src/main/java/noppes/npcs/controllers/data/Animation.java +++ b/src/main/java/noppes/npcs/controllers/data/Animation.java @@ -5,6 +5,7 @@ import net.minecraft.nbt.NBTTagList; import noppes.npcs.api.handler.data.IAnimation; import noppes.npcs.api.handler.data.IFrame; import noppes.npcs.constants.EnumAnimationPart; +import noppes.npcs.controllers.AnimationController; import java.util.ArrayList; @@ -15,7 +16,6 @@ public class Animation implements IAnimation { public int currentFrame = 0; public int currentFrameTime = 0; - public int id = 0; public String name; public float speed = 1.0F; public byte smooth = 0; @@ -28,13 +28,11 @@ public class Animation implements IAnimation { public Animation(){} - public Animation(int id, String name){ - this.id = id; + public Animation(String name){ this.name = name; } - public Animation(int id, String name, float speed, byte smooth){ - this.id = id; + public Animation(String name, float speed, byte smooth){ this.name = name; this.speed = speed; this.smooth = smooth; @@ -76,10 +74,6 @@ public class Animation implements IAnimation { return this; } - public int getId() { - return id; - } - public IAnimation setName(String name) { this.name = name; return this; @@ -152,15 +146,17 @@ public class Animation implements IAnimation { return this.loop; } + public IAnimation save() { + return AnimationController.instance.saveAnimation(this); + } + public void readFromNBT(NBTTagCompound compound){ name = compound.getString("Name"); - id = compound.getInteger("ID"); speed = compound.getFloat("Speed"); smooth = compound.getByte("Smooth"); loop = compound.getInteger("Loop"); renderTicks = compound.getBoolean("RenderTicks"); - currentFrame = compound.getInteger("CurrentFrame"); ArrayList frames = new ArrayList(); NBTTagList list = compound.getTagList("Frames", 10); @@ -181,13 +177,11 @@ public class Animation implements IAnimation { public NBTTagCompound writeToNBT(){ NBTTagCompound compound = new NBTTagCompound(); compound.setString("Name", name); - compound.setInteger("ID", id); compound.setFloat("Speed", speed); compound.setByte("Smooth", smooth); compound.setInteger("Loop",loop); compound.setBoolean("RenderTicks", renderTicks); - compound.setInteger("CurrentFrame", currentFrame); NBTTagList list = new NBTTagList(); for(Frame frame : frames){ diff --git a/src/main/java/noppes/npcs/scripted/NpcAPI.java b/src/main/java/noppes/npcs/scripted/NpcAPI.java index 3a1667950..f90b75f47 100644 --- a/src/main/java/noppes/npcs/scripted/NpcAPI.java +++ b/src/main/java/noppes/npcs/scripted/NpcAPI.java @@ -164,6 +164,11 @@ public class NpcAPI extends AbstractNpcAPI { return TransportController.getInstance(); } + public IAnimationHandler getAnimations() { + this.checkWorld(); + return AnimationController.instance; + } + @Override public String[] getAllBiomeNames() { List biomes = new ArrayList<>(); @@ -567,12 +572,12 @@ public class NpcAPI extends AbstractNpcAPI { return this.millisToTime(ticks * 50); } - public IAnimation createAnimation(int id, String name) { - return new Animation(id, name); + public IAnimation createAnimation(String name) { + return new Animation(name); } - public IAnimation createAnimation(int id, String name, float speed, byte smooth) { - return new Animation(id, name, speed, smooth); + public IAnimation createAnimation(String name, float speed, byte smooth) { + return new Animation(name, speed, smooth); } public IFrame createFrame(int duration) {