mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-20 08:49:41 +00:00
IMPORTED KeyPreset FROM EASINGS BRANCH
This commit is contained in:
1 parent
ebd005c2df
commit
cd00fcf013
3 files changed
+746
No files matched your search
@@ -0,0 +1,77 @@
|
||||
package noppes.npcs.client.key;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import noppes.npcs.CustomNpcs;
|
||||
import noppes.npcs.LogWriter;
|
||||
import noppes.npcs.util.NBTJsonUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class KeyPresetManager {
|
||||
public List<KeyPreset> keys = new ArrayList<>();
|
||||
public String fileName;
|
||||
|
||||
public KeyPresetManager(String fileName) {
|
||||
this.fileName = fileName;
|
||||
}
|
||||
|
||||
public KeyPreset add(String name) {
|
||||
KeyPreset preset = new KeyPreset(name);
|
||||
keys.add(preset);
|
||||
return preset;
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
for (KeyPreset key : keys)
|
||||
key.tick();
|
||||
}
|
||||
|
||||
|
||||
public void load() {
|
||||
try {
|
||||
File dir = getDir();
|
||||
File file = new File(dir, fileName + ".json");
|
||||
if (!file.exists())
|
||||
return;
|
||||
|
||||
NBTTagCompound compound = NBTJsonUtil.LoadFile(file);
|
||||
for (KeyPreset key : keys)
|
||||
key.readFromNbt(compound);
|
||||
|
||||
} catch (Exception e) {
|
||||
LogWriter.except(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void save() {
|
||||
try {
|
||||
File dir = getDir();
|
||||
String filename = fileName + ".json";
|
||||
|
||||
File newFile = new File(dir, filename + "_new");
|
||||
File originalFile = new File(dir, filename);
|
||||
|
||||
NBTTagCompound compound = new NBTTagCompound();
|
||||
for (KeyPreset key : keys)
|
||||
key.writeToNbt(compound);
|
||||
|
||||
NBTJsonUtil.SaveFile(newFile, compound);
|
||||
|
||||
if (originalFile.exists())
|
||||
originalFile.delete();
|
||||
|
||||
newFile.renameTo(originalFile);
|
||||
} catch (Exception e) {
|
||||
LogWriter.except(e);
|
||||
}
|
||||
}
|
||||
|
||||
public File getDir() {
|
||||
File dir = new File(CustomNpcs.Dir, "keypresets");
|
||||
if (!dir.exists())
|
||||
dir.mkdir();
|
||||
return dir;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user