ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/misc/WorldData.java

107 lines
4 KiB
Java
Raw Normal View History

/*
2016-05-16 22:52:27 +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
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-05-16 22:54:42 +02:00
* © 2015-2016 Ellpeck
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.misc;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import de.ellpeck.actuallyadditions.mod.util.playerdata.PersistentServerData;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
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;
public WorldData(String tag){
super(tag);
}
public static void makeDirty(){
2015-10-23 16:54:33 +02:00
if(instance != null){
instance.markDirty();
}
}
2016-03-18 23:47:22 +01:00
public static void init(MinecraftServer server){
2015-10-23 16:54:33 +02:00
if(server != null){
World world = server.getEntityWorld();
if(!world.isRemote){
clearOldData();
ModUtil.LOGGER.info("Loading WorldData!");
WorldData savedData = (WorldData)world.loadItemData(WorldData.class, WorldData.DATA_TAG);
2015-10-23 16:54:33 +02:00
//Generate new SavedData
if(savedData == null){
ModUtil.LOGGER.info("No WorldData found, creating...");
2015-10-23 16:54:33 +02:00
savedData = new WorldData(WorldData.DATA_TAG);
world.setItemData(WorldData.DATA_TAG, savedData);
}
else{
ModUtil.LOGGER.info("WorldData sucessfully received!");
2015-10-23 16:54:33 +02:00
}
//Set the current SavedData to the retreived one
WorldData.instance = savedData;
2015-10-23 16:54:33 +02:00
}
}
}
public static void clearOldData(){
if(!LaserRelayConnectionHandler.getInstance().networks.isEmpty()){
ModUtil.LOGGER.info("Clearing leftover Laser Relay Connection Data from other worlds!");
2015-12-20 20:35:10 +01:00
LaserRelayConnectionHandler.getInstance().networks.clear();
}
if(!PersistentServerData.playerSaveData.isEmpty()){
ModUtil.LOGGER.info("Clearing leftover Persistent Server Data from other worlds!");
2015-12-20 20:35:10 +01:00
PersistentServerData.playerSaveData.clear();
}
}
@Override
public void readFromNBT(NBTTagCompound compound){
//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));
LaserRelayConnectionHandler.getInstance().networks.add(network);
2015-10-21 00:22:50 +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);
}
}
@Override
public void writeToNBT(NBTTagCompound compound){
//Laser World Data
2015-10-29 19:51:00 +01:00
NBTTagList networkList = new NBTTagList();
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);
//Player Data
2015-10-29 19:51:00 +01:00
NBTTagList playerList = new NBTTagList();
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-29 19:51:00 +01:00
compound.setTag("PlayerData", playerList);
}
}