2022-05-24 01:07:50 -05:00
|
|
|
package noppes.npcs.controllers.data;
|
2018-09-13 12:03:28 -04:00
|
|
|
|
|
|
|
|
import net.minecraft.entity.Entity;
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2021-10-20 23:04:52 -05:00
|
|
|
import net.minecraft.nbt.CompressedStreamTools;
|
2018-09-13 12:03:28 -04:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
import net.minecraftforge.common.IExtendedEntityProperties;
|
2023-01-12 15:07:45 -06:00
|
|
|
import noppes.npcs.AnimationDataShared;
|
2021-09-30 21:44:30 -05:00
|
|
|
import noppes.npcs.CustomNpcs;
|
2021-10-20 23:04:52 -05:00
|
|
|
import noppes.npcs.LogWriter;
|
2018-09-13 12:03:28 -04:00
|
|
|
import noppes.npcs.constants.EnumRoleType;
|
2022-05-24 01:07:50 -05:00
|
|
|
import noppes.npcs.controllers.PlayerDataController;
|
2018-09-13 12:03:28 -04:00
|
|
|
import noppes.npcs.entity.EntityCustomNpc;
|
|
|
|
|
import noppes.npcs.entity.EntityNPCInterface;
|
2022-05-23 20:41:32 -05:00
|
|
|
import noppes.npcs.entity.data.DataSkinOverlays;
|
2021-10-11 06:49:30 -05:00
|
|
|
import noppes.npcs.entity.data.DataTimers;
|
2018-09-13 12:03:28 -04:00
|
|
|
import noppes.npcs.roles.RoleCompanion;
|
2022-06-12 01:26:23 -05:00
|
|
|
import noppes.npcs.scripted.NpcAPI;
|
2022-11-15 15:25:53 -05:00
|
|
|
import noppes.npcs.api.entity.ICustomNpc;
|
|
|
|
|
import noppes.npcs.api.handler.*;
|
2021-10-20 23:04:52 -05:00
|
|
|
import noppes.npcs.util.NBTJsonUtil;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
2018-09-13 12:03:28 -04:00
|
|
|
|
2022-06-12 01:26:23 -05:00
|
|
|
public class PlayerData implements IExtendedEntityProperties, IPlayerData {
|
|
|
|
|
public PlayerDialogData dialogData = new PlayerDialogData(this);
|
|
|
|
|
public PlayerBankData bankData = new PlayerBankData(this);
|
|
|
|
|
public PlayerQuestData questData = new PlayerQuestData(this);
|
|
|
|
|
public PlayerTransportData transportData = new PlayerTransportData(this);
|
|
|
|
|
public PlayerFactionData factionData = new PlayerFactionData(this);
|
|
|
|
|
public PlayerItemGiverData itemgiverData = new PlayerItemGiverData(this);
|
|
|
|
|
public PlayerMailData mailData = new PlayerMailData(this);
|
2023-01-12 15:07:45 -06:00
|
|
|
public AnimationDataShared modelData = new AnimationDataShared(this);
|
2022-06-12 01:26:23 -05:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public DataTimers timers = new DataTimers(this);
|
2022-05-23 20:41:32 -05:00
|
|
|
public DataSkinOverlays skinOverlays = new DataSkinOverlays(this);
|
2021-09-30 21:44:30 -05:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
public EntityNPCInterface editingNpc;
|
|
|
|
|
public NBTTagCompound cloned;
|
|
|
|
|
|
|
|
|
|
public EntityPlayer player;
|
|
|
|
|
|
|
|
|
|
public String playername = "";
|
|
|
|
|
public String uuid = "";
|
2021-11-16 10:41:04 -06:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
private EntityNPCInterface activeCompanion = null;
|
|
|
|
|
public int companionID = 0;
|
|
|
|
|
|
2021-11-16 10:41:04 -06:00
|
|
|
public boolean isGUIOpen = false;
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
@Override
|
2022-11-11 15:15:42 -03:00
|
|
|
public void saveNBTData(NBTTagCompound nbtTagCompound) {
|
|
|
|
|
//Do Nothing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void savePlayerDataOnFile() {
|
2018-09-13 12:03:28 -04:00
|
|
|
PlayerDataController.instance.savePlayerData(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void loadNBTData(NBTTagCompound compound) {
|
2022-11-11 15:15:42 -03:00
|
|
|
//Do Nothing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void loadPlayerDataFromFile() {
|
2018-09-13 12:03:28 -04:00
|
|
|
NBTTagCompound data = PlayerDataController.instance.loadPlayerData(player.getPersistentID().toString());
|
|
|
|
|
if(data.hasNoTags()){
|
|
|
|
|
data = PlayerDataController.instance.loadPlayerDataOld(player.getCommandSenderName());
|
|
|
|
|
}
|
|
|
|
|
setNBT(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setNBT(NBTTagCompound data){
|
|
|
|
|
dialogData.loadNBTData(data);
|
|
|
|
|
bankData.loadNBTData(data);
|
|
|
|
|
questData.loadNBTData(data);
|
|
|
|
|
transportData.loadNBTData(data);
|
|
|
|
|
factionData.loadNBTData(data);
|
|
|
|
|
itemgiverData.loadNBTData(data);
|
2021-10-11 06:49:30 -05:00
|
|
|
mailData.loadNBTData(data);
|
|
|
|
|
timers.readFromNBT(data);
|
2022-05-23 20:41:32 -05:00
|
|
|
skinOverlays.readFromNBT(data);
|
2022-12-31 18:47:13 -06:00
|
|
|
modelData.readFromNBT(data);
|
2018-09-13 12:03:28 -04:00
|
|
|
|
|
|
|
|
if(player != null){
|
|
|
|
|
playername = player.getCommandSenderName();
|
|
|
|
|
uuid = player.getPersistentID().toString();
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
playername = data.getString("PlayerName");
|
|
|
|
|
uuid = data.getString("UUID");
|
|
|
|
|
}
|
|
|
|
|
companionID = data.getInteger("PlayerCompanionId");
|
|
|
|
|
if(data.hasKey("PlayerCompanion") && !hasCompanion()){
|
|
|
|
|
EntityCustomNpc npc = new EntityCustomNpc(player.worldObj);
|
|
|
|
|
npc.readEntityFromNBT(data.getCompoundTag("PlayerCompanion"));
|
|
|
|
|
npc.setPosition(player.posX, player.posY, player.posZ);
|
|
|
|
|
if(npc.advanced.role == EnumRoleType.Companion){
|
|
|
|
|
setCompanion(npc);
|
|
|
|
|
((RoleCompanion)npc.roleInterface).setSitting(false);
|
|
|
|
|
player.worldObj.spawnEntityInWorld(npc);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-16 10:41:04 -06:00
|
|
|
isGUIOpen = data.getBoolean("isGUIOpen");
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
public NBTTagCompound getNBT() {
|
|
|
|
|
if(player != null){
|
|
|
|
|
playername = player.getCommandSenderName();
|
|
|
|
|
uuid = player.getPersistentID().toString();
|
|
|
|
|
}
|
|
|
|
|
NBTTagCompound compound = new NBTTagCompound();
|
|
|
|
|
dialogData.saveNBTData(compound);
|
|
|
|
|
bankData.saveNBTData(compound);
|
|
|
|
|
questData.saveNBTData(compound);
|
|
|
|
|
transportData.saveNBTData(compound);
|
|
|
|
|
factionData.saveNBTData(compound);
|
|
|
|
|
itemgiverData.saveNBTData(compound);
|
|
|
|
|
mailData.saveNBTData(compound);
|
2021-10-11 06:49:30 -05:00
|
|
|
timers.writeToNBT(compound);
|
2022-05-23 20:41:32 -05:00
|
|
|
skinOverlays.writeToNBT(compound);
|
2022-12-31 18:47:13 -06:00
|
|
|
modelData.writeToNBT(compound);
|
2021-10-11 06:49:30 -05:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
compound.setString("PlayerName", playername);
|
|
|
|
|
compound.setString("UUID", uuid);
|
|
|
|
|
compound.setInteger("PlayerCompanionId", companionID);
|
2021-11-16 10:41:04 -06:00
|
|
|
compound.setBoolean("isGUIOpen",isGUIOpen);
|
2018-09-13 12:03:28 -04:00
|
|
|
|
|
|
|
|
if(hasCompanion()){
|
|
|
|
|
NBTTagCompound nbt = new NBTTagCompound();
|
|
|
|
|
if(activeCompanion.writeToNBTOptional(nbt))
|
|
|
|
|
compound.setTag("PlayerCompanion", nbt);
|
|
|
|
|
}
|
|
|
|
|
return compound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void init(Entity entity, World world) {
|
|
|
|
|
|
|
|
|
|
}
|
2021-11-16 10:41:04 -06:00
|
|
|
|
|
|
|
|
public void setGUIOpen(boolean bool) {
|
|
|
|
|
isGUIOpen = bool;
|
2022-11-11 15:15:42 -03:00
|
|
|
savePlayerDataOnFile();
|
2021-11-16 10:41:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean getGUIOpen() {
|
2022-11-11 15:15:42 -03:00
|
|
|
loadPlayerDataFromFile();
|
2021-11-16 10:41:04 -06:00
|
|
|
return isGUIOpen;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
public boolean hasCompanion(){
|
|
|
|
|
return activeCompanion != null && !activeCompanion.isDead;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setCompanion(EntityNPCInterface npc) {
|
|
|
|
|
if(npc != null && npc.advanced.role != EnumRoleType.Companion)//shouldnt happen
|
|
|
|
|
return;
|
|
|
|
|
companionID++;
|
|
|
|
|
activeCompanion = npc;
|
|
|
|
|
if(npc != null)
|
|
|
|
|
((RoleCompanion)npc.roleInterface).companionID = companionID;
|
2022-11-11 15:15:42 -03:00
|
|
|
savePlayerDataOnFile();
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void updateCompanion(World world) {
|
|
|
|
|
if(!hasCompanion() || world == activeCompanion.worldObj)
|
|
|
|
|
return;
|
|
|
|
|
RoleCompanion role = (RoleCompanion) activeCompanion.roleInterface;
|
|
|
|
|
role.owner = player;
|
|
|
|
|
if(!role.isFollowing())
|
|
|
|
|
return;
|
|
|
|
|
NBTTagCompound nbt = new NBTTagCompound();
|
|
|
|
|
activeCompanion.writeToNBTOptional(nbt);
|
|
|
|
|
activeCompanion.isDead = true;
|
|
|
|
|
|
|
|
|
|
EntityCustomNpc npc = new EntityCustomNpc(world);
|
|
|
|
|
npc.readEntityFromNBT(nbt);
|
|
|
|
|
npc.setPosition(player.posX, player.posY, player.posZ);
|
|
|
|
|
setCompanion(npc);
|
|
|
|
|
((RoleCompanion)npc.roleInterface).setSitting(false);
|
|
|
|
|
world.spawnEntityInWorld(npc);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 23:04:52 -05:00
|
|
|
public static NBTTagCompound loadPlayerDataOld(String player) {
|
|
|
|
|
File saveDir = CustomNpcs.getWorldSaveDirectory("playerdata");
|
|
|
|
|
String filename = player;
|
|
|
|
|
if (player.isEmpty()) {
|
|
|
|
|
filename = "noplayername";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
filename = filename + ".dat";
|
|
|
|
|
|
|
|
|
|
File file;
|
|
|
|
|
try {
|
|
|
|
|
file = new File(saveDir, filename);
|
|
|
|
|
if (file.exists()) {
|
|
|
|
|
NBTTagCompound comp = CompressedStreamTools.readCompressed(new FileInputStream(file));
|
|
|
|
|
file.delete();
|
|
|
|
|
file = new File(saveDir, filename + "_old");
|
|
|
|
|
if (file.exists()) {
|
|
|
|
|
file.delete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return comp;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception var6) {
|
|
|
|
|
LogWriter.except(var6);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
file = new File(saveDir, filename + "_old");
|
|
|
|
|
if (file.exists()) {
|
|
|
|
|
return CompressedStreamTools.readCompressed(new FileInputStream(file));
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception var5) {
|
|
|
|
|
LogWriter.except(var5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new NBTTagCompound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static NBTTagCompound loadPlayerData(String player) {
|
|
|
|
|
File saveDir = CustomNpcs.getWorldSaveDirectory("playerdata");
|
|
|
|
|
String filename = player;
|
|
|
|
|
if (player.isEmpty()) {
|
|
|
|
|
filename = "noplayername";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
filename = filename + ".json";
|
|
|
|
|
File file = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
file = new File(saveDir, filename);
|
|
|
|
|
if (file.exists()) {
|
|
|
|
|
return NBTJsonUtil.LoadFile(file);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception var5) {
|
|
|
|
|
LogWriter.error("Error loading: " + file.getAbsolutePath(), var5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new NBTTagCompound();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 21:44:30 -05:00
|
|
|
public static PlayerData get(EntityPlayer player) {
|
|
|
|
|
if(player.worldObj.isRemote) {
|
|
|
|
|
return CustomNpcs.proxy.getPlayerData(player);
|
|
|
|
|
} else {
|
|
|
|
|
PlayerData data = new PlayerData();
|
2021-10-20 23:04:52 -05:00
|
|
|
if (data.player == null) {
|
2021-09-30 21:44:30 -05:00
|
|
|
data.player = player;
|
2021-10-20 23:04:52 -05:00
|
|
|
NBTTagCompound compound = loadPlayerData(player.getPersistentID().toString());
|
|
|
|
|
if (compound.hasNoTags()) {
|
|
|
|
|
compound = loadPlayerDataOld(player.getCommandSenderName());
|
|
|
|
|
}
|
2021-09-30 21:44:30 -05:00
|
|
|
|
|
|
|
|
data.setNBT(compound);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-12 01:26:23 -05:00
|
|
|
|
|
|
|
|
public void setCompanion(ICustomNpc npc) {
|
|
|
|
|
this.setCompanion((EntityNPCInterface) npc.getMCEntity());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ICustomNpc getCompanion() {
|
|
|
|
|
return (ICustomNpc) NpcAPI.Instance().getIEntity(activeCompanion);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getCompanionID() {
|
|
|
|
|
return companionID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IPlayerDialogData getDialogData() {
|
|
|
|
|
return dialogData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IPlayerBankData getBankData() {
|
|
|
|
|
return bankData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IPlayerQuestData getQuestData() {
|
|
|
|
|
return questData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IPlayerTransportData getTransportData() {
|
|
|
|
|
return transportData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IPlayerFactionData getFactionData() {
|
|
|
|
|
return factionData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IPlayerItemGiverData getItemGiverData() {
|
|
|
|
|
return itemgiverData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IPlayerMailData getMailData() {
|
|
|
|
|
return mailData;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-31 18:47:13 -06:00
|
|
|
public IPlayerModelData getModelData() {
|
|
|
|
|
return modelData;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-12 01:26:23 -05:00
|
|
|
public void save() {
|
|
|
|
|
PlayerDataController.instance.savePlayerData(this);
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|