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

438 lines
15 KiB
Java
Raw Normal View History

package noppes.npcs.controllers.data;
2018-09-13 12:03:28 -04:00
2025-01-29 13:20:04 -05:00
import kamkeel.npcs.addon.DBCAddon;
import kamkeel.npcs.controllers.ProfileController;
2025-02-27 15:54:42 -05:00
import kamkeel.npcs.controllers.data.profile.Profile;
import kamkeel.npcs.network.packets.data.AchievementPacket;
import kamkeel.npcs.network.packets.data.ChatAlertPacket;
2025-02-02 22:47:40 -05:00
import kamkeel.npcs.network.packets.request.party.PartyInvitePacket;
2018-09-13 12:03:28 -04:00
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
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;
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
2023-01-12 21:33:06 -05:00
import noppes.npcs.api.entity.ICustomNpc;
import noppes.npcs.api.handler.*;
import noppes.npcs.config.ConfigMain;
2018-09-13 12:03:28 -04:00
import noppes.npcs.constants.EnumRoleType;
2025-02-19 00:21:40 -05:00
import noppes.npcs.controllers.CustomEffectController;
import noppes.npcs.controllers.PartyController;
import noppes.npcs.controllers.PlayerDataController;
import noppes.npcs.controllers.data.action.ActionManager;
2018-09-13 12:03:28 -04:00
import noppes.npcs.entity.EntityCustomNpc;
import noppes.npcs.entity.EntityNPCInterface;
import noppes.npcs.entity.data.DataSkinOverlays;
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;
import noppes.npcs.scripted.ScreenSize;
import noppes.npcs.util.CustomNPCsThreader;
import noppes.npcs.util.NBTJsonUtil;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashSet;
2023-10-31 23:36:34 -04:00
import java.util.UUID;
2018-09-13 12:03:28 -04:00
2022-06-12 01:26:23 -05:00
public class PlayerData implements IExtendedEntityProperties, IPlayerData {
2025-03-13 17:14:45 -04:00
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);
public AnimationData animationData = new AnimationData(this);
2025-02-10 18:12:58 -05:00
public PlayerEffectData effectData = new PlayerEffectData(this);
2025-03-13 17:14:45 -04:00
public DataTimers timers = new DataTimers(this);
public DataSkinOverlays skinOverlays = new DataSkinOverlays(this);
2025-03-05 19:40:55 -05:00
public MagicData magicData = new MagicData();
public ActionManager actionManager = new ActionManager();
2025-04-09 01:55:34 -04:00
public PlayerDataScript scriptData;
2025-03-13 17:14:45 -04:00
public EntityNPCInterface editingNpc;
public NBTTagCompound cloned;
2025-03-13 17:14:45 -04:00
public UUID partyUUID = null;
private final HashSet<UUID> partyInvites = new HashSet<>();
2025-03-13 17:14:45 -04:00
public EntityPlayer player;
2018-09-13 12:03:28 -04:00
2025-03-13 17:14:45 -04:00
public String playername = "";
public String uuid = "";
2025-03-13 17:14:45 -04:00
private EntityNPCInterface activeCompanion = null;
public int companionID = 0;
2018-09-13 12:03:28 -04:00
2025-03-13 17:14:45 -04:00
public boolean isGUIOpen = false;
2024-02-28 20:58:00 -05:00
public boolean hadInteract = true;
public boolean updateClient = false;
2025-03-13 17:14:45 -04:00
public ScreenSize screenSize = new ScreenSize(-1, -1);
2025-02-18 06:25:09 -05:00
public int profileSlot = 0;
2025-03-13 17:14:45 -04:00
public void onLogin() {
2025-02-19 00:21:40 -05:00
// Continue playing animation for self when re-logging
AnimationData animationData = this.animationData;
if (animationData != null && animationData.isClientAnimating()) {
Animation currentAnimation = animationData.currentClientAnimation;
NBTTagCompound compound = currentAnimation.writeToNBT();
animationData.viewAnimation(currentAnimation, animationData, compound,
animationData.isClientAnimating(), currentAnimation.currentFrame, currentAnimation.currentFrameTime);
}
2025-02-19 00:21:40 -05:00
CustomEffectController controller = CustomEffectController.getInstance();
UUID playerID = player.getPersistentID();
// Only add if there are saved effects and none are registered yet.
if (!controller.playerEffects.containsKey(playerID)) {
controller.playerEffects.put(playerID, effectData.getEffects());
}
}
2025-03-13 17:14:45 -04:00
public void onLogout() {
this.partyInvites.clear();
2025-04-26 20:52:08 -04:00
this.actionManager.clear();
2025-03-13 17:14:45 -04:00
}
@Override
public void saveNBTData(NBTTagCompound nbtTagCompound) {
}
@Override
public void loadNBTData(NBTTagCompound compound) {
}
public void setNBT(NBTTagCompound data) {
dialogData.loadNBTData(data);
bankData.loadNBTData(data);
questData.loadNBTData(data);
transportData.loadNBTData(data);
factionData.loadNBTData(data);
itemgiverData.loadNBTData(data);
mailData.loadNBTData(data);
timers.readFromNBT(data);
skinOverlays.readFromNBT(data);
animationData.readFromNBT(data);
2025-02-10 18:12:58 -05:00
effectData.readFromNBT(data);
2025-03-05 19:40:55 -05:00
magicData.readToNBT(data);
2018-09-13 12:03:28 -04:00
2025-03-13 17:14:45 -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");
2025-02-18 06:25:09 -05:00
profileSlot = data.getInteger("ProfileSlot");
2025-03-13 17:14:45 -04:00
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);
}
}
isGUIOpen = data.getBoolean("isGUIOpen");
2024-04-15 14:40:26 -04:00
DBCAddon.instance.readFromNBT(this, data);
2025-03-13 17:14:45 -04:00
}
2024-03-29 02:15:26 -04:00
public NBTTagCompound getNBT() {
2025-03-13 17:14:45 -04:00
if (player != null) {
2024-03-29 02:15:26 -04:00
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);
timers.writeToNBT(compound);
skinOverlays.writeToNBT(compound);
animationData.writeToNBT(compound);
2025-02-10 18:12:58 -05:00
effectData.writeToNBT(compound);
2025-03-05 19:40:55 -05:00
magicData.writeToNBT(compound);
2024-03-29 02:15:26 -04:00
compound.setString("PlayerName", playername);
compound.setString("UUID", uuid);
compound.setInteger("PlayerCompanionId", companionID);
2025-03-13 17:14:45 -04:00
compound.setBoolean("isGUIOpen", isGUIOpen);
2025-02-18 06:25:09 -05:00
compound.setInteger("ProfileSlot", profileSlot);
2024-03-29 02:15:26 -04:00
2025-03-13 17:14:45 -04:00
if (hasCompanion()) {
2024-03-29 02:15:26 -04:00
NBTTagCompound nbt = new NBTTagCompound();
2025-03-13 17:14:45 -04:00
if (activeCompanion.writeToNBTOptional(nbt))
2024-03-29 02:15:26 -04:00
compound.setTag("PlayerCompanion", nbt);
}
2024-04-15 14:40:26 -04:00
DBCAddon.instance.writeToNBT(this, compound);
2024-03-29 02:15:26 -04:00
return compound;
}
2018-09-13 12:03:28 -04:00
2025-03-13 17:14:45 -04:00
public NBTTagCompound getSyncNBT() {
NBTTagCompound compound = new NBTTagCompound();
dialogData.saveNBTData(compound);
questData.saveNBTData(compound);
factionData.saveNBTData(compound);
return compound;
}
2025-03-13 17:14:45 -04:00
public void setSyncNBT(NBTTagCompound data) {
2024-03-28 18:16:17 -04:00
dialogData.loadNBTData(data);
questData.loadNBTData(data);
factionData.loadNBTData(data);
}
2025-03-13 17:14:45 -04:00
public NBTTagCompound getPlayerEffects() {
2025-02-10 18:12:58 -05:00
NBTTagCompound compound = new NBTTagCompound();
effectData.writeToNBT(compound);
return compound;
}
2025-03-13 17:14:45 -04:00
public void setPlayerEffects(NBTTagCompound data) {
2025-02-10 18:12:58 -05:00
effectData.readFromNBT(data);
}
2024-03-29 02:15:26 -04:00
public NBTTagCompound getSyncNBTFull() {
2025-03-13 17:14:45 -04:00
if (player != null) {
2024-03-29 02:15:26 -04:00
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);
mailData.saveNBTData(compound);
compound.setString("PlayerName", playername);
compound.setString("UUID", uuid);
2024-04-15 14:40:26 -04:00
DBCAddon.instance.writeToNBT(this, compound);
2024-03-29 02:15:26 -04:00
return compound;
}
2025-03-13 17:14:45 -04:00
public void setSyncNBTFull(NBTTagCompound data) {
2024-03-29 02:15:26 -04:00
dialogData.loadNBTData(data);
bankData.loadNBTData(data);
questData.loadNBTData(data);
transportData.loadNBTData(data);
factionData.loadNBTData(data);
mailData.loadNBTData(data);
2025-03-13 17:14:45 -04:00
if (player != null) {
2024-03-29 02:15:26 -04:00
playername = player.getCommandSenderName();
uuid = player.getPersistentID().toString();
2025-03-13 17:14:45 -04:00
} else {
2024-03-29 02:15:26 -04:00
playername = data.getString("PlayerName");
uuid = data.getString("UUID");
}
2024-04-15 14:40:26 -04:00
DBCAddon.instance.readFromNBT(this, data);
2024-03-29 02:15:26 -04:00
}
2025-03-13 17:14:45 -04:00
public void getDBCSync(NBTTagCompound compound) {
2024-04-15 19:20:24 -04:00
DBCAddon.instance.writeToNBT(this, compound);
}
2025-03-13 17:14:45 -04:00
public void setDBCSync(NBTTagCompound data) {
2024-04-15 19:20:24 -04:00
DBCAddon.instance.readFromNBT(this, data);
}
2025-03-13 17:14:45 -04:00
@Override
public void init(Entity entity, World world) {
}
public void setGUIOpen(boolean bool) {
this.isGUIOpen = bool;
}
public boolean getGUIOpen() {
return this.isGUIOpen;
}
public ScreenSize getScreenSize() {
return screenSize;
}
public void setScreenSize(ScreenSize size) {
screenSize = size;
}
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;
save();
}
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);
}
public void inviteToParty(Party party) {
if (party != null && this.partyUUID == null && !this.partyInvites.contains(party.getPartyUUID())) {
this.partyInvites.add(party.getPartyUUID());
2025-01-29 21:50:55 -05:00
AchievementPacket.sendAchievement((EntityPlayerMP) player, true, "party.inviteAlert", party.getPartyLeader().getCommandSenderName());
ChatAlertPacket.sendChatAlert((EntityPlayerMP) player, "\u00A7a", "party.inviteChat", " ", party.getPartyLeader().getCommandSenderName(), "!");
2025-03-13 17:14:45 -04:00
}
}
public void ignoreInvite(UUID uuid) {
if (uuid != null) {
this.partyInvites.remove(uuid);
PartyInvitePacket.sendInviteData((EntityPlayerMP) player);
}
}
public void acceptInvite(UUID uuid) {
if (uuid != null) {
this.partyInvites.remove(uuid);
Party party = PartyController.Instance().getParty(uuid);
if (party != null) {
if (!party.getIsLocked()) {
party.addPlayer(player);
PartyController.Instance().pingPartyUpdate(party);
}
}
2025-03-13 17:14:45 -04:00
}
}
2024-03-03 20:19:08 -05:00
public Party getPlayerParty() {
if (partyUUID != null) {
return PartyController.Instance().getParty(partyUUID);
}
return null;
}
2025-03-13 17:14:45 -04:00
public HashSet<UUID> getPartyInvites() {
return (HashSet<UUID>) this.partyInvites.clone();
}
2025-03-13 17:14:45 -04:00
public void setCompanion(ICustomNpc npc) {
this.setCompanion((EntityNPCInterface) npc.getMCEntity());
}
2022-06-12 01:26:23 -05:00
2025-03-13 17:14:45 -04:00
public ICustomNpc getCompanion() {
return (ICustomNpc) NpcAPI.Instance().getIEntity(activeCompanion);
}
2022-06-12 01:26:23 -05:00
2025-03-13 17:14:45 -04:00
public int getCompanionID() {
return companionID;
}
2022-06-12 01:26:23 -05:00
2025-03-13 17:14:45 -04:00
public IPlayerDialogData getDialogData() {
return dialogData;
}
2022-06-12 01:26:23 -05:00
2025-03-13 17:14:45 -04:00
public IPlayerBankData getBankData() {
return bankData;
}
2022-06-12 01:26:23 -05:00
2025-03-13 17:14:45 -04:00
public IPlayerQuestData getQuestData() {
return questData;
}
2022-06-12 01:26:23 -05:00
2025-03-13 17:14:45 -04:00
public IPlayerTransportData getTransportData() {
return transportData;
}
2022-06-12 01:26:23 -05:00
2025-03-13 17:14:45 -04:00
public IPlayerFactionData getFactionData() {
return factionData;
}
2022-06-12 01:26:23 -05:00
2025-03-13 17:14:45 -04:00
public IPlayerItemGiverData getItemGiverData() {
return itemgiverData;
}
2022-06-12 01:26:23 -05:00
2025-03-13 17:14:45 -04:00
public IPlayerMailData getMailData() {
return mailData;
}
2022-06-12 01:26:23 -05:00
2025-03-13 17:14:45 -04:00
public synchronized void save() {
// Don't Save this is a Modification of a Profile's PlayerData
2025-02-20 21:44:15 -05:00
Profile profile = ProfileController.Instance.getProfile(UUID.fromString(uuid));
2025-03-13 17:14:45 -04:00
if (profile != null && profile.currentSlotId != this.profileSlot) {
2025-02-20 21:44:15 -05:00
ProfileController.Instance.saveOffline(profile, UUID.fromString(uuid));
return;
}
2025-03-13 17:14:45 -04:00
final NBTTagCompound compound = getNBT();
final String filename;
if (ConfigMain.DatFormat) {
filename = uuid + ".dat";
} else {
filename = uuid + ".json";
}
PlayerDataController.Instance.putPlayerMap(playername, uuid);
PlayerDataController.Instance.putPlayerDataCache(uuid, this);
CustomNPCsThreader.customNPCThread.execute(() -> {
try {
File saveDir = PlayerDataController.Instance.getSaveDir();
File file = new File(saveDir, filename + "_new");
File file1 = new File(saveDir, filename);
if (ConfigMain.DatFormat) {
CompressedStreamTools.writeCompressed(compound, new FileOutputStream(file));
} else {
NBTJsonUtil.SaveFile(file, compound);
}
if (file1.exists()) {
file1.delete();
}
file.renameTo(file1);
} catch (Exception e) {
LogWriter.except(e);
}
});
}
public void load() {
NBTTagCompound data = PlayerDataController.Instance.loadPlayerData(player.getPersistentID().toString());
if (data.hasNoTags()) {
data = PlayerDataController.Instance.loadPlayerDataOld(player.getCommandSenderName());
}
if (data.hasNoTags()) {
data = getNBT();
}
2025-03-13 17:14:45 -04:00
setNBT(data);
}
public static PlayerData get(EntityPlayer player) {
2025-03-13 17:14:45 -04:00
if (player.worldObj.isRemote)
return CustomNpcs.proxy.getPlayerData(player);
return PlayerDataController.Instance.getPlayerData(player);
}
2018-09-13 12:03:28 -04:00
}