Changed player data to use variables instead of NBT

This commit is contained in:
Ellpeck 2016-11-11 21:41:07 +01:00
parent c85a3c72a1
commit 5507e2ce60
7 changed files with 65 additions and 101 deletions

View file

@ -22,13 +22,13 @@ public final class PlayerData{
List<PlayerSave> data = WorldData.getWorldUnspecificData().playerSaveData; List<PlayerSave> data = WorldData.getWorldUnspecificData().playerSaveData;
//Get Data from existing data //Get Data from existing data
for(PlayerSave save : data){ for(PlayerSave save : data){
if(save.theId != null && save.theId.equals(id)){ if(save.id != null && save.id.equals(id)){
return save; return save;
} }
} }
//Add Data if none is existant //Add Data if none is existant
PlayerSave aSave = new PlayerSave(id, new NBTTagCompound()); PlayerSave aSave = new PlayerSave(id);
data.add(aSave); data.add(aSave);
return aSave; return aSave;
} }
@ -39,29 +39,25 @@ public final class PlayerData{
public static class PlayerSave{ public static class PlayerSave{
public final UUID theId; public UUID id;
public NBTTagCompound theCompound;
public PlayerSave(UUID theId, NBTTagCompound theCompound){ public boolean displayTesla;
this.theId = theId; public boolean bookGottenAlready;
this.theCompound = theCompound;
public PlayerSave(UUID id){
this.id = id;
} }
public static PlayerSave fromNBT(NBTTagCompound compound){ public void readFromNBT(NBTTagCompound compound){
UUID theID = new UUID(compound.getLong("MostSignificant"), compound.getLong("LeastSignificant")); this.displayTesla = compound.getBoolean("DisplayTesla");
NBTTagCompound theCompound = compound.getCompoundTag("Tag"); this.bookGottenAlready = compound.getBoolean("BookGotten");
return new PlayerSave(theID, theCompound);
} }
public NBTTagCompound toNBT(){ public void writeToNBT(NBTTagCompound compound){
NBTTagCompound compound = new NBTTagCompound(); compound.setBoolean("DisplayTesla", this.displayTesla);
compound.setLong("LeastSignificant", this.theId.getLeastSignificantBits()); compound.setBoolean("BookGotten", this.bookGottenAlready);
compound.setLong("MostSignificant", this.theId.getMostSignificantBits());
compound.setTag("Tag", this.theCompound);
return compound;
} }
} }

View file

@ -28,6 +28,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
public class WorldData{ public class WorldData{
@ -141,8 +142,14 @@ public class WorldData{
this.playerSaveData.clear(); this.playerSaveData.clear();
NBTTagList playerList = compound.getTagList("PlayerData", 10); NBTTagList playerList = compound.getTagList("PlayerData", 10);
for(int i = 0; i < playerList.tagCount(); i++){ for(int i = 0; i < playerList.tagCount(); i++){
PlayerSave aSave = PlayerSave.fromNBT(playerList.getCompoundTagAt(i)); NBTTagCompound player = playerList.getCompoundTagAt(i);
this.playerSaveData.add(aSave);
UUID id = player.getUniqueId("UUID");
NBTTagCompound data = player.getCompoundTag("Data");
PlayerSave save = new PlayerSave(id);
save.readFromNBT(data);
this.playerSaveData.add(save);
} }
} }
@ -156,8 +163,15 @@ public class WorldData{
//Player Data //Player Data
NBTTagList playerList = new NBTTagList(); NBTTagList playerList = new NBTTagList();
for(PlayerSave theSave : this.playerSaveData){ for(PlayerSave save : this.playerSaveData){
playerList.appendTag(theSave.toNBT()); NBTTagCompound player = new NBTTagCompound();
player.setUniqueId("UUID", save.id);
NBTTagCompound data = new NBTTagCompound();
save.writeToNBT(data);
player.setTag("Data", data);
playerList.appendTag(player);
} }
compound.setTag("PlayerData", playerList); compound.setTag("PlayerData", playerList);
} }

View file

@ -26,10 +26,7 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks; import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingDropsEvent; import net.minecraftforge.event.entity.living.LivingDropsEvent;
import net.minecraftforge.event.world.WorldEvent; import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
@ -56,29 +53,6 @@ public class CommonEvents{
} }
} }
@SubscribeEvent
public void livingDeathEvent(LivingDeathEvent event){
if(event.getEntityLiving().worldObj != null && !event.getEntityLiving().worldObj.isRemote && event.getEntityLiving() instanceof EntityPlayer){
EntityPlayer player = (EntityPlayer)event.getEntityLiving();
PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(player);
NBTTagList deaths = data.theCompound.getTagList("Deaths", 10);
while(deaths.tagCount() >= 5){
deaths.removeTag(0);
}
NBTTagCompound death = new NBTTagCompound();
death.setDouble("X", player.posX);
death.setDouble("Y", player.posY);
death.setDouble("Z", player.posZ);
deaths.appendTag(death);
data.theCompound.setTag("Deaths", deaths);
//player.addChatComponentMessage(new TextComponentTranslation("info."+ModUtil.MOD_ID+".deathRecorded"));
}
}
@SubscribeEvent @SubscribeEvent
public void onEntityDropEvent(LivingDropsEvent event){ public void onEntityDropEvent(LivingDropsEvent event){
if(event.getEntityLiving().worldObj != null && !event.getEntityLiving().worldObj.isRemote && event.getSource().getEntity() instanceof EntityPlayer){ if(event.getEntityLiving().worldObj != null && !event.getEntityLiving().worldObj.isRemote && event.getSource().getEntity() instanceof EntityPlayer){
@ -95,15 +69,9 @@ public class CommonEvents{
public void onLogInEvent(PlayerEvent.PlayerLoggedInEvent event){ public void onLogInEvent(PlayerEvent.PlayerLoggedInEvent event){
if(!event.player.worldObj.isRemote && event.player instanceof EntityPlayerMP){ if(!event.player.worldObj.isRemote && event.player instanceof EntityPlayerMP){
EntityPlayerMP player = (EntityPlayerMP)event.player; EntityPlayerMP player = (EntityPlayerMP)event.player;
PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(player); PacketHandlerHelper.sendPlayerDataPacket(player, true, true);
if(!data.theCompound.hasNoTags()){
PacketHandlerHelper.sendPlayerDataToClientPacket(player, data.theCompound, true);
ModUtil.LOGGER.info("Sending Player Data to player "+player.getName()+" with UUID "+player.getUniqueID()+"."); ModUtil.LOGGER.info("Sending Player Data to player "+player.getName()+" with UUID "+player.getUniqueID()+".");
} }
else{
ModUtil.LOGGER.info("Not sending Player Data to player "+player.getName()+" with UUID "+player.getUniqueID()+" because he doesn't have any.");
}
}
} }
@SubscribeEvent @SubscribeEvent
@ -115,9 +83,9 @@ public class CommonEvents{
String name = event.crafting.getItem().getRegistryName().toString(); String name = event.crafting.getItem().getRegistryName().toString();
if(name != null && name.toLowerCase(Locale.ROOT).contains(ModUtil.MOD_ID)){ if(name != null && name.toLowerCase(Locale.ROOT).contains(ModUtil.MOD_ID)){
PlayerData.PlayerSave compound = PlayerData.getDataFromPlayer(event.player); PlayerData.PlayerSave save = PlayerData.getDataFromPlayer(event.player);
if(compound != null && !compound.theCompound.getBoolean("BookGottenAlready")){ if(save != null && !save.bookGottenAlready){
compound.theCompound.setBoolean("BookGottenAlready", true); save.bookGottenAlready = true;
EntityItem entityItem = new EntityItem(event.player.worldObj, event.player.posX, event.player.posY, event.player.posZ, new ItemStack(InitItems.itemBooklet)); EntityItem entityItem = new EntityItem(event.player.worldObj, event.player.posX, event.player.posY, event.player.posZ, new ItemStack(InitItems.itemBooklet));
entityItem.setPickupDelay(0); entityItem.setPickupDelay(0);

View file

@ -18,7 +18,7 @@ import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import de.ellpeck.actuallyadditions.mod.util.StringUtil; import de.ellpeck.actuallyadditions.mod.util.StringUtil;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui; import net.minecraft.client.gui.Gui;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.text.TextFormatting; import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.fml.client.config.GuiUtils; import net.minecraftforge.fml.client.config.GuiUtils;
@ -50,7 +50,7 @@ public class EnergyDisplay extends Gui{
this.outline = outline; this.outline = outline;
this.drawTextNextTo = drawTextNextTo; this.drawTextNextTo = drawTextNextTo;
this.displayTesla = PlayerData.getDataFromPlayer(Minecraft.getMinecraft().thePlayer).theCompound.getBoolean("DisplayTesla"); this.displayTesla = PlayerData.getDataFromPlayer(Minecraft.getMinecraft().thePlayer).displayTesla;
} }
public void draw(){ public void draw(){
@ -108,11 +108,10 @@ public class EnergyDisplay extends Gui{
} }
private void changeDisplayMode(){ private void changeDisplayMode(){
NBTTagCompound data = new NBTTagCompound();
this.displayTesla = !this.displayTesla; this.displayTesla = !this.displayTesla;
data.setBoolean("DisplayTesla", this.displayTesla);
PacketHandlerHelper.sendChangePlayerDataPacket(data); EntityPlayer player = Minecraft.getMinecraft().thePlayer;
PlayerData.getDataFromPlayer(player).displayTesla = this.displayTesla;
PacketHandlerHelper.sendPlayerDataPacket(player, true, false);
} }
} }

View file

@ -66,7 +66,7 @@ public abstract class ItemEnergy extends ItemEnergyContainer{
@Override @Override
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean bool){ public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean bool){
NumberFormat format = NumberFormat.getInstance(); NumberFormat format = NumberFormat.getInstance();
boolean tesla = PlayerData.getDataFromPlayer(player).theCompound.getBoolean("DisplayTesla"); boolean tesla = PlayerData.getDataFromPlayer(player).displayTesla;
list.add(format.format(this.getEnergyStored(stack))+"/"+format.format(this.getMaxEnergyStored(stack))+(tesla ? " T" : " RF")); list.add(format.format(this.getEnergyStored(stack))+"/"+format.format(this.getMaxEnergyStored(stack))+(tesla ? " T" : " RF"));
} }

View file

@ -38,7 +38,10 @@ import java.util.UUID;
public final class PacketHandler{ public final class PacketHandler{
public static SimpleNetworkWrapper theNetwork;
public static final List<IDataHandler> DATA_HANDLERS = new ArrayList<IDataHandler>(); public static final List<IDataHandler> DATA_HANDLERS = new ArrayList<IDataHandler>();
public static final IDataHandler PARTICLE_HANDLER = new IDataHandler(){ public static final IDataHandler PARTICLE_HANDLER = new IDataHandler(){
@Override @Override
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
@ -111,35 +114,19 @@ public final class PacketHandler{
} }
} }
}; };
public static final IDataHandler PLAYER_DATA_TO_CLIENT_HANDLER = new IDataHandler(){ public static final IDataHandler CHANGE_PLAYER_DATA_HANDLER = new IDataHandler(){
@Override @Override
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
public void handleData(NBTTagCompound compound){ public void handleData(NBTTagCompound compound){
NBTTagCompound data = compound.getCompoundTag("Data"); NBTTagCompound data = compound.getCompoundTag("Data");
UUID id = compound.getUniqueId("UUID"); UUID id = compound.getUniqueId("UUID");
PlayerData.getDataFromPlayer(id).theCompound = data; PlayerData.getDataFromPlayer(id).readFromNBT(data);
if(compound.getBoolean("Log")){ if(compound.getBoolean("Log")){
ModUtil.LOGGER.info("Receiving Player Data for current player with UUID "+id+"."); ModUtil.LOGGER.info("Receiving (new or changed) Player Data for current player with UUID "+id+".");
} }
} }
}; };
public static SimpleNetworkWrapper theNetwork;
public static final IDataHandler CHANGE_PLAYER_DATA_HANDLER = new IDataHandler(){
@Override
public void handleData(NBTTagCompound compound){
NBTTagCompound data = compound.getCompoundTag("Data");
World world = DimensionManager.getWorld(compound.getInteger("WorldID"));
EntityPlayer player = (EntityPlayer)world.getEntityByID(compound.getInteger("PlayerID"));
if(player != null){
PlayerData.PlayerSave playerData = PlayerData.getDataFromPlayer(player);
playerData.theCompound.merge(data);
if(player instanceof EntityPlayerMP){
PacketHandlerHelper.sendPlayerDataToClientPacket(player, playerData.theCompound, false);
}
}
}
};
public static void init(){ public static void init(){
theNetwork = NetworkRegistry.INSTANCE.newSimpleChannel(ModUtil.MOD_ID); theNetwork = NetworkRegistry.INSTANCE.newSimpleChannel(ModUtil.MOD_ID);
@ -152,7 +139,6 @@ public final class PacketHandler{
DATA_HANDLERS.add(GUI_STRING_TO_TILE_HANDLER); DATA_HANDLERS.add(GUI_STRING_TO_TILE_HANDLER);
DATA_HANDLERS.add(GUI_NUMBER_TO_TILE_HANDLER); DATA_HANDLERS.add(GUI_NUMBER_TO_TILE_HANDLER);
DATA_HANDLERS.add(CHANGE_PLAYER_DATA_HANDLER); DATA_HANDLERS.add(CHANGE_PLAYER_DATA_HANDLER);
DATA_HANDLERS.add(PLAYER_DATA_TO_CLIENT_HANDLER);
DATA_HANDLERS.add(GUI_BUTTON_TO_CONTAINER_HANDLER); DATA_HANDLERS.add(GUI_BUTTON_TO_CONTAINER_HANDLER);
} }
} }

View file

@ -10,6 +10,7 @@
package de.ellpeck.actuallyadditions.mod.network; package de.ellpeck.actuallyadditions.mod.network;
import de.ellpeck.actuallyadditions.mod.data.PlayerData;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.EntityPlayerMP;
@ -31,22 +32,22 @@ public final class PacketHandlerHelper{
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(compound, PacketHandler.GUI_BUTTON_TO_TILE_HANDLER)); PacketHandler.theNetwork.sendToServer(new PacketClientToServer(compound, PacketHandler.GUI_BUTTON_TO_TILE_HANDLER));
} }
public static void sendChangePlayerDataPacket(NBTTagCompound data){ public static void sendPlayerDataPacket(EntityPlayer player, boolean log, boolean toClient){
NBTTagCompound compound = new NBTTagCompound();
compound.setTag("Data", data);
Minecraft mc = Minecraft.getMinecraft();
compound.setInteger("WorldID", mc.theWorld.provider.getDimension());
compound.setInteger("PlayerID", mc.thePlayer.getEntityId());
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(compound, PacketHandler.CHANGE_PLAYER_DATA_HANDLER));
}
public static void sendPlayerDataToClientPacket(EntityPlayer player, NBTTagCompound data, boolean log){
if(player instanceof EntityPlayerMP){
NBTTagCompound compound = new NBTTagCompound(); NBTTagCompound compound = new NBTTagCompound();
compound.setUniqueId("UUID", player.getUniqueID()); compound.setUniqueId("UUID", player.getUniqueID());
compound.setTag("Data", data);
compound.setBoolean("Log", log); compound.setBoolean("Log", log);
PacketHandler.theNetwork.sendTo(new PacketServerToClient(compound, PacketHandler.PLAYER_DATA_TO_CLIENT_HANDLER), (EntityPlayerMP)player);
NBTTagCompound data = new NBTTagCompound();
PlayerData.getDataFromPlayer(player).writeToNBT(data);
compound.setTag("Data", data);
if(toClient){
if(player instanceof EntityPlayerMP){
PacketHandler.theNetwork.sendTo(new PacketServerToClient(compound, PacketHandler.CHANGE_PLAYER_DATA_HANDLER), (EntityPlayerMP)player);
}
}
else{
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(compound, PacketHandler.CHANGE_PLAYER_DATA_HANDLER));
} }
} }
} }