Make player data less hideous to use.

Closes #149
This commit is contained in:
Ellpeck 2016-07-12 22:13:50 +02:00
parent 32391142ed
commit 9bafff59d4
4 changed files with 38 additions and 19 deletions

View file

@ -14,48 +14,50 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import java.util.ArrayList; 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 final class PlayerData{
public static PlayerSave getDataFromPlayer(EntityPlayer player){ public static PlayerSave getDataFromPlayer(String name){
ArrayList<PlayerSave> data = WorldData.PLAYER_SAVE_DATA; ArrayList<PlayerSave> data = WorldData.PLAYER_SAVE_DATA;
//Get Data from existing data //Get Data from existing data
for(PlayerSave save : data){ for(PlayerSave save : data){
if(save.thePlayerUUID.equals(player.getUniqueID())){ if(save.theName != null && save.theName.equals(name)){
return save; return save;
} }
} }
//Add Data if none is existant //Add Data if none is existant
PlayerSave aSave = new PlayerSave(player.getUniqueID(), new NBTTagCompound()); PlayerSave aSave = new PlayerSave(name, new NBTTagCompound());
data.add(aSave); data.add(aSave);
return aSave; return aSave;
} }
public static PlayerSave getDataFromPlayer(EntityPlayer player){
return getDataFromPlayer(player.getName());
}
public static class PlayerSave{ public static class PlayerSave{
public final UUID thePlayerUUID; public final String theName;
public NBTTagCompound theCompound; public NBTTagCompound theCompound;
public PlayerSave(UUID theUUID, NBTTagCompound theCompound){ public PlayerSave(String name, NBTTagCompound theCompound){
this.thePlayerUUID = theUUID; this.theName = name;
this.theCompound = theCompound; this.theCompound = theCompound;
} }
public static PlayerSave fromNBT(NBTTagCompound compound){ 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"); NBTTagCompound theCompound = compound.getCompoundTag("Tag");
return new PlayerSave(theID, theCompound); return new PlayerSave(name, theCompound);
} }
public NBTTagCompound toNBT(){ public NBTTagCompound toNBT(){
NBTTagCompound compound = new NBTTagCompound(); NBTTagCompound compound = new NBTTagCompound();
compound.setLong("LeastSignificant", this.thePlayerUUID.getLeastSignificantBits()); compound.setString("Name", this.theName);
compound.setLong("MostSignificant", this.thePlayerUUID.getMostSignificantBits());
compound.setTag("Tag", this.theCompound); compound.setTag("Tag", this.theCompound);
return compound; return compound;
} }
} }

View file

@ -97,7 +97,15 @@ public class CommonEvents{
EntityPlayerMP player = (EntityPlayerMP)event.getEntity(); EntityPlayerMP player = (EntityPlayerMP)event.getEntity();
PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(player); PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(player);
if(!data.theCompound.hasNoTags()){ 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.");
} }
} }
} }

View file

@ -23,6 +23,7 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.EnumRarity; import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult; import net.minecraft.util.EnumActionResult;
@ -93,7 +94,10 @@ public class ItemLaserWrench extends ItemBase{
save.theCompound.setInteger("LaserWrenchMode", currMode); save.theCompound.setInteger("LaserWrenchMode", currMode);
if(player instanceof EntityPlayerMP){ 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+"!")); player.addChatComponentMessage(new TextComponentString("Mode changed to "+WrenchMode.values()[currMode].name+"!"));

View file

@ -98,9 +98,11 @@ public final class PacketHandler{
@Override @Override
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
public void handleData(NBTTagCompound compound){ public void handleData(NBTTagCompound compound){
EntityPlayer player = Minecraft.getMinecraft().thePlayer; NBTTagCompound data = compound.getCompoundTag("Data");
if(player != null){ String name = compound.getString("Name");
PlayerData.getDataFromPlayer(player).theCompound = compound; 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.PlayerSave playerData = PlayerData.getDataFromPlayer(player);
playerData.theCompound.merge(data); playerData.theCompound.merge(data);
if(player instanceof EntityPlayerMP){ 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);
} }
} }
} }