Fixed weird player data updating in certain situations

Closes #630
This commit is contained in:
Ellpeck 2017-02-13 15:23:28 +01:00
parent 2aefe84ff2
commit d35130e525
10 changed files with 117 additions and 45 deletions

View file

@ -151,6 +151,11 @@ public class ActuallyAdditions{
} }
} }
@EventHandler
public void serverStopped(FMLServerStoppedEvent event){
WorldData.clear();
}
@EventHandler @EventHandler
public void missingMapping(FMLMissingMappingsEvent event){ public void missingMapping(FMLMissingMappingsEvent event){
int totalRemaps = 0; int totalRemaps = 0;

View file

@ -151,7 +151,7 @@ public abstract class GuiBooklet extends GuiBookletBase{
data.lastOpenBooklet = this; data.lastOpenBooklet = this;
if(change){ if(change){
PacketHandlerHelper.sendPlayerDataPacket(this.mc.player, true, false); PacketHandlerHelper.sendPlayerDataToServer(true, 0);
} }
} }

View file

@ -247,7 +247,7 @@ public class GuiMainPage extends GuiBooklet{
PlayerSave data = PlayerData.getDataFromPlayer(this.mc.player); PlayerSave data = PlayerData.getDataFromPlayer(this.mc.player);
data.didBookTutorial = true; data.didBookTutorial = true;
PacketHandlerHelper.sendPlayerDataPacket(this.mc.player, true, false); PacketHandlerHelper.sendPlayerDataToServer(true, 1);
} }
} }
else{ else{

View file

@ -71,13 +71,7 @@ public final class PlayerData{
this.batWingsFlyTime = compound.getInteger("BatWingsFlyTime"); this.batWingsFlyTime = compound.getInteger("BatWingsFlyTime");
NBTTagList bookmarks = compound.getTagList("Bookmarks", 8); NBTTagList bookmarks = compound.getTagList("Bookmarks", 8);
for(int i = 0; i < bookmarks.tagCount(); i++){ this.loadBookmarks(bookmarks);
String strg = bookmarks.getStringTagAt(i);
if(strg != null && !strg.isEmpty()){
IBookletPage page = BookletUtils.getBookletPageById(strg);
this.bookmarks[i] = page;
}
}
if(!savingToFile){ if(!savingToFile){
this.shouldDisableBatWings = compound.getBoolean("ShouldDisableWings"); this.shouldDisableBatWings = compound.getBoolean("ShouldDisableWings");
@ -91,17 +85,33 @@ public final class PlayerData{
compound.setBoolean("HasBatWings", this.hasBatWings); compound.setBoolean("HasBatWings", this.hasBatWings);
compound.setInteger("BatWingsFlyTime", this.batWingsFlyTime); compound.setInteger("BatWingsFlyTime", this.batWingsFlyTime);
NBTTagList bookmarks = new NBTTagList(); compound.setTag("Bookmarks", this.saveBookmarks());
for(IBookletPage bookmark : this.bookmarks){
bookmarks.appendTag(new NBTTagString(bookmark == null ? "" : bookmark.getIdentifier()));
}
compound.setTag("Bookmarks", bookmarks);
if(!savingToFile){ if(!savingToFile){
compound.setBoolean("ShouldDisableWings", this.shouldDisableBatWings); compound.setBoolean("ShouldDisableWings", this.shouldDisableBatWings);
} }
} }
public NBTTagList saveBookmarks(){
NBTTagList bookmarks = new NBTTagList();
for(IBookletPage bookmark : this.bookmarks){
bookmarks.appendTag(new NBTTagString(bookmark == null ? "" : bookmark.getIdentifier()));
}
return bookmarks;
}
public void loadBookmarks(NBTTagList bookmarks){
for(int i = 0; i < bookmarks.tagCount(); i++){
String strg = bookmarks.getStringTagAt(i);
if(strg != null && !strg.isEmpty()){
IBookletPage page = BookletUtils.getBookletPageById(strg);
this.bookmarks[i] = page;
}
else{
this.bookmarks[i] = null;
}
}
}
} }

View file

@ -102,6 +102,12 @@ public class WorldData extends WorldSavedData{
return data; return data;
} }
public static void clear(){
if(data != null){
data = null;
}
}
public static WorldData get(World world){ public static WorldData get(World world){
return get(world, false); return get(world, false);
} }

View file

@ -13,6 +13,7 @@ package de.ellpeck.actuallyadditions.mod.event;
import de.ellpeck.actuallyadditions.mod.blocks.IHudDisplay; import de.ellpeck.actuallyadditions.mod.blocks.IHudDisplay;
import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues; import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues;
import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues; import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues;
import de.ellpeck.actuallyadditions.mod.data.WorldData;
import de.ellpeck.actuallyadditions.mod.inventory.gui.EnergyDisplay; import de.ellpeck.actuallyadditions.mod.inventory.gui.EnergyDisplay;
import de.ellpeck.actuallyadditions.mod.items.ItemWingsOfTheBats; import de.ellpeck.actuallyadditions.mod.items.ItemWingsOfTheBats;
import de.ellpeck.actuallyadditions.mod.tile.IEnergyDisplay; import de.ellpeck.actuallyadditions.mod.tile.IEnergyDisplay;
@ -39,6 +40,8 @@ import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.ItemTooltipEvent; import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.Phase;
import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.oredict.OreDictionary; import net.minecraftforge.oredict.OreDictionary;
@ -57,6 +60,17 @@ public class ClientEvents{
MinecraftForge.EVENT_BUS.register(this); MinecraftForge.EVENT_BUS.register(this);
} }
@SubscribeEvent
public void onClientTick(ClientTickEvent event){
if(event.phase == Phase.END){
Minecraft mc = Minecraft.getMinecraft();
if(mc.world == null){
WorldData.clear();
}
}
}
@SubscribeEvent @SubscribeEvent
public void onTooltipEvent(ItemTooltipEvent event){ public void onTooltipEvent(ItemTooltipEvent event){
ItemStack stack = event.getItemStack(); ItemStack stack = event.getItemStack();

View file

@ -70,7 +70,7 @@ public class CommonEvents{
public void onLogInEvent(PlayerEvent.PlayerLoggedInEvent event){ public void onLogInEvent(PlayerEvent.PlayerLoggedInEvent event){
if(!event.player.world.isRemote && event.player instanceof EntityPlayerMP){ if(!event.player.world.isRemote && event.player instanceof EntityPlayerMP){
EntityPlayerMP player = (EntityPlayerMP)event.player; EntityPlayerMP player = (EntityPlayerMP)event.player;
PacketHandlerHelper.sendPlayerDataPacket(player, true, true); PacketHandlerHelper.syncPlayerData(player, 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()+".");
} }
} }

View file

@ -188,7 +188,7 @@ public class ItemWingsOfTheBats extends ItemBase{
} }
if(shouldSend){ if(shouldSend){
PacketHandlerHelper.sendPlayerDataPacket(player, false, true); PacketHandlerHelper.syncPlayerData(player, false);
data.shouldDisableBatWings = false; //was set only temporarily to send it data.shouldDisableBatWings = false; //was set only temporarily to send it
} }
} }

View file

@ -39,7 +39,6 @@ import net.minecraftforge.fml.relauncher.SideOnly;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID;
public final class PacketHandler{ public final class PacketHandler{
@ -137,37 +136,52 @@ public final class PacketHandler{
} }
} }
}; };
public static final IDataHandler CHANGE_PLAYER_DATA_HANDLER = new IDataHandler(){ public static final IDataHandler SYNC_PLAYER_DATA = new IDataHandler(){
@Override @Override
@SideOnly(Side.CLIENT)
public void handleData(NBTTagCompound compound, MessageContext context){ public void handleData(NBTTagCompound compound, MessageContext context){
NBTTagCompound dataTag = compound.getCompoundTag("Data"); NBTTagCompound dataTag = compound.getCompoundTag("Data");
UUID id = compound.getUniqueId("UUID"); EntityPlayer player = ActuallyAdditions.proxy.getCurrentPlayer();
EntityPlayer player = null;
if(context.side == Side.SERVER){
int dim = compound.getInteger("Dimension");
World world = DimensionManager.getWorld(dim);
if(world != null){
player = world.getPlayerEntityByUUID(id);
}
}
else{
player = ActuallyAdditions.proxy.getCurrentPlayer();
}
if(player != null){ if(player != null){
PlayerData.getDataFromPlayer(player).readFromNBT(dataTag, false); PlayerData.getDataFromPlayer(player).readFromNBT(dataTag, false);
WorldData.get(player.getEntityWorld()).markDirty();
if(compound.getBoolean("Log")){ if(compound.getBoolean("Log")){
ModUtil.LOGGER.info("Receiving (new or changed) Player Data for player "+player.getName()+" with UUID "+id+"."); ModUtil.LOGGER.info("Receiving (new or changed) Player Data for player "+player.getName()+".");
} }
} }
else{ else{
ModUtil.LOGGER.error("Tried to receive Player Data for player with UUID "+id+", but he doesn't seem to be present!"); ModUtil.LOGGER.error("Tried to receive Player Data for the current player, but he doesn't seem to be present!");
} }
} }
}; };
public static final IDataHandler PLAYER_DATA_TO_SERVER = new IDataHandler(){
@Override
public void handleData(NBTTagCompound compound, MessageContext context){
World world = DimensionManager.getWorld(compound.getInteger("World"));
EntityPlayer player = world.getPlayerEntityByUUID(compound.getUniqueId("UUID"));
if(player != null){
PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(player);
int type = compound.getInteger("Type");
if(type == 0){
data.loadBookmarks(compound.getTagList("Bookmarks", 8));
}
else if(type == 1){
data.didBookTutorial = compound.getBoolean("DidBookTutorial");
}
WorldData.get(world).markDirty();
if(compound.getBoolean("Log")){
ModUtil.LOGGER.info("Receiving changed Player Data for player "+player.getName()+".");
}
}
else{
ModUtil.LOGGER.error("Tried to receive Player Data for player "+player.getName()+", but he doesn't seem to be present!");
}
}
};
public static SimpleNetworkWrapper theNetwork; public static SimpleNetworkWrapper theNetwork;
public static void init(){ public static void init(){
@ -180,8 +194,9 @@ public final class PacketHandler{
DATA_HANDLERS.add(GUI_BUTTON_TO_TILE_HANDLER); DATA_HANDLERS.add(GUI_BUTTON_TO_TILE_HANDLER);
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(SYNC_PLAYER_DATA);
DATA_HANDLERS.add(GUI_BUTTON_TO_CONTAINER_HANDLER); DATA_HANDLERS.add(GUI_BUTTON_TO_CONTAINER_HANDLER);
DATA_HANDLERS.add(LASER_PARTICLE_HANDLER); DATA_HANDLERS.add(LASER_PARTICLE_HANDLER);
DATA_HANDLERS.add(PLAYER_DATA_TO_SERVER);
} }
} }

View file

@ -11,15 +11,19 @@
package de.ellpeck.actuallyadditions.mod.network; package de.ellpeck.actuallyadditions.mod.network;
import de.ellpeck.actuallyadditions.mod.data.PlayerData; import de.ellpeck.actuallyadditions.mod.data.PlayerData;
import de.ellpeck.actuallyadditions.mod.data.PlayerData.PlayerSave;
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;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public final class PacketHandlerHelper{ public final class PacketHandlerHelper{
@SideOnly(Side.CLIENT)
public static void sendButtonPacket(TileEntity tile, int buttonId){ public static void sendButtonPacket(TileEntity tile, int buttonId){
NBTTagCompound compound = new NBTTagCompound(); NBTTagCompound compound = new NBTTagCompound();
BlockPos pos = tile.getPos(); BlockPos pos = tile.getPos();
@ -32,26 +36,44 @@ 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 sendPlayerDataPacket(EntityPlayer player, boolean log, boolean toClient){ public static void syncPlayerData(EntityPlayer player, boolean log){
NBTTagCompound compound = new NBTTagCompound(); NBTTagCompound compound = new NBTTagCompound();
compound.setUniqueId("UUID", player.getUniqueID());
compound.setBoolean("Log", log); compound.setBoolean("Log", log);
NBTTagCompound data = new NBTTagCompound(); NBTTagCompound data = new NBTTagCompound();
PlayerData.getDataFromPlayer(player).writeToNBT(data, false); PlayerData.getDataFromPlayer(player).writeToNBT(data, false);
compound.setTag("Data", data); compound.setTag("Data", data);
if(toClient){ if(player instanceof EntityPlayerMP){
if(player instanceof EntityPlayerMP){ PacketHandler.theNetwork.sendTo(new PacketServerToClient(compound, PacketHandler.SYNC_PLAYER_DATA), (EntityPlayerMP)player);
PacketHandler.theNetwork.sendTo(new PacketServerToClient(compound, PacketHandler.CHANGE_PLAYER_DATA_HANDLER), (EntityPlayerMP)player);
}
}
else{
compound.setInteger("Dimension", player.world.provider.getDimension());
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(compound, PacketHandler.CHANGE_PLAYER_DATA_HANDLER));
} }
} }
@SideOnly(Side.CLIENT)
public static void sendPlayerDataToServer(boolean log, int type){
NBTTagCompound compound = new NBTTagCompound();
compound.setBoolean("Log", log);
compound.setInteger("Type", type);
EntityPlayer player = Minecraft.getMinecraft().player;
if(player != null){
compound.setInteger("World", player.world.provider.getDimension());
compound.setUniqueId("UUID", player.getUniqueID());
PlayerSave data = PlayerData.getDataFromPlayer(player);
if(type == 0){
compound.setTag("Bookmarks", data.saveBookmarks());
}
else if(type == 1){
compound.setBoolean("DidBookTutorial", data.didBookTutorial);
}
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(compound, PacketHandler.PLAYER_DATA_TO_SERVER));
}
}
@SideOnly(Side.CLIENT)
public static void sendNumberPacket(TileEntity tile, double number, int id){ public static void sendNumberPacket(TileEntity tile, double number, int id){
NBTTagCompound compound = new NBTTagCompound(); NBTTagCompound compound = new NBTTagCompound();
compound.setInteger("X", tile.getPos().getX()); compound.setInteger("X", tile.getPos().getX());