From 0d45083c434d9b9444952f9f80099aba379feff2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Mon, 28 Mar 2016 00:18:34 +0200 Subject: [PATCH] Added overload to add world position to a network (still goes through the TE). Mainly for more comfortable delayed adding when previous TE at that location may now be invalid/replaced. --- src/main/java/li/cil/oc/api/Network.java | 17 ++++++++++++-- .../java/li/cil/oc/api/detail/NetworkAPI.java | 14 ++++++++++-- .../li/cil/oc/server/network/Network.scala | 22 +++++++++++++++---- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/main/java/li/cil/oc/api/Network.java b/src/main/java/li/cil/oc/api/Network.java index a159f39e8..dc921f411 100644 --- a/src/main/java/li/cil/oc/api/Network.java +++ b/src/main/java/li/cil/oc/api/Network.java @@ -8,6 +8,8 @@ import li.cil.oc.api.network.Visibility; import li.cil.oc.api.network.WirelessEndpoint; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.BlockPos; +import net.minecraft.world.IBlockAccess; /** * This class provides factories for networks and nodes. @@ -29,8 +31,7 @@ import net.minecraft.tileentity.TileEntity; */ public final class Network { /** - * Tries to add a tile entity's network node(s) at the specified coordinates - * to adjacent networks. + * Convenience overload for {@link #joinOrCreateNetwork(IBlockAccess, BlockPos)}. *

* If the tile entity implements {@link Environment} its one node will be * connected to any existing adjacent tile entity nodes. If none exist a @@ -48,6 +49,18 @@ public final class Network { API.network.joinOrCreateNetwork(tileEntity); } + /** + * Tries to add network node(s) at the specified coordinates to adjacent + * networks. + * + * @param world the world containing the location to connect. + * @param pos the position at which to update the network. + */ + public static void joinOrCreateNetwork(final IBlockAccess world, final BlockPos pos) { + if (API.network != null) + API.network.joinOrCreateNetwork(world, pos); + } + /** * Creates a new network with the specified node as its initial node. *

diff --git a/src/main/java/li/cil/oc/api/detail/NetworkAPI.java b/src/main/java/li/cil/oc/api/detail/NetworkAPI.java index 444a12927..ab63f2d69 100644 --- a/src/main/java/li/cil/oc/api/detail/NetworkAPI.java +++ b/src/main/java/li/cil/oc/api/detail/NetworkAPI.java @@ -7,11 +7,12 @@ import li.cil.oc.api.network.Visibility; import li.cil.oc.api.network.WirelessEndpoint; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.BlockPos; +import net.minecraft.world.IBlockAccess; public interface NetworkAPI { /** - * Tries to add a tile entity's network node(s) at the specified coordinates - * to adjacent networks. + * Convenience overload for {@link #joinOrCreateNetwork(IBlockAccess, BlockPos)}. *

* If the tile entity implements {@link Environment} its one node will be * connected to any existing adjacent tile entity nodes. If none exist a @@ -26,6 +27,15 @@ public interface NetworkAPI { */ void joinOrCreateNetwork(TileEntity tileEntity); + /** + * Tries to add network node(s) at the specified coordinates to adjacent + * networks. + * + * @param world the world containing the location to connect. + * @param pos the position at which to update the network. + */ + void joinOrCreateNetwork(IBlockAccess world, BlockPos pos); + /** * Creates a new network with the specified node as its initial node. *

diff --git a/src/main/scala/li/cil/oc/server/network/Network.scala b/src/main/scala/li/cil/oc/server/network/Network.scala index baf22c53c..a9afe7d1c 100644 --- a/src/main/scala/li/cil/oc/server/network/Network.scala +++ b/src/main/scala/li/cil/oc/server/network/Network.scala @@ -15,6 +15,8 @@ import net.minecraft.item.EnumDyeColor import net.minecraft.nbt._ import net.minecraft.tileentity.TileEntity import net.minecraft.util.EnumFacing +import net.minecraft.util.BlockPos +import net.minecraft.world.IBlockAccess import scala.collection.JavaConverters._ import scala.collection.mutable @@ -421,8 +423,9 @@ private class Network private(private val data: mutable.Map[String, Network.Vert } object Network extends api.detail.NetworkAPI { - override def joinOrCreateNetwork(tileEntity: TileEntity): Unit = - if (!tileEntity.isInvalid && tileEntity.getWorld != null && !tileEntity.getWorld.isRemote) { + override def joinOrCreateNetwork(world: IBlockAccess, pos: BlockPos): Unit = { + val tileEntity = world.getTileEntity(pos) + if (tileEntity != null && !tileEntity.isInvalid && tileEntity.getWorld != null && !tileEntity.getWorld.isRemote) { for (side <- EnumFacing.values) { val npos = tileEntity.getPos.offset(side) if (tileEntity.getWorld.isBlockLoaded(npos)) { @@ -447,6 +450,17 @@ object Network extends api.detail.NetworkAPI { } } } + } + + override def joinOrCreateNetwork(tileEntity: TileEntity): Unit = { + if (tileEntity != null) { + val world = tileEntity.getWorld + val pos = tileEntity.getPos + if (world != null && pos != null) { + joinOrCreateNetwork(world, pos) + } + } + } def joinNewNetwork(node: ImmutableNode): Unit = node match { case mutableNode: MutableNode if mutableNode.network == null => @@ -472,8 +486,8 @@ object Network extends api.detail.NetworkAPI { private def getConnectionColor(tileEntity: TileEntity): Int = { if (tileEntity != null) { - if (tileEntity.hasCapability(Capabilities.ColoredCapability, EnumFacing.DOWN)) { - val colored = tileEntity.getCapability(Capabilities.ColoredCapability, EnumFacing.DOWN) + if (tileEntity.hasCapability(Capabilities.ColoredCapability, null)) { + val colored = tileEntity.getCapability(Capabilities.ColoredCapability, null) if (colored != null && colored.controlsConnectivity) return colored.getColor } }