diff --git a/src/main/java/dan200/computercraft/core/apis/BitAPI.java b/src/main/java/dan200/computercraft/core/apis/BitAPI.java index 8d4021b2c..f88a555e2 100644 --- a/src/main/java/dan200/computercraft/core/apis/BitAPI.java +++ b/src/main/java/dan200/computercraft/core/apis/BitAPI.java @@ -6,10 +6,13 @@ package dan200.computercraft.core.apis; +import dan200.computercraft.api.lua.ICallContext; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.MethodResult; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import static dan200.computercraft.core.apis.ArgumentHelper.getInt; @@ -62,8 +65,9 @@ public class BitAPI implements ILuaAPI }; } + @Nonnull @Override - public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException + public MethodResult callMethod( @Nonnull ICallContext context, int method, @Nonnull Object[] args ) throws LuaException { int ret = 0; switch(method) { @@ -89,7 +93,15 @@ public class BitAPI implements ILuaAPI ret = getInt( args, 0 ) >>> getInt( args, 1 ); break; } - - return new Object[]{ ret&0xFFFFFFFFL }; + + return MethodResult.of( ret & 0xFFFFFFFFL ); + } + + @Nullable + @Override + @Deprecated + public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + { + return callMethod( (ICallContext) context, method, arguments ).evaluate( context ); } } diff --git a/src/main/java/dan200/computercraft/core/apis/BufferAPI.java b/src/main/java/dan200/computercraft/core/apis/BufferAPI.java index 3035ef763..3046d0905 100644 --- a/src/main/java/dan200/computercraft/core/apis/BufferAPI.java +++ b/src/main/java/dan200/computercraft/core/apis/BufferAPI.java @@ -6,12 +6,11 @@ package dan200.computercraft.core.apis; -import dan200.computercraft.api.lua.ILuaContext; -import dan200.computercraft.api.lua.ILuaObject; -import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.*; import dan200.computercraft.core.terminal.TextBuffer; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import static dan200.computercraft.core.apis.ArgumentHelper.getString; import static dan200.computercraft.core.apis.ArgumentHelper.optInt; @@ -40,27 +39,28 @@ public class BufferAPI implements ILuaAPI }; } + @Nonnull @Override - public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + public MethodResult callMethod( @Nonnull ICallContext context, int method, @Nonnull Object[] arguments ) throws LuaException { switch( method ) { case 0: { // len - return new Object[] { m_buffer.length() }; + return MethodResult.of( m_buffer.length() ); } case 1: { // tostring - return new Object[] { m_buffer.toString() }; + return MethodResult.of( m_buffer.toString() ); } case 2: { // read int start = optInt( arguments, 0, 0 ); int end = optInt( arguments, 1, m_buffer.length() ); - return new Object[] { m_buffer.read( start, end ) }; + return MethodResult.of( m_buffer.read( start, end ) ); } case 3: { @@ -69,7 +69,7 @@ public class BufferAPI implements ILuaAPI int start = optInt( arguments, 1, 0 ); int end = optInt( arguments, 2, start + text.length() ); m_buffer.write( text, start, end ); - return null; + return MethodResult.empty(); } case 4: { @@ -78,14 +78,22 @@ public class BufferAPI implements ILuaAPI int start = optInt( arguments, 1, 0 ); int end = optInt( arguments, 2, m_buffer.length() ); m_buffer.fill( text, start, end ); - return null; + return MethodResult.empty(); } default: { - return null; + return MethodResult.empty(); } } } + + @Nullable + @Override + @Deprecated + public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + { + return callMethod( (ICallContext) context, method, arguments ).evaluate( context ); + } } public BufferAPI( IAPIEnvironment _env ) @@ -124,8 +132,9 @@ public class BufferAPI implements ILuaAPI }; } + @Nonnull @Override - public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + public MethodResult callMethod( @Nonnull ICallContext context, int method, @Nonnull Object[] arguments ) throws LuaException { switch( method ) { @@ -138,12 +147,20 @@ public class BufferAPI implements ILuaAPI throw ArgumentHelper.badArgument( 1, "positive number", Integer.toString( repetitions ) ); } TextBuffer buffer = new TextBuffer( text, repetitions ); - return new Object[] { new BufferLuaObject( buffer ) }; + return MethodResult.of( new BufferLuaObject( buffer ) ); } default: { - return null; + return MethodResult.empty(); } } } + + @Nullable + @Override + @Deprecated + public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + { + return callMethod( (ICallContext) context, method, arguments ).evaluate( context ); + } } diff --git a/src/main/java/dan200/computercraft/core/apis/FSAPI.java b/src/main/java/dan200/computercraft/core/apis/FSAPI.java index 7cf8bdc30..6dd1a4563 100644 --- a/src/main/java/dan200/computercraft/core/apis/FSAPI.java +++ b/src/main/java/dan200/computercraft/core/apis/FSAPI.java @@ -6,8 +6,10 @@ package dan200.computercraft.core.apis; +import dan200.computercraft.api.lua.ICallContext; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.MethodResult; import dan200.computercraft.core.apis.handles.BinaryInputHandle; import dan200.computercraft.core.apis.handles.BinaryOutputHandle; import dan200.computercraft.core.apis.handles.EncodedInputHandle; @@ -16,6 +18,7 @@ import dan200.computercraft.core.filesystem.FileSystem; import dan200.computercraft.core.filesystem.FileSystemException; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.io.InputStream; import java.io.OutputStream; import java.util.HashMap; @@ -83,8 +86,9 @@ public class FSAPI implements ILuaAPI }; } + @Nonnull @Override - public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException + public MethodResult callMethod( @Nonnull ICallContext context, int method, @Nonnull Object[] args ) throws LuaException { switch( method ) { @@ -98,7 +102,7 @@ public class FSAPI implements ILuaAPI for(int i=0; i= 0 ) { - return new Object[]{ freeSpace }; + return MethodResult.of( freeSpace ); } - return new Object[]{ "unlimited" }; + return MethodResult.of( "unlimited" ); } catch( FileSystemException e ) { throw new LuaException( e.getMessage() ); } @@ -297,7 +301,7 @@ public class FSAPI implements ILuaAPI for(int i=0; i headers, boolean binary ) throws HTTPRequestException - { - // Parse the URL - m_urlString = url; - m_url = checkURL( m_urlString ); - m_binary = binary; - - // Start the thread - m_cancelled = false; - m_complete = false; - m_success = false; - m_result = null; - m_responseCode = -1; - - Thread thread = new Thread( () -> - { - try - { - // Connect to the URL - HttpURLConnection connection = (HttpURLConnection)m_url.openConnection(); - - if( postText != null ) - { - connection.setRequestMethod( "POST" ); - connection.setDoOutput( true ); - } - else - { - connection.setRequestMethod( "GET" ); - } - - // Set headers - connection.setRequestProperty( "accept-charset", "UTF-8" ); - if( postText != null ) - { - connection.setRequestProperty( "content-type", "application/x-www-form-urlencoded; charset=utf-8" ); - connection.setRequestProperty( "content-encoding", "UTF-8" ); - } - if( headers != null ) - { - for( Map.Entry header : headers.entrySet() ) - { - connection.setRequestProperty( header.getKey(), header.getValue() ); - } - } - - // Send POST text - if( postText != null ) - { - OutputStream os = connection.getOutputStream(); - OutputStreamWriter osw; - try - { - osw = new OutputStreamWriter( os, "UTF-8" ); - } - catch( UnsupportedEncodingException e ) - { - osw = new OutputStreamWriter( os ); - } - BufferedWriter writer = new BufferedWriter( osw ); - writer.write( postText, 0, postText.length() ); - writer.close(); - } - - // Read response - InputStream is; - int code = connection.getResponseCode(); - boolean responseSuccess; - if (code >= 200 && code < 400) { - is = connection.getInputStream(); - responseSuccess = true; - } else { - is = connection.getErrorStream(); - responseSuccess = false; - } - - byte[] result = ByteStreams.toByteArray( is ); - is.close(); - - synchronized( m_lock ) - { - if( m_cancelled ) - { - // We cancelled - m_complete = true; - m_success = false; - m_result = null; - } - else - { - // We completed - m_complete = true; - m_success = responseSuccess; - m_result = result; - m_responseCode = connection.getResponseCode(); - m_encoding = connection.getContentEncoding(); - - Joiner joiner = Joiner.on( ',' ); - Map headers1 = m_responseHeaders = new HashMap<>(); - for (Map.Entry> header : connection.getHeaderFields().entrySet()) { - headers1.put(header.getKey(), joiner.join( header.getValue() )); - } - } - } - - connection.disconnect(); // disconnect - - } - catch( IOException e ) - { - synchronized( m_lock ) - { - // There was an error - m_complete = true; - m_success = false; - m_result = null; - } - } - } ); - thread.setDaemon(true); - thread.start(); - } - - public String getURL() { - return m_urlString; - } - - public void cancel() - { - synchronized(m_lock) { - m_cancelled = true; - } - } - - public boolean isComplete() - { - synchronized(m_lock) { - return m_complete; - } - } - - public int getResponseCode() { - synchronized(m_lock) { - return m_responseCode; - } - } - - public Map getResponseHeaders() { - synchronized (m_lock) { - return m_responseHeaders; - } - } - - public boolean wasSuccessful() - { - synchronized(m_lock) { - return m_success; - } - } - - public boolean isBinary() - { - return m_binary; - } - - public InputStream getContents() - { - byte[] result; - synchronized(m_lock) { - result = m_result; - } - - if( result != null ) { - return new ByteArrayInputStream( result ); - } - return null; - } - - public String getEncoding() { - return m_encoding; - } - - private final Object m_lock = new Object(); - private final URL m_url; - private final String m_urlString; - - private boolean m_complete; - private boolean m_cancelled; - private boolean m_success; - private String m_encoding; - private byte[] m_result; - private boolean m_binary; - private int m_responseCode; - private Map m_responseHeaders; -} diff --git a/src/main/java/dan200/computercraft/core/apis/OSAPI.java b/src/main/java/dan200/computercraft/core/apis/OSAPI.java index 7ec4b6878..5e22877b5 100644 --- a/src/main/java/dan200/computercraft/core/apis/OSAPI.java +++ b/src/main/java/dan200/computercraft/core/apis/OSAPI.java @@ -6,11 +6,14 @@ package dan200.computercraft.core.apis; +import dan200.computercraft.api.lua.ICallContext; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.MethodResult; import dan200.computercraft.shared.util.StringUtil; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.*; import static dan200.computercraft.core.apis.ArgumentHelper.*; @@ -220,7 +223,8 @@ public class OSAPI implements ILuaAPI } @Override - public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException + @Nonnull + public MethodResult callMethod( @Nonnull ICallContext context, int method, @Nonnull Object[] args ) throws LuaException { switch( method ) { @@ -228,7 +232,7 @@ public class OSAPI implements ILuaAPI { // queueEvent queueLuaEvent( getString( args, 0 ), trimArray( args, 1 ) ); - return null; + return MethodResult.empty(); } case 1: { @@ -237,7 +241,7 @@ public class OSAPI implements ILuaAPI synchronized( m_timers ) { m_timers.put( m_nextTimerToken, new Timer( (int)Math.round( timer / 0.05 ) ) ); - return new Object[] { m_nextTimerToken++ }; + return MethodResult.of( m_nextTimerToken++ ); } } case 2: @@ -252,33 +256,33 @@ public class OSAPI implements ILuaAPI { int day = (time > m_time) ? m_day : (m_day + 1); m_alarms.put( m_nextAlarmToken, new Alarm( time, day ) ); - return new Object[] { m_nextAlarmToken++ }; + return MethodResult.of( m_nextAlarmToken++ ); } } case 3: { // shutdown m_apiEnvironment.shutdown(); - return null; + return MethodResult.empty(); } case 4: { // reboot m_apiEnvironment.reboot(); - return null; + return MethodResult.empty(); } case 5: case 6: { // computerID/getComputerID - return new Object[] { getComputerID() }; + return MethodResult.of( getComputerID() ); } case 7: { // setComputerLabel String label = optString( args, 0, null ); m_apiEnvironment.setLabel( StringUtil.normaliseLabel( label ) ); - return null; + return MethodResult.empty(); } case 8: case 9: @@ -287,16 +291,16 @@ public class OSAPI implements ILuaAPI String label = m_apiEnvironment.getLabel(); if( label != null ) { - return new Object[] { label }; + return MethodResult.of( label ); } - return null; + return MethodResult.empty(); } case 10: { // clock synchronized( m_timers ) { - return new Object[] { (double)m_clock * 0.05 }; + return MethodResult.of( (double)m_clock * 0.05 ); } } case 11: @@ -309,19 +313,19 @@ public class OSAPI implements ILuaAPI { // Get Hour of day (UTC) Calendar c = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) ); - return new Object[] { getTimeForCalendar( c ) }; + return MethodResult.of( getTimeForCalendar( c ) ); } case "local": { // Get Hour of day (local time) Calendar c = Calendar.getInstance(); - return new Object[] { getTimeForCalendar( c ) }; + return MethodResult.of( getTimeForCalendar( c ) ); } case "ingame": // Get ingame hour synchronized( m_alarms ) { - return new Object[] { m_time }; + return MethodResult.of( m_time ); } default: throw new LuaException( "Unsupported operation" ); @@ -337,19 +341,19 @@ public class OSAPI implements ILuaAPI { // Get numbers of days since 1970-01-01 (utc) Calendar c = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) ); - return new Object[] { getDayForCalendar( c ) }; + return MethodResult.of( getDayForCalendar( c ) ); } case "local": { // Get numbers of days since 1970-01-01 (local time) Calendar c = Calendar.getInstance(); - return new Object[] { getDayForCalendar( c ) }; + return MethodResult.of( getDayForCalendar( c ) ); } case "ingame": // Get game day synchronized( m_alarms ) { - return new Object[] { m_day }; + return MethodResult.of( m_day ); } default: throw new LuaException( "Unsupported operation" ); @@ -366,7 +370,7 @@ public class OSAPI implements ILuaAPI m_timers.remove( token ); } } - return null; + return MethodResult.empty(); } case 14: { @@ -379,7 +383,7 @@ public class OSAPI implements ILuaAPI m_alarms.remove( token ); } } - return null; + return MethodResult.empty(); } case 15: { @@ -391,21 +395,21 @@ public class OSAPI implements ILuaAPI { // Get utc epoch Calendar c = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) ); - return new Object[] { getEpochForCalendar( c ) }; + return MethodResult.of( getEpochForCalendar( c ) ); } case "local": { // Get local epoch Calendar c = Calendar.getInstance(); - return new Object[] { getEpochForCalendar( c ) }; + return MethodResult.of( getEpochForCalendar( c ) ); } case "ingame": // Get in-game epoch synchronized( m_alarms ) { - return new Object[] { + return MethodResult.of( m_day * 86400000 + (int) (m_time * 3600000.0f) - }; + ); } default: throw new LuaException( "Unsupported operation" ); @@ -413,11 +417,18 @@ public class OSAPI implements ILuaAPI } default: { - return null; + return MethodResult.empty(); } } } + @Nullable + @Override + @Deprecated + public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + { + return callMethod( (ICallContext) context, method, arguments ).evaluate( context ); + } // Private methods private void queueLuaEvent( String event, Object[] args ) diff --git a/src/main/java/dan200/computercraft/core/apis/PeripheralAPI.java b/src/main/java/dan200/computercraft/core/apis/PeripheralAPI.java index 390bd5563..4d7bfc16d 100644 --- a/src/main/java/dan200/computercraft/core/apis/PeripheralAPI.java +++ b/src/main/java/dan200/computercraft/core/apis/PeripheralAPI.java @@ -8,8 +8,10 @@ package dan200.computercraft.core.apis; import dan200.computercraft.api.filesystem.IMount; import dan200.computercraft.api.filesystem.IWritableMount; +import dan200.computercraft.api.lua.ICallContext; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.MethodResult; import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.core.computer.Computer; @@ -19,6 +21,7 @@ import dan200.computercraft.core.filesystem.FileSystem; import dan200.computercraft.core.filesystem.FileSystemException; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.*; import static dan200.computercraft.core.apis.ArgumentHelper.getString; @@ -98,7 +101,7 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange m_mounts.clear(); } - public Object[] call( ILuaContext context, String methodName, Object[] arguments ) throws LuaException, InterruptedException + public MethodResult call( ICallContext context, String methodName, Object[] arguments ) throws LuaException { int method = -1; synchronized( this ) @@ -388,8 +391,9 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange }; } + @Nonnull @Override - public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException, InterruptedException + public MethodResult callMethod( @Nonnull ICallContext context, int method, @Nonnull Object[] args ) throws LuaException { switch( method ) { @@ -409,7 +413,7 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange } } } - return new Object[] { present }; + return MethodResult.of( present ); } case 1: { @@ -428,10 +432,10 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange } if( type != null ) { - return new Object[] { type }; + return MethodResult.of( type ); } } - return null; + return MethodResult.empty(); } case 2: { @@ -455,9 +459,9 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange for(int i=0; i 0 }; + return MethodResult.of( m_environment.getOutput( side ) > 0 ); } case 3: { // getInput int side = parseSide( args ); - return new Object[] { m_environment.getInput( side ) > 0 }; + return MethodResult.of( m_environment.getInput( side ) > 0 ); } case 4: { @@ -111,19 +115,19 @@ public class RedstoneAPI implements ILuaAPI int side = parseSide( args ); int output = getInt( args, 1 ); m_environment.setBundledOutput( side, output ); - return null; + return MethodResult.empty(); } case 5: { // getBundledOutput int side = parseSide( args ); - return new Object[] { m_environment.getBundledOutput( side ) }; + return MethodResult.of( m_environment.getBundledOutput( side ) ); } case 6: { // getBundledInput int side = parseSide( args ); - return new Object[] { m_environment.getBundledInput( side ) }; + return MethodResult.of( m_environment.getBundledInput( side ) ); } case 7: { @@ -131,7 +135,7 @@ public class RedstoneAPI implements ILuaAPI int side = parseSide( args ); int mask = getInt( args, 1 ); int input = m_environment.getBundledInput( side ); - return new Object[] { ((input & mask) == mask) }; + return MethodResult.of( ((input & mask) == mask) ); } case 8: case 9: @@ -144,28 +148,36 @@ public class RedstoneAPI implements ILuaAPI throw new LuaException( "Expected number in range 0-15" ); } m_environment.setOutput( side, output ); - return null; + return MethodResult.empty(); } case 10: case 11: { // getAnalogOutput/getAnalogueOutput int side = parseSide( args ); - return new Object[] { m_environment.getOutput( side ) }; + return MethodResult.of( m_environment.getOutput( side ) ); } case 12: case 13: { // getAnalogInput/getAnalogueInput int side = parseSide( args ); - return new Object[] { m_environment.getInput( side ) }; + return MethodResult.of( m_environment.getInput( side ) ); } default: { - return null; + return MethodResult.empty(); } } } + + @Nullable + @Override + @Deprecated + public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + { + return callMethod( (ICallContext) context, method, arguments ).evaluate( context ); + } private int parseSide( Object[] args ) throws LuaException { diff --git a/src/main/java/dan200/computercraft/core/apis/TermAPI.java b/src/main/java/dan200/computercraft/core/apis/TermAPI.java index 38a64b059..3e596ea31 100644 --- a/src/main/java/dan200/computercraft/core/apis/TermAPI.java +++ b/src/main/java/dan200/computercraft/core/apis/TermAPI.java @@ -6,14 +6,17 @@ package dan200.computercraft.core.apis; +import dan200.computercraft.api.lua.ICallContext; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.MethodResult; import dan200.computercraft.core.computer.IComputerEnvironment; import dan200.computercraft.core.terminal.Terminal; import dan200.computercraft.shared.util.Palette; import org.apache.commons.lang3.ArrayUtils; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import static dan200.computercraft.core.apis.ArgumentHelper.*; @@ -97,11 +100,9 @@ public class TermAPI implements ILuaAPI return colour; } - public static Object[] encodeColour( int colour ) throws LuaException + public static MethodResult encodeColour( int colour ) { - return new Object[] { - 1 << colour - }; + return MethodResult.of( 1 << colour ); } public static void setColour( Terminal terminal, int colour, double r, double g, double b ) @@ -113,8 +114,9 @@ public class TermAPI implements ILuaAPI } } + @Nonnull @Override - public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException + public MethodResult callMethod( @Nonnull ICallContext context, int method, @Nonnull Object[] args ) throws LuaException { switch( method ) { @@ -133,7 +135,7 @@ public class TermAPI implements ILuaAPI m_terminal.write( text ); m_terminal.setCursorPos( m_terminal.getCursorX() + text.length(), m_terminal.getCursorY() ); } - return null; + return MethodResult.empty(); } case 1: { @@ -143,7 +145,7 @@ public class TermAPI implements ILuaAPI { m_terminal.scroll(y); } - return null; + return MethodResult.empty(); } case 2: { @@ -154,7 +156,7 @@ public class TermAPI implements ILuaAPI { m_terminal.setCursorPos( x, y ); } - return null; + return MethodResult.empty(); } case 3: { @@ -164,7 +166,7 @@ public class TermAPI implements ILuaAPI { m_terminal.setCursorBlink( b ); } - return null; + return MethodResult.empty(); } case 4: { @@ -175,7 +177,7 @@ public class TermAPI implements ILuaAPI x = m_terminal.getCursorX(); y = m_terminal.getCursorY(); } - return new Object[] { x + 1, y + 1 }; + return MethodResult.of( x + 1, y + 1 ); } case 5: { @@ -186,7 +188,7 @@ public class TermAPI implements ILuaAPI width = m_terminal.getWidth(); height = m_terminal.getHeight(); } - return new Object[] { width, height }; + return MethodResult.of( width, height ); } case 6: { @@ -195,7 +197,7 @@ public class TermAPI implements ILuaAPI { m_terminal.clear(); } - return null; + return MethodResult.empty(); } case 7: { @@ -204,7 +206,7 @@ public class TermAPI implements ILuaAPI { m_terminal.clearLine(); } - return null; + return MethodResult.empty(); } case 8: case 9: @@ -215,7 +217,7 @@ public class TermAPI implements ILuaAPI { m_terminal.setTextColour( colour ); } - return null; + return MethodResult.empty(); } case 10: case 11: @@ -226,13 +228,13 @@ public class TermAPI implements ILuaAPI { m_terminal.setBackgroundColour( colour ); } - return null; + return MethodResult.empty(); } case 12: case 13: { // isColour/isColor - return new Object[] { m_environment.isColour() }; + return MethodResult.of( m_environment.isColour() ); } case 14: case 15: @@ -262,7 +264,7 @@ public class TermAPI implements ILuaAPI m_terminal.blit( text, textColour, backgroundColour ); m_terminal.setCursorPos( m_terminal.getCursorX() + text.length(), m_terminal.getCursorY() ); } - return null; + return MethodResult.empty(); } case 19: case 20: @@ -282,7 +284,7 @@ public class TermAPI implements ILuaAPI double b = getReal( args, 3 ); setColour( m_terminal, colour, r, g, b ); } - return null; + return MethodResult.empty(); } case 21: case 22: @@ -293,18 +295,26 @@ public class TermAPI implements ILuaAPI { if ( m_terminal.getPalette() != null ) { - return ArrayUtils.toObject( m_terminal.getPalette().getColour( colour ) ); + return MethodResult.of( (Object[]) ArrayUtils.toObject( m_terminal.getPalette().getColour( colour ) ) ); } } - return null; + return MethodResult.empty(); } default: { - return null; + return MethodResult.empty(); } } } - + + @Nullable + @Override + @Deprecated + public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + { + return callMethod( (ICallContext) context, method, arguments ).evaluate( context ); + } + private static int getHighestBit( int group ) { int bit = 0; diff --git a/src/main/java/dan200/computercraft/core/apis/handles/BinaryInputHandle.java b/src/main/java/dan200/computercraft/core/apis/handles/BinaryInputHandle.java index 25cdaca5e..c0877dc09 100644 --- a/src/main/java/dan200/computercraft/core/apis/handles/BinaryInputHandle.java +++ b/src/main/java/dan200/computercraft/core/apis/handles/BinaryInputHandle.java @@ -1,8 +1,9 @@ package dan200.computercraft.core.apis.handles; import com.google.common.io.ByteStreams; -import dan200.computercraft.api.lua.ILuaContext; +import dan200.computercraft.api.lua.ICallContext; import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.MethodResult; import javax.annotation.Nonnull; import java.io.IOException; @@ -32,8 +33,9 @@ public class BinaryInputHandle extends HandleGeneric }; } + @Nonnull @Override - public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException + public MethodResult callMethod( @Nonnull ICallContext context, int method, @Nonnull Object[] args ) throws LuaException { switch( method ) { @@ -52,19 +54,19 @@ public class BinaryInputHandle extends HandleGeneric byte[] bytes = new byte[ count ]; count = m_stream.read( bytes ); - if( count < 0 ) return null; + if( count < 0 ) return MethodResult.empty(); if( count < bytes.length ) bytes = Arrays.copyOf( bytes, count ); - return new Object[] { bytes }; + return MethodResult.of( bytes ); } else { int b = m_stream.read(); - return b == -1 ? null : new Object[] { b }; + return b == -1 ? MethodResult.empty() : MethodResult.of( b ); } } catch( IOException e ) { - return null; + return MethodResult.empty(); } case 1: // readAll @@ -72,18 +74,18 @@ public class BinaryInputHandle extends HandleGeneric try { byte[] out = ByteStreams.toByteArray( m_stream ); - return out == null ? null : new Object[] { out }; + return out == null ? MethodResult.empty() : MethodResult.of( out ); } catch( IOException e ) { - return null; + return MethodResult.empty(); } case 2: //close close(); - return null; + return MethodResult.empty(); default: - return null; + return MethodResult.empty(); } } } diff --git a/src/main/java/dan200/computercraft/core/apis/handles/BinaryOutputHandle.java b/src/main/java/dan200/computercraft/core/apis/handles/BinaryOutputHandle.java index ae90b677d..f4c05deef 100644 --- a/src/main/java/dan200/computercraft/core/apis/handles/BinaryOutputHandle.java +++ b/src/main/java/dan200/computercraft/core/apis/handles/BinaryOutputHandle.java @@ -1,7 +1,8 @@ package dan200.computercraft.core.apis.handles; -import dan200.computercraft.api.lua.ILuaContext; +import dan200.computercraft.api.lua.ICallContext; import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.MethodResult; import dan200.computercraft.core.apis.ArgumentHelper; import dan200.computercraft.shared.util.StringUtil; @@ -30,8 +31,9 @@ public class BinaryOutputHandle extends HandleGeneric }; } + @Nonnull @Override - public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException + public MethodResult callMethod( @Nonnull ICallContext context, int method, @Nonnull Object[] args ) throws LuaException { switch( method ) { @@ -54,7 +56,7 @@ public class BinaryOutputHandle extends HandleGeneric { throw ArgumentHelper.badArgument( 0, "string or number", args.length > 0 ? args[ 0 ] : null ); } - return null; + return MethodResult.empty(); } catch( IOException e ) { @@ -66,18 +68,18 @@ public class BinaryOutputHandle extends HandleGeneric try { m_writer.flush(); - return null; + return MethodResult.empty(); } catch( IOException e ) { - return null; + return MethodResult.empty(); } case 2: //close close(); - return null; + return MethodResult.empty(); default: - return null; + return MethodResult.empty(); } } } diff --git a/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java b/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java index 9f722f797..5d2f7fdf3 100644 --- a/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java +++ b/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java @@ -1,12 +1,13 @@ package dan200.computercraft.core.apis.handles; -import dan200.computercraft.api.lua.ILuaContext; +import dan200.computercraft.api.lua.ICallContext; import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.MethodResult; import javax.annotation.Nonnull; import java.io.*; -import static dan200.computercraft.core.apis.ArgumentHelper.*; +import static dan200.computercraft.core.apis.ArgumentHelper.optInt; public class EncodedInputHandle extends HandleGeneric { @@ -55,8 +56,9 @@ public class EncodedInputHandle extends HandleGeneric }; } + @Nonnull @Override - public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException + public MethodResult callMethod( @Nonnull ICallContext context, int method, @Nonnull Object[] args ) throws LuaException { switch( method ) { @@ -68,16 +70,16 @@ public class EncodedInputHandle extends HandleGeneric String line = m_reader.readLine(); if( line != null ) { - return new Object[] { line }; + return MethodResult.of( line ); } else { - return null; + return MethodResult.empty(); } } catch( IOException e ) { - return null; + return MethodResult.empty(); } case 1: // readAll @@ -95,16 +97,16 @@ public class EncodedInputHandle extends HandleGeneric result.append( "\n" ); } } - return new Object[] { result.toString() }; + return MethodResult.of( result.toString() ); } catch( IOException e ) { - return null; + return MethodResult.empty(); } case 2: // close close(); - return null; + return MethodResult.empty(); case 3: // read checkOpen(); @@ -117,16 +119,16 @@ public class EncodedInputHandle extends HandleGeneric } char[] bytes = new char[ count ]; count = m_reader.read( bytes ); - if( count < 0 ) return null; + if( count < 0 ) return MethodResult.empty(); String str = new String( bytes, 0, count ); - return new Object[] { str }; + return MethodResult.of( str ); } catch( IOException e ) { - return null; + return MethodResult.empty(); } default: - return null; + return MethodResult.empty(); } } } diff --git a/src/main/java/dan200/computercraft/core/apis/handles/EncodedOutputHandle.java b/src/main/java/dan200/computercraft/core/apis/handles/EncodedOutputHandle.java index 89eb6dd2f..125f0faf6 100644 --- a/src/main/java/dan200/computercraft/core/apis/handles/EncodedOutputHandle.java +++ b/src/main/java/dan200/computercraft/core/apis/handles/EncodedOutputHandle.java @@ -1,7 +1,8 @@ package dan200.computercraft.core.apis.handles; -import dan200.computercraft.api.lua.ILuaContext; +import dan200.computercraft.api.lua.ICallContext; import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.MethodResult; import javax.annotation.Nonnull; import java.io.*; @@ -53,8 +54,9 @@ public class EncodedOutputHandle extends HandleGeneric }; } + @Nonnull @Override - public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException + public MethodResult callMethod( @Nonnull ICallContext context, int method, @Nonnull Object[] args ) throws LuaException { switch( method ) { @@ -74,7 +76,7 @@ public class EncodedOutputHandle extends HandleGeneric try { m_writer.write( text, 0, text.length() ); - return null; + return MethodResult.empty(); } catch( IOException e ) { @@ -98,7 +100,7 @@ public class EncodedOutputHandle extends HandleGeneric { m_writer.write( text, 0, text.length() ); m_writer.newLine(); - return null; + return MethodResult.empty(); } catch( IOException e ) { @@ -111,18 +113,18 @@ public class EncodedOutputHandle extends HandleGeneric try { m_writer.flush(); - return null; + return MethodResult.empty(); } catch( IOException e ) { - return null; + return MethodResult.empty(); } case 3: // close close(); - return null; + return MethodResult.empty(); default: - return null; + return MethodResult.empty(); } } } diff --git a/src/main/java/dan200/computercraft/core/apis/handles/HandleGeneric.java b/src/main/java/dan200/computercraft/core/apis/handles/HandleGeneric.java index c1cbc68e7..e7cbf9521 100644 --- a/src/main/java/dan200/computercraft/core/apis/handles/HandleGeneric.java +++ b/src/main/java/dan200/computercraft/core/apis/handles/HandleGeneric.java @@ -1,8 +1,12 @@ package dan200.computercraft.core.apis.handles; +import dan200.computercraft.api.lua.ICallContext; +import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.ILuaObject; import dan200.computercraft.api.lua.LuaException; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.io.Closeable; import java.io.IOException; @@ -32,4 +36,12 @@ public abstract class HandleGeneric implements ILuaObject { } } + + @Nullable + @Override + @Deprecated + public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + { + return callMethod( (ICallContext) context, method, arguments ).evaluate( context ); + } } diff --git a/src/main/java/dan200/computercraft/core/apis/http/HTTPRequest.java b/src/main/java/dan200/computercraft/core/apis/http/HTTPRequest.java index ff106af4b..050d8472d 100644 --- a/src/main/java/dan200/computercraft/core/apis/http/HTTPRequest.java +++ b/src/main/java/dan200/computercraft/core/apis/http/HTTPRequest.java @@ -9,15 +9,14 @@ package dan200.computercraft.core.apis.http; import com.google.common.base.Joiner; import com.google.common.io.ByteStreams; import dan200.computercraft.ComputerCraft; -import dan200.computercraft.api.lua.ILuaContext; -import dan200.computercraft.api.lua.ILuaObject; -import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.*; import dan200.computercraft.core.apis.HTTPRequestException; import dan200.computercraft.core.apis.IAPIEnvironment; import dan200.computercraft.core.apis.handles.BinaryInputHandle; import dan200.computercraft.core.apis.handles.EncodedInputHandle; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.io.*; import java.net.*; import java.util.Arrays; @@ -259,8 +258,9 @@ public class HTTPRequest implements HTTPTask.IHTTPTask return newMethods; } + @Nonnull @Override - public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException, InterruptedException + public MethodResult callMethod( @Nonnull ICallContext context, int method, @Nonnull Object[] args ) throws LuaException { if( method < methodOffset ) { @@ -271,19 +271,27 @@ public class HTTPRequest implements HTTPTask.IHTTPTask case 0: { // getResponseCode - return new Object[] { responseCode }; + return MethodResult.of( responseCode ); } case 1: { // getResponseHeaders - return new Object[] { responseHeaders }; + return MethodResult.of( responseHeaders ); } default: { - return null; + return MethodResult.empty(); } } } + + @Nullable + @Override + @Deprecated + public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + { + return callMethod( (ICallContext) context, method, arguments ).evaluate( context ); + } }; } } diff --git a/src/main/java/dan200/computercraft/shared/computer/apis/CommandAPI.java b/src/main/java/dan200/computercraft/shared/computer/apis/CommandAPI.java index 2c8b23453..0b293ca0f 100644 --- a/src/main/java/dan200/computercraft/shared/computer/apis/CommandAPI.java +++ b/src/main/java/dan200/computercraft/shared/computer/apis/CommandAPI.java @@ -8,8 +8,10 @@ package dan200.computercraft.shared.computer.apis; import com.google.common.collect.ImmutableMap; import dan200.computercraft.ComputerCraft; +import dan200.computercraft.api.lua.ICallContext; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.MethodResult; import dan200.computercraft.core.apis.ILuaAPI; import dan200.computercraft.shared.computer.blocks.TileCommandComputer; import dan200.computercraft.shared.util.WorldUtil; @@ -24,6 +26,7 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.HashMap; import java.util.Map; @@ -97,7 +100,7 @@ public class CommandAPI implements ILuaAPI sender.clearOutput(); int result = commandManager.executeCommand( sender, command ); - return new Object[]{ (result > 0), sender.copyOutput() }; + return new Object[] { (result > 0), sender.copyOutput() }; } catch( Throwable t ) { @@ -105,7 +108,7 @@ public class CommandAPI implements ILuaAPI { ComputerCraft.log.error( "Error running command.", t ); } - return new Object[]{ false, createOutput( "Java Exception Thrown: " + t.toString() ) }; + return new Object[] { false, createOutput( "Java Exception Thrown: " + t.toString() ) }; } } else @@ -146,7 +149,8 @@ public class CommandAPI implements ILuaAPI } @Override - public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + @Nonnull + public MethodResult callMethod( @Nonnull ICallContext context, int method, @Nonnull Object[] arguments ) throws LuaException { switch( method ) { @@ -154,19 +158,19 @@ public class CommandAPI implements ILuaAPI { // exec final String command = getString( arguments, 0 ); - return context.executeMainThreadTask( () -> doCommand( command ) ); + return MethodResult.onMainThread( () -> MethodResult.of( doCommand( command ) ) ); } case 1: { // execAsync final String command = getString( arguments, 0 ); long taskID = context.issueMainThreadTask( () -> doCommand( command ) ); - return new Object[] { taskID }; + return MethodResult.of( taskID ); } case 2: { // list - return context.executeMainThreadTask( () -> + return MethodResult.onMainThread( () -> { int i = 1; Map result = new HashMap<>(); @@ -197,7 +201,7 @@ public class CommandAPI implements ILuaAPI } } } - return new Object[]{ result }; + return MethodResult.of( result ); } ); } case 3: @@ -205,7 +209,7 @@ public class CommandAPI implements ILuaAPI // getBlockPosition // This is probably safe to do on the Lua thread. Probably. BlockPos pos = m_computer.getPos(); - return new Object[] { pos.getX(), pos.getY(), pos.getZ() }; + return MethodResult.of( pos.getX(), pos.getY(), pos.getZ() ); } case 4: { @@ -216,7 +220,7 @@ public class CommandAPI implements ILuaAPI final int maxx = getInt( arguments, 3 ); final int maxy = getInt( arguments, 4 ); final int maxz = getInt( arguments, 5 ); - return context.executeMainThreadTask( () -> + return MethodResult.onMainThread( () -> { // Get the details of the block World world = m_computer.getWorld(); @@ -251,7 +255,7 @@ public class CommandAPI implements ILuaAPI } } } - return new Object[]{ results }; + return MethodResult.of( results ); } ); } case 5: @@ -260,14 +264,14 @@ public class CommandAPI implements ILuaAPI final int x = getInt( arguments, 0 ); final int y = getInt( arguments, 1 ); final int z = getInt( arguments, 2 ); - return context.executeMainThreadTask( () -> + return MethodResult.onMainThread( () -> { // Get the details of the block World world = m_computer.getWorld(); BlockPos position = new BlockPos( x, y, z ); if( WorldUtil.isBlockInWorld( world, position ) ) { - return new Object[]{ getBlockInfo( world, position ) }; + return MethodResult.of( getBlockInfo( world, position ) ); } else { @@ -277,8 +281,16 @@ public class CommandAPI implements ILuaAPI } default: { - return null; + return MethodResult.empty(); } } } + + @Nullable + @Override + @Deprecated + public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + { + return callMethod( (ICallContext) context, method, arguments ).evaluate( context ); + } } diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerPeripheral.java b/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerPeripheral.java index 321889c3d..62a3662f1 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/ComputerPeripheral.java @@ -6,13 +6,16 @@ package dan200.computercraft.shared.computer.blocks; +import dan200.computercraft.api.lua.ICallContext; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.MethodResult; import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.shared.computer.core.ServerComputer; import javax.annotation.Nonnull; +import javax.annotation.Nullable; public class ComputerPeripheral implements IPeripheral @@ -25,7 +28,7 @@ public class ComputerPeripheral m_type = type; m_computer = computer; } - + // IPeripheral implementation @Nonnull @@ -49,8 +52,9 @@ public class ComputerPeripheral }; } + @Nonnull @Override - public Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException + public MethodResult callMethod( @Nonnull IComputerAccess computer, @Nonnull ICallContext context, int method, @Nonnull Object[] arguments ) throws LuaException { switch( method ) { @@ -58,42 +62,48 @@ public class ComputerPeripheral { // turnOn m_computer.turnOn(); - return null; + return MethodResult.empty(); } case 1: { // shutdown m_computer.shutdown(); - return null; + return MethodResult.empty(); } case 2: { // reboot m_computer.reboot(); - return null; + return MethodResult.empty(); } case 3: { // getID - return new Object[] { - m_computer.assignID() - }; + return MethodResult.of( m_computer.assignID() ); } case 4: { // isOn - return new Object[] { m_computer.isOn() }; + return MethodResult.of( m_computer.isOn() ); } case 5: // getLabel - return new Object[] { m_computer.getLabel() }; + return MethodResult.of( m_computer.getLabel() ); default: { - return null; + return MethodResult.empty(); } } } + @Nullable + @Override + @Deprecated + public Object[] callMethod( @Nonnull IComputerAccess access, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + { + return callMethod( access, (ICallContext) context, method, arguments ).evaluate( context ); + } + @Override public boolean equals( IPeripheral other ) { diff --git a/src/main/java/dan200/computercraft/shared/peripheral/commandblock/CommandBlockPeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/commandblock/CommandBlockPeripheral.java index a84070184..19a333ac8 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/commandblock/CommandBlockPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/commandblock/CommandBlockPeripheral.java @@ -6,14 +6,17 @@ package dan200.computercraft.shared.peripheral.commandblock; +import dan200.computercraft.api.lua.ICallContext; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.MethodResult; import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IPeripheral; import net.minecraft.tileentity.TileEntityCommandBlock; import net.minecraft.util.math.BlockPos; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import static dan200.computercraft.core.apis.ArgumentHelper.getString; @@ -46,17 +49,18 @@ public class CommandBlockPeripheral implements IPeripheral }; } + @Nonnull @Override - public Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull final Object[] arguments ) throws LuaException, InterruptedException + public MethodResult callMethod( @Nonnull IComputerAccess computer, @Nonnull ICallContext context, int method, @Nonnull final Object[] arguments ) throws LuaException { - switch (method) + switch( method ) { case 0: { // getCommand - return context.executeMainThreadTask( () -> new Object[] { - m_commandBlock.getCommandBlockLogic().getCommand() - } ); + return MethodResult.onMainThread( () -> + MethodResult.of( m_commandBlock.getCommandBlockLogic().getCommand() ) + ); } case 1: { @@ -69,27 +73,35 @@ public class CommandBlockPeripheral implements IPeripheral m_commandBlock.getWorld().markBlockRangeForRenderUpdate( pos, pos ); return null; } ); - return null; + return MethodResult.empty(); } case 2: { // runCommand - return context.executeMainThreadTask( () -> + return MethodResult.onMainThread( () -> { m_commandBlock.getCommandBlockLogic().trigger( m_commandBlock.getWorld() ); int result = m_commandBlock.getCommandBlockLogic().getSuccessCount(); if( result > 0 ) { - return new Object[] { true }; + return MethodResult.of( true ); } else { - return new Object[] { false, "Command failed" }; + return MethodResult.of( false, "Command failed" ); } } ); } } - return null; + return MethodResult.empty(); + } + + @Nullable + @Override + @Deprecated + public Object[] callMethod( @Nonnull IComputerAccess access, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + { + return callMethod( access, (ICallContext) context, method, arguments ).evaluate( context ); } @Override diff --git a/src/main/java/dan200/computercraft/shared/peripheral/diskdrive/DiskDrivePeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/diskdrive/DiskDrivePeripheral.java index 59ffea4bf..a6c77e37b 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/diskdrive/DiskDrivePeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/diskdrive/DiskDrivePeripheral.java @@ -6,8 +6,10 @@ package dan200.computercraft.shared.peripheral.diskdrive; +import dan200.computercraft.api.lua.ICallContext; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.MethodResult; import dan200.computercraft.api.media.IMedia; import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IPeripheral; @@ -17,6 +19,7 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import static dan200.computercraft.core.apis.ArgumentHelper.optString; @@ -55,17 +58,18 @@ public class DiskDrivePeripheral implements IPeripheral }; } + @Nonnull @Override - public Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException + public MethodResult callMethod( @Nonnull IComputerAccess computer, @Nonnull ICallContext context, int method, @Nonnull Object[] arguments ) throws LuaException { switch( method ) { case 0: { // isPresent - return new Object[] { + return MethodResult.of( m_diskDrive.getDiskStack() != null - }; + ); } case 1: { @@ -73,9 +77,9 @@ public class DiskDrivePeripheral implements IPeripheral IMedia media = m_diskDrive.getDiskMedia(); if( media != null ) { - return new Object[] { media.getLabel( m_diskDrive.getDiskStack() ) }; + return MethodResult.of( media.getLabel( m_diskDrive.getDiskStack() ) ); } - return null; + return MethodResult.empty(); } case 2: { @@ -96,21 +100,21 @@ public class DiskDrivePeripheral implements IPeripheral throw new LuaException( "Disk label cannot be changed" ); } } - return null; + return MethodResult.empty(); } case 3: { // hasData - return new Object[] { + return MethodResult.of( m_diskDrive.getDiskMountPath( computer ) != null - }; + ); } case 4: { // getMountPath - return new Object[] { + return MethodResult.of( m_diskDrive.getDiskMountPath( computer ) - }; + ); } case 5: { @@ -118,9 +122,9 @@ public class DiskDrivePeripheral implements IPeripheral IMedia media = m_diskDrive.getDiskMedia(); if( media != null ) { - return new Object[] { media.getAudio( m_diskDrive.getDiskStack() ) != null }; + return MethodResult.of( media.getAudio( m_diskDrive.getDiskStack() ) != null ); } - return new Object[] { false }; + return MethodResult.of( false ); } case 6: { @@ -128,27 +132,27 @@ public class DiskDrivePeripheral implements IPeripheral IMedia media = m_diskDrive.getDiskMedia(); if( media != null ) { - return new Object[] { media.getAudioTitle( m_diskDrive.getDiskStack() ) }; + return MethodResult.of( media.getAudioTitle( m_diskDrive.getDiskStack() ) ); } - return new Object[] { false }; + return MethodResult.of( false ); } case 7: { // playAudio m_diskDrive.playDiskAudio(); - return null; + return MethodResult.empty(); } case 8: { // stopAudio m_diskDrive.stopDiskAudio(); - return null; + return MethodResult.empty(); } case 9: { // eject m_diskDrive.ejectDisk(); - return null; + return MethodResult.empty(); } case 10: { @@ -159,18 +163,27 @@ public class DiskDrivePeripheral implements IPeripheral Item item = disk.getItem(); if( item instanceof ItemDiskLegacy ) { - return new Object[] { ((ItemDiskLegacy)item).getDiskID( disk ) }; + return MethodResult.of( ((ItemDiskLegacy)item).getDiskID( disk ) ); } } - return null; + return MethodResult.empty(); } default: { - return null; + return MethodResult.empty(); } } } + @Nullable + @Override + @Deprecated + public Object[] callMethod( @Nonnull IComputerAccess access, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + { + return callMethod( access, (ICallContext) context, method, arguments ).evaluate( context ); + } + + @Override public void attach( @Nonnull IComputerAccess computer ) { diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/ModemPeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/ModemPeripheral.java index ec05ae783..61986b9a3 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/ModemPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/ModemPeripheral.java @@ -6,8 +6,10 @@ package dan200.computercraft.shared.peripheral.modem; +import dan200.computercraft.api.lua.ICallContext; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.MethodResult; import dan200.computercraft.api.network.IPacketNetwork; import dan200.computercraft.api.network.IPacketReceiver; import dan200.computercraft.api.network.IPacketSender; @@ -20,6 +22,7 @@ import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import static dan200.computercraft.core.apis.ArgumentHelper.getInt; @@ -157,8 +160,9 @@ public abstract class ModemPeripheral return channel; } + @Nonnull @Override - public Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + public MethodResult callMethod( @Nonnull IComputerAccess computer, @Nonnull ICallContext context, int method, @Nonnull Object[] arguments ) throws LuaException { switch( method ) { @@ -183,7 +187,7 @@ public abstract class ModemPeripheral } } } - return null; + return MethodResult.empty(); } case 1: { @@ -192,7 +196,7 @@ public abstract class ModemPeripheral synchronized( this ) { boolean open = m_channels.contains( channel ); - return new Object[] { open }; + return MethodResult.of( open ); } } case 2: @@ -210,7 +214,7 @@ public abstract class ModemPeripheral } } } - return null; + return MethodResult.empty(); } case 3: { @@ -228,7 +232,7 @@ public abstract class ModemPeripheral } } } - return null; + return MethodResult.empty(); } case 4: { @@ -253,7 +257,7 @@ public abstract class ModemPeripheral } } } - return null; + return MethodResult.empty(); } case 5: { @@ -262,18 +266,26 @@ public abstract class ModemPeripheral { if( m_network != null ) { - return new Object[] { m_network.isWireless() }; + return MethodResult.of( m_network.isWireless() ); } } - return new Object[] { false }; + return MethodResult.of(false); } default: { - return null; + return MethodResult.empty(); } } } - + + @Nullable + @Override + @Deprecated + public Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + { + return callMethod( computer, (ICallContext) context, method, arguments ).evaluate( context ); + } + @Override public synchronized void attach( @Nonnull IComputerAccess computer ) { diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/TileCable.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/TileCable.java index b463a0c25..4c16f9f4f 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/TileCable.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/TileCable.java @@ -10,8 +10,9 @@ import com.google.common.base.Objects; import dan200.computercraft.ComputerCraft; import dan200.computercraft.api.filesystem.IMount; import dan200.computercraft.api.filesystem.IWritableMount; -import dan200.computercraft.api.lua.ILuaContext; +import dan200.computercraft.api.lua.ICallContext; import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.MethodResult; import dan200.computercraft.api.network.IPacketNetwork; import dan200.computercraft.api.network.IPacketReceiver; import dan200.computercraft.api.network.Packet; @@ -119,8 +120,9 @@ public class TileCable extends TileModemBase return newMethods; } + @Nonnull @Override - public Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + public MethodResult callMethod( @Nonnull IComputerAccess computer, @Nonnull ICallContext context, int method, @Nonnull Object[] arguments ) throws LuaException { String[] methods = super.getMethodNames(); switch( method - methods.length ) @@ -136,14 +138,14 @@ public class TileCable extends TileModemBase { table.put( idx++, name ); } - return new Object[] { table }; + return MethodResult.of( table ); } } case 1: { // isPresentRemote String type = m_entity.getTypeRemote( getString( arguments, 0 ) ); - return new Object[] { type != null }; + return MethodResult.of( type != null ); } case 2: { @@ -151,9 +153,9 @@ public class TileCable extends TileModemBase String type = m_entity.getTypeRemote( getString( arguments, 0 ) ); if( type != null ) { - return new Object[] { type }; + return MethodResult.of( type ); } - return null; + return MethodResult.empty(); } case 3: { @@ -165,9 +167,9 @@ public class TileCable extends TileModemBase for(int i=0; i 0 && args[0] != null ) { - text = args[0].toString(); - } else { + if( args.length > 0 && args[ 0 ] != null ) + { + text = args[ 0 ].toString(); + } + else + { text = ""; } Terminal terminal = m_monitor.getTerminal().getTerminal(); terminal.write( text ); terminal.setCursorPos( terminal.getCursorX() + text.length(), terminal.getCursorY() ); - return null; + return MethodResult.empty(); } case 1: { @@ -94,7 +101,7 @@ public class MonitorPeripheral implements IPeripheral int value = getInt( args, 0 ); Terminal terminal = m_monitor.getTerminal().getTerminal(); terminal.scroll( value ); - return null; + return MethodResult.empty(); } case 2: { @@ -103,7 +110,7 @@ public class MonitorPeripheral implements IPeripheral int y = getInt( args, 1 ) - 1; Terminal terminal = m_monitor.getTerminal().getTerminal(); terminal.setCursorPos( x, y ); - return null; + return MethodResult.empty(); } case 3: { @@ -111,39 +118,39 @@ public class MonitorPeripheral implements IPeripheral boolean blink = getBoolean( args, 0 ); Terminal terminal = m_monitor.getTerminal().getTerminal(); terminal.setCursorBlink( blink ); - return null; + return MethodResult.empty(); } case 4: { // getCursorPos Terminal terminal = m_monitor.getTerminal().getTerminal(); - return new Object[] { + return MethodResult.of( terminal.getCursorX() + 1, terminal.getCursorY() + 1 - }; + ); } case 5: { // getSize Terminal terminal = m_monitor.getTerminal().getTerminal(); - return new Object[] { + return MethodResult.of( terminal.getWidth(), terminal.getHeight() - }; + ); } case 6: { // clear Terminal terminal = m_monitor.getTerminal().getTerminal(); terminal.clear(); - return null; + return MethodResult.empty(); } case 7: { // clearLine Terminal terminal = m_monitor.getTerminal().getTerminal(); terminal.clearLine(); - return null; + return MethodResult.empty(); } case 8: { @@ -154,7 +161,7 @@ public class MonitorPeripheral implements IPeripheral throw new LuaException( "Expected number in range 0.5-5" ); } m_monitor.setTextScale( scale ); - return null; + return MethodResult.empty(); } case 9: case 10: @@ -163,7 +170,7 @@ public class MonitorPeripheral implements IPeripheral int colour = TermAPI.parseColour( args ); Terminal terminal = m_monitor.getTerminal().getTerminal(); terminal.setTextColour( colour ); - return null; + return MethodResult.empty(); } case 11: case 12: @@ -172,15 +179,15 @@ public class MonitorPeripheral implements IPeripheral int colour = TermAPI.parseColour( args ); Terminal terminal = m_monitor.getTerminal().getTerminal(); terminal.setBackgroundColour( colour ); - return null; + return MethodResult.empty(); } case 13: case 14: { // isColour/isColor - return new Object[] { + return MethodResult.of( m_monitor.getTerminal().isColour() - }; + ); } case 15: case 16: @@ -210,7 +217,7 @@ public class MonitorPeripheral implements IPeripheral Terminal terminal = m_monitor.getTerminal().getTerminal(); terminal.blit( text, textColour, backgroundColour ); terminal.setCursorPos( terminal.getCursorX() + text.length(), terminal.getCursorY() ); - return null; + return MethodResult.empty(); } case 20: case 21: @@ -232,7 +239,7 @@ public class MonitorPeripheral implements IPeripheral double b = getReal( args, 3 ); TermAPI.setColour( terminal, colour, r, g, b ); } - return null; + return MethodResult.empty(); } case 22: case 23: @@ -245,12 +252,20 @@ public class MonitorPeripheral implements IPeripheral if( palette != null ) { - return ArrayUtils.toObject( palette.getColour( colour ) ); + return MethodResult.of( (Object[]) ArrayUtils.toObject( palette.getColour( colour ) ) ); } - return null; + return MethodResult.pullEvent(); } } - return null; + return MethodResult.empty(); + } + + @Nullable + @Override + @Deprecated + public Object[] callMethod( @Nonnull IComputerAccess access, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + { + return callMethod( access, (ICallContext) context, method, arguments ).evaluate( context ); } @Override @@ -270,7 +285,7 @@ public class MonitorPeripheral implements IPeripheral { if( other != null && other instanceof MonitorPeripheral ) { - MonitorPeripheral otherMonitor = (MonitorPeripheral)other; + MonitorPeripheral otherMonitor = (MonitorPeripheral) other; if( otherMonitor.m_monitor == this.m_monitor ) { return true; diff --git a/src/main/java/dan200/computercraft/shared/peripheral/printer/PrinterPeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/printer/PrinterPeripheral.java index 1a97be3d1..033ca9d62 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/printer/PrinterPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/printer/PrinterPeripheral.java @@ -6,13 +6,16 @@ package dan200.computercraft.shared.peripheral.printer; +import dan200.computercraft.api.lua.ICallContext; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.MethodResult; import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.core.terminal.Terminal; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import static dan200.computercraft.core.apis.ArgumentHelper.getInt; import static dan200.computercraft.core.apis.ArgumentHelper.optString; @@ -50,8 +53,9 @@ public class PrinterPeripheral implements IPeripheral }; } + @Nonnull @Override - public Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException + public MethodResult callMethod( @Nonnull IComputerAccess computer, @Nonnull ICallContext context, int method, @Nonnull Object[] args ) throws LuaException { switch( method ) { @@ -68,7 +72,7 @@ public class PrinterPeripheral implements IPeripheral Terminal page = getCurrentPage(); page.write( text ); page.setCursorPos( page.getCursorX() + text.length(), page.getCursorY() ); - return null; + return MethodResult.empty(); } case 1: { @@ -77,7 +81,7 @@ public class PrinterPeripheral implements IPeripheral int y = getInt( args, 1 ) - 1; Terminal page = getCurrentPage(); page.setCursorPos( x, y ); - return null; + return MethodResult.empty(); } case 2: { @@ -85,7 +89,7 @@ public class PrinterPeripheral implements IPeripheral Terminal page = getCurrentPage(); int x = page.getCursorX(); int y = page.getCursorY(); - return new Object[] { x + 1, y + 1 }; + return MethodResult.of( x + 1, y + 1 ); } case 3: { @@ -93,23 +97,23 @@ public class PrinterPeripheral implements IPeripheral Terminal page = getCurrentPage(); int width = page.getWidth(); int height = page.getHeight(); - return new Object[] { width, height }; + return MethodResult.of( width, height ); } case 4: { // newPage - return new Object[] { m_printer.startNewPage() }; + return MethodResult.of( m_printer.startNewPage() ); } case 5: { // endPage getCurrentPage(); - return new Object[] { m_printer.endCurrentPage() }; + return MethodResult.of( m_printer.endCurrentPage() ); } case 6: { // getInkLevel - return new Object[] { m_printer.getInkLevel() }; + return MethodResult.of( m_printer.getInkLevel() ); } case 7: { @@ -117,20 +121,28 @@ public class PrinterPeripheral implements IPeripheral String title = optString( args, 0, "" ); getCurrentPage(); m_printer.setPageTitle( title ); - return null; + return MethodResult.empty(); } case 8: { // getPaperLevel - return new Object[] { m_printer.getPaperLevel() }; + return MethodResult.of( m_printer.getPaperLevel() ); } default: { - return null; + return MethodResult.empty(); } } } + @Nullable + @Override + @Deprecated + public Object[] callMethod( @Nonnull IComputerAccess access, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + { + return callMethod( access, (ICallContext) context, method, arguments ).evaluate( context ); + } + @Override public boolean equals( IPeripheral other ) { diff --git a/src/main/java/dan200/computercraft/shared/peripheral/speaker/SpeakerPeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/speaker/SpeakerPeripheral.java index 65831aba3..23de56d47 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/speaker/SpeakerPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/speaker/SpeakerPeripheral.java @@ -7,9 +7,7 @@ package dan200.computercraft.shared.peripheral.speaker; import dan200.computercraft.ComputerCraft; -import dan200.computercraft.api.lua.ILuaContext; -import dan200.computercraft.api.lua.ILuaTask; -import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.*; import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IPeripheral; import net.minecraft.util.ResourceLocation; @@ -92,21 +90,22 @@ public class SpeakerPeripheral implements IPeripheral { }; } + @Nonnull @Override - public Object[] callMethod( @Nonnull IComputerAccess computerAccess, @Nonnull ILuaContext context, int methodIndex, @Nonnull Object[] args ) throws LuaException + public MethodResult callMethod( @Nonnull IComputerAccess computer, @Nonnull ICallContext context, int methodIndex, @Nonnull Object[] args ) throws LuaException { switch( methodIndex ) { // playSound case 0: { - return playSound(args, context, false); + return MethodResult.of( playSound( args, context, false ) ); } // playNote case 1: { - return playNote(args, context); + return MethodResult.of( playNote( args, context ) ); } default: @@ -117,8 +116,16 @@ public class SpeakerPeripheral implements IPeripheral { } } - @Nonnull - private synchronized Object[] playNote( Object[] arguments, ILuaContext context ) throws LuaException + @Nullable + @Override + @Deprecated + public Object[] callMethod( @Nonnull IComputerAccess access, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + { + return callMethod( access, (ICallContext) context, method, arguments ).evaluate( context ); + } + + + private synchronized boolean playNote( Object[] arguments, ICallContext context ) throws LuaException { String name = getString(arguments, 0); float volume = (float) optReal( arguments, 1, 1.0 ); @@ -131,7 +138,7 @@ public class SpeakerPeripheral implements IPeripheral { } // If the resource location for note block notes changes, this method call will need to be updated - Object[] returnValue = playSound( + boolean success = playSound( new Object[] { "block.note." + name, (double)Math.min( volume, 3f ), @@ -139,16 +146,15 @@ public class SpeakerPeripheral implements IPeripheral { }, context, true ); - if( returnValue[0] instanceof Boolean && (Boolean) returnValue[0] ) + if( success ) { m_notesThisTick++; } - return returnValue; + return success; } - @Nonnull - private synchronized Object[] playSound( Object[] arguments, ILuaContext context, boolean isNote ) throws LuaException + private synchronized boolean playSound( Object[] arguments, ICallContext context, boolean isNote ) throws LuaException { String name = getString(arguments, 0); float volume = (float) optReal( arguments, 1, 1.0 ); @@ -178,16 +184,16 @@ public class SpeakerPeripheral implements IPeripheral { }); m_lastPlayTime = m_clock; - return new Object[]{true}; // Success, return true + return true; // Success, return true } else { - return new Object[]{false}; // Failed - sound not existent, return false + return false; // Failed - sound not existent, return false } } else { - return new Object[]{false}; // Failed - rate limited, return false + return false; // Failed - rate limited, return false } } } diff --git a/src/main/java/dan200/computercraft/shared/pocket/apis/PocketAPI.java b/src/main/java/dan200/computercraft/shared/pocket/apis/PocketAPI.java index 46377e98e..d7997e942 100644 --- a/src/main/java/dan200/computercraft/shared/pocket/apis/PocketAPI.java +++ b/src/main/java/dan200/computercraft/shared/pocket/apis/PocketAPI.java @@ -7,8 +7,10 @@ package dan200.computercraft.shared.pocket.apis; import dan200.computercraft.ComputerCraft; +import dan200.computercraft.api.lua.ICallContext; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.MethodResult; import dan200.computercraft.api.pocket.IPocketUpgrade; import dan200.computercraft.core.apis.ILuaAPI; import dan200.computercraft.shared.pocket.core.PocketServerComputer; @@ -21,6 +23,7 @@ import net.minecraft.util.NonNullList; import net.minecraftforge.items.wrapper.PlayerMainInvWrapper; import javax.annotation.Nonnull; +import javax.annotation.Nullable; public class PocketAPI implements ILuaAPI { @@ -64,14 +67,15 @@ public class PocketAPI implements ILuaAPI }; } + @Nonnull @Override - public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + public MethodResult callMethod( @Nonnull ICallContext context, int method, @Nonnull Object[] arguments ) throws LuaException { switch( method ) { case 0: // equipBack - return context.executeMainThreadTask( () -> + return MethodResult.onMainThread( () -> { if( !(m_computer.getEntity() instanceof EntityPlayer) ) { @@ -109,12 +113,12 @@ public class PocketAPI implements ILuaAPI // Set the new upgrade m_computer.setUpgrade( newUpgrade ); - return null; + return MethodResult.empty(); } ); case 1: // unequipBack - return context.executeMainThreadTask( () -> + return MethodResult.onMainThread( () -> { if( !(m_computer.getEntity() instanceof EntityPlayer) ) { @@ -140,13 +144,21 @@ public class PocketAPI implements ILuaAPI } } - return null; + return MethodResult.empty(); } ); default: - return null; + return MethodResult.empty(); } } + @Override + @Nullable + @Deprecated + public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + { + return callMethod( (ICallContext) context, method, arguments ).evaluate( context ); + } + private static IPocketUpgrade findUpgrade( NonNullList inv, int start, IPocketUpgrade previous ) { for( int i = 0; i < inv.size(); i++ ) diff --git a/src/main/java/dan200/computercraft/shared/turtle/apis/TurtleAPI.java b/src/main/java/dan200/computercraft/shared/turtle/apis/TurtleAPI.java index 1d5b6083f..a11a03479 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/apis/TurtleAPI.java +++ b/src/main/java/dan200/computercraft/shared/turtle/apis/TurtleAPI.java @@ -6,8 +6,10 @@ package dan200.computercraft.shared.turtle.apis; +import dan200.computercraft.api.lua.ICallContext; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.MethodResult; import dan200.computercraft.api.turtle.ITurtleAccess; import dan200.computercraft.api.turtle.ITurtleCommand; import dan200.computercraft.api.turtle.TurtleSide; @@ -18,6 +20,7 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.HashMap; import java.util.Map; import java.util.Optional; @@ -110,9 +113,9 @@ public class TurtleAPI implements ILuaAPI }; } - private Object[] tryCommand( ILuaContext context, ITurtleCommand command ) throws LuaException, InterruptedException + private MethodResult tryCommand( ITurtleCommand command ) throws LuaException { - return m_turtle.executeCommand( context, command ); + return m_turtle.executeCommand( command ); } private int parseSlotNumber( Object[] arguments, int index ) throws LuaException @@ -166,84 +169,85 @@ public class TurtleAPI implements ILuaAPI } @Override - public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException, InterruptedException + @Nonnull + public MethodResult callMethod( @Nonnull ICallContext context, int method, @Nonnull Object[] args ) throws LuaException { switch( method ) { case 0: { // forward - return tryCommand( context, new TurtleMoveCommand( MoveDirection.Forward ) ); + return tryCommand( new TurtleMoveCommand( MoveDirection.Forward ) ); } case 1: { // back - return tryCommand( context, new TurtleMoveCommand( MoveDirection.Back ) ); + return tryCommand( new TurtleMoveCommand( MoveDirection.Back ) ); } case 2: { // up - return tryCommand( context, new TurtleMoveCommand( MoveDirection.Up ) ); + return tryCommand( new TurtleMoveCommand( MoveDirection.Up ) ); } case 3: { // down - return tryCommand( context, new TurtleMoveCommand( MoveDirection.Down ) ); + return tryCommand( new TurtleMoveCommand( MoveDirection.Down ) ); } case 4: { // turnLeft - return tryCommand( context, new TurtleTurnCommand( TurnDirection.Left ) ); + return tryCommand( new TurtleTurnCommand( TurnDirection.Left ) ); } case 5: { // turnRight - return tryCommand( context, new TurtleTurnCommand( TurnDirection.Right ) ); + return tryCommand( new TurtleTurnCommand( TurnDirection.Right ) ); } case 6: { // dig Optional side = parseSide( args, 0 ); - return tryCommand( context, new TurtleDigCommand( InteractDirection.Forward, side ) ); + return tryCommand( new TurtleDigCommand( InteractDirection.Forward, side ) ); } case 7: { // digUp Optional side = parseSide( args, 0 ); - return tryCommand( context, new TurtleDigCommand( InteractDirection.Up, side ) ); + return tryCommand( new TurtleDigCommand( InteractDirection.Up, side ) ); } case 8: { // digDown Optional side = parseSide( args, 0 ); - return tryCommand( context, new TurtleDigCommand( InteractDirection.Down, side ) ); + return tryCommand( new TurtleDigCommand( InteractDirection.Down, side ) ); } case 9: { // place - return tryCommand( context, new TurtlePlaceCommand( InteractDirection.Forward, args ) ); + return tryCommand( new TurtlePlaceCommand( InteractDirection.Forward, args ) ); } case 10: { // placeUp - return tryCommand( context, new TurtlePlaceCommand( InteractDirection.Up, args ) ); + return tryCommand( new TurtlePlaceCommand( InteractDirection.Up, args ) ); } case 11: { // placeDown - return tryCommand( context, new TurtlePlaceCommand( InteractDirection.Down, args ) ); + return tryCommand( new TurtlePlaceCommand( InteractDirection.Down, args ) ); } case 12: { // drop int count = parseCount( args, 0 ); - return tryCommand( context, new TurtleDropCommand( InteractDirection.Forward, count ) ); + return tryCommand( new TurtleDropCommand( InteractDirection.Forward, count ) ); } case 13: { // select int slot = parseSlotNumber( args, 0 ); - return tryCommand( context, new TurtleSelectCommand( slot ) ); + return tryCommand( new TurtleSelectCommand( slot ) ); } case 14: { @@ -252,11 +256,11 @@ public class TurtleAPI implements ILuaAPI ItemStack stack = m_turtle.getInventory().getStackInSlot( slot ); if( !stack.isEmpty() ) { - return new Object[] { stack.getCount() }; + return MethodResult.of( stack.getCount() ); } else { - return new Object[] { 0 }; + return MethodResult.of( 0 ); } } case 15: @@ -266,162 +270,162 @@ public class TurtleAPI implements ILuaAPI ItemStack stack = m_turtle.getInventory().getStackInSlot( slot ); if( !stack.isEmpty() ) { - return new Object[] { + return MethodResult.of( Math.min( stack.getMaxStackSize(), 64 ) - stack.getCount() - }; + ); } - return new Object[] { 64 }; + return MethodResult.of( 64 ); } case 16: { // detect - return tryCommand( context, new TurtleDetectCommand( InteractDirection.Forward ) ); + return tryCommand( new TurtleDetectCommand( InteractDirection.Forward ) ); } case 17: { // detectUp - return tryCommand( context, new TurtleDetectCommand( InteractDirection.Up ) ); + return tryCommand( new TurtleDetectCommand( InteractDirection.Up ) ); } case 18: { // detectDown - return tryCommand( context, new TurtleDetectCommand( InteractDirection.Down ) ); + return tryCommand( new TurtleDetectCommand( InteractDirection.Down ) ); } case 19: { // compare - return tryCommand( context, new TurtleCompareCommand( InteractDirection.Forward ) ); + return tryCommand( new TurtleCompareCommand( InteractDirection.Forward ) ); } case 20: { // compareUp - return tryCommand( context, new TurtleCompareCommand( InteractDirection.Up ) ); + return tryCommand( new TurtleCompareCommand( InteractDirection.Up ) ); } case 21: { // compareDown - return tryCommand( context, new TurtleCompareCommand( InteractDirection.Down ) ); + return tryCommand( new TurtleCompareCommand( InteractDirection.Down ) ); } case 22: { // attack Optional side = parseSide( args, 0 ); - return tryCommand( context, new TurtleAttackCommand( InteractDirection.Forward, side ) ); + return tryCommand( new TurtleAttackCommand( InteractDirection.Forward, side ) ); } case 23: { // attackUp Optional side = parseSide( args, 0 ); - return tryCommand( context, new TurtleAttackCommand( InteractDirection.Up, side ) ); + return tryCommand( new TurtleAttackCommand( InteractDirection.Up, side ) ); } case 24: { // attackDown Optional side = parseSide( args, 0 ); - return tryCommand( context, new TurtleAttackCommand( InteractDirection.Down, side ) ); + return tryCommand( new TurtleAttackCommand( InteractDirection.Down, side ) ); } case 25: { // dropUp int count = parseCount( args, 0 ); - return tryCommand( context, new TurtleDropCommand( InteractDirection.Up, count ) ); + return tryCommand( new TurtleDropCommand( InteractDirection.Up, count ) ); } case 26: { // dropDown int count = parseCount( args, 0 ); - return tryCommand( context, new TurtleDropCommand( InteractDirection.Down, count ) ); + return tryCommand( new TurtleDropCommand( InteractDirection.Down, count ) ); } case 27: { // suck int count = parseCount( args, 0 ); - return tryCommand( context, new TurtleSuckCommand( InteractDirection.Forward, count ) ); + return tryCommand( new TurtleSuckCommand( InteractDirection.Forward, count ) ); } case 28: { // suckUp int count = parseCount( args, 0 ); - return tryCommand( context, new TurtleSuckCommand( InteractDirection.Up, count ) ); + return tryCommand( new TurtleSuckCommand( InteractDirection.Up, count ) ); } case 29: { // suckDown int count = parseCount( args, 0 ); - return tryCommand( context, new TurtleSuckCommand( InteractDirection.Down, count ) ); + return tryCommand( new TurtleSuckCommand( InteractDirection.Down, count ) ); } case 30: { // getFuelLevel if( m_turtle.isFuelNeeded() ) { - return new Object[] { m_turtle.getFuelLevel() }; + return MethodResult.of( m_turtle.getFuelLevel() ); } else { - return new Object[] { "unlimited" }; + return MethodResult.of( "unlimited" ); } } case 31: { // refuel int count = parseCount( args, 0 ); - return tryCommand( context, new TurtleRefuelCommand( count ) ); + return tryCommand( new TurtleRefuelCommand( count ) ); } case 32: { // compareTo int slot = parseSlotNumber( args, 0 ); - return tryCommand( context, new TurtleCompareToCommand( slot ) ); + return tryCommand( new TurtleCompareToCommand( slot ) ); } case 33: { // transferTo int slot = parseSlotNumber( args, 0 ); int count = parseCount( args, 1 ); - return tryCommand( context, new TurtleTransferToCommand( slot, count ) ); + return tryCommand( new TurtleTransferToCommand( slot, count ) ); } case 34: { // getSelectedSlot - return new Object[] { m_turtle.getSelectedSlot() + 1 }; + return MethodResult.of( m_turtle.getSelectedSlot() + 1 ); } case 35: { // getFuelLimit if( m_turtle.isFuelNeeded() ) { - return new Object[] { m_turtle.getFuelLimit() }; + return MethodResult.of( m_turtle.getFuelLimit() ); } else { - return new Object[] { "unlimited" }; + return MethodResult.of( "unlimited" ); } } case 36: { // equipLeft - return tryCommand( context, new TurtleEquipCommand( TurtleSide.Left ) ); + return tryCommand( new TurtleEquipCommand( TurtleSide.Left ) ); } case 37: { // equipRight - return tryCommand( context, new TurtleEquipCommand( TurtleSide.Right ) ); + return tryCommand( new TurtleEquipCommand( TurtleSide.Right ) ); } case 38: { // inspect - return tryCommand( context, new TurtleInspectCommand( InteractDirection.Forward ) ); + return tryCommand( new TurtleInspectCommand( InteractDirection.Forward ) ); } case 39: { // inspectUp - return tryCommand( context, new TurtleInspectCommand( InteractDirection.Up ) ); + return tryCommand( new TurtleInspectCommand( InteractDirection.Up ) ); } case 40: { // inspectDown - return tryCommand( context, new TurtleInspectCommand( InteractDirection.Down ) ); + return tryCommand( new TurtleInspectCommand( InteractDirection.Down ) ); } case 41: { @@ -439,17 +443,25 @@ public class TurtleAPI implements ILuaAPI table.put( "name", name ); table.put( "damage", damage ); table.put( "count", count ); - return new Object[] { table }; + return MethodResult.of( table ); } else { - return new Object[] { null }; + return MethodResult.of( new Object[] { null } ); } } default: { - return null; + return MethodResult.empty(); } } } + + @Override + @Nullable + @Deprecated + public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + { + return callMethod( (ICallContext) context, method, arguments ).evaluate( context ); + } } 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 31edaae6a..fe77117da 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java +++ b/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java @@ -712,6 +712,7 @@ public class TurtleBrain implements ITurtleAccess @Nonnull @Override + @Deprecated public Object[] executeCommand( @Nonnull ILuaContext context, @Nonnull ITurtleCommand command ) throws LuaException, InterruptedException { if( getWorld().isRemote ) diff --git a/src/main/java/dan200/computercraft/shared/turtle/upgrades/CraftingTablePeripheral.java b/src/main/java/dan200/computercraft/shared/turtle/upgrades/CraftingTablePeripheral.java index a2fb78bce..d0e7bbc9a 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/upgrades/CraftingTablePeripheral.java +++ b/src/main/java/dan200/computercraft/shared/turtle/upgrades/CraftingTablePeripheral.java @@ -6,19 +6,21 @@ package dan200.computercraft.shared.turtle.upgrades; +import dan200.computercraft.api.lua.ICallContext; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.MethodResult; import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.turtle.ITurtleAccess; import dan200.computercraft.shared.turtle.core.TurtleCraftCommand; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import static dan200.computercraft.core.apis.ArgumentHelper.optInt; -public class CraftingTablePeripheral - implements IPeripheral +public class CraftingTablePeripheral implements IPeripheral { private final ITurtleAccess m_turtle; @@ -26,7 +28,7 @@ public class CraftingTablePeripheral { m_turtle = turtle; } - + // IPeripheral implementation @Nonnull @@ -35,7 +37,7 @@ public class CraftingTablePeripheral { return "workbench"; } - + @Nonnull @Override public String[] getMethodNames() @@ -44,7 +46,7 @@ public class CraftingTablePeripheral "craft", }; } - + private int parseCount( Object[] arguments ) throws LuaException { int count = optInt( arguments, 0, 64 ); @@ -54,9 +56,10 @@ public class CraftingTablePeripheral } return count; } - + @Override - public Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + @Nonnull + public MethodResult callMethod( @Nonnull IComputerAccess computer, @Nonnull ICallContext context, int method, @Nonnull Object[] arguments ) throws LuaException { switch( method ) { @@ -64,15 +67,24 @@ public class CraftingTablePeripheral { // craft final int limit = parseCount( arguments ); - return m_turtle.executeCommand( context, new TurtleCraftCommand( limit ) ); + return m_turtle.executeCommand( new TurtleCraftCommand( limit ) ); } default: { - return null; + return MethodResult.empty(); } } } + + @Nullable + @Override + @Deprecated + public Object[] callMethod( @Nonnull IComputerAccess access, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException + { + return callMethod( access, (ICallContext) context, method, arguments ).evaluate( context ); + } + @Override public boolean equals( IPeripheral other ) { diff --git a/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleModem.java b/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleModem.java index 05a7e364f..c0c2e2de6 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleModem.java +++ b/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleModem.java @@ -31,8 +31,7 @@ import javax.vecmath.Matrix4f; public class TurtleModem implements ITurtleUpgrade { - private static class Peripheral extends WirelessModemPeripheral - implements IPeripheral + private static class Peripheral extends WirelessModemPeripheral implements IPeripheral { private final ITurtleAccess m_turtle;