From 9bafff59d4617b970b9dd3b67efc9fdd80f4a586 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 12 Jul 2016 22:13:50 +0200 Subject: [PATCH] Make player data less hideous to use. Closes #149 --- .../mod/data/PlayerData.java | 28 ++++++++++--------- .../mod/event/CommonEvents.java | 10 ++++++- .../mod/items/ItemLaserWrench.java | 6 +++- .../mod/network/PacketHandler.java | 13 ++++++--- 4 files changed, 38 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/data/PlayerData.java b/src/main/java/de/ellpeck/actuallyadditions/mod/data/PlayerData.java index 2c4f5b8d7..036938e65 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/data/PlayerData.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/data/PlayerData.java @@ -14,48 +14,50 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; import java.util.ArrayList; -import java.util.UUID; +//Yes, this is name based instead of UUID-based. +//It just works better this way because of vanilla quirks. Don't judge me. public final class PlayerData{ - public static PlayerSave getDataFromPlayer(EntityPlayer player){ + public static PlayerSave getDataFromPlayer(String name){ ArrayList data = WorldData.PLAYER_SAVE_DATA; //Get Data from existing data for(PlayerSave save : data){ - if(save.thePlayerUUID.equals(player.getUniqueID())){ + if(save.theName != null && save.theName.equals(name)){ return save; } } //Add Data if none is existant - PlayerSave aSave = new PlayerSave(player.getUniqueID(), new NBTTagCompound()); + PlayerSave aSave = new PlayerSave(name, new NBTTagCompound()); data.add(aSave); return aSave; } + public static PlayerSave getDataFromPlayer(EntityPlayer player){ + return getDataFromPlayer(player.getName()); + } + public static class PlayerSave{ - public final UUID thePlayerUUID; + public final String theName; public NBTTagCompound theCompound; - public PlayerSave(UUID theUUID, NBTTagCompound theCompound){ - this.thePlayerUUID = theUUID; + public PlayerSave(String name, NBTTagCompound theCompound){ + this.theName = name; this.theCompound = theCompound; } public static PlayerSave fromNBT(NBTTagCompound compound){ - UUID theID = new UUID(compound.getLong("MostSignificant"), compound.getLong("LeastSignificant")); + String name = compound.getString("Name"); NBTTagCompound theCompound = compound.getCompoundTag("Tag"); - return new PlayerSave(theID, theCompound); + return new PlayerSave(name, theCompound); } public NBTTagCompound toNBT(){ NBTTagCompound compound = new NBTTagCompound(); - compound.setLong("LeastSignificant", this.thePlayerUUID.getLeastSignificantBits()); - compound.setLong("MostSignificant", this.thePlayerUUID.getMostSignificantBits()); - + compound.setString("Name", this.theName); compound.setTag("Tag", this.theCompound); - return compound; } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/event/CommonEvents.java b/src/main/java/de/ellpeck/actuallyadditions/mod/event/CommonEvents.java index e9e7f431b..d053430b2 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/event/CommonEvents.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/event/CommonEvents.java @@ -97,7 +97,15 @@ public class CommonEvents{ EntityPlayerMP player = (EntityPlayerMP)event.getEntity(); PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(player); if(!data.theCompound.hasNoTags()){ - PacketHandler.theNetwork.sendTo(new PacketServerToClient(data.theCompound, PacketHandler.PLAYER_DATA_TO_CLIENT_HANDLER), player); + NBTTagCompound compound = new NBTTagCompound(); + compound.setString("Name", player.getName()); + compound.setTag("Data", data.theCompound); + compound.setBoolean("Log", true); + PacketHandler.theNetwork.sendTo(new PacketServerToClient(compound, PacketHandler.PLAYER_DATA_TO_CLIENT_HANDLER), player); + ModUtil.LOGGER.info("Sending Player Data to player "+player.getName()+" with info "+data.theCompound+"."); + } + else{ + ModUtil.LOGGER.info("Not sending Player Data to player "+player.getName()+" because he doesn't have any."); } } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemLaserWrench.java b/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemLaserWrench.java index a2318f1d7..34d70f351 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemLaserWrench.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemLaserWrench.java @@ -23,6 +23,7 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.EnumRarity; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ActionResult; import net.minecraft.util.EnumActionResult; @@ -93,7 +94,10 @@ public class ItemLaserWrench extends ItemBase{ save.theCompound.setInteger("LaserWrenchMode", currMode); if(player instanceof EntityPlayerMP){ - PacketHandler.theNetwork.sendTo(new PacketServerToClient(save.theCompound, PacketHandler.PLAYER_DATA_TO_CLIENT_HANDLER), (EntityPlayerMP)player); + NBTTagCompound compound = new NBTTagCompound(); + compound.setString("Name", player.getName()); + compound.setTag("Data", save.theCompound); + PacketHandler.theNetwork.sendTo(new PacketServerToClient(compound, PacketHandler.PLAYER_DATA_TO_CLIENT_HANDLER), (EntityPlayerMP)player); } player.addChatComponentMessage(new TextComponentString("Mode changed to "+WrenchMode.values()[currMode].name+"!")); diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/network/PacketHandler.java b/src/main/java/de/ellpeck/actuallyadditions/mod/network/PacketHandler.java index 360c5db25..7501078da 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/network/PacketHandler.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/network/PacketHandler.java @@ -98,9 +98,11 @@ public final class PacketHandler{ @Override @SideOnly(Side.CLIENT) public void handleData(NBTTagCompound compound){ - EntityPlayer player = Minecraft.getMinecraft().thePlayer; - if(player != null){ - PlayerData.getDataFromPlayer(player).theCompound = compound; + NBTTagCompound data = compound.getCompoundTag("Data"); + String name = compound.getString("Name"); + PlayerData.getDataFromPlayer(name).theCompound = data; + if(compound.getBoolean("Log")){ + ModUtil.LOGGER.info("Receiving Player Data for player "+name+" with info "+data+"."); } } }; @@ -133,7 +135,10 @@ public final class PacketHandler{ PlayerData.PlayerSave playerData = PlayerData.getDataFromPlayer(player); playerData.theCompound.merge(data); if(player instanceof EntityPlayerMP){ - PacketHandler.theNetwork.sendTo(new PacketServerToClient(playerData.theCompound, PLAYER_DATA_TO_CLIENT_HANDLER), (EntityPlayerMP)player); + NBTTagCompound tag = new NBTTagCompound(); + tag.setString("Name", player.getName()); + tag.setTag("Data", playerData.theCompound); + PacketHandler.theNetwork.sendTo(new PacketServerToClient(tag, PLAYER_DATA_TO_CLIENT_HANDLER), (EntityPlayerMP)player); } } }