2018-09-13 12:03:28 -04:00
|
|
|
package noppes.npcs.controllers;
|
|
|
|
|
|
|
|
|
|
import net.minecraft.nbt.CompressedStreamTools;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import noppes.npcs.CustomNpcs;
|
|
|
|
|
|
2023-01-12 21:33:06 -05:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
|
2024-03-05 13:51:03 -05:00
|
|
|
import static noppes.npcs.util.CustomNPCsThreader.customNPCThread;
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
public class GlobalDataController {
|
2025-03-12 20:12:01 -04:00
|
|
|
public static GlobalDataController Instance;
|
|
|
|
|
private int itemGiverId = 0;
|
2024-03-05 13:51:03 -05:00
|
|
|
|
2025-03-12 20:12:01 -04:00
|
|
|
public GlobalDataController() {
|
|
|
|
|
Instance = this;
|
|
|
|
|
load();
|
|
|
|
|
}
|
2024-03-05 13:51:03 -05:00
|
|
|
|
2025-03-12 20:12:01 -04:00
|
|
|
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);
|
|
|
|
|
}
|
2024-03-05 13:51:03 -05:00
|
|
|
|
2025-03-12 20:12:01 -04:00
|
|
|
} catch (Exception ee) {
|
|
|
|
|
ee.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void loadData(File file) throws Exception {
|
2025-07-23 05:43:50 -04:00
|
|
|
NBTTagCompound nbttagcompound1;
|
|
|
|
|
try (FileInputStream fis = new FileInputStream(file)) {
|
|
|
|
|
nbttagcompound1 = CompressedStreamTools.readCompressed(fis);
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
itemGiverId = nbttagcompound1.getInteger("itemGiverId");
|
2025-03-12 20:12:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void saveData() {
|
2024-03-05 13:51:03 -05:00
|
|
|
customNPCThread.execute(() -> {
|
|
|
|
|
try {
|
|
|
|
|
File saveDir = CustomNpcs.getWorldSaveDirectory();
|
2018-09-13 12:03:28 -04:00
|
|
|
|
2024-03-05 13:51:03 -05:00
|
|
|
NBTTagCompound nbttagcompound = new NBTTagCompound();
|
|
|
|
|
nbttagcompound.setInteger("itemGiverId", itemGiverId);
|
2022-06-12 01:26:23 -05:00
|
|
|
|
2024-03-05 13:51:03 -05: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));
|
2025-03-12 20:12:01 -04:00
|
|
|
if (file1.exists()) {
|
2024-03-05 13:51:03 -05:00
|
|
|
file1.delete();
|
|
|
|
|
}
|
|
|
|
|
file2.renameTo(file1);
|
2025-03-12 20:12:01 -04:00
|
|
|
if (file2.exists()) {
|
2024-03-05 13:51:03 -05:00
|
|
|
file2.delete();
|
|
|
|
|
}
|
|
|
|
|
file.renameTo(file2);
|
2025-03-12 20:12:01 -04:00
|
|
|
if (file.exists()) {
|
2024-03-05 13:51:03 -05:00
|
|
|
file.delete();
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
2024-03-05 13:51:03 -05:00
|
|
|
});
|
2025-03-12 20:12:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int incrementItemGiverId() {
|
|
|
|
|
itemGiverId++;
|
2024-03-05 13:51:03 -05:00
|
|
|
saveData();
|
2025-03-12 20:12:01 -04:00
|
|
|
return itemGiverId;
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|