2015-10-19 17:50:43 +02:00
|
|
|
/*
|
|
|
|
* This file ("WorldData.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-11-02 20:55:19 +01:00
|
|
|
* © 2015 Ellpeck
|
2015-10-19 17:50:43 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package ellpeck.actuallyadditions.misc;
|
|
|
|
|
|
|
|
import ellpeck.actuallyadditions.util.ModUtil;
|
2015-10-22 22:18:33 +02:00
|
|
|
import ellpeck.actuallyadditions.util.playerdata.PersistentServerData;
|
2015-10-19 17:50:43 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2015-10-29 17:36:30 +01:00
|
|
|
import net.minecraft.nbt.NBTTagList;
|
2015-10-19 17:50:43 +02:00
|
|
|
import net.minecraft.server.MinecraftServer;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.minecraft.world.WorldSavedData;
|
|
|
|
|
|
|
|
public class WorldData extends WorldSavedData{
|
|
|
|
|
|
|
|
public static final String DATA_TAG = ModUtil.MOD_ID+"WorldData";
|
2015-10-23 16:54:33 +02:00
|
|
|
public static WorldData instance;
|
2015-10-19 17:50:43 +02:00
|
|
|
|
2015-10-20 00:22:36 +02:00
|
|
|
public WorldData(String tag){
|
|
|
|
super(tag);
|
2015-10-19 17:50:43 +02:00
|
|
|
}
|
|
|
|
|
2015-10-23 16:54:33 +02:00
|
|
|
public static void makeDirty(){
|
|
|
|
if(instance != null){
|
|
|
|
instance.markDirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void init(MinecraftServer server){
|
|
|
|
if(server != null){
|
|
|
|
World world = server.getEntityWorld();
|
|
|
|
if(!world.isRemote){
|
|
|
|
WorldSavedData savedData = world.loadItemData(WorldData.class, WorldData.DATA_TAG);
|
|
|
|
//Generate new SavedData
|
|
|
|
if(savedData == null){
|
|
|
|
savedData = new WorldData(WorldData.DATA_TAG);
|
|
|
|
world.setItemData(WorldData.DATA_TAG, savedData);
|
|
|
|
}
|
|
|
|
//Set the current SavedData to the retreived one
|
|
|
|
if(savedData instanceof WorldData){
|
|
|
|
WorldData.instance = (WorldData)savedData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-19 17:50:43 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readFromNBT(NBTTagCompound compound){
|
2015-10-22 22:18:33 +02:00
|
|
|
//Laser World Data
|
2015-10-29 19:51:00 +01:00
|
|
|
NBTTagList networkList = compound.getTagList("Networks", 10);
|
|
|
|
for(int i = 0; i < networkList.tagCount(); i++){
|
|
|
|
LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getInstance().readNetworkFromNBT(networkList.getCompoundTagAt(i));
|
2015-10-29 17:36:30 +01:00
|
|
|
LaserRelayConnectionHandler.getInstance().networks.add(network);
|
2015-10-21 00:22:50 +02:00
|
|
|
}
|
2015-10-22 22:18:33 +02:00
|
|
|
|
|
|
|
//Player Data
|
2015-10-29 19:51:00 +01:00
|
|
|
NBTTagList playerList = compound.getTagList("PlayerData", 10);
|
|
|
|
for(int i = 0; i < playerList.tagCount(); i++){
|
|
|
|
PersistentServerData.PlayerSave aSave = PersistentServerData.PlayerSave.fromNBT(playerList.getCompoundTagAt(i));
|
|
|
|
PersistentServerData.playerSaveData.add(aSave);
|
2015-10-22 22:18:33 +02:00
|
|
|
}
|
2015-10-19 17:50:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToNBT(NBTTagCompound compound){
|
2015-10-22 22:18:33 +02:00
|
|
|
//Laser World Data
|
2015-10-29 19:51:00 +01:00
|
|
|
NBTTagList networkList = new NBTTagList();
|
2015-10-29 17:36:30 +01:00
|
|
|
for(LaserRelayConnectionHandler.Network network : LaserRelayConnectionHandler.getInstance().networks){
|
2015-10-29 19:51:00 +01:00
|
|
|
networkList.appendTag(LaserRelayConnectionHandler.getInstance().writeNetworkToNBT(network));
|
2015-10-21 00:22:50 +02:00
|
|
|
}
|
2015-10-29 19:51:00 +01:00
|
|
|
compound.setTag("Networks", networkList);
|
2015-10-22 22:18:33 +02:00
|
|
|
|
|
|
|
//Player Data
|
2015-10-29 19:51:00 +01:00
|
|
|
NBTTagList playerList = new NBTTagList();
|
2015-10-22 22:18:33 +02:00
|
|
|
for(int i = 0; i < PersistentServerData.playerSaveData.size(); i++){
|
|
|
|
PersistentServerData.PlayerSave theSave = PersistentServerData.playerSaveData.get(i);
|
2015-10-29 19:51:00 +01:00
|
|
|
playerList.appendTag(theSave.toNBT());
|
2015-10-22 22:18:33 +02:00
|
|
|
}
|
2015-10-29 19:51:00 +01:00
|
|
|
compound.setTag("PlayerData", playerList);
|
2015-10-19 17:50:43 +02:00
|
|
|
}
|
|
|
|
}
|