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

358 lines
11 KiB
Java
Raw Normal View History

2018-09-13 12:03:28 -04:00
package noppes.npcs.controllers;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.Entity;
2022-01-10 15:21:48 -06:00
import net.minecraft.entity.EntityList;
2018-09-13 12:03:28 -04:00
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
2018-09-13 12:03:28 -04:00
import net.minecraft.util.ChatComponentText;
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
2022-01-10 15:21:48 -06:00
import noppes.npcs.NoppesUtilServer;
2022-11-15 15:25:53 -05:00
import noppes.npcs.api.IPos;
import noppes.npcs.api.IWorld;
2023-01-12 21:33:06 -05:00
import noppes.npcs.api.entity.IEntity;
import noppes.npcs.api.handler.ICloneHandler;
2023-03-22 19:43:36 -04:00
import noppes.npcs.controllers.data.TagMap;
2023-01-12 21:33:06 -05:00
import noppes.npcs.scripted.CustomNPCsException;
import noppes.npcs.scripted.NpcAPI;
2018-09-13 12:03:28 -04:00
import noppes.npcs.util.NBTJsonUtil;
2023-01-12 21:33:06 -05:00
import java.io.File;
import java.io.FileInputStream;
2023-03-22 19:43:36 -04:00
import java.util.*;
2023-01-12 21:33:06 -05:00
2022-01-10 15:21:48 -06:00
public class ServerCloneController implements ICloneHandler {
2018-09-13 12:03:28 -04:00
public static ServerCloneController Instance;
2023-03-22 19:43:36 -04:00
2018-09-13 12:03:28 -04:00
public ServerCloneController(){
loadClones();
}
2023-03-22 19:43:36 -04:00
2018-09-13 12:03:28 -04:00
private void loadClones(){
try {
File dir = new File(getDir(), "..");
2023-03-22 19:43:36 -04:00
File file = new File(dir, "clonednpcs.dat");
if(file.exists()){
Map<Integer, Map<String, NBTTagCompound>> clones = loadOldClones(file);
file.delete();
2018-09-13 12:03:28 -04:00
file = new File(dir, "clonednpcs.dat_old");
if(file.exists())
file.delete();
2023-03-22 19:43:36 -04:00
2018-09-13 12:03:28 -04:00
for(int tab : clones.keySet()){
Map<String, NBTTagCompound> map = clones.get(tab);
for(String name: map.keySet()){
saveClone(tab, name, map.get(name));
}
}
2023-03-22 19:43:36 -04:00
}
2018-09-13 12:03:28 -04:00
} catch (Exception e) {
LogWriter.except(e);
}
}
public File getDir(){
File dir = new File(CustomNpcs.getWorldSaveDirectory(), "clones");
if(!dir.exists())
dir.mkdir();
return dir;
}
2023-03-22 19:43:36 -04:00
2018-09-13 12:03:28 -04:00
private Map<Integer, Map<String, NBTTagCompound>> loadOldClones(File file) throws Exception{
Map<Integer, Map<String, NBTTagCompound>> clones = new HashMap<Integer, Map<String, NBTTagCompound>>();
2023-03-22 19:43:36 -04:00
NBTTagCompound nbttagcompound1 = CompressedStreamTools.readCompressed(new FileInputStream(file));
NBTTagList list = nbttagcompound1.getTagList("Data", 10);
if(list == null){
return clones;
}
for(int i = 0; i < list.tagCount(); i++)
{
NBTTagCompound compound = list.getCompoundTagAt(i);
if(!compound.hasKey("ClonedTab")){
compound.setInteger("ClonedTab", 1);
}
Map<String, NBTTagCompound> tab = clones.get(compound.getInteger("ClonedTab"));
if(tab == null)
clones.put(compound.getInteger("ClonedTab"), tab = new HashMap<String, NBTTagCompound>());
String name = compound.getString("ClonedName");
int number = 1;
while(tab.containsKey(name)){
number++;
name = String.format("%s%s", compound.getString("ClonedName"), number);
}
compound.removeTag("ClonedName");
compound.removeTag("ClonedTab");
compound.removeTag("ClonedDate");
cleanTags(compound);
tab.put(name, compound);
}
2018-09-13 12:03:28 -04:00
return clones;
}
2023-03-22 19:43:36 -04:00
2018-09-13 12:03:28 -04:00
public NBTTagCompound getCloneData(ICommandSender player, String name, int tab) {
File file = new File(new File(getDir(), tab + ""), name + ".json");
if(!file.exists()){
if(player != null)
player.addChatMessage(new ChatComponentText("Could not find clone file"));
return null;
}
try {
return NBTJsonUtil.LoadFile(file);
} catch (Exception e) {
LogWriter.error("Error loading: " + file.getAbsolutePath(), e);
if(player != null)
player.addChatMessage(new ChatComponentText(e.getMessage()));
}
return null;
}
2023-03-22 19:43:36 -04:00
2018-09-13 12:03:28 -04:00
public void saveClone(int tab, String name, NBTTagCompound compound){
try {
File dir = new File(getDir(), tab + "");
if(!dir.exists())
dir.mkdir();
String filename = name + ".json";
2023-03-22 19:43:36 -04:00
File file = new File(dir, filename + "_new");
File file2 = new File(dir, filename);
NBTJsonUtil.SaveFile(file, compound);
addToTagMap(compound, name, tab);
2023-03-22 19:43:36 -04:00
if(file2.exists()){
file2.delete();
}
file.renameTo(file2);
2018-09-13 12:03:28 -04:00
} catch (Exception e) {
LogWriter.except(e);
}
}
public List<String> getClones(int tab){
List<String> list = new ArrayList<String>();
File dir = new File(getDir(), tab + "");
if(!dir.exists() || !dir.isDirectory())
return list;
for(String file : dir.list()){
if(file.endsWith(".json"))
list.add(file.substring(0, file.length() - 5));
}
return list;
}
2023-03-22 19:43:36 -04:00
public List<String> getClonesDate(int tab){
List<String> list = new ArrayList<String>();
File dir = new File(getDir(), tab + "");
if(!dir.exists() || !dir.isDirectory())
return list;
File[] files = dir.listFiles();
Arrays.sort(files, new Comparator<File>(){
public int compare(File f1, File f2)
{
return Long.compare(f1.lastModified(), f2.lastModified());
}
});
for(File file : files){
String fileName = file.getName();
if(fileName.endsWith(".json"))
list.add(fileName.substring(0, fileName.length() - 5));
}
return list;
}
2018-09-13 12:03:28 -04:00
public boolean removeClone(String name, int tab){
2023-03-22 19:43:36 -04:00
File file = new File(new File(getDir(), tab + ""), name + ".json");
if(!file.exists())
return false;
file.delete();
2023-03-22 19:47:13 -04:00
removeFromTagMap(name, tab);
2023-03-22 19:43:36 -04:00
return true;
2018-09-13 12:03:28 -04:00
}
2023-03-22 19:43:36 -04:00
2018-09-13 12:03:28 -04:00
public String addClone(NBTTagCompound nbttagcompound, String name, int tab) {
cleanTags(nbttagcompound);
saveClone(tab, name, nbttagcompound);
return name;
}
2023-03-22 19:43:36 -04:00
public String addClone(NBTTagCompound nbttagcompound, String name, int tab, NBTTagCompound tempTags) {
cleanTagList(nbttagcompound, tempTags);
cleanTags(nbttagcompound);
saveClone(tab, name, nbttagcompound);
return name;
}
public NBTTagCompound cleanTagList(NBTTagCompound nbttagcompound, NBTTagCompound tempTags){
HashSet<UUID> tagUUIDs = new HashSet<UUID>();
if(nbttagcompound.hasKey("TagUUIDs")){
NBTTagList nbtTagList = nbttagcompound.getTagList("TagUUIDs",8);
for (int i = 0; i < nbtTagList.tagCount(); i++) {
tagUUIDs.add(UUID.fromString(nbtTagList.getStringTagAt(i)));
}
nbttagcompound.removeTag("TagUUIDs");
}
if(tempTags.hasKey("TempTagUUIDs")){
NBTTagList nbtTagList = tempTags.getTagList("TempTagUUIDs",8);
for (int i = 0; i < nbtTagList.tagCount(); i++) {
tagUUIDs.add(UUID.fromString(nbtTagList.getStringTagAt(i)));
}
tempTags.removeTag("TempTagUUIDs");
}
if(tagUUIDs.size() > 0){
NBTTagList nbtTagList = new NBTTagList();
for (UUID uuid : tagUUIDs) {
nbtTagList.appendTag(new NBTTagString(uuid.toString()));
}
nbttagcompound.setTag("TagUUIDs", nbtTagList);
}
return nbttagcompound;
}
2023-03-22 19:43:36 -04:00
public boolean addToTagMap(NBTTagCompound nbttagcompound, String name, int tab){
HashSet<UUID> tagUUIDs = new HashSet<UUID>();
if(nbttagcompound.hasKey("TagUUIDs")){
NBTTagList nbtTagList = nbttagcompound.getTagList("TagUUIDs",8);
for (int i = 0; i < nbtTagList.tagCount(); i++) {
tagUUIDs.add(UUID.fromString(nbtTagList.getStringTagAt(i)));
}
}
if(tagUUIDs.size() > 0){
TagMap tagMap = ServerTagMapController.Instance.getTagMap(tab);
tagMap.putClone(name, tagUUIDs);
ServerTagMapController.Instance.saveTagMap(tagMap);
2023-03-22 19:47:13 -04:00
return true;
}
return false;
}
public boolean removeFromTagMap(String name, int tab){
TagMap tagMap = ServerTagMapController.Instance.getTagMap(tab);
if(tagMap.removeClone(name)){
ServerTagMapController.Instance.saveTagMap(tagMap);
2023-03-22 19:43:36 -04:00
return true;
}
return false;
}
2018-09-13 12:03:28 -04:00
public void cleanTags(NBTTagCompound nbttagcompound){
if(nbttagcompound.hasKey("ItemGiverId"))
nbttagcompound.setInteger("ItemGiverId", 0);
if(nbttagcompound.hasKey("TransporterId"))
nbttagcompound.setInteger("TransporterId", -1);
2023-03-22 19:43:36 -04:00
2018-09-13 12:03:28 -04:00
nbttagcompound.removeTag("StartPosNew");
nbttagcompound.removeTag("StartPos");
nbttagcompound.removeTag("MovingPathNew");
nbttagcompound.removeTag("Pos");
nbttagcompound.removeTag("Riding");
if(!nbttagcompound.hasKey("ModRev"))
nbttagcompound.setInteger("ModRev", 1);
if(nbttagcompound.hasKey("TransformRole")){
NBTTagCompound adv = nbttagcompound.getCompoundTag("TransformRole");
adv.setInteger("TransporterId", -1);
nbttagcompound.setTag("TransformRole", adv);
}
if(nbttagcompound.hasKey("TransformJob")){
NBTTagCompound adv = nbttagcompound.getCompoundTag("TransformJob");
adv.setInteger("ItemGiverId", 0);
nbttagcompound.setTag("TransformJob", adv);
}
2023-03-22 19:43:36 -04:00
2018-09-13 12:03:28 -04:00
if(nbttagcompound.hasKey("TransformAI")){
NBTTagCompound adv = nbttagcompound.getCompoundTag("TransformAI");
adv.removeTag("StartPosNew");
adv.removeTag("StartPos");
adv.removeTag("MovingPathNew");
nbttagcompound.setTag("TransformAI", adv);
}
}
2022-01-10 15:21:48 -06:00
2022-07-06 01:29:43 -07:00
public IEntity spawn(double x, double y, double z, int tab, String name, IWorld world, boolean ignoreProtection) {
2022-01-10 15:21:48 -06:00
NBTTagCompound compound = this.getCloneData((ICommandSender)null, name, tab);
if (compound == null) {
throw new CustomNPCsException("Unknown clone tab:" + tab + " name:" + name, new Object[0]);
} else {
2022-07-06 01:29:43 -07:00
Entity entity;
if (!ignoreProtection) {
entity = NoppesUtilServer.spawnCloneWithProtection(compound, (int) x, (int) y, (int) z, world.getMCWorld());
} else {
entity = NoppesUtilServer.spawnClone(compound, (int) x, (int) y, (int) z, world.getMCWorld());
}
return entity == null ? null : NpcAPI.Instance().getIEntity(entity);
2022-01-10 15:21:48 -06:00
}
}
public IEntity spawn(IPos pos, int tab, String name, IWorld world, boolean ignoreProtection) {
return this.spawn(pos.getX(),pos.getY(),pos.getZ(),tab,name,world,ignoreProtection);
}
2022-07-06 01:29:43 -07:00
public IEntity spawn(double x, double y, double z, int tab, String name, IWorld world) {
return spawn(x, y, z, tab, name, world, true);
}
public IEntity spawn(IPos pos, int tab, String name, IWorld world) {
return this.spawn(pos.getX(),pos.getY(),pos.getZ(),tab,name,world);
}
2022-06-29 12:21:30 -05:00
public IEntity[] getTab(int tab, IWorld world) {
File dir = new File(getDir(), tab + "");
if (!dir.exists() || !dir.isDirectory() || dir.listFiles() == null) {
return new IEntity[]{};
}
ArrayList<IEntity> arrayList = new ArrayList<>();
try {
for (File file : dir.listFiles()) {
if (file.getName().endsWith(".json")) {
NBTTagCompound compound = NBTJsonUtil.LoadFile(file);
Instance.cleanTags(compound);
Entity entity = EntityList.createEntityFromNBT(compound, world.getMCWorld());
arrayList.add(entity == null ? null : NpcAPI.Instance().getIEntity(entity));
}
}
} catch (Exception ignored) {}
return arrayList.toArray(new IEntity[]{});
}
2022-01-10 15:21:48 -06:00
public IEntity get(int tab, String name, IWorld world) {
NBTTagCompound compound = this.getCloneData((ICommandSender)null, name, tab);
if (compound == null) {
throw new CustomNPCsException("Unknown clone tab:" + tab + " name:" + name, new Object[0]);
} else {
Instance.cleanTags(compound);
Entity entity = EntityList.createEntityFromNBT(compound, world.getMCWorld());
return entity == null ? null : NpcAPI.Instance().getIEntity(entity);
2022-01-10 15:21:48 -06:00
}
}
2024-03-28 01:34:12 -04:00
public boolean has(int tab, String name) {
NBTTagCompound compound = this.getCloneData((ICommandSender)null, name, tab);
return compound != null;
}
public void set(int tab, String name, IEntity entity) {
2022-01-10 15:21:48 -06:00
NBTTagCompound compound = new NBTTagCompound();
2024-03-28 01:13:15 -04:00
if (!entity.getMCEntity().writeMountToNBT(compound))
2022-01-10 15:21:48 -06:00
throw new CustomNPCsException("Cannot save dead entities", new Object[0]);
2024-03-28 01:13:15 -04:00
this.cleanTags(compound);
this.saveClone(tab, name, compound);
2022-01-10 15:21:48 -06:00
}
public void remove(int tab, String name) {
this.removeClone(name, tab);
}
2018-09-13 12:03:28 -04:00
}