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

108 lines
3.6 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;
2018-05-10 11:38:58 +02:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.data.PlayerData.PlayerSave;
import de.ellpeck.actuallyadditions.mod.misc.apiimpl.LaserRelayConnectionHandler;
import io.netty.util.internal.ConcurrentSet;
2021-02-26 22:15:48 +01:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
2017-06-17 00:48:49 +02:00
import net.minecraft.world.storage.WorldSavedData;
2021-02-26 22:15:48 +01:00
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
// TODO: [port] validate this still works
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";
public static final String SAVE_NAME = ActuallyAdditions.MODID + "_worldsave";
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<>();
public WorldData() {
super(SAVE_NAME);
}
public static WorldData get(World world) {
WorldData storage = ((ServerWorld) world).getDataStorage().get(WorldData::new, SAVE_NAME);
2017-09-13 18:50:12 +02:00
if (storage == null) {
storage = new WorldData();
storage.setDirty();
((ServerWorld) world).getDataStorage().set(storage);
}
return storage;
}
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
@Override
public void load(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);
UUID id = player.getUUID("UUID");
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
public CompoundNBT save(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) {
networkList.add(LaserRelayConnectionHandler.writeNetworkToNBT(network));
2016-06-05 12:52:59 +02:00
}
compound.put("Networks", networkList);
2016-06-05 12:52:59 +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();
player.putUUID("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);
player.put("Data", data);
playerList.add(player);
2016-06-05 12:52:59 +02:00
}
compound.put("PlayerData", playerList);
return compound;
}
}