Files
CustomNPC-Plus/src/main/java/noppes/npcs/controllers/LinkedNpcController.java
T

197 lines
4.7 KiB
Java
Raw Normal View History

2018-09-13 12:03:28 -04:00
package noppes.npcs.controllers;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import noppes.npcs.NBTTags;
import noppes.npcs.entity.EntityCustomNpc;
import noppes.npcs.entity.EntityNPCInterface;
import noppes.npcs.util.JsonException;
import noppes.npcs.util.NBTJsonUtil;
2023-01-12 21:33:06 -05:00
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
2018-09-13 12:03:28 -04:00
public class LinkedNpcController {
public static LinkedNpcController Instance;
public List<LinkedData> list = new ArrayList<LinkedData>();
2025-02-06 22:37:49 +03:00
2018-09-13 12:03:28 -04:00
public LinkedNpcController(){
Instance = this;
load();
}
private void load(){
try {
loadNpcs();
} catch (Exception e) {
LogWriter.except(e);
}
}
2025-02-06 22:37:49 +03:00
2018-09-13 12:03:28 -04:00
public File getDir(){
File dir = new File(CustomNpcs.getWorldSaveDirectory(), "linkednpcs");
if(!dir.exists())
dir.mkdir();
return dir;
}
2025-02-06 22:37:49 +03:00
2018-09-13 12:03:28 -04:00
private void loadNpcs(){
LogWriter.info("Loading Linked Npcs");
File dir = getDir();
if(dir.exists()){
List<LinkedData> list = new ArrayList<LinkedData>();
for(File file : dir.listFiles()){
if(file.getName().endsWith(".json")){
try{
NBTTagCompound compound = NBTJsonUtil.LoadFile(file);;
LinkedData linked = new LinkedData();
linked.setNBT(compound);
list.add(linked);
}
catch(Exception e){
LogWriter.error("Error loading: " + file.getAbsolutePath(), e);
}
}
}
2025-02-06 22:37:49 +03:00
2018-09-13 12:03:28 -04:00
this.list = list;
}
LogWriter.info("Done loading Linked Npcs");
}
2025-02-06 22:37:49 +03:00
2018-09-13 12:03:28 -04:00
public void save(){
for(LinkedData npc : list){
try {
saveNpc(npc);
} catch (IOException e) {
LogWriter.except(e);
}
}
}
2025-02-06 22:37:49 +03:00
2018-09-13 12:03:28 -04:00
private void saveNpc(LinkedData npc) throws IOException {
File file = new File(getDir(), npc.name + ".json_new");
2025-02-06 22:37:49 +03:00
File file1 = new File(getDir(), npc.name + ".json");
2018-09-13 12:03:28 -04:00
try{
NBTJsonUtil.SaveFile(file, npc.getNBT());
if(file1.exists()){
file1.delete();
}
file.renameTo(file1);
2025-02-06 22:37:49 +03:00
2018-09-13 12:03:28 -04:00
} catch (JsonException e) {
LogWriter.except(e);
}
}
public static class LinkedData{
public String name = "LinkedNpc";
public long time = 0;
public NBTTagCompound data = new NBTTagCompound();
2025-02-06 22:37:49 +03:00
2018-09-13 12:03:28 -04:00
public LinkedData(){
time = System.currentTimeMillis();
}
2025-02-06 22:37:49 +03:00
2018-09-13 12:03:28 -04:00
public void setNBT(NBTTagCompound compound){
name = compound.getString("LinkedName");
data = compound.getCompoundTag("NPCData");
}
2025-02-06 22:37:49 +03:00
2018-09-13 12:03:28 -04:00
public NBTTagCompound getNBT(){
NBTTagCompound compound = new NBTTagCompound();
compound.setString("LinkedName", name);
compound.setTag("NPCData", data);
return compound;
}
}
public void loadNpcData(EntityNPCInterface npc) {
if(npc.linkedName.isEmpty())
return;
LinkedData data = getData(npc.linkedName);
if(data == null){
npc.linkedLast = 0;
npc.linkedName = "";
npc.linkedData = null;
}
else{
npc.linkedData = data;
2025-02-06 22:37:49 +03:00
if(npc.posX == 0 && npc.posY == 0 && npc.posZ == 0)
2018-09-13 12:03:28 -04:00
return;
npc.linkedLast = data.time;
2025-02-10 21:32:55 -05:00
List<int[]> points = npc.ais.getMovingPath();
2018-09-13 12:03:28 -04:00
NBTTagCompound compound = NBTTags.NBTMerge(readNpcData(npc), data.data);
2025-02-06 22:37:49 +03:00
2018-09-13 12:03:28 -04:00
npc.display.readToNBT(compound);
npc.stats.readToNBT(compound);
npc.advanced.readToNBT(compound);
npc.inventory.readEntityFromNBT(compound);
if(compound.hasKey("ModelData"))
((EntityCustomNpc)npc).modelData.readFromNBT(compound.getCompoundTag("ModelData"));
2025-02-06 22:37:49 +03:00
2025-02-10 21:32:55 -05:00
npc.ais.readToNBT(compound);
2018-09-13 12:03:28 -04:00
npc.transform.readToNBT(compound);
2025-02-10 21:32:55 -05:00
npc.ais.setMovingPath(points);
2018-09-13 12:03:28 -04:00
npc.updateClient = true;
}
}
2025-02-06 22:37:49 +03:00
2018-09-13 12:03:28 -04:00
private void cleanTags(NBTTagCompound compound){
compound.removeTag("MovingPathNew");
}
2025-02-06 22:37:49 +03:00
2018-09-13 12:03:28 -04:00
public LinkedData getData(String name){
for(LinkedData data : list){
if(data.name.equalsIgnoreCase(name))
return data;
}
return null;
}
2025-02-06 22:37:49 +03:00
2018-09-13 12:03:28 -04:00
private NBTTagCompound readNpcData(EntityNPCInterface npc){
NBTTagCompound compound = new NBTTagCompound();
npc.display.writeToNBT(compound);
npc.inventory.writeEntityToNBT(compound);
npc.stats.writeToNBT(compound);
2025-02-10 21:32:55 -05:00
npc.ais.writeToNBT(compound);
2018-09-13 12:03:28 -04:00
npc.advanced.writeToNBT(compound);
npc.transform.writeToNBT(compound);
compound.setTag("ModelData", ((EntityCustomNpc)npc).modelData.writeToNBT());
return compound;
}
2025-02-06 22:37:49 +03:00
2018-09-13 12:03:28 -04:00
public void saveNpcData(EntityNPCInterface npc) {
NBTTagCompound compound = readNpcData(npc);
cleanTags(compound);
2025-02-06 22:37:49 +03:00
2018-09-13 12:03:28 -04:00
if(npc.linkedData.data.equals(compound))
return;
2025-02-06 22:37:49 +03:00
2018-09-13 12:03:28 -04:00
npc.linkedData.data = compound;
npc.linkedData.time = System.currentTimeMillis();
save();
}
public void removeData(String name) {
Iterator<LinkedData> ita = list.iterator();
while(ita.hasNext()){
if(ita.next().name.equalsIgnoreCase(name))
ita.remove();
}
save();
}
2025-02-06 22:37:49 +03:00
2018-09-13 12:03:28 -04:00
public void addData(String name){
if(getData(name) != null || name.isEmpty())
return;
2025-02-06 22:37:49 +03:00
2018-09-13 12:03:28 -04:00
LinkedData data = new LinkedData();
data.name = name;
list.add(data);
save();
}
}