mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-04-07 03:06:58 +00:00
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.
This commit is contained in:
parent
34602ec4be
commit
63dc0daa09
@ -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();
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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<Object, Object> 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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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 );
|
||||
}
|
||||
}
|
||||
|
@ -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() ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
|
@ -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 ) );
|
||||
|
@ -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 );
|
||||
}
|
||||
|
@ -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<ResourceLocation, IPeripheral> 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()
|
||||
|
@ -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 ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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:
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 )
|
||||
|
Loading…
x
Reference in New Issue
Block a user