From 1428350c67decca72e8fb7f1932f308d770ece0e Mon Sep 17 00:00:00 2001 From: raoulvdberge Date: Sat, 13 May 2017 15:16:47 +0200 Subject: [PATCH] Remove the map and use the world saved data, #986 --- .../refinedstorage/apiimpl/API.java | 17 +++++-- .../apiimpl/network/NetworkNodeManager.java | 46 ++++++++----------- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/API.java b/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/API.java index e894b762f..612b2ca93 100755 --- a/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/API.java +++ b/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/API.java @@ -36,13 +36,12 @@ import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; +import net.minecraft.world.storage.MapStorage; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fml.common.discovery.ASMDataTable; import javax.annotation.Nonnull; import java.lang.reflect.Field; -import java.util.HashMap; -import java.util.Map; import java.util.Set; public class API implements IRSAPI { @@ -50,7 +49,6 @@ public class API implements IRSAPI { private IComparer comparer = new Comparer(); private INetworkNodeRegistry networkNodeRegistry = new NetworkNodeRegistry(); - private Map networkNodeManagers = new HashMap<>(); private IStorageDiskBehavior storageDiskBehavior = new StorageDiskBehavior(); private ISoldererRegistry soldererRegistry = new SoldererRegistry(); private ICraftingTaskRegistry craftingTaskRegistry = new CraftingTaskRegistry(); @@ -98,7 +96,18 @@ public class API implements IRSAPI { throw new IllegalStateException("Attempting to access network node manager on the client"); } - return networkNodeManagers.computeIfAbsent(world.provider.getDimension(), m -> NetworkNodeManager.getManager(world)); + MapStorage storage = world.getPerWorldStorage(); + NetworkNodeManager instance = (NetworkNodeManager) storage.getOrLoadData(NetworkNodeManager.class, NetworkNodeManager.NAME); + + if (instance == null) { + System.out.println("[RS DEBUG] Initializing Network Node Manager for " + world.provider.getDimension()); + + instance = new NetworkNodeManager(NetworkNodeManager.NAME); + + storage.setData(NetworkNodeManager.NAME, instance); + } + + return instance; } @Override diff --git a/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/network/NetworkNodeManager.java b/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/network/NetworkNodeManager.java index 4a28da04e..5467034f3 100644 --- a/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/network/NetworkNodeManager.java +++ b/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/network/NetworkNodeManager.java @@ -6,9 +6,7 @@ import com.raoulvdberge.refinedstorage.apiimpl.API; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.util.math.BlockPos; -import net.minecraft.world.World; import net.minecraft.world.WorldSavedData; -import net.minecraft.world.storage.MapStorage; import net.minecraftforge.common.util.Constants; import javax.annotation.Nullable; @@ -18,7 +16,7 @@ import java.util.Map; import java.util.function.Function; public class NetworkNodeManager extends WorldSavedData implements INetworkNodeManager { - private static final String NAME = "refinedstorage_nodes"; + public static final String NAME = "refinedstorage_nodes"; private static final String NBT_NODES = "Nodes"; private static final String NBT_NODE_ID = "Id"; @@ -37,20 +35,28 @@ public class NetworkNodeManager extends WorldSavedData implements INetworkNodeMa clear(); - NBTTagList list = tag.getTagList(NBT_NODES, Constants.NBT.TAG_COMPOUND); + if (tag.hasKey(NBT_NODES)) { + NBTTagList list = tag.getTagList(NBT_NODES, Constants.NBT.TAG_COMPOUND); - for (int i = 0; i < list.tagCount(); ++i) { - NBTTagCompound nodeTag = list.getCompoundTagAt(i); + int nodesRead = 0; - String id = nodeTag.getString(NBT_NODE_ID); - NBTTagCompound data = nodeTag.getCompoundTag(NBT_NODE_DATA); - BlockPos pos = BlockPos.fromLong(nodeTag.getLong(NBT_NODE_POS)); + for (int i = 0; i < list.tagCount(); ++i) { + NBTTagCompound nodeTag = list.getCompoundTagAt(i); - Function factory = API.instance().getNetworkNodeRegistry().get(id); + String id = nodeTag.getString(NBT_NODE_ID); + NBTTagCompound data = nodeTag.getCompoundTag(NBT_NODE_DATA); + BlockPos pos = BlockPos.fromLong(nodeTag.getLong(NBT_NODE_POS)); - if (factory != null) { - setNode(pos, factory.apply(data)); + Function factory = API.instance().getNetworkNodeRegistry().get(id); + + if (factory != null) { + setNode(pos, factory.apply(data)); + + ++nodesRead; + } } + + System.out.println("[RS DEBUG] Read " + nodesRead + " nodes!"); } } @@ -75,22 +81,6 @@ public class NetworkNodeManager extends WorldSavedData implements INetworkNodeMa return tag; } - public static NetworkNodeManager getManager(World world) { - MapStorage storage = world.getPerWorldStorage(); - NetworkNodeManager instance = (NetworkNodeManager) storage.getOrLoadData(NetworkNodeManager.class, NAME); - - if (instance == null) { - System.out.println("[RS DEBUG] Initializing Network Node Manager for " + world.provider.getDimension()); - instance = new NetworkNodeManager(NAME); - - storage.setData(NAME, instance); - } else { - System.out.println("[RS DEBUG] Network Node Manager for " + world.provider.getDimension() + " already exists, OK..."); - } - - return instance; - } - @Nullable @Override public INetworkNode getNode(BlockPos pos) {