From 7fc9008c5849d7061d6dcd449013e91740a7ceb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Wed, 22 Oct 2014 23:14:21 +0200 Subject: [PATCH] Implemented MFR RedNet support. --- build.gradle | 2 + src/main/java/crazypants/enderio/EnderIO.java | 2 +- .../enderio/conduit/BlockConduitBundle.java | 73 +++++++++--- .../conduit/redstone/IRedstoneConduit.java | 3 + .../conduit/redstone/RedstoneConduit.java | 105 +++++++++++++++++- .../api/rednet/IRedNetInfo.java | 28 +++++ .../api/rednet/IRedNetInputNode.java | 52 +++++++++ .../api/rednet/IRedNetLogicCircuit.java | 19 ++++ .../api/rednet/IRedNetLogicPoint.java | 36 ++++++ .../api/rednet/IRedNetNetworkContainer.java | 32 ++++++ .../api/rednet/IRedNetOmniNode.java | 22 ++++ .../api/rednet/IRedNetOutputNode.java | 51 +++++++++ .../connectivity/IRedNetConnection.java | 26 +++++ .../connectivity/IRedNetDecorative.java | 11 ++ .../connectivity/IRedNetNoConnection.java | 13 +++ .../rednet/connectivity/IRedstoneAlike.java | 10 ++ .../connectivity/RedNetConnectionType.java | 86 ++++++++++++++ 17 files changed, 551 insertions(+), 20 deletions(-) create mode 100644 src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetInfo.java create mode 100644 src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetInputNode.java create mode 100644 src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetLogicCircuit.java create mode 100644 src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetLogicPoint.java create mode 100644 src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetNetworkContainer.java create mode 100644 src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetOmniNode.java create mode 100644 src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetOutputNode.java create mode 100644 src/main/java/powercrystals/minefactoryreloaded/api/rednet/connectivity/IRedNetConnection.java create mode 100644 src/main/java/powercrystals/minefactoryreloaded/api/rednet/connectivity/IRedNetDecorative.java create mode 100644 src/main/java/powercrystals/minefactoryreloaded/api/rednet/connectivity/IRedNetNoConnection.java create mode 100644 src/main/java/powercrystals/minefactoryreloaded/api/rednet/connectivity/IRedstoneAlike.java create mode 100644 src/main/java/powercrystals/minefactoryreloaded/api/rednet/connectivity/RedNetConnectionType.java diff --git a/build.gradle b/build.gradle index 84cd7abf..72f2bb84 100644 --- a/build.gradle +++ b/build.gradle @@ -79,6 +79,7 @@ processResources { jar { from { configurations.exportedCompile.collect { it.isDirectory() ? it : zipTree(it) } } + exclude 'powercrystals/**' } task sourceJar(type: Jar) { @@ -94,6 +95,7 @@ task javadocJar(type: Jar, dependsOn: javadoc) { task deobfJar(type: Jar) { from sourceSets.main.output from { configurations.exportedCompile.collect { it.isDirectory() ? it : zipTree(it) } } + exclude 'powercrystals/**' classifier = 'dev' } diff --git a/src/main/java/crazypants/enderio/EnderIO.java b/src/main/java/crazypants/enderio/EnderIO.java index 28d099e3..dfa68f49 100644 --- a/src/main/java/crazypants/enderio/EnderIO.java +++ b/src/main/java/crazypants/enderio/EnderIO.java @@ -147,7 +147,7 @@ import crazypants.enderio.teleport.ItemTravelStaff; import crazypants.enderio.teleport.TeleportRecipes; import crazypants.util.EntityUtil; -@Mod(modid = MODID, name = MOD_NAME, version = VERSION, dependencies = "required-after:Forge@10.13.0.1150,);", guiFactory = "crazypants.enderio.config.ConfigFactoryEIO") +@Mod(modid = MODID, name = MOD_NAME, version = VERSION, dependencies = "required-after:Forge@10.13.0.1150,);after:MineFactoryReloaded", guiFactory = "crazypants.enderio.config.ConfigFactoryEIO") public class EnderIO { public static final String MODID = "EnderIO"; diff --git a/src/main/java/crazypants/enderio/conduit/BlockConduitBundle.java b/src/main/java/crazypants/enderio/conduit/BlockConduitBundle.java index 076d160f..bb0d9669 100644 --- a/src/main/java/crazypants/enderio/conduit/BlockConduitBundle.java +++ b/src/main/java/crazypants/enderio/conduit/BlockConduitBundle.java @@ -26,6 +26,7 @@ import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.util.ForgeDirection; import buildcraft.api.tools.IToolWrench; import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.Optional; import cpw.mods.fml.common.network.IGuiHandler; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @@ -53,8 +54,11 @@ import crazypants.enderio.machine.painter.PainterUtil; import crazypants.enderio.network.PacketHandler; import crazypants.render.BoundingBox; import crazypants.util.Util; +import powercrystals.minefactoryreloaded.api.rednet.IRedNetOmniNode; +import powercrystals.minefactoryreloaded.api.rednet.connectivity.RedNetConnectionType; -public class BlockConduitBundle extends BlockEio implements IGuiHandler, IFacade { +@Optional.Interface(iface = "powercrystals.minefactoryreloaded.api.rednet.IRedNetOmniNode", modid = "MineFactoryReloaded") +public class BlockConduitBundle extends BlockEio implements IGuiHandler, IFacade, IRedNetOmniNode { private static final String KEY_CONNECTOR_ICON = "enderIO:conduitConnector"; @@ -291,12 +295,7 @@ public class BlockConduitBundle extends BlockEio implements IGuiHandler, IFacade @Override public int isProvidingStrongPower(IBlockAccess world, int x, int y, int z, int par5) { - TileEntity te = world.getTileEntity(x, y, z); - if(!(te instanceof IConduitBundle)) { - return 0; - } - IConduitBundle bundle = (IConduitBundle) te; - IRedstoneConduit con = bundle.getConduit(IRedstoneConduit.class); + IRedstoneConduit con = getRedstoneConduit(world, x, y, z); if(con == null) { return 0; } @@ -306,12 +305,7 @@ public class BlockConduitBundle extends BlockEio implements IGuiHandler, IFacade @Override public int isProvidingWeakPower(IBlockAccess world, int x, int y, int z, int par5) { - TileEntity te = world.getTileEntity(x, y, z); - if(!(te instanceof IConduitBundle)) { - return 0; - } - IConduitBundle bundle = (IConduitBundle) te; - IRedstoneConduit con = bundle.getConduit(IRedstoneConduit.class); + IRedstoneConduit con = getRedstoneConduit(world, x, y, z); if(con == null) { return 0; } @@ -839,4 +833,57 @@ public class BlockConduitBundle extends BlockEio implements IGuiHandler, IFacade return res; } + @Override + public void onInputsChanged(World world, int x, int y, int z, ForgeDirection side, int[] inputValues) { + IRedstoneConduit conduit = getRedstoneConduit(world, x, y, z); + if(conduit == null) { + return; + } + + conduit.onInputsChanged(world, x, y, z, side, inputValues); + } + + @Override + public void onInputChanged(World world, int x, int y, int z, ForgeDirection side, int inputValue) { + // Unused because only called in "Single" mode. + } + + @Override + public int[] getOutputValues(World world, int x, int y, int z, ForgeDirection side) { + IRedstoneConduit conduit = getRedstoneConduit(world, x, y, z); + if(conduit == null) { + return null; + } + + return conduit.getOutputValues(world, x, y, z, side); + } + + @Override + public int getOutputValue(World world, int x, int y, int z, ForgeDirection side, int subnet) { + IRedstoneConduit conduit = getRedstoneConduit(world, x, y, z); + if(conduit == null) { + return 0; + } + + return conduit.getOutputValue(world, x, y, z, side, subnet); + } + + @Override + @Optional.Method(modid = "MineFactoryReloaded") + public RedNetConnectionType getConnectionType(World world, int x, int y, int z, ForgeDirection side) { + IRedstoneConduit conduit = getRedstoneConduit(world, x, y, z); + if(conduit == null) { + return RedNetConnectionType.None; + } + return conduit.canConnectToExternal(side, false) ? RedNetConnectionType.CableAll : RedNetConnectionType.None; + } + + private static IRedstoneConduit getRedstoneConduit(IBlockAccess world, int x, int y, int z) { + TileEntity te = world.getTileEntity(x, y, z); + if(!(te instanceof IConduitBundle)) { + return null; + } + IConduitBundle bundle = (IConduitBundle) te; + return bundle.getConduit(IRedstoneConduit.class); + } } diff --git a/src/main/java/crazypants/enderio/conduit/redstone/IRedstoneConduit.java b/src/main/java/crazypants/enderio/conduit/redstone/IRedstoneConduit.java index aaae1bd6..fbf8cbba 100644 --- a/src/main/java/crazypants/enderio/conduit/redstone/IRedstoneConduit.java +++ b/src/main/java/crazypants/enderio/conduit/redstone/IRedstoneConduit.java @@ -30,8 +30,11 @@ public interface IRedstoneConduit extends IConduit { void updateNetwork(); + // MFR RedNet + int[] getOutputValues(World world, int x, int y, int z, ForgeDirection side); int getOutputValue(World world, int x, int y, int z, ForgeDirection side, int subnet); + void onInputsChanged(World world, int x, int y, int z, ForgeDirection side, int[] inputValues); } diff --git a/src/main/java/crazypants/enderio/conduit/redstone/RedstoneConduit.java b/src/main/java/crazypants/enderio/conduit/redstone/RedstoneConduit.java index 6091f88c..e3266e98 100644 --- a/src/main/java/crazypants/enderio/conduit/redstone/RedstoneConduit.java +++ b/src/main/java/crazypants/enderio/conduit/redstone/RedstoneConduit.java @@ -1,8 +1,10 @@ package crazypants.enderio.conduit.redstone; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; @@ -14,6 +16,7 @@ import net.minecraft.util.IIcon; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; +import cpw.mods.fml.common.Loader; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import crazypants.enderio.EnderIO; @@ -25,6 +28,7 @@ import crazypants.enderio.conduit.geom.CollidableComponent; import crazypants.render.IconUtil; import crazypants.util.BlockCoord; import crazypants.util.DyeColor; +import powercrystals.minefactoryreloaded.api.rednet.IRedNetOutputNode; public class RedstoneConduit extends AbstractConduit implements IRedstoneConduit { @@ -52,11 +56,14 @@ public class RedstoneConduit extends AbstractConduit implements IRedstoneConduit protected RedstoneConduitNetwork network; - protected final Set externalSignals = new HashSet(); + protected final List> externalSignals = new ArrayList>(); protected boolean neighbourDirty = true; public RedstoneConduit() { + for (ForgeDirection ignored : ForgeDirection.VALID_DIRECTIONS) { + externalSignals.add(new HashSet()); + } } @Override @@ -119,6 +126,26 @@ public class RedstoneConduit extends AbstractConduit implements IRedstoneConduit Signal signal = new Signal(loc.x, loc.y, loc.z, dir, input - 1, getSignalColor(dir)); res.add(signal); } + + if (Loader.isModLoaded("MineFactoryReloaded")) { + // Add stored RedNet input. See onInputsChanged below for more information. + res.addAll(externalSignals.get(dir.ordinal())); + + // Manually check if neighbors are outputting bundled redstone signals. + // This is required to directly support other blocks implementing the + // RedNet API, without requiring a piece of RedNet cable in-between. + int[] bundledInput = getExternalBundledPowerLevel(dir); + if(bundledInput != null) { + BlockCoord loc = getLocation().getLocation(dir); + for (int subnet = 0; subnet < bundledInput.length; ++subnet) { + if(bundledInput[subnet] > 1) { // force signal strength reduction to avoid cycles + int color = convertColorForRedNet(subnet); + Signal signal = new Signal(loc.x, loc.y, loc.z, dir, bundledInput[subnet] - 1, DyeColor.fromIndex(color)); + res.add(signal); + } + } + } + } } } @@ -185,6 +212,19 @@ public class RedstoneConduit extends AbstractConduit implements IRedstoneConduit return res; } + protected int[] getExternalBundledPowerLevel(ForgeDirection dir) { + World world = getBundle().getEntity().getWorldObj(); + BlockCoord loc = getLocation(); + loc = loc.getLocation(dir); + + Block block = world.getBlock(loc.x, loc.y, loc.z); + if(block instanceof IRedNetOutputNode) { + return ((IRedNetOutputNode) block).getOutputValues(world, loc.x, loc.y, loc.z, dir.getOpposite()); + } + + return null; + } + @Override public int isProvidingStrongPower(ForgeDirection toDirection) { return 0; @@ -223,31 +263,84 @@ public class RedstoneConduit extends AbstractConduit implements IRedstoneConduit @Override public int[] getOutputValues(World world, int x, int y, int z, ForgeDirection side) { int[] result = new int[16]; - Set outs = getNetworkOutputs(side); + + Set outs = network != null ? network.getSignals() : null; if(outs != null) { + BlockCoord loc = getLocation().getLocation(side); for (Signal s : outs) { - result[s.color.ordinal()] = s.strength; + // Avoid "feedback loops", i.e. don't report an output on a side where + // we have an input (otherwise a RedNet cable connected to a conduit + // will keep a signal high, even if the original source vanishes). + // Note that it's still possible to get loops if there are two + // connections between a conduit set and a RedNet network. I'm not + // sure there's anything we could do to avoid this, though, nor am I + // convinced we should. + if(s.dir != side || s.x != loc.x || s.y != loc.y || s.z != loc.z) { + int subnet = convertColorForRedNet(s.color.ordinal()); + result[subnet] = s.strength; + } } } + return result; } @Override public int getOutputValue(World world, int x, int y, int z, ForgeDirection side, int subnet) { - Set outs = getNetworkOutputs(side); + Set outs = network != null ? network.getSignals() : null; if(outs != null) { + BlockCoord loc = getLocation().getLocation(side); + int color = convertColorForRedNet(subnet); for (Signal s : outs) { - if(subnet == s.color.ordinal()) { - return s.strength; + // Avoid "feedback loops", see comment in getOutputValues. + if(s.dir != side || s.x != loc.x || s.y != loc.y || s.z != loc.z) { + if(s.color.ordinal() == color) { + return s.strength; + } } } } + return 0; } + + @Override + public void onInputsChanged(World world, int x, int y, int z, ForgeDirection side, int[] inputValues) { + // Check if anything changed, if so mark neighbor dirty to trigger an + // update in the next tick. We have to iterate over the colors in the + // outer loop to make sure we check all of them, because for channels + // with zero signal strength no signals are stored. + Set inputs = getNetworkInputs(side); + externalSignals.get(side.ordinal()).clear(); + BlockCoord loc = getLocation().getLocation(side); + for (int subnet = 0; subnet < inputValues.length; ++subnet) { + int color = convertColorForRedNet(subnet); + int newInput = inputValues[subnet]; + int oldInput = 0; + for (Signal input : inputs) { + if(input.color.ordinal() == color) { + oldInput = input.strength; + break; + } + } + + neighbourDirty |= oldInput != newInput; + + // Store external inputs to allow regenerating the global list of signals + // in getNetworkInputs. This is required for RedNet cables to work, e.g. + if(newInput > 1) { // force signal strength reduction to avoid cycles + externalSignals.get(side.ordinal()).add(new Signal(loc.x, loc.y, loc.z, side, newInput - 1, DyeColor.fromIndex(color))); + } + } + } @Override public boolean onNeighborChange(IBlockAccess world, int x, int y, int z, int tileX, int tileY, int tileZ) { return false; } + // RedNet refers to colors in inverse order... + private static int convertColorForRedNet(int colorOrSubnet) { + return 15 - colorOrSubnet; + } } diff --git a/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetInfo.java b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetInfo.java new file mode 100644 index 00000000..0fc75c78 --- /dev/null +++ b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetInfo.java @@ -0,0 +1,28 @@ +package powercrystals.minefactoryreloaded.api.rednet; + +import java.util.List; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.util.IChatComponent; +import net.minecraft.world.IBlockAccess; +import net.minecraftforge.common.util.ForgeDirection; + +/** + * Defines a Block that can print information about itself using the RedNet Meter. This must be implemented on your Block class. + */ +public interface IRedNetInfo +{ + /** + * This function appends information to a list provided to it. + * + * @param world Reference to the world. + * @param x X coordinate of the block. + * @param y Y coordinate of the block. + * @param z Z coordinate of the block. + * @param side The side of the block that is being queried. + * @param player Player doing the querying - this can be NULL. + * @param info The list that the information should be appended to. + */ + public void getRedNetInfo(IBlockAccess world, int x, int y, int z, + ForgeDirection side, EntityPlayer player, List info); +} diff --git a/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetInputNode.java b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetInputNode.java new file mode 100644 index 00000000..a814af3c --- /dev/null +++ b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetInputNode.java @@ -0,0 +1,52 @@ +package powercrystals.minefactoryreloaded.api.rednet; + +import powercrystals.minefactoryreloaded.api.rednet.connectivity.IRedNetConnection; + +import net.minecraft.world.World; +import net.minecraftforge.common.util.ForgeDirection; + +/** + * Defines a Block that can connect to RedNet cables. This must be implemented on your Block class. + *

+ * Note that when you implement this, the RedNet network makes several assumptions about your code - + * It will not clamp values to 0 <= x <= 15. This means you must be able to accept any possible integer + * without crashing, even negatives. It will also assume that calling the onInput(s)Changed() methods + * are sufficient, and will not issue block updates. In Single mode, it will call onInputChanged. + *

+ * RedNet cables have their subnets indicated to the user by colored bands on the cable. + * The color of a given subnet is the same as the wool with metadata equal to the subnet number. + *

+ * For reference:
+ * 0:White, 1:Orange, 2:Magenta, 3:LightBlue, 4:Yellow, 5:Lime, 6:Pink, 7:Gray, + * 8:LightGray, 9:Cyan, 10:Purple, 11:Blue, 12:Brown, 13:Green, 14:Red, 15:Black + */ +public interface IRedNetInputNode extends IRedNetConnection +{ + /** + * Called when the input values to this block change. Only called if your block is connected in "All" mode. + * Do not issue a network value update from inside this method call; it will be ignored. Issue your updates + * on the next tick. + * + * @param world The world this block is in. + * @param x This block's X coordinate. + * @param y This block's Y coordinate. + * @param z This block's Z coordinate. + * @param side The side the input values are being changed on. + * @param inputValues The new set of input values. This array will be 16 elements long. Do not alter or cache. + */ + public void onInputsChanged(World world, int x, int y, int z, ForgeDirection side, int[] inputValues); + + /** + * Called when the input value to this block changes. Only called if your block is connected in "Single" mode. + * Do not issue a network value update from inside this method call; it will be ignored. Issue your updates + * on the next tick. + * + * @param world The world this block is in. + * @param x This block's X coordinate. + * @param y This block's Y coordinate. + * @param z This block's Z coordinate. + * @param side The side the input values are being changed on. + * @param inputValue The new input value + */ + public void onInputChanged(World world, int x, int y, int z, ForgeDirection side, int inputValue); +} diff --git a/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetLogicCircuit.java b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetLogicCircuit.java new file mode 100644 index 00000000..981ce06b --- /dev/null +++ b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetLogicCircuit.java @@ -0,0 +1,19 @@ +package powercrystals.minefactoryreloaded.api.rednet; + +import net.minecraft.nbt.NBTTagCompound; + +public interface IRedNetLogicCircuit +{ + public byte getInputCount(); + + public byte getOutputCount(); + + public int[] recalculateOutputValues(long worldTime, int[] inputValues); + + public String getUnlocalizedName(); + public String getInputPinLabel(int pin); + public String getOutputPinLabel(int pin); + + public void readFromNBT(NBTTagCompound tag); + public void writeToNBT(NBTTagCompound tag); +} diff --git a/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetLogicPoint.java b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetLogicPoint.java new file mode 100644 index 00000000..dd05969e --- /dev/null +++ b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetLogicPoint.java @@ -0,0 +1,36 @@ +package powercrystals.minefactoryreloaded.api.rednet; + +/** + * + * @author skyboy + */ +public interface IRedNetLogicPoint +{ + /** + * + * @param out + * @return + */ + public void transformOutput(int[] out); + + /** + * + * @param out + * @return + */ + public void transformOutput(int out); + + /** + * + * @param in + * @return + */ + public int[] transformInput(int[] in); + + /** + * + * @param in + * @return + */ + public int transformInput(int in); +} diff --git a/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetNetworkContainer.java b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetNetworkContainer.java new file mode 100644 index 00000000..c9fb0bf8 --- /dev/null +++ b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetNetworkContainer.java @@ -0,0 +1,32 @@ +package powercrystals.minefactoryreloaded.api.rednet; + +import net.minecraft.world.World; +import net.minecraftforge.common.util.ForgeDirection; + +/** + * + * You should not implement this yourself. Instead, use this to look for cables to notify from your IRedNetOmniNode as this does not + * require a block update. This will be implemented on the cable's Block class. + * + */ +public interface IRedNetNetworkContainer +{ + /** + * Tells the network to recalculate all subnets. + * @param world The world this cable is in. + * @param x The x-coordinate of this cable. + * @param x The y-coordinate of this cable. + * @param x The z-coordinate of this cable. + */ + public void updateNetwork(World world, int x, int y, int z, ForgeDirection from); + + /** + * Tells the network to recalculate a specific subnet. + * @param world The world this cable is in. + * @param x The x-coordinate of this cable. + * @param x The y-coordinate of this cable. + * @param x The z-coordinate of this cable. + * @param subnet The subnet to recalculate. + */ + public void updateNetwork(World world, int x, int y, int z, int subnet, ForgeDirection from); +} diff --git a/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetOmniNode.java b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetOmniNode.java new file mode 100644 index 00000000..f2bdf397 --- /dev/null +++ b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetOmniNode.java @@ -0,0 +1,22 @@ +package powercrystals.minefactoryreloaded.api.rednet; + +/** + * Defines a Block that can connect to RedNet cables. This must be implemented on your Block class. + *

+ * Note that when you implement this, the RedNet network makes several assumptions about your code - + * It will not clamp values to 0 <= x <= 15. This means you must be able to accept any possible integer + * without crashing, even negatives. It will also assume that calling the onInput(s)Changed() methods + * are sufficient, and will not issue block updates. It will never call the vanilla redstone output + * methods, and will only query the methods contained in this interface. + *

+ * RedNet cables have their subnets indicated to the user by colored bands on the cable. + * The color of a given subnet is the same as the wool with metadata equal to the subnet number. + *

+ * For reference:
+ * 0:White, 1:Orange, 2:Magenta, 3:LightBlue, 4:Yellow, 5:Lime, 6:Pink, 7:Gray, + * 8:LightGray, 9:Cyan, 10:Purple, 11:Blue, 12:Brown, 13:Green, 14:Red, 15:Black + */ +public interface IRedNetOmniNode extends IRedNetInputNode, IRedNetOutputNode +{ + // this is merely provided for convenience +} diff --git a/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetOutputNode.java b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetOutputNode.java new file mode 100644 index 00000000..34281875 --- /dev/null +++ b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/IRedNetOutputNode.java @@ -0,0 +1,51 @@ +package powercrystals.minefactoryreloaded.api.rednet; + +import net.minecraft.world.World; +import net.minecraftforge.common.util.ForgeDirection; + +import powercrystals.minefactoryreloaded.api.rednet.connectivity.IRedNetConnection; + +/** + * Defines a Block that can connect to RedNet cables. This must be implemented on your Block class. + *

+ * Note that when you implement this, the RedNet network makes several assumptions about your code - + * It will never call the vanilla redstone output methods, querying only the methods contained in + * this interface, and will not issue block updates. + *

+ * RedNet cables have their subnets indicated to the user by colored bands on the cable. + * The color of a given subnet is the same as the wool with metadata equal to the subnet number. + *

+ * For reference:
+ * 0:White, 1:Orange, 2:Magenta, 3:LightBlue, 4:Yellow, 5:Lime, 6:Pink, 7:Gray, + * 8:LightGray, 9:Cyan, 10:Purple, 11:Blue, 12:Brown, 13:Green, 14:Red, 15:Black + */ +public interface IRedNetOutputNode extends IRedNetConnection +{ + /** + * Returns the output values of this RedNet node. + * This array must be 16 elements long. + * Only called if your block is connected in "All" mode. + * + * @param world The world this block is in. + * @param x This block's X coordinate. + * @param y This block's Y coordinate. + * @param z This block's Z coordinate. + * @param side The side the output values are required for. + * @return The output values. + */ + public int[] getOutputValues(World world, int x, int y, int z, ForgeDirection side); + + /** + * Returns the output value of this RedNet node for a given subnet. + * Must be the same as getOutputValues(world, x, y, z, side)[subnet]. + * + * @param world The world this block is in. + * @param x This block's X coordinate. + * @param y This block's Y coordinate. + * @param z This block's Z coordinate. + * @param side The side the output value is required for. + * @param subnet The subnet to get the output value for (0-15). + * @return The output value. + */ + public int getOutputValue(World world, int x, int y, int z, ForgeDirection side, int subnet); +} diff --git a/src/main/java/powercrystals/minefactoryreloaded/api/rednet/connectivity/IRedNetConnection.java b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/connectivity/IRedNetConnection.java new file mode 100644 index 00000000..003800d8 --- /dev/null +++ b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/connectivity/IRedNetConnection.java @@ -0,0 +1,26 @@ +package powercrystals.minefactoryreloaded.api.rednet.connectivity; + +import net.minecraft.world.World; +import net.minecraftforge.common.util.ForgeDirection; + +/** + * Defines a Block that can connect to RedNet cables. This must be implemented on your Block class. + */ +public interface IRedNetConnection +{ + /** + * Returns the connection type of this Block. If this value must be changed + * while the block is alive, it must notify neighbors of a change. + *

+ * For nodes that want to interact with rednet, + * see IRedNetInputNode, IRedNetOutputNode, and IRedNetOmniNode + * + * @param world The world this block is in. + * @param x This block's X coordinate. + * @param y This block's Y coordinate. + * @param z This block's Z coordinate. + * @param side The side that connection information is required for. + * @return The connection type. + */ + public RedNetConnectionType getConnectionType(World world, int x, int y, int z, ForgeDirection side); +} diff --git a/src/main/java/powercrystals/minefactoryreloaded/api/rednet/connectivity/IRedNetDecorative.java b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/connectivity/IRedNetDecorative.java new file mode 100644 index 00000000..89de12b1 --- /dev/null +++ b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/connectivity/IRedNetDecorative.java @@ -0,0 +1,11 @@ +package powercrystals.minefactoryreloaded.api.rednet.connectivity; + +/** + * This must be implemented on your Block class. + *

+ * RedNet cables will treat your block similar to a vanilla block that's not redstone active. + */ +public interface IRedNetDecorative +{ + +} diff --git a/src/main/java/powercrystals/minefactoryreloaded/api/rednet/connectivity/IRedNetNoConnection.java b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/connectivity/IRedNetNoConnection.java new file mode 100644 index 00000000..16225a74 --- /dev/null +++ b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/connectivity/IRedNetNoConnection.java @@ -0,0 +1,13 @@ +package powercrystals.minefactoryreloaded.api.rednet.connectivity; + +/** + * This must be implemented on your Block class. + *

+ * RedNet cables will not connect to your block. + *
+ * This behavior can be overridden in subclasses by IRedNetConnection + */ +public interface IRedNetNoConnection +{ + +} diff --git a/src/main/java/powercrystals/minefactoryreloaded/api/rednet/connectivity/IRedstoneAlike.java b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/connectivity/IRedstoneAlike.java new file mode 100644 index 00000000..f3784cfb --- /dev/null +++ b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/connectivity/IRedstoneAlike.java @@ -0,0 +1,10 @@ +package powercrystals.minefactoryreloaded.api.rednet.connectivity; + +/** + * This must be implemented on your Block class. + *

+ * RedNet cables will treat your block similar to a redstone dust, and subtract one from the power value. + */ +public interface IRedstoneAlike { + +} diff --git a/src/main/java/powercrystals/minefactoryreloaded/api/rednet/connectivity/RedNetConnectionType.java b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/connectivity/RedNetConnectionType.java new file mode 100644 index 00000000..08ba24c1 --- /dev/null +++ b/src/main/java/powercrystals/minefactoryreloaded/api/rednet/connectivity/RedNetConnectionType.java @@ -0,0 +1,86 @@ +package powercrystals.minefactoryreloaded.api.rednet.connectivity; + +import java.util.HashMap; +import java.util.Map; + +/** + * Defines how RedNet cable connects to a block + *

+ * None: RedNet will never connect to this block (if this is all you want: use IRedNetNoConnection) + *

+ * CableSingle: Connections will use the cable renderer with a single band, best used for whole blocks + *
+ * PlateSingle: Connections will use the plate renderer with a single band, used for conveyers and rails + *

+ * CableAll: Connections permit access to all 16 bands + *
+ * PlateAll: Connections permit access to all 16 bands + *

+ * Forced connection modes are best used for decoration blocks: RedNet will not connect normally, + * but will if the user forces it. Typically, IRedNetDecorative is desired for this instead + *

+ * ForcedCableSingle: Connections permit access to a single band, only when the cable is in forced connection mode + *
+ * ForcedPlateSingle: Connections permit access to a single band, only when the cable is in forced connection mode + *

+ * ForcedCableAll: Connections permit access to all 16 bands, only when the cable is in forced connection mode + *
+ * ForcedPlateAll: Connections permit access to all 16 bands, only when the cable is in forced connection mode + *

+ * The decorative nodes are for when you want rednet to decide how to connect to your block, + * but also need to receive full updates from the network. + *

+ * DecorativeSingle: Connections permit access to a single band, using standard connection logic + *
+ * DecorativeAll: Connections permit access to all 16 bands, using standard connection logic + *
+ * ForcedDecorativeSingle: Connections permit access to a single band, only when the cable is in forced connection mode + *
+ * ForcedDecorativeAll: Connections permit access to all 16 bands, only when the cable is in forced connection mode + */ +public enum RedNetConnectionType +{ + None, // 0; 0000000 + CableSingle, // 11; 0001011 + PlateSingle, // 13; 0001101 + CableAll, // 19; 0010011 + PlateAll, // 21; 0010101 + ForcedCableSingle, // 43; 0101011 + ForcedPlateSingle, // 45; 0101101 + ForcedCableAll, // 51; 0110011 + ForcedPlateAll, // 53; 0110101 + DecorativeSingle, // NA; 0001001 + DecorativeAll, // NA; 0010001 + ForcedDecorativeSingle, // NA; 0101001 + ForcedDecorativeAll; // NA; 0110001 + + public final boolean isConnected = this.ordinal() != 0; // 0 bit (mask: 1) + public final boolean isSingleSubnet = this.name().endsWith("Single"); // 3 bit (mask: 8) + public final boolean isAllSubnets = this.name().endsWith("All"); // 4 bit (mask: 16) + public final boolean isPlate = this.name().contains("Plate"); // 2 bit (mask: 4) + public final boolean isCable = this.name().contains("Cable"); // 1 bit (mask: 2) + public final boolean isConnectionForced = this.name().startsWith("Forced"); // 5 bit (mask: 32) + public final boolean isDecorative = this.name().contains("Decorative"); + public final short flags = toFlags(isConnected, isCable, isPlate, + isSingleSubnet, isAllSubnets, isConnectionForced); + + public static final RedNetConnectionType fromFlags(short flags) + { + return connections.get(flags); + } + + private static final short toFlags(boolean ...flags) + { + short ret = 0; + for (int i = flags.length; i --> 0;) + ret |= (flags[i] ? 1 : 0) << i; + return ret; + } + + private static final Map connections = new HashMap(); + + static { + for (RedNetConnectionType type : RedNetConnectionType.values()) + connections.put(type.flags, type); + } +}