Moved server-side Player Data to WorldSaveData because ExtendedEntityProperties doesn't work properly

This commit is contained in:
Ellpeck 2015-10-22 22:18:33 +02:00
parent 2f0b73930e
commit 4d082485e0
6 changed files with 63 additions and 70 deletions

View file

@ -100,14 +100,9 @@ public class InitBlocks{
public static Block blockLaserRelay; public static Block blockLaserRelay;
//TODO: Plan for Laser Power Transmitters: //TODO: Plan for Laser Power Transmitters:
//TODO: Connectable with Laser Wrench //TODO: When there is a block in the way, they don't render their laser and don't transmit
//TODO: Generate a Network that is (maybe) stored via WorldSavedData //TODO: They stay connected and can be connected together even though they're blocked
//TODO: Whenever a Transmitter is fed with energy, it tries to find a connector that is connected to a block that needs energy //TODO: If blocked, they display the block coords blocking them on right-click
//TODO: The power then gets transferred through a Laser Entity that will find its way past all of the transmitters on its way to the block in need
//TODO: Or maybe, instead, if I can't be bothered, the points will be connected through shiny lines that light up or flash when power is transmitted
//TODO: Save the Connections via an Array of "ConnectionPairs" which stores two connected lasers to make calculating the laser entity easier
//TODO: -> To find every laser one is connected to, just go through the array, find the laser itself and get the second part of the pair
//TODO: Notify Client of Connections through NBT -> Every laser adds itself to the connection list on the client
public static void init(){ public static void init(){
ModUtil.LOGGER.info("Initializing Blocks..."); ModUtil.LOGGER.info("Initializing Blocks...");

View file

@ -16,12 +16,14 @@ import ellpeck.actuallyadditions.achievement.InitAchievements;
import ellpeck.actuallyadditions.achievement.TheAchievements; import ellpeck.actuallyadditions.achievement.TheAchievements;
import ellpeck.actuallyadditions.config.values.ConfigBoolValues; import ellpeck.actuallyadditions.config.values.ConfigBoolValues;
import ellpeck.actuallyadditions.items.InitItems; import ellpeck.actuallyadditions.items.InitItems;
import ellpeck.actuallyadditions.misc.WorldData;
import ellpeck.actuallyadditions.util.IActAddItemOrBlock; import ellpeck.actuallyadditions.util.IActAddItemOrBlock;
import ellpeck.actuallyadditions.util.playerdata.PersistentServerData; import ellpeck.actuallyadditions.util.playerdata.PersistentServerData;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
public class CraftEvent{ public class CraftEvent{
@ -31,9 +33,10 @@ public class CraftEvent{
if(ConfigBoolValues.GIVE_BOOKLET_ON_FIRST_CRAFT.isEnabled()){ if(ConfigBoolValues.GIVE_BOOKLET_ON_FIRST_CRAFT.isEnabled()){
if(!event.player.worldObj.isRemote && event.crafting.getItem() != InitItems.itemLexicon && (event.crafting.getItem() instanceof IActAddItemOrBlock || Block.getBlockFromItem(event.crafting.getItem()) instanceof IActAddItemOrBlock)){ if(!event.player.worldObj.isRemote && event.crafting.getItem() != InitItems.itemLexicon && (event.crafting.getItem() instanceof IActAddItemOrBlock || Block.getBlockFromItem(event.crafting.getItem()) instanceof IActAddItemOrBlock)){
PersistentServerData data = PersistentServerData.get(event.player); NBTTagCompound compound = PersistentServerData.getDataFromPlayer(event.player);
if(data != null && !data.bookGottenAlready){ if(compound != null && !compound.getBoolean("BookGottenAlready")){
data.bookGottenAlready = true; compound.setBoolean("BookGottenAlready", true);
WorldData.makeDirty();
EntityItem entityItem = new EntityItem(event.player.worldObj, event.player.posX, event.player.posY, event.player.posZ, new ItemStack(InitItems.itemLexicon)); EntityItem entityItem = new EntityItem(event.player.worldObj, event.player.posX, event.player.posY, event.player.posZ, new ItemStack(InitItems.itemLexicon));
entityItem.delayBeforeCanPickup = 0; entityItem.delayBeforeCanPickup = 0;

View file

@ -1,29 +0,0 @@
/*
* This file ("EntityConstructingEvent.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015 Ellpeck
*/
package ellpeck.actuallyadditions.event;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import ellpeck.actuallyadditions.util.ModUtil;
import ellpeck.actuallyadditions.util.playerdata.PersistentServerData;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.event.entity.EntityEvent;
public class EntityConstructingEvent{
@SubscribeEvent
public void onEntityConstructing(EntityEvent.EntityConstructing event){
if(event.entity instanceof EntityPlayer && !event.entity.worldObj.isRemote){
if(PersistentServerData.get((EntityPlayer)event.entity) == null){
event.entity.registerExtendedProperties(ModUtil.MOD_ID, new PersistentServerData());
}
}
}
}

View file

@ -24,13 +24,13 @@ public class InitEvents{
ModUtil.LOGGER.info("Initializing Events..."); ModUtil.LOGGER.info("Initializing Events...");
Util.registerEvent(new SmeltEvent()); Util.registerEvent(new SmeltEvent());
Util.registerEvent(new CraftEvent()); Util.registerEvent(new CraftEvent());
Util.registerEvent(new LivingDropEvent()); Util.registerEvent(new LivingDropEvent());
Util.registerEvent(new PickupEvent()); Util.registerEvent(new PickupEvent());
Util.registerEvent(new EntityLivingEvent()); Util.registerEvent(new EntityLivingEvent());
Util.registerEvent(new BucketFillEvent()); Util.registerEvent(new BucketFillEvent());
Util.registerEvent(new LogoutEvent()); Util.registerEvent(new LogoutEvent());
Util.registerEvent(new EntityConstructingEvent());
Util.registerEvent(new WorldLoadEvent()); Util.registerEvent(new WorldLoadEvent());
MinecraftForge.TERRAIN_GEN_BUS.register(new WorldDecorationEvent()); MinecraftForge.TERRAIN_GEN_BUS.register(new WorldDecorationEvent());
} }

View file

@ -11,6 +11,7 @@
package ellpeck.actuallyadditions.misc; package ellpeck.actuallyadditions.misc;
import ellpeck.actuallyadditions.util.ModUtil; import ellpeck.actuallyadditions.util.ModUtil;
import ellpeck.actuallyadditions.util.playerdata.PersistentServerData;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.world.World; import net.minecraft.world.World;
@ -30,8 +31,8 @@ public class WorldData extends WorldSavedData{
@Override @Override
public void readFromNBT(NBTTagCompound compound){ public void readFromNBT(NBTTagCompound compound){
//Laser World Data
int netAmount = compound.getInteger("LaserNetworkAmount"); int netAmount = compound.getInteger("LaserNetworkAmount");
LaserRelayConnectionHandler.getInstance().networks.clear(); LaserRelayConnectionHandler.getInstance().networks.clear();
for(int i = 0; i < netAmount; i++){ for(int i = 0; i < netAmount; i++){
ArrayList<LaserRelayConnectionHandler.ConnectionPair> network = LaserRelayConnectionHandler.getInstance().readNetworkFromNBT(compound, "LaserNetwork"+i); ArrayList<LaserRelayConnectionHandler.ConnectionPair> network = LaserRelayConnectionHandler.getInstance().readNetworkFromNBT(compound, "LaserNetwork"+i);
@ -39,16 +40,34 @@ public class WorldData extends WorldSavedData{
LaserRelayConnectionHandler.getInstance().networks.add(network); LaserRelayConnectionHandler.getInstance().networks.add(network);
} }
} }
//Player Data
int dataSize = compound.getInteger("PersistentDataSize");
PersistentServerData.playerSaveData.clear();
for(int i = 0; i < dataSize; i++){
PersistentServerData.PlayerSave aSave = PersistentServerData.PlayerSave.fromNBT(compound, "PlayerSaveData"+i);
if(aSave != null){
PersistentServerData.playerSaveData.add(aSave);
}
}
} }
@Override @Override
public void writeToNBT(NBTTagCompound compound){ public void writeToNBT(NBTTagCompound compound){
//Laser World Data
int netAmount = LaserRelayConnectionHandler.getInstance().networks.size(); int netAmount = LaserRelayConnectionHandler.getInstance().networks.size();
compound.setInteger("LaserNetworkAmount", netAmount); compound.setInteger("LaserNetworkAmount", netAmount);
for(int i = 0; i < netAmount; i++){ for(int i = 0; i < netAmount; i++){
LaserRelayConnectionHandler.getInstance().writeNetworkToNBT(LaserRelayConnectionHandler.getInstance().networks.get(i), compound, "LaserNetwork"+i); LaserRelayConnectionHandler.getInstance().writeNetworkToNBT(LaserRelayConnectionHandler.getInstance().networks.get(i), compound, "LaserNetwork"+i);
} }
//Player Data
compound.setInteger("PersistentDataSize", PersistentServerData.playerSaveData.size());
for(int i = 0; i < PersistentServerData.playerSaveData.size(); i++){
PersistentServerData.PlayerSave theSave = PersistentServerData.playerSaveData.get(i);
theSave.toNBT(compound, "PlayerSaveData"+i);
}
} }
public static void makeDirty(){ public static void makeDirty(){

View file

@ -10,47 +10,52 @@
package ellpeck.actuallyadditions.util.playerdata; package ellpeck.actuallyadditions.util.playerdata;
import ellpeck.actuallyadditions.util.ModUtil;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.common.IExtendedEntityProperties;
public class PersistentServerData implements IExtendedEntityProperties{ import java.util.ArrayList;
import java.util.UUID;
public boolean bookGottenAlready; public class PersistentServerData{
public static PersistentServerData get(EntityPlayer player){ public static ArrayList<PlayerSave> playerSaveData = new ArrayList<PlayerSave>();
IExtendedEntityProperties properties = player.getExtendedProperties(ModUtil.MOD_ID);
if(properties != null && properties instanceof PersistentServerData){
return (PersistentServerData)properties;
}
return null;
}
@Override public static NBTTagCompound getDataFromPlayer(EntityPlayer player){
public void saveNBTData(NBTTagCompound aComp){ //Get Data from existing data
NBTTagCompound compound = new NBTTagCompound(); for(PlayerSave save : playerSaveData){
if(save.thePlayerUUID.equals(player.getUniqueID())){
compound.setBoolean("BookGotten", this.bookGottenAlready); return save.theCompound;
aComp.setTag(ModUtil.MOD_ID, compound);
}
@Override
public void loadNBTData(NBTTagCompound aComp){
NBTBase base = aComp.getTag(ModUtil.MOD_ID);
if(base != null && base instanceof NBTTagCompound){
NBTTagCompound compound = (NBTTagCompound)base;
this.bookGottenAlready = compound.getBoolean("BookGotten");
} }
} }
@Override //Add Data if none is existant
public void init(Entity entity, World world){ PlayerSave aSave = new PlayerSave(player.getUniqueID(), new NBTTagCompound());
playerSaveData.add(aSave);
return aSave.theCompound;
}
public static class PlayerSave{
public UUID thePlayerUUID;
public NBTTagCompound theCompound;
public PlayerSave(UUID theUUID, NBTTagCompound theCompound){
this.thePlayerUUID = theUUID;
this.theCompound = theCompound;
}
public void toNBT(NBTTagCompound compound, String name){
compound.setLong(name+"LeastSignificant", this.thePlayerUUID.getLeastSignificantBits());
compound.setLong(name+"MostSignificant", this.thePlayerUUID.getMostSignificantBits());
compound.setTag(name+"Tag", this.theCompound);
}
public static PlayerSave fromNBT(NBTTagCompound compound, String name){
UUID theID = new UUID(compound.getLong(name+"MostSignificant"), compound.getLong(name+"LeastSignificant"));
NBTTagCompound theCompound = compound.getCompoundTag(name+"Tag");
return new PlayerSave(theID, theCompound);
}
}
} }
}