Skip to content

Commit

Permalink
Add the default implementation of wired networks
Browse files Browse the repository at this point in the history
  • Loading branch information
SquidDev committed Feb 21, 2018
1 parent 4651e36 commit 74f5093
Show file tree
Hide file tree
Showing 9 changed files with 1,326 additions and 1 deletion.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ dependencies {
deobfProvided "mezz.jei:jei_1.12:4.7.5.86:api"
runtime "mezz.jei:jei_1.12:4.7.5.86"
shade 'org.squiddev:Cobalt:0.3.1'

testCompile 'junit:junit:4.11'
}

javadoc {
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/dan200/computercraft/ComputerCraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import dan200.computercraft.api.media.IMedia;
import dan200.computercraft.api.media.IMediaProvider;
import dan200.computercraft.api.network.IPacketNetwork;
import dan200.computercraft.api.network.wired.IWiredElement;
import dan200.computercraft.api.network.wired.IWiredNode;
import dan200.computercraft.api.network.wired.IWiredProvider;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.peripheral.IPeripheralProvider;
import dan200.computercraft.api.permissions.ITurtlePermissionProvider;
Expand Down Expand Up @@ -57,6 +60,7 @@
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
import dan200.computercraft.shared.turtle.upgrades.*;
import dan200.computercraft.shared.util.*;
import dan200.computercraft.shared.wired.WiredNode;
import io.netty.buffer.Unpooled;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
Expand All @@ -69,6 +73,7 @@
import net.minecraft.util.NonNullList;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.config.ConfigCategory;
import net.minecraftforge.common.config.Configuration;
Expand Down Expand Up @@ -259,6 +264,7 @@ public static class Config {
private static List<ITurtlePermissionProvider> permissionProviders = new ArrayList<>();
private static final Map<String, IPocketUpgrade> pocketUpgrades = new HashMap<>();
private static final Set<ILuaAPIFactory> apiFactories = new LinkedHashSet<>();
private static final Set<IWiredProvider> wiredProviders = new LinkedHashSet<>();

// Implementation
@Mod.Instance( value = ComputerCraft.MOD_ID )
Expand Down Expand Up @@ -730,6 +736,16 @@ public static void registerAPIFactory( ILuaAPIFactory provider )
}
}

public static void registerWiredProvider( IWiredProvider provider )
{
if( provider != null ) wiredProviders.add( provider );
}

public static IWiredNode createWiredNodeForElement( IWiredElement element )
{
return new WiredNode( element );
}

public static IPeripheral getPeripheralAt( World world, BlockPos pos, EnumFacing side )
{
// Try the handlers in order:
Expand All @@ -751,6 +767,24 @@ public static IPeripheral getPeripheralAt( World world, BlockPos pos, EnumFacing
return null;
}

public static IWiredElement getWiredElementAt( IBlockAccess world, BlockPos pos, EnumFacing side )
{
// Try the handlers in order:
for( IWiredProvider provider : wiredProviders )
{
try
{
IWiredElement element = provider.getElement( world, pos, side );
if( element != null ) return element;
}
catch( Exception e )
{
ComputerCraft.log.error( "Wired element provider " + provider + " errored.", e );
}
}
return null;
}

public static int getDefaultBundledRedstoneOutput( World world, BlockPos pos, EnumFacing side )
{
if( WorldUtil.isBlockInWorld( world, pos ) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
import dan200.computercraft.shared.turtle.inventory.ContainerTurtle;
import dan200.computercraft.shared.util.*;
import dan200.computercraft.shared.wired.DefaultWiredProvider;
import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
Expand Down Expand Up @@ -291,7 +292,7 @@ public void registerItems( RegistryEvent.Register<Item> event )
// Command Computer
registry.register( new ItemCommandComputer( ComputerCraft.Blocks.commandComputer ).setRegistryName( new ResourceLocation( ComputerCraft.MOD_ID, "command_computer" ) ) );

// Command Computer
// Advanced modem
registry.register( new ItemAdvancedModem( ComputerCraft.Blocks.advancedModem ).setRegistryName( new ResourceLocation( ComputerCraft.MOD_ID, "advanced_modem" ) ) );

// Items
Expand Down Expand Up @@ -482,6 +483,9 @@ private void registerTileEntities()

// Register media providers
ComputerCraftAPI.registerMediaProvider( new DefaultMediaProvider() );

// Register network providers
ComputerCraftAPI.registerWiredProvider( new DefaultWiredProvider() );
}

private void registerForgeHandlers()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dan200.computercraft.shared.wired;

import dan200.computercraft.api.network.wired.IWiredElement;
import dan200.computercraft.api.network.wired.IWiredElementTile;
import dan200.computercraft.api.network.wired.IWiredProvider;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class DefaultWiredProvider implements IWiredProvider
{
@Nullable
@Override
public IWiredElement getElement( @Nonnull IBlockAccess world, @Nonnull BlockPos pos, @Nonnull EnumFacing side )
{
TileEntity te = world.getTileEntity( pos );
return te instanceof IWiredElementTile ? ((IWiredElementTile) te).getWiredElement( side ) : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package dan200.computercraft.shared.wired;

import dan200.computercraft.ComputerCraft;

/**
* Verifies certain elements of a network are "well formed".
*
* This adds substantial overhead to network modification, and so should only be enabled
* in a development environment.
*/
public class InvariantChecker
{
private static final boolean ENABLED = false;

public static void checkNode( WiredNode node )
{
if( !ENABLED ) return;

WiredNetwork network = node.network;
if( network == null )
{
ComputerCraft.log.error( "Node's network is null", new Exception() );
return;
}

if( network.nodes == null || !network.nodes.contains( node ) )
{
ComputerCraft.log.error( "Node's network does not contain node", new Exception() );
}

for( WiredNode neighbour : node.neighbours )
{
if( !neighbour.neighbours.contains( node ) )
{
ComputerCraft.log.error( "Neighbour is missing node", new Exception() );
}
}
}

public static void checkNetwork( WiredNetwork network )
{
if( !ENABLED ) return;

for( WiredNode node : network.nodes ) checkNode( node );
}
}
Loading

0 comments on commit 74f5093

Please sign in to comment.