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
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 Ellpeck
|
2016-06-04 13:51:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package de.ellpeck.actuallyadditions.mod.data;
|
|
|
|
|
2016-08-02 13:08:22 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.laser.Network;
|
2018-05-10 11:38:58 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
2016-06-15 16:43:59 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.data.PlayerData.PlayerSave;
|
2016-12-04 15:03:01 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.misc.apiimpl.LaserRelayConnectionHandler;
|
2016-06-04 13:51:06 +02:00
|
|
|
import io.netty.util.internal.ConcurrentSet;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
|
|
|
import net.minecraft.nbt.ListNBT;
|
2016-06-04 13:51:06 +02:00
|
|
|
import net.minecraft.world.World;
|
2017-06-17 00:48:49 +02:00
|
|
|
import net.minecraft.world.storage.WorldSavedData;
|
2016-06-04 13:51:06 +02:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
2018-07-07 11:35:39 +02:00
|
|
|
public class WorldData extends WorldSavedData {
|
2016-06-04 13:51:06 +02:00
|
|
|
|
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<>();
|
2016-06-04 13:51:06 +02:00
|
|
|
|
2018-07-07 11:35:39 +02:00
|
|
|
public WorldData(String name) {
|
2016-12-28 14:55:03 +01:00
|
|
|
super(name);
|
2016-06-04 13:51:06 +02:00
|
|
|
}
|
|
|
|
|
2018-07-07 11:35:39 +02:00
|
|
|
public static WorldData get(World world, boolean forceLoad) {
|
|
|
|
WorldData w = getInternal(world, forceLoad);
|
2021-02-26 22:15:48 +01:00
|
|
|
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) {
|
2016-12-28 14:55:03 +01:00
|
|
|
WorldSavedData savedData = world.loadData(WorldData.class, DATA_TAG);
|
2016-08-13 02:21:50 +02:00
|
|
|
|
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
|
|
|
|
2016-12-28 14:55:03 +01: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!");
|
2016-06-04 13:51:06 +02:00
|
|
|
}
|
2018-07-07 11:35:39 +02:00
|
|
|
} else {
|
2016-12-28 14:55:03 +01:00
|
|
|
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!");
|
2016-06-04 13:51:06 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-28 14:55:03 +01:00
|
|
|
return data;
|
2016-06-04 13:51:06 +02:00
|
|
|
}
|
|
|
|
|
2018-07-07 11:35:39 +02:00
|
|
|
public static void clear() {
|
|
|
|
if (data != null) {
|
2017-02-13 15:23:28 +01:00
|
|
|
data = null;
|
2018-05-10 11:38:58 +02:00
|
|
|
ActuallyAdditions.LOGGER.info("Unloaded WorldData!");
|
2017-02-13 15:23:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-07 11:35:39 +02:00
|
|
|
public static WorldData get(World world) {
|
2016-12-28 14:55:03 +01:00
|
|
|
return get(world, false);
|
2016-06-04 13:51:06 +02:00
|
|
|
}
|
2016-06-05 12:52:59 +02:00
|
|
|
|
2018-07-07 11:35:39 +02:00
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void readFromNBT(CompoundNBT compound) {
|
2018-07-07 11:35:39 +02:00
|
|
|
this.laserRelayNetworks.clear();
|
2021-02-26 22:15:48 +01: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();
|
2021-02-26 22:15:48 +01:00
|
|
|
ListNBT playerList = compound.getList("PlayerData", 10);
|
|
|
|
for (int i = 0; i < playerList.size(); i++) {
|
|
|
|
CompoundNBT player = playerList.getCompound(i);
|
2016-11-11 21:41:07 +01:00
|
|
|
|
|
|
|
UUID id = player.getUniqueId("UUID");
|
2021-02-26 22:15:48 +01:00
|
|
|
CompoundNBT data = player.getCompoundTag("Data");
|
2016-11-11 21:41:07 +01:00
|
|
|
|
|
|
|
PlayerSave save = new PlayerSave(id);
|
2016-11-22 19:35:52 +01:00
|
|
|
save.readFromNBT(data, true);
|
2016-11-22 18:26:29 +01:00
|
|
|
this.playerSaveData.put(id, save);
|
2016-06-05 12:52:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-28 14:55:03 +01:00
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public CompoundNBT writeToNBT(CompoundNBT compound) {
|
2016-06-05 12:52:59 +02:00
|
|
|
//Laser World Data
|
2021-02-26 22:15:48 +01:00
|
|
|
ListNBT networkList = new ListNBT();
|
2018-07-07 11:35:39 +02:00
|
|
|
for (Network network : this.laserRelayNetworks) {
|
2016-06-05 12:52:59 +02:00
|
|
|
networkList.appendTag(LaserRelayConnectionHandler.writeNetworkToNBT(network));
|
|
|
|
}
|
|
|
|
compound.setTag("Networks", networkList);
|
|
|
|
|
2016-08-13 02:21:50 +02:00
|
|
|
//Player Data
|
2021-02-26 22:15:48 +01:00
|
|
|
ListNBT playerList = new ListNBT();
|
2018-07-07 11:35:39 +02:00
|
|
|
for (PlayerSave save : this.playerSaveData.values()) {
|
2021-02-26 22:15:48 +01:00
|
|
|
CompoundNBT player = new CompoundNBT();
|
2016-11-11 21:41:07 +01:00
|
|
|
player.setUniqueId("UUID", save.id);
|
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
CompoundNBT data = new CompoundNBT();
|
2016-11-22 19:35:52 +01:00
|
|
|
save.writeToNBT(data, true);
|
2016-11-11 21:41:07 +01:00
|
|
|
player.setTag("Data", data);
|
|
|
|
|
|
|
|
playerList.appendTag(player);
|
2016-06-05 12:52:59 +02:00
|
|
|
}
|
2016-08-13 02:21:50 +02:00
|
|
|
compound.setTag("PlayerData", playerList);
|
2016-12-28 14:55:03 +01:00
|
|
|
|
|
|
|
return compound;
|
|
|
|
}
|
2016-06-04 13:51:06 +02:00
|
|
|
}
|