PlayerData:

-Renamed loadPlayerDataFromFile() to load(), and moved it next to the save() function for later addition to the API.
-Moved saving/loading to EP functions where they used to be.
-Removed now unused PlayerData.get() and loadPlayerDataOld() functions

ServerEventsHandler:
-Added new EntityConstructing event to add the PlayerData EP earlier.

NoppesUtilPlayer:
-questCompletion() now saves player data right before its return statement.
This commit is contained in:
Luis C.M committed 2023-05-21 02:48:30 -05:00
1 parent dc42beee15
commit cbf99be297
5 files changed
+30 -84

No files matched your search

@@ -441,6 +441,8 @@ public class NoppesUtilPlayer {
questData.trackQuest(nextQuest.quest);
}
playerData.save();
return true;
}
@@ -1,6 +1,7 @@
package noppes.npcs;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.EventPriority;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.relauncher.Side;
import net.minecraft.block.Block;
@@ -20,6 +21,7 @@ import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.MathHelper;
import net.minecraft.util.StatCollector;
import net.minecraft.village.MerchantRecipeList;
import net.minecraftforge.event.entity.EntityEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
@@ -253,7 +255,7 @@ public class ServerEventsHandler {
doExcalibur((EntityPlayer) event.source.getEntity(),event.entityLiving);
}
if(event.source.getEntity() instanceof EntityNPCInterface && event.entityLiving != null){
if(event.source.getEntity() instanceof EntityNPCInterface){
EntityNPCInterface npc = (EntityNPCInterface) event.source.getEntity();
Line line = npc.advanced.getKillLine();
if(line != null)
@@ -358,6 +360,15 @@ public class ServerEventsHandler {
questData.checkQuestCompletion(playerData, EnumQuestType.Item);
}
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void onEntityConstructing(EntityEvent.EntityConstructing event) {
if (event.entity instanceof EntityPlayerMP) {
PlayerData data = new PlayerData();
data.player = (EntityPlayer) event.entity;
event.entity.registerExtendedProperties("CustomNpcsData", data);
}
}
@SubscribeEvent
public void world(EntityJoinWorldEvent event){
if(event.world.isRemote || !(event.entity instanceof EntityPlayer))
@@ -366,12 +377,6 @@ public class ServerEventsHandler {
data.updateCompanion(event.world);
}
@SubscribeEvent
public void world(PlayerEvent.SaveToFile event){
PlayerData data = PlayerDataController.instance.getPlayerData((EntityPlayer) event.entity);
data.save();
}
@SubscribeEvent
public void populateChunk(PopulateChunkEvent.Post event){
NPCSpawning.performWorldGenSpawning(event.world, event.chunkX, event.chunkZ, event.rand);
@@ -12,7 +12,6 @@ import net.minecraft.util.ChatComponentText;
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import noppes.npcs.controllers.data.*;
import noppes.npcs.util.CustomNPCsThreader;
import noppes.npcs.util.NBTJsonUtil;
import java.io.*;
@@ -272,7 +271,7 @@ public class PlayerDataController {
if(data == null){
player.registerExtendedProperties("CustomNpcsData", data = new PlayerData());
data.player = player;
data.loadPlayerDataFromFile();
data.load();
}
data.player = player;
return data;
@@ -5,6 +5,7 @@ import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.NBTTags;
import noppes.npcs.api.entity.ICustomNpc;
import noppes.npcs.api.entity.IPlayer;
import noppes.npcs.api.handler.IPlayerFactionData;
import noppes.npcs.api.handler.data.IFaction;
import noppes.npcs.controllers.FactionController;
import noppes.npcs.controllers.PlayerDataController;
@@ -133,8 +134,8 @@ public class Faction implements IFaction {
}
public int playerStatus(IPlayer player) {
PlayerFactionData data = PlayerData.get((EntityPlayer) player.getMCEntity()).factionData;
int points = data.getFactionPoints(this.id);
IPlayerFactionData data = player.getData().getFactionData();
int points = data.getPoints(this.id);
if (points >= this.friendlyPoints) {
return 1;
} else {
@@ -2,7 +2,6 @@ package noppes.npcs.controllers.data;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.common.IExtendedEntityProperties;
@@ -23,7 +22,6 @@ import noppes.npcs.util.CustomNPCsThreader;
import noppes.npcs.util.NBTJsonUtil;
import java.io.File;
import java.io.FileInputStream;
public class PlayerData implements IExtendedEntityProperties, IPlayerData {
public PlayerDialogData dialogData = new PlayerDialogData(this);
@@ -53,20 +51,12 @@ public class PlayerData implements IExtendedEntityProperties, IPlayerData {
@Override
public void saveNBTData(NBTTagCompound nbtTagCompound) {
//Do Nothing
this.save();
}
@Override
public void loadNBTData(NBTTagCompound compound) {
//Do Nothing
}
public void loadPlayerDataFromFile() {
NBTTagCompound data = PlayerDataController.instance.loadPlayerData(player.getPersistentID().toString());
if(data.hasNoTags()){
data = PlayerDataController.instance.loadPlayerDataOld(player.getCommandSenderName());
}
setNBT(data);
this.load();
}
public void setNBT(NBTTagCompound data){
@@ -138,13 +128,11 @@ public class PlayerData implements IExtendedEntityProperties, IPlayerData {
}
public void setGUIOpen(boolean bool) {
isGUIOpen = bool;
save();
this.isGUIOpen = bool;
}
public boolean getGUIOpen() {
loadPlayerDataFromFile();
return isGUIOpen;
return this.isGUIOpen;
}
public boolean hasCompanion(){
@@ -180,44 +168,6 @@ public class PlayerData implements IExtendedEntityProperties, IPlayerData {
world.spawnEntityInWorld(npc);
}
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;
@@ -240,25 +190,6 @@ public class PlayerData implements IExtendedEntityProperties, IPlayerData {
return new NBTTagCompound();
}
public static PlayerData get(EntityPlayer player) {
if(player.worldObj.isRemote) {
return CustomNpcs.proxy.getPlayerData(player);
} else {
PlayerData data = new PlayerData();
if (data.player == null) {
data.player = player;
NBTTagCompound compound = loadPlayerData(player.getPersistentID().toString());
if (compound.hasNoTags()) {
compound = loadPlayerDataOld(player.getCommandSenderName());
}
data.setNBT(compound);
}
return data;
}
}
public void setCompanion(ICustomNpc npc) {
this.setCompanion((EntityNPCInterface) npc.getMCEntity());
}
@@ -318,4 +249,12 @@ public class PlayerData implements IExtendedEntityProperties, IPlayerData {
});
PlayerDataController.instance.putPlayerMap(playername, uuid);
}
public void load() {
NBTTagCompound data = PlayerDataController.instance.loadPlayerData(player.getPersistentID().toString());
if(data.hasNoTags()){
data = PlayerDataController.instance.loadPlayerDataOld(player.getCommandSenderName());
}
setNBT(data);
}
}