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

View file

@ -28,6 +28,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
public class WorldData{
@ -141,8 +142,14 @@ public class WorldData{
this.playerSaveData.clear();
NBTTagList playerList = compound.getTagList("PlayerData", 10);
for(int i = 0; i < playerList.tagCount(); i++){
PlayerSave aSave = PlayerSave.fromNBT(playerList.getCompoundTagAt(i));
this.playerSaveData.add(aSave);
NBTTagCompound player = playerList.getCompoundTagAt(i);
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
NBTTagList playerList = new NBTTagList();
for(PlayerSave theSave : this.playerSaveData){
playerList.appendTag(theSave.toNBT());
for(PlayerSave save : this.playerSaveData){
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);
}

View file

@ -26,10 +26,7 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
import net.minecraftforge.event.world.WorldEvent;
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
public void onEntityDropEvent(LivingDropsEvent event){
if(event.getEntityLiving().worldObj != null && !event.getEntityLiving().worldObj.isRemote && event.getSource().getEntity() instanceof EntityPlayer){
@ -95,14 +69,8 @@ public class CommonEvents{
public void onLogInEvent(PlayerEvent.PlayerLoggedInEvent event){
if(!event.player.worldObj.isRemote && event.player instanceof EntityPlayerMP){
EntityPlayerMP player = (EntityPlayerMP)event.player;
PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(player);
if(!data.theCompound.hasNoTags()){
PacketHandlerHelper.sendPlayerDataToClientPacket(player, data.theCompound, true);
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.");
}
PacketHandlerHelper.sendPlayerDataPacket(player, true, true);
ModUtil.LOGGER.info("Sending Player Data to player "+player.getName()+" with UUID "+player.getUniqueID()+".");
}
}
@ -115,9 +83,9 @@ public class CommonEvents{
String name = event.crafting.getItem().getRegistryName().toString();
if(name != null && name.toLowerCase(Locale.ROOT).contains(ModUtil.MOD_ID)){
PlayerData.PlayerSave compound = PlayerData.getDataFromPlayer(event.player);
if(compound != null && !compound.theCompound.getBoolean("BookGottenAlready")){
compound.theCompound.setBoolean("BookGottenAlready", true);
PlayerData.PlayerSave save = PlayerData.getDataFromPlayer(event.player);
if(save != null && !save.bookGottenAlready){
save.bookGottenAlready = true;
EntityItem entityItem = new EntityItem(event.player.worldObj, event.player.posX, event.player.posY, event.player.posZ, new ItemStack(InitItems.itemBooklet));
entityItem.setPickupDelay(0);

View file

@ -18,7 +18,7 @@ import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
import net.minecraft.client.Minecraft;
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.minecraftforge.fml.client.config.GuiUtils;
@ -50,7 +50,7 @@ public class EnergyDisplay extends Gui{
this.outline = outline;
this.drawTextNextTo = drawTextNextTo;
this.displayTesla = PlayerData.getDataFromPlayer(Minecraft.getMinecraft().thePlayer).theCompound.getBoolean("DisplayTesla");
this.displayTesla = PlayerData.getDataFromPlayer(Minecraft.getMinecraft().thePlayer).displayTesla;
}
public void draw(){
@ -108,11 +108,10 @@ public class EnergyDisplay extends Gui{
}
private void changeDisplayMode(){
NBTTagCompound data = new NBTTagCompound();
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
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean bool){
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"));
}

View file

@ -38,7 +38,10 @@ import java.util.UUID;
public final class PacketHandler{
public static SimpleNetworkWrapper theNetwork;
public static final List<IDataHandler> DATA_HANDLERS = new ArrayList<IDataHandler>();
public static final IDataHandler PARTICLE_HANDLER = new IDataHandler(){
@Override
@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
@SideOnly(Side.CLIENT)
public void handleData(NBTTagCompound compound){
NBTTagCompound data = compound.getCompoundTag("Data");
UUID id = compound.getUniqueId("UUID");
PlayerData.getDataFromPlayer(id).theCompound = data;
PlayerData.getDataFromPlayer(id).readFromNBT(data);
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(){
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_NUMBER_TO_TILE_HANDLER);
DATA_HANDLERS.add(CHANGE_PLAYER_DATA_HANDLER);
DATA_HANDLERS.add(PLAYER_DATA_TO_CLIENT_HANDLER);
DATA_HANDLERS.add(GUI_BUTTON_TO_CONTAINER_HANDLER);
}
}

View file

@ -10,6 +10,7 @@
package de.ellpeck.actuallyadditions.mod.network;
import de.ellpeck.actuallyadditions.mod.data.PlayerData;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
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));
}
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));
}
compound.setUniqueId("UUID", player.getUniqueID());
compound.setBoolean("Log", log);
public static void sendPlayerDataToClientPacket(EntityPlayer player, NBTTagCompound data, boolean log){
if(player instanceof EntityPlayerMP){
NBTTagCompound compound = new NBTTagCompound();
compound.setUniqueId("UUID", player.getUniqueID());
compound.setTag("Data", data);
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));
}
}
}