From ca34b2a1b8326b4458a43c8b1a6de5b49cf5150c Mon Sep 17 00:00:00 2001 From: SquidDev Date: Tue, 2 Apr 2019 21:37:19 +0100 Subject: [PATCH 01/10] Update the dependency info a little --- README.md | 2 +- build.gradle | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ee3c63844..ab2cb561f 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ dependencies { } dependencies { - implementation "org.squiddev:cc-tweaked:${mc_version}-${cct_version}" + implementation "org.squiddev:cc-tweaked-${mc_version}:${cct_version}" } ``` diff --git a/build.gradle b/build.gradle index ccf90db0f..51516b88b 100644 --- a/build.gradle +++ b/build.gradle @@ -230,7 +230,7 @@ curseforge { project { id = '282001' releaseType = 'release' - changelog = "Release notes can be found on the GitHub repository (https://github.com/SquidDev-CC/CC-Tweaked/releases/tag/v${project.version})." + changelog = "Release notes can be found on the GitHub repository (https://github.com/SquidDev-CC/CC-Tweaked/releases/tag/v${mc_version}-${mod_version})." } } From 0e191e42a0d3e25c5e0fdbaa248559254856eb9d Mon Sep 17 00:00:00 2001 From: "Wilma456 (Jakob0815)" Date: Wed, 3 Apr 2019 15:58:04 +0200 Subject: [PATCH 02/10] Update de_de.lang (#166) --- src/main/resources/assets/computercraft/lang/de_de.lang | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/resources/assets/computercraft/lang/de_de.lang b/src/main/resources/assets/computercraft/lang/de_de.lang index 6e01501d2..7460a071c 100644 --- a/src/main/resources/assets/computercraft/lang/de_de.lang +++ b/src/main/resources/assets/computercraft/lang/de_de.lang @@ -1,3 +1,5 @@ +itemGroup.computercraft=CC: Tweaked + tile.computercraft:computer.name=Computer tile.computercraft:advanced_computer.name=Erweiterter Computer tile.computercraft:drive.name=Diskettenlaufwerk @@ -158,7 +160,10 @@ gui.computercraft:config.default_computer_settings=Computer-Standardeinstellunge gui.computercraft:config.debug_enabled=Debug-Library aktivieren gui.computercraft:config.log_computer_errors=Computerfehler loggen +gui.computercraft:config.execution=Ausführung gui.computercraft:config.execution.computer_threads=Computer Threads +gui.computercraft:config.execution.max_main_global_time=Globales Servertick Zeitlimit +gui.computercraft:config.execution.max_main_computer_time=Computer Servertick Zeitlimit gui.computercraft:config.http=HTTP gui.computercraft:config.http.enabled=HTTP-API aktivieren From 765c31315a460f70913d8e78a00de5094ad46b4c Mon Sep 17 00:00:00 2001 From: SquidDev Date: Sat, 6 Apr 2019 22:42:03 +0100 Subject: [PATCH 03/10] Make redstone updates identical to how MC does it Also fixes rather embarassing bug with redstone not updating. --- .../shared/util/RedstoneUtil.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main/java/dan200/computercraft/shared/util/RedstoneUtil.java b/src/main/java/dan200/computercraft/shared/util/RedstoneUtil.java index 85e5c6717..7f30c8db1 100644 --- a/src/main/java/dan200/computercraft/shared/util/RedstoneUtil.java +++ b/src/main/java/dan200/computercraft/shared/util/RedstoneUtil.java @@ -11,6 +11,9 @@ import net.minecraft.block.state.IBlockState; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; +import net.minecraftforge.event.ForgeEventFactory; + +import java.util.EnumSet; public final class RedstoneUtil { @@ -30,17 +33,12 @@ public final class RedstoneUtil public static void propagateRedstoneOutput( World world, BlockPos pos, EnumFacing side ) { - // Propagate ordinary output + // Propagate ordinary output. See BlockRedstoneDiode.notifyNeighbors IBlockState block = world.getBlockState( pos ); + if( ForgeEventFactory.onNeighborNotify( world, pos, block, EnumSet.of( side ), false ).isCanceled() ) return; + BlockPos neighbourPos = pos.offset( side ); - IBlockState neighbour = world.getBlockState( neighbourPos ); - if( neighbour.getBlock().isAir( neighbour, world, neighbourPos ) ) - { - world.neighborChanged( neighbourPos, block.getBlock(), pos ); - if( neighbour.getBlock().isNormalCube( neighbour, world, neighbourPos ) ) - { - world.notifyNeighborsOfStateExcept( neighbourPos, block.getBlock(), side.getOpposite() ); - } - } + world.neighborChanged( neighbourPos, block.getBlock(), pos ); + world.notifyNeighborsOfStateExcept( neighbourPos, block.getBlock(), side.getOpposite() ); } } From 4569af2130393ce2eea23aae697898db13e8a949 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Sat, 6 Apr 2019 22:42:44 +0100 Subject: [PATCH 04/10] Bump version --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 0c76c812c..b86e88cf1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ # Mod properties -mod_version=1.82.0 +mod_version=1.82.1 # Minecraft properties mc_version=1.12.2 From 6be330ae8d0a907e01ac0266db1fafd7916d1324 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Sun, 7 Apr 2019 14:52:05 +0100 Subject: [PATCH 05/10] Expose Computer.getRootMount again It's a little evil, but we need it for CCEmuX. There's other ways of achieving this, but not with supporting CC and CC:T. --- .../java/dan200/computercraft/core/computer/Computer.java | 7 +++++++ .../computercraft/core/computer/ComputerExecutor.java | 8 ++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/dan200/computercraft/core/computer/Computer.java b/src/main/java/dan200/computercraft/core/computer/Computer.java index fc1c9609c..f0f41df39 100644 --- a/src/main/java/dan200/computercraft/core/computer/Computer.java +++ b/src/main/java/dan200/computercraft/core/computer/Computer.java @@ -7,6 +7,7 @@ package dan200.computercraft.core.computer; import com.google.common.base.Objects; +import dan200.computercraft.api.filesystem.IWritableMount; import dan200.computercraft.api.lua.ILuaAPI; import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IWorkMonitor; @@ -244,6 +245,12 @@ public class Computer tick(); } + @Deprecated + public IWritableMount getRootMount() + { + return executor.getRootMount(); + } + @Deprecated public static final String[] s_sideNames = IAPIEnvironment.SIDE_NAMES; } diff --git a/src/main/java/dan200/computercraft/core/computer/ComputerExecutor.java b/src/main/java/dan200/computercraft/core/computer/ComputerExecutor.java index 9fe3d5f48..78350d411 100644 --- a/src/main/java/dan200/computercraft/core/computer/ComputerExecutor.java +++ b/src/main/java/dan200/computercraft/core/computer/ComputerExecutor.java @@ -338,7 +338,7 @@ final class ComputerExecutor } } - private FileSystem createFileSystem() + IWritableMount getRootMount() { if( rootMount == null ) { @@ -347,11 +347,15 @@ final class ComputerExecutor computer.getComputerEnvironment().getComputerSpaceLimit() ); } + return rootMount; + } + private FileSystem createFileSystem() + { FileSystem filesystem = null; try { - filesystem = new FileSystem( "hdd", rootMount ); + filesystem = new FileSystem( "hdd", getRootMount() ); IMount romMount = getRomMount(); if( romMount == null ) From 4205f18f0c39b0e87dcbf6a9aa503f10dd1bb0f5 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Sun, 7 Apr 2019 15:30:27 +0100 Subject: [PATCH 06/10] Publish jar files to GH too I'm not sure if I'll keep this, as it's a little redundant. We're on an older version of the plugin, as 2.2.7 seems to fail for me. Closes #165. --- build.gradle | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/build.gradle b/build.gradle index 51516b88b..e41dc1861 100644 --- a/build.gradle +++ b/build.gradle @@ -18,6 +18,7 @@ buildscript { plugins { id 'com.matthewprenger.cursegradle' version '1.0.10' + id "com.github.breadmoirai.github-release" version "2.2.4" } apply plugin: 'net.minecraftforge.gradle.forge' @@ -287,6 +288,20 @@ uploadArchives { } } +githubRelease { + token project.hasProperty('githubApiKey') ? project.githubApiKey : '' + owner 'SquidDev-CC' + repo 'CC-Tweaked' + targetCommitish (mc_version == "1.12.2" ? "master" : mc_version) + + tagName "v${mc_version}-${mod_version}" + releaseName "[${mc_version}] ${mod_version}" + body '' + prerelease false + + releaseAssets.from(jar.archivePath) +} + test { useJUnitPlatform() testLogging { From f3ce44042f5e4ebd847b40cb06b7b2fd315cf645 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Mon, 8 Apr 2019 14:52:24 +0100 Subject: [PATCH 07/10] Update all inputs if the block is not a neighbour Fixes #172, and acts as a workaround for MrTJP/ProjectRed#1472. --- .../computer/blocks/TileComputerBase.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java index 70545de15..a02538ac0 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java @@ -309,19 +309,21 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT if( getWorld() == null || getWorld().isRemote ) return; ServerComputer computer = getServerComputer(); - if( computer != null ) + if( computer == null ) return; + + BlockPos pos = computer.getPosition(); + for( EnumFacing dir : EnumFacing.VALUES ) { - BlockPos pos = computer.getPosition(); - for( EnumFacing dir : EnumFacing.VALUES ) + BlockPos offset = pos.offset( dir ); + if( offset.equals( neighbour ) ) { - BlockPos offset = pos.offset( dir ); - if( offset.equals( neighbour ) ) - { - updateSideInput( computer, dir, offset ); - break; - } + updateSideInput( computer, dir, offset ); + return; } } + + // If the position is not any adjacent one, update all inputs. + updateInput(); } public void updateOutput() From 34602ec4be9e801f0eb6addefdb4c937ff56c912 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Mon, 8 Apr 2019 14:54:57 +0100 Subject: [PATCH 08/10] Move null checks from Preconditions to Objects --- .../java/dan200/computercraft/core/apis/ApiFactories.java | 4 ++-- .../java/dan200/computercraft/core/apis/ComputerAccess.java | 3 +-- .../java/dan200/computercraft/shared/BundledRedstone.java | 4 ++-- .../java/dan200/computercraft/shared/TurtlePermissions.java | 4 ++-- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/dan200/computercraft/core/apis/ApiFactories.java b/src/main/java/dan200/computercraft/core/apis/ApiFactories.java index 54b55cc86..6db2eecc8 100644 --- a/src/main/java/dan200/computercraft/core/apis/ApiFactories.java +++ b/src/main/java/dan200/computercraft/core/apis/ApiFactories.java @@ -6,13 +6,13 @@ package dan200.computercraft.core.apis; -import com.google.common.base.Preconditions; import dan200.computercraft.api.lua.ILuaAPIFactory; import javax.annotation.Nonnull; import java.util.Collection; import java.util.Collections; import java.util.LinkedHashSet; +import java.util.Objects; public final class ApiFactories { @@ -25,7 +25,7 @@ public final class ApiFactories public static void register( @Nonnull ILuaAPIFactory factory ) { - Preconditions.checkNotNull( factory, "provider cannot be null" ); + Objects.requireNonNull( factory, "provider cannot be null" ); factories.add( factory ); } diff --git a/src/main/java/dan200/computercraft/core/apis/ComputerAccess.java b/src/main/java/dan200/computercraft/core/apis/ComputerAccess.java index 693a4ede4..52341b92b 100644 --- a/src/main/java/dan200/computercraft/core/apis/ComputerAccess.java +++ b/src/main/java/dan200/computercraft/core/apis/ComputerAccess.java @@ -6,7 +6,6 @@ package dan200.computercraft.core.apis; -import com.google.common.base.Preconditions; import dan200.computercraft.api.filesystem.IMount; import dan200.computercraft.api.filesystem.IWritableMount; import dan200.computercraft.api.peripheral.IComputerAccess; @@ -121,7 +120,7 @@ public abstract class ComputerAccess implements IComputerAccess @Override public void queueEvent( @Nonnull final String event, final Object[] arguments ) { - Preconditions.checkNotNull( event, "event cannot be null" ); + Objects.requireNonNull( event, "event cannot be null" ); m_environment.queueEvent( event, arguments ); } diff --git a/src/main/java/dan200/computercraft/shared/BundledRedstone.java b/src/main/java/dan200/computercraft/shared/BundledRedstone.java index 1cf8dbdeb..ea390a6f6 100644 --- a/src/main/java/dan200/computercraft/shared/BundledRedstone.java +++ b/src/main/java/dan200/computercraft/shared/BundledRedstone.java @@ -6,7 +6,6 @@ package dan200.computercraft.shared; -import com.google.common.base.Preconditions; import dan200.computercraft.ComputerCraft; import dan200.computercraft.api.redstone.IBundledRedstoneProvider; import dan200.computercraft.shared.common.DefaultBundledRedstoneProvider; @@ -16,6 +15,7 @@ import net.minecraft.world.World; import javax.annotation.Nonnull; import java.util.LinkedHashSet; +import java.util.Objects; import java.util.Set; public final class BundledRedstone @@ -26,7 +26,7 @@ public final class BundledRedstone public static void register( @Nonnull IBundledRedstoneProvider provider ) { - Preconditions.checkNotNull( provider, "provider cannot be null" ); + Objects.requireNonNull( provider, "provider cannot be null" ); providers.add( provider ); } diff --git a/src/main/java/dan200/computercraft/shared/TurtlePermissions.java b/src/main/java/dan200/computercraft/shared/TurtlePermissions.java index 14629fac0..ec9cf4f0e 100644 --- a/src/main/java/dan200/computercraft/shared/TurtlePermissions.java +++ b/src/main/java/dan200/computercraft/shared/TurtlePermissions.java @@ -6,7 +6,6 @@ package dan200.computercraft.shared; -import com.google.common.base.Preconditions; import dan200.computercraft.ComputerCraft; import dan200.computercraft.api.permissions.ITurtlePermissionProvider; import dan200.computercraft.api.turtle.event.TurtleActionEvent; @@ -20,6 +19,7 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import javax.annotation.Nonnull; import java.util.Collection; import java.util.LinkedHashSet; +import java.util.Objects; @Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID ) public final class TurtlePermissions @@ -32,7 +32,7 @@ public final class TurtlePermissions public static void register( @Nonnull ITurtlePermissionProvider upgrade ) { - Preconditions.checkNotNull( upgrade, "upgrade cannot be null" ); + Objects.requireNonNull( upgrade, "upgrade cannot be null" ); providers.add( upgrade ); } From 63dc0daa096d0d76521abd7e74784e4eeb1d54d1 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Tue, 9 Apr 2019 10:02:54 +0100 Subject: [PATCH 09/10] Convert computer sides to an enum Previously we just relied on magic int values, which was confusing and potentially error-prone. We could use EnumFacing, but that's a) dependent on MC and b) incorrect, as we're referring to local coordinates. --- .../core/apis/IAPIEnvironment.java | 23 +++--- .../core/apis/PeripheralAPI.java | 75 ++++++------------ .../computercraft/core/apis/RedstoneAPI.java | 75 ++++++------------ .../computercraft/core/computer/Computer.java | 6 +- .../core/computer/ComputerSide.java | 56 +++++++++++++ .../core/computer/ComputerSystem.java | 8 +- .../core/computer/Environment.java | 79 ++++++++++--------- .../shared/command/CommandComputerCraft.java | 8 +- .../shared/computer/blocks/TileComputer.java | 12 +-- .../computer/blocks/TileComputerBase.java | 31 +++----- .../shared/computer/core/ServerComputer.java | 21 ++++- .../pocket/core/PocketServerComputer.java | 5 +- .../pocket/items/ItemPocketComputer.java | 5 +- .../shared/turtle/blocks/TileTurtle.java | 11 +-- .../shared/turtle/core/TurtleBrain.java | 7 +- .../shared/util/DirectionUtil.java | 36 ++------- 16 files changed, 228 insertions(+), 230 deletions(-) create mode 100644 src/main/java/dan200/computercraft/core/computer/ComputerSide.java diff --git a/src/main/java/dan200/computercraft/core/apis/IAPIEnvironment.java b/src/main/java/dan200/computercraft/core/apis/IAPIEnvironment.java index 4cb3670a9..92ef98d32 100644 --- a/src/main/java/dan200/computercraft/core/apis/IAPIEnvironment.java +++ b/src/main/java/dan200/computercraft/core/apis/IAPIEnvironment.java @@ -8,6 +8,7 @@ package dan200.computercraft.core.apis; import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IWorkMonitor; +import dan200.computercraft.core.computer.ComputerSide; import dan200.computercraft.core.computer.IComputerEnvironment; import dan200.computercraft.core.filesystem.FileSystem; import dan200.computercraft.core.terminal.Terminal; @@ -18,16 +19,10 @@ import javax.annotation.Nullable; public interface IAPIEnvironment { - String[] SIDE_NAMES = new String[] { - "bottom", "top", "back", "front", "right", "left", - }; - - int SIDE_COUNT = 6; - @FunctionalInterface interface IPeripheralChangeListener { - void onPeripheralChanged( int side, @Nullable IPeripheral newPeripheral ); + void onPeripheralChanged( ComputerSide side, @Nullable IPeripheral newPeripheral ); } int getComputerID(); @@ -49,22 +44,22 @@ public interface IAPIEnvironment void queueEvent( String event, Object[] args ); - void setOutput( int side, int output ); + void setOutput( ComputerSide side, int output ); - int getOutput( int side ); + int getOutput( ComputerSide side ); - int getInput( int side ); + int getInput( ComputerSide side ); - void setBundledOutput( int side, int output ); + void setBundledOutput( ComputerSide side, int output ); - int getBundledOutput( int side ); + int getBundledOutput( ComputerSide side ); - int getBundledInput( int side ); + int getBundledInput( ComputerSide side ); void setPeripheralChangeListener( @Nullable IPeripheralChangeListener listener ); @Nullable - IPeripheral getPeripheral( int side ); + IPeripheral getPeripheral( ComputerSide side ); String getLabel(); diff --git a/src/main/java/dan200/computercraft/core/apis/PeripheralAPI.java b/src/main/java/dan200/computercraft/core/apis/PeripheralAPI.java index 6066cc7b0..eb77fe12b 100644 --- a/src/main/java/dan200/computercraft/core/apis/PeripheralAPI.java +++ b/src/main/java/dan200/computercraft/core/apis/PeripheralAPI.java @@ -12,6 +12,7 @@ import dan200.computercraft.api.lua.ILuaAPI; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.peripheral.IPeripheral; +import dan200.computercraft.core.computer.ComputerSide; import dan200.computercraft.core.tracking.TrackingField; import javax.annotation.Nonnull; @@ -245,32 +246,33 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange // IPeripheralChangeListener @Override - public void onPeripheralChanged( int side, IPeripheral newPeripheral ) + public void onPeripheralChanged( ComputerSide side, IPeripheral newPeripheral ) { synchronized( m_peripherals ) { - if( m_peripherals[side] != null ) + int index = side.ordinal(); + if( m_peripherals[index] != null ) { // Queue a detachment - final PeripheralWrapper wrapper = m_peripherals[side]; + final PeripheralWrapper wrapper = m_peripherals[index]; if( wrapper.isAttached() ) wrapper.detach(); // Queue a detachment event - m_environment.queueEvent( "peripheral_detach", new Object[] { IAPIEnvironment.SIDE_NAMES[side] } ); + m_environment.queueEvent( "peripheral_detach", new Object[] { side.getName() } ); } // Assign the new peripheral - m_peripherals[side] = newPeripheral == null ? null - : new PeripheralWrapper( newPeripheral, IAPIEnvironment.SIDE_NAMES[side] ); + m_peripherals[index] = newPeripheral == null ? null + : new PeripheralWrapper( newPeripheral, side.getName() ); - if( m_peripherals[side] != null ) + if( m_peripherals[index] != null ) { // Queue an attachment - final PeripheralWrapper wrapper = m_peripherals[side]; + final PeripheralWrapper wrapper = m_peripherals[index]; if( m_running && !wrapper.isAttached() ) wrapper.attach(); // Queue an attachment event - m_environment.queueEvent( "peripheral", new Object[] { IAPIEnvironment.SIDE_NAMES[side] } ); + m_environment.queueEvent( "peripheral", new Object[] { side.getName() } ); } } } @@ -337,16 +339,13 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange { // isPresent boolean present = false; - int side = parseSide( args ); - if( side >= 0 ) + ComputerSide side = ComputerSide.valueOfInsensitive( getString( args, 0 ) ); + if( side != null ) { synchronized( m_peripherals ) { - PeripheralWrapper p = m_peripherals[side]; - if( p != null ) - { - present = true; - } + PeripheralWrapper p = m_peripherals[side.ordinal()]; + if( p != null ) present = true; } } return new Object[] { present }; @@ -354,21 +353,14 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange case 1: { // getType - String type = null; - int side = parseSide( args ); - if( side >= 0 ) + ComputerSide side = ComputerSide.valueOfInsensitive( getString( args, 0 ) ); + if( side != null ) { + String type = null; synchronized( m_peripherals ) { - PeripheralWrapper p = m_peripherals[side]; - if( p != null ) - { - type = p.getType(); - } - } - if( type != null ) - { - return new Object[] { type }; + PeripheralWrapper p = m_peripherals[side.ordinal()]; + if( p != null ) return new Object[] { p.getType() }; } } return null; @@ -377,12 +369,12 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange { // getMethods String[] methods = null; - int side = parseSide( args ); - if( side >= 0 ) + ComputerSide side = ComputerSide.valueOfInsensitive( getString( args, 0 ) ); + if( side != null ) { synchronized( m_peripherals ) { - PeripheralWrapper p = m_peripherals[side]; + PeripheralWrapper p = m_peripherals[side.ordinal()]; if( p != null ) { methods = p.getMethods(); @@ -403,16 +395,16 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange case 3: { // call - int side = parseSide( args ); + ComputerSide side = ComputerSide.valueOfInsensitive( getString( args, 0 ) ); String methodName = getString( args, 1 ); Object[] methodArgs = Arrays.copyOfRange( args, 2, args.length ); - if( side >= 0 ) + if( side != null ) { PeripheralWrapper p; synchronized( m_peripherals ) { - p = m_peripherals[side]; + p = m_peripherals[side.ordinal()]; } if( p != null ) { @@ -425,19 +417,4 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange return null; } } - - // Privates - - private int parseSide( Object[] args ) throws LuaException - { - String side = getString( args, 0 ); - for( int n = 0; n < IAPIEnvironment.SIDE_NAMES.length; n++ ) - { - if( side.equals( IAPIEnvironment.SIDE_NAMES[n] ) ) - { - return n; - } - } - return -1; - } } diff --git a/src/main/java/dan200/computercraft/core/apis/RedstoneAPI.java b/src/main/java/dan200/computercraft/core/apis/RedstoneAPI.java index 2ce68a58c..d36ba9186 100644 --- a/src/main/java/dan200/computercraft/core/apis/RedstoneAPI.java +++ b/src/main/java/dan200/computercraft/core/apis/RedstoneAPI.java @@ -9,6 +9,7 @@ package dan200.computercraft.core.apis; import dan200.computercraft.api.lua.ILuaAPI; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.core.computer.ComputerSide; import javax.annotation.Nonnull; import java.util.HashMap; @@ -64,56 +65,40 @@ public class RedstoneAPI implements ILuaAPI { // getSides Map table = new HashMap<>(); - for( int i = 0; i < IAPIEnvironment.SIDE_NAMES.length; i++ ) + for( int i = 0; i < ComputerSide.NAMES.length; i++ ) { - table.put( i + 1, IAPIEnvironment.SIDE_NAMES[i] ); + table.put( i + 1, ComputerSide.NAMES[i] ); } return new Object[] { table }; } case 1: { // setOutput - int side = parseSide( args ); + ComputerSide side = parseSide( args ); boolean output = getBoolean( args, 1 ); m_environment.setOutput( side, output ? 15 : 0 ); return null; } - case 2: - { - // getOutput - int side = parseSide( args ); - return new Object[] { m_environment.getOutput( side ) > 0 }; - } - case 3: - { - // getInput - int side = parseSide( args ); - return new Object[] { m_environment.getInput( side ) > 0 }; - } + case 2: // getOutput + return new Object[] { m_environment.getOutput( parseSide( args ) ) > 0 }; + case 3: // getInput + return new Object[] { m_environment.getInput( parseSide( args ) ) > 0 }; case 4: { // setBundledOutput - int side = parseSide( args ); + ComputerSide side = parseSide( args ); int output = getInt( args, 1 ); m_environment.setBundledOutput( side, output ); return null; } - case 5: - { - // getBundledOutput - int side = parseSide( args ); - return new Object[] { m_environment.getBundledOutput( side ) }; - } - case 6: - { - // getBundledInput - int side = parseSide( args ); - return new Object[] { m_environment.getBundledInput( side ) }; - } + case 5: // getBundledOutput + return new Object[] { m_environment.getBundledOutput( parseSide( args ) ) }; + case 6: // getBundledInput + return new Object[] { m_environment.getBundledInput( parseSide( args ) ) }; case 7: { // testBundledInput - int side = parseSide( args ); + ComputerSide side = parseSide( args ); int mask = getInt( args, 1 ); int input = m_environment.getBundledInput( side ); return new Object[] { (input & mask) == mask }; @@ -122,7 +107,7 @@ public class RedstoneAPI implements ILuaAPI case 9: { // setAnalogOutput/setAnalogueOutput - int side = parseSide( args ); + ComputerSide side = parseSide( args ); int output = getInt( args, 1 ); if( output < 0 || output > 15 ) { @@ -132,34 +117,20 @@ public class RedstoneAPI implements ILuaAPI return null; } case 10: - case 11: - { - // getAnalogOutput/getAnalogueOutput - int side = parseSide( args ); - return new Object[] { m_environment.getOutput( side ) }; - } + case 11: // getAnalogOutput/getAnalogueOutput + return new Object[] { m_environment.getOutput( parseSide( args ) ) }; case 12: - case 13: - { - // getAnalogInput/getAnalogueInput - int side = parseSide( args ); - return new Object[] { m_environment.getInput( side ) }; - } + case 13: // getAnalogInput/getAnalogueInput + return new Object[] { m_environment.getInput( parseSide( args ) ) }; default: return null; } } - private static int parseSide( Object[] args ) throws LuaException + private static ComputerSide parseSide( Object[] args ) throws LuaException { - String side = getString( args, 0 ); - for( int n = 0; n < IAPIEnvironment.SIDE_NAMES.length; n++ ) - { - if( side.equals( IAPIEnvironment.SIDE_NAMES[n] ) ) - { - return n; - } - } - throw new LuaException( "Invalid side." ); + ComputerSide side = ComputerSide.valueOfInsensitive( getString( args, 0 ) ); + if( side == null ) throw new LuaException( "Invalid side." ); + return side; } } diff --git a/src/main/java/dan200/computercraft/core/computer/Computer.java b/src/main/java/dan200/computercraft/core/computer/Computer.java index f0f41df39..e1ee67083 100644 --- a/src/main/java/dan200/computercraft/core/computer/Computer.java +++ b/src/main/java/dan200/computercraft/core/computer/Computer.java @@ -223,13 +223,13 @@ public class Computer @Deprecated public IPeripheral getPeripheral( int side ) { - return internalEnvironment.getPeripheral( side ); + return internalEnvironment.getPeripheral( ComputerSide.valueOf( side ) ); } @Deprecated public void setPeripheral( int side, IPeripheral peripheral ) { - internalEnvironment.setPeripheral( side, peripheral ); + internalEnvironment.setPeripheral( ComputerSide.valueOf( side ), peripheral ); } @Deprecated @@ -252,5 +252,5 @@ public class Computer } @Deprecated - public static final String[] s_sideNames = IAPIEnvironment.SIDE_NAMES; + public static final String[] s_sideNames = ComputerSide.NAMES; } diff --git a/src/main/java/dan200/computercraft/core/computer/ComputerSide.java b/src/main/java/dan200/computercraft/core/computer/ComputerSide.java new file mode 100644 index 000000000..d2937fd3b --- /dev/null +++ b/src/main/java/dan200/computercraft/core/computer/ComputerSide.java @@ -0,0 +1,56 @@ +/* + * This file is part of ComputerCraft - http://www.computercraft.info + * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission. + * Send enquiries to dratcliffe@gmail.com + */ + +package dan200.computercraft.core.computer; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +/** + * A side on a computer. Unlike {@link net.minecraft.util.EnumFacing}, this is relative to the direction the computer is + * facing.. + */ +public enum ComputerSide +{ + BOTTOM( "bottom" ), + TOP( "top" ), + BACK( "back" ), + FRONT( "front" ), + RIGHT( "right" ), + LEFT( "left" ); + + public static final String[] NAMES = new String[] { "bottom", "top", "back", "front", "right", "left" }; + + public static final int COUNT = 6; + + private static final ComputerSide[] VALUES = values(); + + private final String name; + + ComputerSide( String name ) {this.name = name;} + + @Nonnull + public static ComputerSide valueOf( int side ) + { + return VALUES[side]; + } + + @Nullable + public static ComputerSide valueOfInsensitive( @Nonnull String name ) + { + for( ComputerSide side : VALUES ) + { + if( side.name.equalsIgnoreCase( name ) ) return side; + } + + return null; + } + + public String getName() + { + return name; + } +} diff --git a/src/main/java/dan200/computercraft/core/computer/ComputerSystem.java b/src/main/java/dan200/computercraft/core/computer/ComputerSystem.java index e6c39ec83..94eead340 100644 --- a/src/main/java/dan200/computercraft/core/computer/ComputerSystem.java +++ b/src/main/java/dan200/computercraft/core/computer/ComputerSystem.java @@ -26,12 +26,12 @@ import javax.annotation.Nullable; */ public class ComputerSystem extends ComputerAccess implements IComputerSystem { - private final IAPIEnvironment m_environment; + private final IAPIEnvironment environment; ComputerSystem( IAPIEnvironment environment ) { super( environment ); - this.m_environment = environment; + this.environment = environment; } @Nonnull @@ -45,7 +45,7 @@ public class ComputerSystem extends ComputerAccess implements IComputerSystem @Override public IFileSystem getFileSystem() { - FileSystem fs = m_environment.getFileSystem(); + FileSystem fs = environment.getFileSystem(); return fs == null ? null : fs.getMountWrapper(); } @@ -53,6 +53,6 @@ public class ComputerSystem extends ComputerAccess implements IComputerSystem @Override public String getLabel() { - return m_environment.getLabel(); + return environment.getLabel(); } } diff --git a/src/main/java/dan200/computercraft/core/computer/Environment.java b/src/main/java/dan200/computercraft/core/computer/Environment.java index e1612dd6d..b23ad377e 100644 --- a/src/main/java/dan200/computercraft/core/computer/Environment.java +++ b/src/main/java/dan200/computercraft/core/computer/Environment.java @@ -41,17 +41,17 @@ public final class Environment implements IAPIEnvironment private final Computer computer; private boolean internalOutputChanged = false; - private final int[] internalOutput = new int[SIDE_COUNT]; - private final int[] internalBundledOutput = new int[SIDE_COUNT]; + private final int[] internalOutput = new int[ComputerSide.COUNT]; + private final int[] internalBundledOutput = new int[ComputerSide.COUNT]; - private final int[] externalOutput = new int[SIDE_COUNT]; - private final int[] externalBundledOutput = new int[SIDE_COUNT]; + private final int[] externalOutput = new int[ComputerSide.COUNT]; + private final int[] externalBundledOutput = new int[ComputerSide.COUNT]; private boolean inputChanged = false; - private final int[] input = new int[SIDE_COUNT]; - private final int[] bundledInput = new int[SIDE_COUNT]; + private final int[] input = new int[ComputerSide.COUNT]; + private final int[] bundledInput = new int[ComputerSide.COUNT]; - private final IPeripheral[] peripherals = new IPeripheral[SIDE_COUNT]; + private final IPeripheral[] peripherals = new IPeripheral[ComputerSide.COUNT]; private IPeripheralChangeListener peripheralListener = null; Environment( Computer computer ) @@ -111,85 +111,89 @@ public final class Environment implements IAPIEnvironment } @Override - public int getInput( int side ) + public int getInput( ComputerSide side ) { - return input[side]; + return input[side.ordinal()]; } @Override - public int getBundledInput( int side ) + public int getBundledInput( ComputerSide side ) { - return bundledInput[side]; + return bundledInput[side.ordinal()]; } @Override - public void setOutput( int side, int output ) + public void setOutput( ComputerSide side, int output ) { + int index = side.ordinal(); synchronized( internalOutput ) { - if( internalOutput[side] != output ) + if( internalOutput[index] != output ) { - internalOutput[side] = output; + internalOutput[index] = output; internalOutputChanged = true; } } } @Override - public int getOutput( int side ) + public int getOutput( ComputerSide side ) { synchronized( internalOutput ) { - return computer.isOn() ? internalOutput[side] : 0; + return computer.isOn() ? internalOutput[side.ordinal()] : 0; } } @Override - public void setBundledOutput( int side, int output ) + public void setBundledOutput( ComputerSide side, int output ) { + int index = side.ordinal(); synchronized( internalOutput ) { - if( internalBundledOutput[side] != output ) + if( internalBundledOutput[index] != output ) { - internalBundledOutput[side] = output; + internalBundledOutput[index] = output; internalOutputChanged = true; } } } @Override - public int getBundledOutput( int side ) + public int getBundledOutput( ComputerSide side ) { synchronized( internalOutput ) { - return computer.isOn() ? internalBundledOutput[side] : 0; + return computer.isOn() ? internalBundledOutput[side.ordinal()] : 0; } } - public int getExternalRedstoneOutput( int side ) + public int getExternalRedstoneOutput( ComputerSide side ) { - return computer.isOn() ? externalOutput[side] : 0; + return computer.isOn() ? externalOutput[side.ordinal()] : 0; } - public int getExternalBundledRedstoneOutput( int side ) + public int getExternalBundledRedstoneOutput( ComputerSide side ) { - return computer.isOn() ? externalBundledOutput[side] : 0; + return computer.isOn() ? externalBundledOutput[side.ordinal()] : 0; } - public void setRedstoneInput( int side, int level ) + public void setRedstoneInput( ComputerSide side, int level ) { - if( input[side] != level ) + int index = side.ordinal(); + if( input[index] != level ) { - input[side] = level; + input[index] = level; inputChanged = true; } } - public void setBundledRedstoneInput( int side, int combination ) + public void setBundledRedstoneInput( ComputerSide side, int combination ) { - if( bundledInput[side] != combination ) + int index = side.ordinal(); + if( bundledInput[index] != combination ) { - bundledInput[side] = combination; + bundledInput[index] = combination; inputChanged = true; } } @@ -222,7 +226,7 @@ public final class Environment implements IAPIEnvironment boolean changed = false; - for( int i = 0; i < SIDE_COUNT; i++ ) + for( int i = 0; i < ComputerSide.COUNT; i++ ) { if( externalOutput[i] != internalOutput[i] ) { @@ -255,24 +259,25 @@ public final class Environment implements IAPIEnvironment } @Override - public IPeripheral getPeripheral( int side ) + public IPeripheral getPeripheral( ComputerSide side ) { synchronized( peripherals ) { - return peripherals[side]; + return peripherals[side.ordinal()]; } } - public void setPeripheral( int side, IPeripheral peripheral ) + public void setPeripheral( ComputerSide side, IPeripheral peripheral ) { synchronized( peripherals ) { - IPeripheral existing = peripherals[side]; + int index = side.ordinal(); + IPeripheral existing = peripherals[index]; if( (existing == null && peripheral != null) || (existing != null && peripheral == null) || (existing != null && !existing.equals( peripheral )) ) { - peripherals[side] = peripheral; + peripherals[index] = peripheral; if( peripheralListener != null ) peripheralListener.onPeripheralChanged( side, peripheral ); } } diff --git a/src/main/java/dan200/computercraft/shared/command/CommandComputerCraft.java b/src/main/java/dan200/computercraft/shared/command/CommandComputerCraft.java index 9ead7a0af..7a6be3151 100644 --- a/src/main/java/dan200/computercraft/shared/command/CommandComputerCraft.java +++ b/src/main/java/dan200/computercraft/shared/command/CommandComputerCraft.java @@ -9,8 +9,8 @@ package dan200.computercraft.shared.command; import com.google.common.collect.Sets; import dan200.computercraft.ComputerCraft; import dan200.computercraft.api.peripheral.IPeripheral; -import dan200.computercraft.core.apis.IAPIEnvironment; import dan200.computercraft.core.computer.Computer; +import dan200.computercraft.core.computer.ComputerSide; import dan200.computercraft.core.tracking.ComputerTracker; import dan200.computercraft.core.tracking.Tracking; import dan200.computercraft.core.tracking.TrackingContext; @@ -115,12 +115,12 @@ public final class CommandComputerCraft extends CommandDelegate table.row( header( "Position" ), linkPosition( context, computer ) ); table.row( header( "Family" ), text( computer.getFamily().toString() ) ); - for( int i = 0; i < 6; i++ ) + for( ComputerSide side : ComputerSide.values() ) { - IPeripheral peripheral = computer.getPeripheral( i ); + IPeripheral peripheral = computer.getPeripheral( side ); if( peripheral != null ) { - table.row( header( "Peripheral " + IAPIEnvironment.SIDE_NAMES[i] ), text( peripheral.getType() ) ); + table.row( header( "Peripheral " + side.getName() ), text( peripheral.getType() ) ); } } diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputer.java b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputer.java index e19f265ca..210e15098 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputer.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputer.java @@ -7,6 +7,7 @@ package dan200.computercraft.shared.computer.blocks; import dan200.computercraft.ComputerCraft; +import dan200.computercraft.core.computer.ComputerSide; import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.core.ServerComputer; import dan200.computercraft.shared.network.Containers; @@ -112,13 +113,14 @@ public class TileComputer extends TileComputerBase updateInput(); } - // For legacy reasons, computers invert the meaning of "left" and "right" - private static final int[] s_remapSide = new int[] { 0, 1, 2, 3, 5, 4 }; - @Override - protected int remapLocalSide( int localSide ) + protected ComputerSide remapLocalSide( ComputerSide localSide ) { - return s_remapSide[localSide]; + // For legacy reasons, computers invert the meaning of "left" and "right". A computer's front is facing + // towards you, but a turtle's front is facing the other way. + if( localSide == ComputerSide.RIGHT ) return ComputerSide.LEFT; + if( localSide == ComputerSide.LEFT ) return ComputerSide.RIGHT; + return localSide; } public ComputerState getState() diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java index a02538ac0..91913ae72 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java @@ -9,6 +9,7 @@ package dan200.computercraft.shared.computer.blocks; import dan200.computercraft.ComputerCraft; import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheralTile; +import dan200.computercraft.core.computer.ComputerSide; import dan200.computercraft.shared.BundledRedstone; import dan200.computercraft.shared.Peripherals; import dan200.computercraft.shared.common.IDirectionalTile; @@ -124,15 +125,15 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT public boolean getRedstoneConnectivity( EnumFacing side ) { if( side == null ) return false; - int localDir = remapLocalSide( DirectionUtil.toLocal( this, side.getOpposite() ) ); + ComputerSide localDir = remapLocalSide( DirectionUtil.toLocal( this, side.getOpposite() ) ); return !isRedstoneBlockedOnSide( localDir ); } @Override public int getRedstoneOutput( EnumFacing side ) { - int localDir = remapLocalSide( DirectionUtil.toLocal( this, side ) ); - if( !isRedstoneBlockedOnSide( localDir ) && getWorld() != null && !getWorld().isRemote ) + ComputerSide localDir = remapLocalSide( DirectionUtil.toLocal( this, side ) ); + if( !isRedstoneBlockedOnSide( localDir ) && world != null && !world.isRemote ) { ServerComputer computer = getServerComputer(); if( computer != null ) return computer.getRedstoneOutput( localDir ); @@ -143,24 +144,18 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT @Override public boolean getBundledRedstoneConnectivity( @Nonnull EnumFacing side ) { - int localDir = remapLocalSide( DirectionUtil.toLocal( this, side ) ); + ComputerSide localDir = remapLocalSide( DirectionUtil.toLocal( this, side ) ); return !isRedstoneBlockedOnSide( localDir ); } @Override public int getBundledRedstoneOutput( @Nonnull EnumFacing side ) { - int localDir = remapLocalSide( DirectionUtil.toLocal( this, side ) ); - if( !isRedstoneBlockedOnSide( localDir ) ) + ComputerSide localDir = remapLocalSide( DirectionUtil.toLocal( this, side ) ); + if( !isRedstoneBlockedOnSide( localDir ) && !world.isRemote ) { - if( !getWorld().isRemote ) - { - ServerComputer computer = getServerComputer(); - if( computer != null ) - { - return computer.getBundledRedstoneOutput( localDir ); - } - } + ServerComputer computer = getServerComputer(); + if( computer != null ) return computer.getBundledRedstoneOutput( localDir ); } return 0; } @@ -259,17 +254,17 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT m_on = m_startOn; } - protected boolean isPeripheralBlockedOnSide( int localSide ) + protected boolean isPeripheralBlockedOnSide( ComputerSide localSide ) { return false; } - protected boolean isRedstoneBlockedOnSide( int localSide ) + protected boolean isRedstoneBlockedOnSide( ComputerSide localSide ) { return false; } - protected int remapLocalSide( int localSide ) + protected ComputerSide remapLocalSide( ComputerSide localSide ) { return localSide; } @@ -277,7 +272,7 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT private void updateSideInput( ServerComputer computer, EnumFacing dir, BlockPos offset ) { EnumFacing offsetSide = dir.getOpposite(); - int localDir = remapLocalSide( DirectionUtil.toLocal( this, dir ) ); + ComputerSide localDir = remapLocalSide( DirectionUtil.toLocal( this, dir ) ); if( !isRedstoneBlockedOnSide( localDir ) ) { computer.setRedstoneInput( localDir, getWorld().getRedstonePower( offset, dir ) ); diff --git a/src/main/java/dan200/computercraft/shared/computer/core/ServerComputer.java b/src/main/java/dan200/computercraft/shared/computer/core/ServerComputer.java index 1a7cab3b6..49060ef92 100644 --- a/src/main/java/dan200/computercraft/shared/computer/core/ServerComputer.java +++ b/src/main/java/dan200/computercraft/shared/computer/core/ServerComputer.java @@ -14,6 +14,7 @@ import dan200.computercraft.api.lua.ILuaAPI; import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.core.apis.IAPIEnvironment; import dan200.computercraft.core.computer.Computer; +import dan200.computercraft.core.computer.ComputerSide; import dan200.computercraft.core.computer.IComputerEnvironment; import dan200.computercraft.shared.common.ServerTerminal; import dan200.computercraft.shared.network.NetworkHandler; @@ -272,22 +273,22 @@ public class ServerComputer extends ServerTerminal implements IComputer, IComput m_computer.queueEvent( event, arguments ); } - public int getRedstoneOutput( int side ) + public int getRedstoneOutput( ComputerSide side ) { return m_computer.getEnvironment().getExternalRedstoneOutput( side ); } - public void setRedstoneInput( int side, int level ) + public void setRedstoneInput( ComputerSide side, int level ) { m_computer.getEnvironment().setRedstoneInput( side, level ); } - public int getBundledRedstoneOutput( int side ) + public int getBundledRedstoneOutput( ComputerSide side ) { return m_computer.getEnvironment().getExternalBundledRedstoneOutput( side ); } - public void setBundledRedstoneInput( int side, int combination ) + public void setBundledRedstoneInput( ComputerSide side, int combination ) { m_computer.getEnvironment().setBundledRedstoneInput( side, combination ); } @@ -303,12 +304,24 @@ public class ServerComputer extends ServerTerminal implements IComputer, IComput m_computer.addAPI( api ); } + @Deprecated public void setPeripheral( int side, IPeripheral peripheral ) + { + setPeripheral( ComputerSide.valueOf( side ), peripheral ); + } + + public void setPeripheral( ComputerSide side, IPeripheral peripheral ) { m_computer.getEnvironment().setPeripheral( side, peripheral ); } + @Deprecated public IPeripheral getPeripheral( int side ) + { + return getPeripheral( ComputerSide.valueOf( side ) ); + } + + public IPeripheral getPeripheral( ComputerSide side ) { return m_computer.getEnvironment().getPeripheral( side ); } diff --git a/src/main/java/dan200/computercraft/shared/pocket/core/PocketServerComputer.java b/src/main/java/dan200/computercraft/shared/pocket/core/PocketServerComputer.java index 491bdaca1..b6e727381 100644 --- a/src/main/java/dan200/computercraft/shared/pocket/core/PocketServerComputer.java +++ b/src/main/java/dan200/computercraft/shared/pocket/core/PocketServerComputer.java @@ -10,6 +10,7 @@ import dan200.computercraft.ComputerCraft; import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.pocket.IPocketAccess; import dan200.computercraft.api.pocket.IPocketUpgrade; +import dan200.computercraft.core.computer.ComputerSide; import dan200.computercraft.shared.common.IColouredItem; import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.core.ServerComputer; @@ -130,14 +131,14 @@ public class PocketServerComputer extends ServerComputer implements IPocketAcces public void invalidatePeripheral() { IPeripheral peripheral = m_upgrade == null ? null : m_upgrade.createPeripheral( this ); - setPeripheral( 2, peripheral ); + setPeripheral( ComputerSide.BACK, peripheral ); } @Nonnull @Override public Map getUpgrades() { - return m_upgrade == null ? Collections.emptyMap() : Collections.singletonMap( m_upgrade.getUpgradeID(), getPeripheral( 2 ) ); + return m_upgrade == null ? Collections.emptyMap() : Collections.singletonMap( m_upgrade.getUpgradeID(), getPeripheral( ComputerSide.BACK ) ); } public IPocketUpgrade getUpgrade() diff --git a/src/main/java/dan200/computercraft/shared/pocket/items/ItemPocketComputer.java b/src/main/java/dan200/computercraft/shared/pocket/items/ItemPocketComputer.java index 16680acc5..38c92fd82 100644 --- a/src/main/java/dan200/computercraft/shared/pocket/items/ItemPocketComputer.java +++ b/src/main/java/dan200/computercraft/shared/pocket/items/ItemPocketComputer.java @@ -12,6 +12,7 @@ import dan200.computercraft.api.ComputerCraftAPI; import dan200.computercraft.api.filesystem.IMount; import dan200.computercraft.api.media.IMedia; import dan200.computercraft.api.pocket.IPocketUpgrade; +import dan200.computercraft.core.computer.ComputerSide; import dan200.computercraft.shared.PocketUpgrades; import dan200.computercraft.shared.common.IColouredItem; import dan200.computercraft.shared.computer.blocks.ComputerState; @@ -136,7 +137,7 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia, I // Update pocket upgrade if( upgrade != null ) { - upgrade.update( computer, computer.getPeripheral( 2 ) ); + upgrade.update( computer, computer.getPeripheral( ComputerSide.BACK ) ); } } } @@ -165,7 +166,7 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia, I if( upgrade != null ) { computer.updateValues( player, stack, upgrade ); - stop = upgrade.onRightClick( world, computer, computer.getPeripheral( 2 ) ); + stop = upgrade.onRightClick( world, computer, computer.getPeripheral( ComputerSide.BACK ) ); } } diff --git a/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java b/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java index fc1482612..e36edfbac 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java +++ b/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java @@ -12,6 +12,7 @@ import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.turtle.ITurtleAccess; import dan200.computercraft.api.turtle.ITurtleUpgrade; import dan200.computercraft.api.turtle.TurtleSide; +import dan200.computercraft.core.computer.ComputerSide; import dan200.computercraft.shared.computer.blocks.ComputerPeripheral; import dan200.computercraft.shared.computer.blocks.ComputerProxy; import dan200.computercraft.shared.computer.blocks.TileComputerBase; @@ -311,13 +312,13 @@ public class TileTurtle extends TileComputerBase implements ITurtleTile, Default } @Override - protected boolean isPeripheralBlockedOnSide( int localSide ) + protected boolean isPeripheralBlockedOnSide( ComputerSide localSide ) { return hasPeripheralUpgradeOnSide( localSide ); } @Override - protected boolean isRedstoneBlockedOnSide( int localSide ) + protected boolean isRedstoneBlockedOnSide( ComputerSide localSide ) { return false; } @@ -559,15 +560,15 @@ public class TileTurtle extends TileComputerBase implements ITurtleTile, Default // Privates - private boolean hasPeripheralUpgradeOnSide( int side ) + private boolean hasPeripheralUpgradeOnSide( ComputerSide side ) { ITurtleUpgrade upgrade; switch( side ) { - case 4: + case RIGHT: upgrade = getUpgrade( TurtleSide.Right ); break; - case 5: + case LEFT: upgrade = getUpgrade( TurtleSide.Left ); break; default: diff --git a/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java b/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java index ba28c46eb..01df6fcdd 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java +++ b/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java @@ -13,6 +13,7 @@ import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.turtle.*; +import dan200.computercraft.core.computer.ComputerSide; import dan200.computercraft.shared.TurtleUpgrades; import dan200.computercraft.shared.computer.blocks.ComputerProxy; import dan200.computercraft.shared.computer.blocks.TileComputerBase; @@ -827,15 +828,15 @@ public class TurtleBrain implements ITurtleAccess : 0.0f; } - private static int toDirection( TurtleSide side ) + private static ComputerSide toDirection( TurtleSide side ) { switch( side ) { case Left: - return 5; + return ComputerSide.LEFT; case Right: default: - return 4; + return ComputerSide.RIGHT; } } diff --git a/src/main/java/dan200/computercraft/shared/util/DirectionUtil.java b/src/main/java/dan200/computercraft/shared/util/DirectionUtil.java index 778882044..582507730 100644 --- a/src/main/java/dan200/computercraft/shared/util/DirectionUtil.java +++ b/src/main/java/dan200/computercraft/shared/util/DirectionUtil.java @@ -6,6 +6,7 @@ package dan200.computercraft.shared.util; +import dan200.computercraft.core.computer.ComputerSide; import dan200.computercraft.shared.common.IDirectionalTile; import net.minecraft.entity.EntityLivingBase; import net.minecraft.util.EnumFacing; @@ -14,38 +15,17 @@ public final class DirectionUtil { private DirectionUtil() {} - public static int toLocal( IDirectionalTile directional, EnumFacing dir ) + public static ComputerSide toLocal( IDirectionalTile directional, EnumFacing dir ) { EnumFacing front = directional.getDirection(); if( front.getAxis() == EnumFacing.Axis.Y ) front = EnumFacing.NORTH; - EnumFacing back = front.getOpposite(); - EnumFacing left = front.rotateYCCW(); - EnumFacing right = front.rotateY(); - if( dir == front ) - { - return 3; - } - else if( dir == back ) - { - return 2; - } - else if( dir == left ) - { - return 5; - } - else if( dir == right ) - { - return 4; - } - else if( dir == EnumFacing.UP ) - { - return 1; - } - else - { - return 0; - } + if( dir == front ) return ComputerSide.FRONT; + if( dir == front.getOpposite() ) return ComputerSide.BACK; + if( dir == front.rotateYCCW() ) return ComputerSide.LEFT; + if( dir == front.rotateY() ) return ComputerSide.RIGHT; + if( dir == EnumFacing.UP ) return ComputerSide.TOP; + return ComputerSide.BOTTOM; } public static EnumFacing fromEntityRot( EntityLivingBase player ) From b195cab6a70b432e8933bddff8d5071ab6dd98a2 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Tue, 9 Apr 2019 10:35:23 +0100 Subject: [PATCH 10/10] Add a couple of colours unit tests --- .../test-rom/spec/apis/colors_spec.lua | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/test/resources/test-rom/spec/apis/colors_spec.lua diff --git a/src/test/resources/test-rom/spec/apis/colors_spec.lua b/src/test/resources/test-rom/spec/apis/colors_spec.lua new file mode 100644 index 000000000..d1271b625 --- /dev/null +++ b/src/test/resources/test-rom/spec/apis/colors_spec.lua @@ -0,0 +1,43 @@ +describe("The colors library", function() + it("colors.combine", function() + expect(colors.combine(colors.red, colors.brown, colors.green)):equals(0x7000) + end) + + describe("colors.subtract", function() + it("subtracts colours", function() + expect(colors.subtract(0x7000, colors.green)):equals(0x5000) + expect(colors.subtract(0x5000, colors.red)):equals(0x1000) + end) + it("does nothing when color is not present", function() + expect(colors.subtract(0x1000, colors.red)):equals(0x1000) + end) + it("accepts multiple arguments", function() + expect(colors.subtract(0x7000, colors.red, colors.green, colors.red)):equals(0x1000) + end) + end) + + describe("colors.test", function() + it("returns true when present", function() + expect(colors.test(0x7000, colors.green)):equals(true) + end) + it("returns false when absent", function() + expect(colors.test(0x5000, colors.green)):equals(false) + end) + it("allows multiple colors", function() + expect(colors.test(0x7000, 0x5000)):equals(true) + end) + end) + + it("colors.packRGB", function() + expect(colors.packRGB(0.3, 0.5, 0.6)):equals(0x4c7f99) + end) + + it("colors.unpackRGB", function() + expect({ colors.unpackRGB(0x4c7f99) }):same { 0x4c / 0xFF, 0x7f / 0xFF, 0.6 } + end) + + it("colors.rgb8", function() + expect(colors.rgb8(0.3, 0.5, 0.6)):equals(0x4c7f99) + expect({ colors.rgb8(0x4c7f99) }):same { 0x4c / 0xFF, 0x7f / 0xFF, 0.6 } + end) +end )