2018-09-13 12:03:28 -04:00
|
|
|
package noppes.npcs.controllers;
|
|
|
|
|
|
|
|
|
|
import net.minecraft.nbt.CompressedStreamTools;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2022-06-12 01:26:23 -05:00
|
|
|
import net.minecraft.nbt.NBTTagList;
|
2018-09-13 12:03:28 -04:00
|
|
|
import noppes.npcs.CustomNpcs;
|
2022-06-12 01:26:23 -05:00
|
|
|
import noppes.npcs.roles.JobItemGiver;
|
2018-09-13 12:03:28 -04:00
|
|
|
|
2023-01-12 21:33:06 -05:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
public class GlobalDataController {
|
|
|
|
|
public static GlobalDataController instance;
|
|
|
|
|
|
|
|
|
|
private int itemGiverId = 0;
|
2022-06-12 01:26:23 -05:00
|
|
|
public HashMap<Integer, JobItemGiver> itemGivers = new HashMap<>();
|
2018-09-13 12:03:28 -04:00
|
|
|
|
|
|
|
|
public GlobalDataController(){
|
|
|
|
|
instance = this;
|
|
|
|
|
load();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void load(){
|
|
|
|
|
File saveDir = CustomNpcs.getWorldSaveDirectory();
|
|
|
|
|
try {
|
|
|
|
|
File file = new File(saveDir, "global.dat");
|
|
|
|
|
if(file.exists()){
|
|
|
|
|
loadData(file);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
try {
|
|
|
|
|
File file = new File(saveDir, "global.dat_old");
|
|
|
|
|
if(file.exists()){
|
|
|
|
|
loadData(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (Exception ee) {
|
|
|
|
|
ee.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-12 01:26:23 -05:00
|
|
|
private void loadData(File file) throws Exception {
|
2018-09-13 12:03:28 -04:00
|
|
|
NBTTagCompound nbttagcompound1 = CompressedStreamTools.readCompressed(new FileInputStream(file));
|
|
|
|
|
itemGiverId = nbttagcompound1.getInteger("itemGiverId");
|
2022-06-12 01:26:23 -05:00
|
|
|
|
|
|
|
|
NBTTagList jobList = nbttagcompound1.getTagList("ItemGivers", 10);
|
|
|
|
|
for (int i = 0; i < jobList.tagCount(); i++) {
|
|
|
|
|
NBTTagCompound compound = jobList.getCompoundTagAt(i);
|
|
|
|
|
JobItemGiver jobItemGiver = new JobItemGiver();
|
|
|
|
|
jobItemGiver.readFromNBT(compound);
|
|
|
|
|
itemGivers.put(jobItemGiver.itemGiverId, jobItemGiver);
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
public void saveData(){
|
|
|
|
|
try {
|
|
|
|
|
File saveDir = CustomNpcs.getWorldSaveDirectory();
|
|
|
|
|
|
|
|
|
|
NBTTagCompound nbttagcompound = new NBTTagCompound();
|
|
|
|
|
nbttagcompound.setInteger("itemGiverId", itemGiverId);
|
2022-06-12 01:26:23 -05:00
|
|
|
|
|
|
|
|
NBTTagList jobList = new NBTTagList();
|
|
|
|
|
for (JobItemGiver jobItemGiver : itemGivers.values()) {
|
|
|
|
|
NBTTagCompound compound = new NBTTagCompound();
|
|
|
|
|
jobItemGiver.writeToNBT(compound);
|
|
|
|
|
jobList.appendTag(compound);
|
|
|
|
|
}
|
|
|
|
|
nbttagcompound.setTag("ItemGivers", jobList);
|
2018-09-13 12:03:28 -04:00
|
|
|
|
|
|
|
|
File file = new File(saveDir, "global.dat_new");
|
|
|
|
|
File file1 = new File(saveDir, "global.dat_old");
|
|
|
|
|
File file2 = new File(saveDir, "global.dat");
|
|
|
|
|
CompressedStreamTools.writeCompressed(nbttagcompound, new FileOutputStream(file));
|
|
|
|
|
if(file1.exists())
|
|
|
|
|
{
|
|
|
|
|
file1.delete();
|
|
|
|
|
}
|
|
|
|
|
file2.renameTo(file1);
|
|
|
|
|
if(file2.exists())
|
|
|
|
|
{
|
|
|
|
|
file2.delete();
|
|
|
|
|
}
|
|
|
|
|
file.renameTo(file2);
|
|
|
|
|
if(file.exists())
|
|
|
|
|
{
|
|
|
|
|
file.delete();
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public int incrementItemGiverId(){
|
|
|
|
|
itemGiverId++;
|
|
|
|
|
return itemGiverId;
|
|
|
|
|
}
|
|
|
|
|
}
|