2016-06-04 13:51:06 +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://ellpeck.de/actaddlicense
|
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
|
|
|
* © 2015-2016 Ellpeck
|
|
|
|
*/
|
|
|
|
|
|
|
|
package de.ellpeck.actuallyadditions.mod.data;
|
|
|
|
|
2016-06-15 16:43:59 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.data.PlayerData.PlayerSave;
|
2016-06-04 13:51:06 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.misc.LaserRelayConnectionHandler;
|
2016-07-30 17:07:32 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.laser.Network;
|
2016-06-04 13:51:06 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
|
|
|
|
import io.netty.util.internal.ConcurrentSet;
|
|
|
|
import net.minecraft.nbt.CompressedStreamTools;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.minecraft.world.WorldServer;
|
|
|
|
import net.minecraft.world.storage.ISaveHandler;
|
|
|
|
import net.minecraftforge.common.WorldSpecificSaveHandler;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
|
|
public class WorldData{
|
|
|
|
|
|
|
|
public static final String DATA_TAG = ModUtil.MOD_ID+"data";
|
2016-06-05 12:52:59 +02:00
|
|
|
public static final ArrayList<PlayerSave> PLAYER_SAVE_DATA = new ArrayList<PlayerSave>();
|
2016-07-24 18:58:08 +02:00
|
|
|
private static final ConcurrentHashMap<Integer, WorldData> WORLD_DATA = new ConcurrentHashMap<Integer, WorldData>();
|
2016-06-05 12:52:59 +02:00
|
|
|
public final ConcurrentSet<Network> laserRelayNetworks = new ConcurrentSet<Network>();
|
2016-06-17 23:50:38 +02:00
|
|
|
private final ISaveHandler handler;
|
|
|
|
private final int dimension;
|
2016-06-04 13:51:06 +02:00
|
|
|
|
|
|
|
public WorldData(ISaveHandler handler, int dimension){
|
|
|
|
this.handler = handler;
|
|
|
|
this.dimension = dimension;
|
|
|
|
}
|
|
|
|
|
2016-06-04 14:38:20 +02:00
|
|
|
public static WorldData getDataForWorld(World world){
|
|
|
|
int dim = world.provider.getDimension();
|
2016-06-17 23:50:38 +02:00
|
|
|
WorldData data = WORLD_DATA.get(dim);
|
2016-06-04 14:38:20 +02:00
|
|
|
|
2016-06-11 23:43:28 +02:00
|
|
|
if(data == null){
|
2016-06-04 14:38:20 +02:00
|
|
|
data = new WorldData(null, dim);
|
2016-06-11 23:43:28 +02:00
|
|
|
|
|
|
|
if(world.isRemote){
|
2016-06-17 23:50:38 +02:00
|
|
|
WORLD_DATA.put(dim, data);
|
2016-06-11 23:43:28 +02:00
|
|
|
ModUtil.LOGGER.info("Creating temporary WorldData for world "+dim+" on the client!");
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
ModUtil.LOGGER.warn("Trying to get WorldData from world "+dim+" that doesn't have any data!? This shouldn't happen!");
|
|
|
|
}
|
2016-06-04 14:38:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
2016-06-04 13:51:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void load(World world){
|
|
|
|
if(!world.isRemote && world instanceof WorldServer){
|
|
|
|
WorldData data = new WorldData(new WorldSpecificSaveHandler((WorldServer)world, world.getSaveHandler()), world.provider.getDimension());
|
2016-06-17 23:50:38 +02:00
|
|
|
WORLD_DATA.put(data.dimension, data);
|
2016-06-04 13:51:06 +02:00
|
|
|
|
|
|
|
try{
|
|
|
|
File dataFile = data.handler.getMapFileFromName(DATA_TAG+data.dimension);
|
|
|
|
|
|
|
|
if(dataFile != null && dataFile.exists()){
|
|
|
|
FileInputStream stream = new FileInputStream(dataFile);
|
|
|
|
NBTTagCompound compound = CompressedStreamTools.readCompressed(stream);
|
|
|
|
stream.close();
|
|
|
|
data.readFromNBT(compound);
|
|
|
|
|
|
|
|
ModUtil.LOGGER.info("Successfully received WorldData for world "+data.dimension+"!");
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
ModUtil.LOGGER.info("No WorldData found for world "+data.dimension+", creating...");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
catch(Exception e){
|
|
|
|
ModUtil.LOGGER.error("Something went wrong trying to load WorldData for world "+data.dimension+"!", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-05 00:24:10 +02:00
|
|
|
public static void save(World world){
|
2016-06-04 13:51:06 +02:00
|
|
|
if(!world.isRemote){
|
2016-06-17 23:50:38 +02:00
|
|
|
WorldData data = WORLD_DATA.get(world.provider.getDimension());
|
2016-06-04 13:51:06 +02:00
|
|
|
if(data != null && data.handler != null){
|
|
|
|
try{
|
|
|
|
File dataFile = data.handler.getMapFileFromName(DATA_TAG+data.dimension);
|
|
|
|
|
|
|
|
if(dataFile != null){
|
|
|
|
if(!dataFile.exists()){
|
|
|
|
dataFile.createNewFile();
|
2016-06-04 14:20:38 +02:00
|
|
|
ModUtil.LOGGER.info("Creating new WorldData file for world "+data.dimension+"!");
|
2016-06-04 13:51:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NBTTagCompound compound = new NBTTagCompound();
|
|
|
|
data.writeToNBT(compound);
|
|
|
|
FileOutputStream stream = new FileOutputStream(dataFile);
|
|
|
|
CompressedStreamTools.writeCompressed(compound, stream);
|
|
|
|
stream.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(Exception e){
|
|
|
|
ModUtil.LOGGER.error("Something went wrong trying to save WorldData for world "+data.dimension+"!", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
2016-06-04 14:38:20 +02:00
|
|
|
ModUtil.LOGGER.error("Tried to save WorldData for "+world.provider.getDimension()+" without any data being present!?");
|
2016-06-04 13:51:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void unload(World world){
|
|
|
|
if(!world.isRemote){
|
2016-06-17 23:50:38 +02:00
|
|
|
WORLD_DATA.remove(world.provider.getDimension());
|
2016-06-04 14:38:20 +02:00
|
|
|
ModUtil.LOGGER.info("Unloading WorldData for world "+world.provider.getDimension()+"!");
|
2016-06-04 13:51:06 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-05 12:52:59 +02:00
|
|
|
|
|
|
|
private void readFromNBT(NBTTagCompound compound){
|
|
|
|
//Laser World Data
|
|
|
|
this.laserRelayNetworks.clear();
|
|
|
|
|
|
|
|
NBTTagList networkList = compound.getTagList("Networks", 10);
|
|
|
|
for(int i = 0; i < networkList.tagCount(); i++){
|
|
|
|
Network network = LaserRelayConnectionHandler.readNetworkFromNBT(networkList.getCompoundTagAt(i));
|
|
|
|
this.laserRelayNetworks.add(network);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this.dimension == 0){
|
|
|
|
//Player Data
|
|
|
|
PLAYER_SAVE_DATA.clear();
|
|
|
|
|
|
|
|
NBTTagList playerList = compound.getTagList("PlayerData", 10);
|
|
|
|
for(int i = 0; i < playerList.tagCount(); i++){
|
|
|
|
PlayerSave aSave = PlayerSave.fromNBT(playerList.getCompoundTagAt(i));
|
|
|
|
PLAYER_SAVE_DATA.add(aSave);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void writeToNBT(NBTTagCompound compound){
|
|
|
|
//Laser World Data
|
|
|
|
NBTTagList networkList = new NBTTagList();
|
|
|
|
for(Network network : this.laserRelayNetworks){
|
|
|
|
networkList.appendTag(LaserRelayConnectionHandler.writeNetworkToNBT(network));
|
|
|
|
}
|
|
|
|
compound.setTag("Networks", networkList);
|
|
|
|
|
|
|
|
if(this.dimension == 0){
|
|
|
|
//Player Data
|
|
|
|
NBTTagList playerList = new NBTTagList();
|
|
|
|
for(PlayerSave theSave : PLAYER_SAVE_DATA){
|
|
|
|
playerList.appendTag(theSave.toNBT());
|
|
|
|
}
|
|
|
|
compound.setTag("PlayerData", playerList);
|
|
|
|
}
|
|
|
|
}
|
2016-06-04 13:51:06 +02:00
|
|
|
}
|