WIP
This commit is contained in:
@@ -1,15 +1,19 @@
|
||||
package refinedstorage.api.storagenet;
|
||||
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraftforge.event.world.WorldEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.gameevent.TickEvent;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class StorageNetworkEventHandler {
|
||||
@SubscribeEvent
|
||||
public void onWorldTick(TickEvent.WorldTickEvent e) {
|
||||
// @todo: !!!
|
||||
if (e.world.provider.getDimension() == 0) {
|
||||
for (StorageNetwork network : StorageNetworkRegistry.NETWORKS.values()) {
|
||||
Map<BlockPos, StorageNetwork> networks = StorageNetworkRegistry.get(e.world.provider.getDimension());
|
||||
|
||||
if (networks != null) {
|
||||
for (StorageNetwork network : networks.values()) {
|
||||
if (network.getWorld() == null) {
|
||||
network.setWorld(e.world);
|
||||
}
|
||||
|
||||
@@ -6,10 +6,21 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class StorageNetworkRegistry {
|
||||
// @todo: handle multiple dims
|
||||
public static final Map<BlockPos, StorageNetwork> NETWORKS = new HashMap<BlockPos, StorageNetwork>();
|
||||
public static final Map<Integer, Map<BlockPos, StorageNetwork>> NETWORKS = new HashMap<Integer, Map<BlockPos, StorageNetwork>>();
|
||||
|
||||
public static void addStorageNetwork(StorageNetwork network) {
|
||||
NETWORKS.put(network.getPos(), network);
|
||||
public static void add(StorageNetwork network, int dimension) {
|
||||
if (NETWORKS.get(dimension) == null) {
|
||||
NETWORKS.put(dimension, new HashMap<BlockPos, StorageNetwork>());
|
||||
}
|
||||
|
||||
NETWORKS.get(dimension).put(network.getPos(), network);
|
||||
}
|
||||
|
||||
public static StorageNetwork get(BlockPos pos, int dimension) {
|
||||
return get(dimension) == null ? null : get(dimension).get(pos);
|
||||
}
|
||||
|
||||
public static Map<BlockPos, StorageNetwork> get(int dimension) {
|
||||
return NETWORKS.get(dimension);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ import net.minecraft.world.World;
|
||||
import net.minecraft.world.WorldSavedData;
|
||||
import net.minecraftforge.common.util.Constants;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class StorageNetworkSavedData extends WorldSavedData {
|
||||
public static final String NBT_STORAGE_NETWORKS = "StorageNetworks";
|
||||
|
||||
@@ -26,7 +28,7 @@ public class StorageNetworkSavedData extends WorldSavedData {
|
||||
StorageNetwork network = new StorageNetwork(pos);
|
||||
network.readFromNBT(networkTag.getCompoundTag("Data"));
|
||||
|
||||
StorageNetworkRegistry.addStorageNetwork(network);
|
||||
StorageNetworkRegistry.add(network, networkTag.getInteger("Dim"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,13 +36,16 @@ public class StorageNetworkSavedData extends WorldSavedData {
|
||||
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
|
||||
NBTTagList networks = new NBTTagList();
|
||||
|
||||
for (StorageNetwork network : StorageNetworkRegistry.NETWORKS.values()) {
|
||||
NBTTagCompound networkTag = new NBTTagCompound();
|
||||
networkTag.setInteger("X", network.getPos().getX());
|
||||
networkTag.setInteger("Y", network.getPos().getY());
|
||||
networkTag.setInteger("Z", network.getPos().getZ());
|
||||
networkTag.setTag("Data", network.writeToNBT(new NBTTagCompound()));
|
||||
networks.appendTag(networkTag);
|
||||
for (Map.Entry<Integer, Map<BlockPos, StorageNetwork>> entry : StorageNetworkRegistry.NETWORKS.entrySet()) {
|
||||
for (StorageNetwork network : entry.getValue().values()) {
|
||||
NBTTagCompound networkTag = new NBTTagCompound();
|
||||
networkTag.setInteger("X", network.getPos().getX());
|
||||
networkTag.setInteger("Y", network.getPos().getY());
|
||||
networkTag.setInteger("Z", network.getPos().getZ());
|
||||
networkTag.setInteger("Dim", entry.getKey());
|
||||
networkTag.setTag("Data", network.writeToNBT(new NBTTagCompound()));
|
||||
networks.appendTag(networkTag);
|
||||
}
|
||||
}
|
||||
|
||||
tag.setTag(NBT_STORAGE_NETWORKS, networks);
|
||||
|
||||
@@ -100,7 +100,7 @@ public class BlockController extends BlockBase {
|
||||
network.getEnergy().receiveEnergy(tag.getInteger(StorageNetwork.NBT_ENERGY), false);
|
||||
}
|
||||
|
||||
StorageNetworkRegistry.addStorageNetwork(network);
|
||||
StorageNetworkRegistry.add(network, world.provider.getDimension());
|
||||
|
||||
((TileController) world.getTileEntity(pos)).setNetwork(network);
|
||||
}
|
||||
|
||||
@@ -134,7 +134,7 @@ public class ItemWirelessGrid extends ItemEnergyContainer {
|
||||
@Override
|
||||
public ActionResult<ItemStack> onItemRightClick(ItemStack stack, World world, EntityPlayer player, EnumHand hand) {
|
||||
if (!world.isRemote && hasValidNBT(stack) && getDimensionId(stack) == player.dimension) {
|
||||
StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(new BlockPos(getX(stack), getY(stack), getZ(stack)));
|
||||
StorageNetwork network = StorageNetworkRegistry.get(new BlockPos(getX(stack), getY(stack), getZ(stack)), player.worldObj.provider.getDimension());
|
||||
|
||||
if (network != null) {
|
||||
if (network.getWirelessGridHandler().handleOpen(player, hand)) {
|
||||
|
||||
@@ -45,7 +45,7 @@ public class MessageGridCraftingStart extends MessageHandlerPlayerToServer<Messa
|
||||
|
||||
@Override
|
||||
public void handle(MessageGridCraftingStart message, EntityPlayerMP player) {
|
||||
StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(new BlockPos(message.x, message.y, message.z));
|
||||
StorageNetwork network = StorageNetworkRegistry.get(new BlockPos(message.x, message.y, message.z), player.worldObj.provider.getDimension());
|
||||
|
||||
if (network != null && network.canRun()) {
|
||||
network.getStorageHandler().onCraftingRequested(message.id, message.quantity);
|
||||
|
||||
@@ -45,7 +45,7 @@ public class MessageWirelessGridCraftingStart extends MessageHandlerPlayerToServ
|
||||
|
||||
@Override
|
||||
public void handle(MessageWirelessGridCraftingStart message, EntityPlayerMP player) {
|
||||
StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(new BlockPos(message.controllerX, message.controllerY, message.controllerZ));
|
||||
StorageNetwork network = StorageNetworkRegistry.get(new BlockPos(message.controllerX, message.controllerY, message.controllerZ), player.worldObj.provider.getDimension());
|
||||
|
||||
if (network != null && network.canRun()) {
|
||||
network.getStorageHandler().onCraftingRequested(message.id, message.quantity);
|
||||
|
||||
@@ -41,7 +41,7 @@ public class MessageWirelessGridHeldItemPush extends MessageHandlerPlayerToServe
|
||||
|
||||
@Override
|
||||
public void handle(MessageWirelessGridHeldItemPush message, EntityPlayerMP player) {
|
||||
StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(new BlockPos(message.controllerX, message.controllerY, message.controllerZ));
|
||||
StorageNetwork network = StorageNetworkRegistry.get(new BlockPos(message.controllerX, message.controllerY, message.controllerZ), player.worldObj.provider.getDimension());
|
||||
|
||||
if (network != null && network.canRun()) {
|
||||
network.getStorageHandler().onHeldItemPush(message.one, player);
|
||||
|
||||
@@ -45,7 +45,7 @@ public class MessageWirelessGridStoragePull extends MessageHandlerPlayerToServer
|
||||
|
||||
@Override
|
||||
public void handle(MessageWirelessGridStoragePull message, EntityPlayerMP player) {
|
||||
StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(new BlockPos(message.controllerX, message.controllerY, message.controllerZ));
|
||||
StorageNetwork network = StorageNetworkRegistry.get(new BlockPos(message.controllerX, message.controllerY, message.controllerZ), player.worldObj.provider.getDimension());
|
||||
|
||||
if (network != null && network.canRun()) {
|
||||
network.getStorageHandler().onPull(message.id, message.flags, player);
|
||||
|
||||
@@ -83,7 +83,7 @@ public abstract class TileMachine extends TileBase implements ISynchronizedConta
|
||||
}
|
||||
|
||||
private boolean tryConnect(TileController controller) {
|
||||
StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(controller.getPos());
|
||||
StorageNetwork network = StorageNetworkRegistry.get(controller.getPos(), worldObj.provider.getDimension());
|
||||
|
||||
if (!network.canRun()) {
|
||||
return false;
|
||||
|
||||
@@ -63,7 +63,7 @@ public class WirelessGrid implements IGrid {
|
||||
|
||||
@Override
|
||||
public ItemStack onItemPush(EntityPlayer player, ItemStack stack) {
|
||||
StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(controllerPos);
|
||||
StorageNetwork network = StorageNetworkRegistry.get(controllerPos, player.worldObj.provider.getDimension());
|
||||
|
||||
if (network != null && network.canRun()) {
|
||||
network.getWirelessGridHandler().drainEnergy(player, ItemWirelessGrid.USAGE_PUSH);
|
||||
@@ -85,7 +85,7 @@ public class WirelessGrid implements IGrid {
|
||||
}
|
||||
|
||||
public void onClose(EntityPlayer player) {
|
||||
StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(controllerPos);
|
||||
StorageNetwork network = StorageNetworkRegistry.get(controllerPos, player.worldObj.provider.getDimension());
|
||||
|
||||
if (network != null) {
|
||||
network.getWirelessGridHandler().handleClose(player);
|
||||
|
||||
Reference in New Issue
Block a user