2018-09-13 12:03:28 -04:00
|
|
|
package noppes.npcs.controllers;
|
|
|
|
|
|
|
|
|
|
import net.minecraft.nbt.CompressedStreamTools;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
|
|
|
|
import noppes.npcs.CustomNpcs;
|
2022-05-24 01:07:50 -05:00
|
|
|
import noppes.npcs.controllers.data.Bank;
|
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.io.IOException;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
public class BankController {
|
2025-03-13 17:14:45 -04:00
|
|
|
public HashMap<Integer, Bank> banks;
|
|
|
|
|
private String filePath = "";
|
|
|
|
|
|
|
|
|
|
private static BankController instance;
|
|
|
|
|
|
|
|
|
|
public BankController() {
|
|
|
|
|
instance = this;
|
|
|
|
|
banks = new HashMap<Integer, Bank>();
|
|
|
|
|
loadBanks();
|
|
|
|
|
|
|
|
|
|
if (banks.isEmpty()) {
|
|
|
|
|
Bank bank = new Bank();
|
|
|
|
|
bank.id = 0;
|
|
|
|
|
bank.name = "Default Bank";
|
|
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
|
|
bank.slotTypes.put(i, 0);
|
|
|
|
|
|
|
|
|
|
banks.put(bank.id, bank);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static BankController getInstance() {
|
|
|
|
|
if (newInstance()) {
|
|
|
|
|
instance = new BankController();
|
|
|
|
|
}
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static boolean newInstance() {
|
|
|
|
|
if (instance == null)
|
|
|
|
|
return true;
|
|
|
|
|
File file = CustomNpcs.getWorldSaveDirectory();
|
|
|
|
|
if (file == null)
|
|
|
|
|
return false;
|
|
|
|
|
if (!instance.filePath.equals(file.getAbsolutePath())) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void loadBanks() {
|
|
|
|
|
File saveDir = CustomNpcs.getWorldSaveDirectory();
|
|
|
|
|
if (saveDir == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
filePath = saveDir.getAbsolutePath();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
File file = new File(saveDir, "bank.dat");
|
|
|
|
|
if (file.exists()) {
|
|
|
|
|
loadBanks(file);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
try {
|
|
|
|
|
File file = new File(saveDir, "bank.dat_old");
|
|
|
|
|
if (file.exists()) {
|
|
|
|
|
loadBanks(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (Exception ee) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void loadBanks(File file) throws IOException {
|
|
|
|
|
loadBanks(CompressedStreamTools.readCompressed(new FileInputStream(file)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void loadBanks(NBTTagCompound nbttagcompound1) throws IOException {
|
|
|
|
|
HashMap<Integer, Bank> banks = new HashMap<Integer, Bank>();
|
2018-09-13 12:03:28 -04:00
|
|
|
NBTTagList list = nbttagcompound1.getTagList("Data", 10);
|
2025-03-13 17:14:45 -04:00
|
|
|
if (list != null) {
|
|
|
|
|
for (int i = 0; i < list.tagCount(); i++) {
|
|
|
|
|
NBTTagCompound nbttagcompound = list.getCompoundTagAt(i);
|
|
|
|
|
Bank bank = new Bank();
|
|
|
|
|
bank.readEntityFromNBT(nbttagcompound);
|
|
|
|
|
banks.put(bank.id, bank);
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
this.banks = banks;
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public NBTTagCompound getNBT() {
|
2018-09-13 12:03:28 -04:00
|
|
|
|
|
|
|
|
NBTTagList list = new NBTTagList();
|
2025-03-13 17:14:45 -04:00
|
|
|
for (Bank bank : banks.values()) {
|
2018-09-13 12:03:28 -04:00
|
|
|
NBTTagCompound nbtfactions = new NBTTagCompound();
|
|
|
|
|
bank.writeEntityToNBT(nbtfactions);
|
2025-03-13 17:14:45 -04:00
|
|
|
list.appendTag(nbtfactions);
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
NBTTagCompound nbttagcompound = new NBTTagCompound();
|
|
|
|
|
nbttagcompound.setTag("Data", list);
|
|
|
|
|
return nbttagcompound;
|
2025-03-13 17:14:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Bank getBank(int bankId) {
|
|
|
|
|
Bank bank = banks.get(bankId);
|
|
|
|
|
if (bank != null)
|
|
|
|
|
return bank;
|
|
|
|
|
return banks.values().iterator().next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void saveBanks() {
|
|
|
|
|
try {
|
|
|
|
|
File saveDir = CustomNpcs.getWorldSaveDirectory();
|
2018-09-13 12:03:28 -04:00
|
|
|
File file = new File(saveDir, "bank.dat_new");
|
|
|
|
|
File file1 = new File(saveDir, "bank.dat_old");
|
|
|
|
|
File file2 = new File(saveDir, "bank.dat");
|
|
|
|
|
CompressedStreamTools.writeCompressed(getNBT(), new FileOutputStream(file));
|
2025-03-13 17:14:45 -04:00
|
|
|
if (file1.exists()) {
|
2018-09-13 12:03:28 -04:00
|
|
|
file1.delete();
|
|
|
|
|
}
|
|
|
|
|
file2.renameTo(file1);
|
2025-03-13 17:14:45 -04:00
|
|
|
if (file2.exists()) {
|
2018-09-13 12:03:28 -04:00
|
|
|
file2.delete();
|
|
|
|
|
}
|
|
|
|
|
file.renameTo(file2);
|
2025-03-13 17:14:45 -04:00
|
|
|
if (file.exists()) {
|
2018-09-13 12:03:28 -04:00
|
|
|
file.delete();
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void saveBank(Bank bank) {
|
|
|
|
|
if (bank.id < 0) {
|
|
|
|
|
bank.id = getUnusedId();
|
|
|
|
|
}
|
|
|
|
|
banks.put(bank.id, bank);
|
|
|
|
|
saveBanks();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getUnusedId() {
|
|
|
|
|
int id = 0;
|
|
|
|
|
while (true) {
|
|
|
|
|
if (!banks.containsKey(id))
|
|
|
|
|
return id;
|
|
|
|
|
id++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void removeBank(int bank) {
|
|
|
|
|
if (bank < 0 || banks.size() <= 1)
|
|
|
|
|
return;
|
|
|
|
|
banks.remove(bank);
|
|
|
|
|
saveBanks();
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|