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

116 lines
4.1 KiB
Java
Raw Normal View History

2020-09-09 16:49:01 +02:00
package de.ellpeck.actuallyadditions.common.data;
2018-07-07 11:38:33 +02:00
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
2016-08-02 13:08:22 +02:00
import de.ellpeck.actuallyadditions.api.laser.Network;
2020-09-09 16:49:01 +02:00
import de.ellpeck.actuallyadditions.common.ActuallyAdditions;
import de.ellpeck.actuallyadditions.common.data.PlayerData.PlayerSave;
import de.ellpeck.actuallyadditions.common.misc.apiimpl.LaserRelayConnectionHandler;
import io.netty.util.internal.ConcurrentSet;
2020-09-21 19:59:08 +02:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.world.World;
2017-06-17 00:48:49 +02:00
import net.minecraft.world.storage.WorldSavedData;
2018-07-07 11:35:39 +02:00
public class WorldData extends WorldSavedData {
2018-07-07 11:35:39 +02:00
public static final String DATA_TAG = ActuallyAdditions.MODID + "data";
2017-02-04 16:48:22 +01:00
private static WorldData data;
2019-02-27 19:53:05 +01:00
public final ConcurrentSet<Network> laserRelayNetworks = new ConcurrentSet<>();
public final ConcurrentHashMap<UUID, PlayerSave> playerSaveData = new ConcurrentHashMap<>();
2018-07-07 11:35:39 +02:00
public WorldData(String name) {
super(name);
}
2018-07-07 11:35:39 +02:00
public static WorldData get(World world, boolean forceLoad) {
WorldData w = getInternal(world, forceLoad);
if (w == null) ActuallyAdditions.LOGGER.error("An impossible bug has occured.");
return w == null ? new WorldData(DATA_TAG) : w;
2017-09-13 18:50:12 +02:00
}
2018-07-07 11:35:39 +02:00
private static WorldData getInternal(World world, boolean forceLoad) {
if (forceLoad || data == null) {
if (!world.isRemote) {
WorldSavedData savedData = world.loadData(WorldData.class, DATA_TAG);
2018-07-07 11:35:39 +02:00
if (savedData == null) {
2018-05-10 11:38:58 +02:00
ActuallyAdditions.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;
2018-07-07 11:35:39 +02:00
} else {
2017-09-13 18:50:12 +02:00
data = (WorldData) savedData;
2018-05-10 11:38:58 +02:00
ActuallyAdditions.LOGGER.info("Successfully loaded WorldData!");
}
2018-07-07 11:35:39 +02:00
} else {
data = new WorldData(DATA_TAG);
2018-05-10 11:38:58 +02:00
ActuallyAdditions.LOGGER.info("Created temporary WorldData to cache data on the client!");
}
}
return data;
}
2018-07-07 11:35:39 +02:00
public static void clear() {
if (data != null) {
data = null;
2018-05-10 11:38:58 +02:00
ActuallyAdditions.LOGGER.info("Unloaded WorldData!");
}
}
2018-07-07 11:35:39 +02:00
public static WorldData get(World world) {
return get(world, false);
}
2016-06-05 12:52:59 +02:00
2018-07-07 11:35:39 +02:00
@Override
2020-09-21 19:59:08 +02:00
public void read(CompoundNBT compound) {
2018-07-07 11:35:39 +02:00
this.laserRelayNetworks.clear();
2020-09-21 19:59:08 +02:00
ListNBT networkList = compound.getList("Networks", 10);
for (int i = 0; i < networkList.size(); i++) {
Network network = LaserRelayConnectionHandler.readNetworkFromNBT(networkList.getCompound(i));
2016-06-05 12:52:59 +02:00
this.laserRelayNetworks.add(network);
}
2018-07-07 11:35:39 +02:00
this.playerSaveData.clear();
2020-09-21 19:59:08 +02:00
ListNBT playerList = compound.getList("PlayerData", 10);
for (int i = 0; i < playerList.size(); i++) {
CompoundNBT player = playerList.getCompound(i);
UUID id = player.getUniqueId("UUID");
2020-09-21 19:59:08 +02:00
CompoundNBT data = player.getCompound("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
2020-09-21 19:59:08 +02:00
public CompoundNBT write(CompoundNBT compound) {
2016-06-05 12:52:59 +02:00
//Laser World Data
2020-09-21 19:59:08 +02:00
ListNBT networkList = new ListNBT();
2018-07-07 11:35:39 +02:00
for (Network network : this.laserRelayNetworks) {
2020-09-21 19:59:08 +02:00
networkList.add(LaserRelayConnectionHandler.writeNetworkToNBT(network));
2016-06-05 12:52:59 +02:00
}
2020-09-21 19:59:08 +02:00
compound.put("Networks", networkList);
2016-06-05 12:52:59 +02:00
//Player Data
2020-09-21 19:59:08 +02:00
ListNBT playerList = new ListNBT();
2018-07-07 11:35:39 +02:00
for (PlayerSave save : this.playerSaveData.values()) {
2020-09-21 19:59:08 +02:00
CompoundNBT player = new CompoundNBT();
player.putUniqueId("UUID", save.id);
2020-09-21 19:59:08 +02:00
CompoundNBT data = new CompoundNBT();
2016-11-22 19:35:52 +01:00
save.writeToNBT(data, true);
2020-09-21 19:59:08 +02:00
player.put("Data", data);
2020-09-21 19:59:08 +02:00
playerList.add(player);
2016-06-05 12:52:59 +02:00
}
2020-09-21 19:59:08 +02:00
compound.put("PlayerData", playerList);
return compound;
}
}