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

157 lines
5.5 KiB
Java
Raw Normal View History

/*
* 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
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.data;
2016-08-02 13:08:22 +02:00
import de.ellpeck.actuallyadditions.api.laser.Network;
import de.ellpeck.actuallyadditions.mod.data.PlayerData.PlayerSave;
import de.ellpeck.actuallyadditions.mod.misc.apiimpl.LaserRelayConnectionHandler;
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import io.netty.util.internal.ConcurrentSet;
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;
2017-06-17 00:48:49 +02:00
import net.minecraft.world.storage.WorldSavedData;
import net.minecraftforge.common.WorldSpecificSaveHandler;
import java.io.File;
2017-03-08 20:06:55 +01:00
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
public class WorldData extends WorldSavedData{
public static final String DATA_TAG = ModUtil.MOD_ID+"data";
//TODO Remove this as well
public static List<File> legacyLoadWorlds = new ArrayList<File>();
2017-02-04 16:48:22 +01:00
private static WorldData data;
2017-03-08 20:06:55 +01:00
public final ConcurrentSet<Network> laserRelayNetworks = new ConcurrentSet<Network>();
public final ConcurrentHashMap<UUID, PlayerSave> playerSaveData = new ConcurrentHashMap<UUID, PlayerSave>();
public WorldData(String name){
super(name);
}
public static WorldData get(World world, boolean forceLoad){
2017-09-13 18:50:12 +02:00
WorldData w = getInternal(world, forceLoad);
if(w == null) ModUtil.LOGGER.error("What the hell how is this stupid thing null again AEWBFINCEMR");
return w == null ? new WorldData(DATA_TAG) : w;
}
private static WorldData getInternal(World world, boolean forceLoad){
if(forceLoad || data == null){
if(!world.isRemote){
WorldSavedData savedData = world.loadData(WorldData.class, DATA_TAG);
if(!(savedData instanceof WorldData)){
ModUtil.LOGGER.info("No WorldData found, creating...");
2016-06-04 14:38:20 +02:00
WorldData newData = new WorldData(DATA_TAG);
world.setData(DATA_TAG, newData);
data = newData;
}
else{
2017-09-13 18:50:12 +02:00
data = (WorldData) savedData;
ModUtil.LOGGER.info("Successfully loaded WorldData!");
}
}
else{
data = new WorldData(DATA_TAG);
ModUtil.LOGGER.info("Created temporary WorldData to cache data on the client!");
}
}
return data;
}
public static void clear(){
if(data != null){
data = null;
2017-04-01 19:40:39 +02:00
ModUtil.LOGGER.info("Unloaded WorldData!");
}
}
public static WorldData get(World world){
return get(world, false);
}
2016-06-05 12:52:59 +02:00
2017-02-04 16:48:22 +01:00
//TODO Remove old loading mechanic after a while because it's legacy
public static void loadLegacy(World world){
if(!world.isRemote && world instanceof WorldServer){
int dim = world.provider.getDimension();
ISaveHandler handler = new WorldSpecificSaveHandler((WorldServer)world, world.getSaveHandler());
File dataFile = handler.getMapFileFromName(DATA_TAG+dim);
legacyLoadWorlds.add(dataFile);
}
}
//TODO Remove merging once removing old save handler
private void readFromNBT(NBTTagCompound compound, boolean merge){
2016-06-05 12:52:59 +02:00
//Laser World Data
if(!merge){
this.laserRelayNetworks.clear();
}
2016-06-05 12:52:59 +02:00
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);
}
//Player Data
if(!merge){
this.playerSaveData.clear();
}
NBTTagList playerList = compound.getTagList("PlayerData", 10);
for(int i = 0; i < playerList.tagCount(); i++){
NBTTagCompound player = playerList.getCompoundTagAt(i);
UUID id = player.getUniqueId("UUID");
NBTTagCompound data = player.getCompoundTag("Data");
PlayerSave save = new PlayerSave(id);
2016-11-22 19:35:52 +01:00
save.readFromNBT(data, true);
this.playerSaveData.put(id, save);
2016-06-05 12:52:59 +02:00
}
}
@Override
public void readFromNBT(NBTTagCompound compound){
this.readFromNBT(compound, false);
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound){
2016-06-05 12:52:59 +02:00
//Laser World Data
NBTTagList networkList = new NBTTagList();
for(Network network : this.laserRelayNetworks){
networkList.appendTag(LaserRelayConnectionHandler.writeNetworkToNBT(network));
}
compound.setTag("Networks", networkList);
//Player Data
NBTTagList playerList = new NBTTagList();
for(PlayerSave save : this.playerSaveData.values()){
NBTTagCompound player = new NBTTagCompound();
player.setUniqueId("UUID", save.id);
NBTTagCompound data = new NBTTagCompound();
2016-11-22 19:35:52 +01:00
save.writeToNBT(data, true);
player.setTag("Data", data);
playerList.appendTag(player);
2016-06-05 12:52:59 +02:00
}
compound.setTag("PlayerData", playerList);
return compound;
}
}