1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-03-04 02:28:13 +00:00

Merge remote-tracking branch 'SquidDev-CC-ComputerCraft/feature/method-future' into feature/onethread-cobalt

This commit is contained in:
SquidDev 2018-09-09 22:13:45 +01:00
commit f23acef2dd
39 changed files with 1333 additions and 415 deletions

View File

@ -0,0 +1,31 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2018. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/
package dan200.computercraft.api.lua;
import javax.annotation.Nonnull;
/**
* An interface passed to peripherals and {@link ILuaObject}s by computers or turtles, providing methods that allow the
* method to interact with the invoking computer.
*/
public interface ICallContext
{
/**
* Queue a task to be executed on the main server thread at the beginning of next tick, but do not wait for it to
* complete. This should be used when you need to interact with the world in a thread-safe manner but do not care
* about the result or you wish to run asynchronously.
*
* When the task has finished, it will enqueue a {@code task_completed} event, which takes the task id, a success
* value and the return values, or an error message if it failed. If you need to wait on this event, it may be
* better to use {@link MethodResult#onMainThread(ILuaCallable)}.
*
* @param task The task to execute on the main thread.
* @return The "id" of the task. This will be the first argument to the {@code task_completed} event.
* @throws LuaException If the task could not be queued.
*/
long issueMainThreadTask( @Nonnull ILuaTask task ) throws LuaException;
}

View File

@ -0,0 +1,31 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2018. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/
package dan200.computercraft.api.lua;
import javax.annotation.Nonnull;
/**
* A function which calls performs an action in a specific context (such as on the server thread) and returns a result.
*
* @see MethodResult#onMainThread(ILuaCallable)
* @see ILuaContext#executeMainThreadTask(ILuaTask)
*/
@FunctionalInterface
public interface ILuaCallable
{
/**
* Run the code within the specified context and return the result to continue with.
*
* @return The result of executing this function. Note that this may not be evaluated within the same context as
* this call is.
* @throws LuaException If you throw any exception from this function, a lua error will be raised with the
* same message as your exception. Use this to throw appropriate errors if the wrong
* arguments are supplied to your method.
*/
@Nonnull
MethodResult execute() throws LuaException;
}

View File

@ -1,6 +1,6 @@
/* /*
* This file is part of the public ComputerCraft API - http://www.computercraft.info * This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only. * Copyright Daniel Ratcliffe, 2011-2018. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info. * For help using the API, and posting your mods, visit the forums at computercraft.info.
*/ */
@ -13,8 +13,11 @@ import javax.annotation.Nullable;
* An interface passed to peripherals and {@link ILuaObject}s by computers or turtles, providing methods * An interface passed to peripherals and {@link ILuaObject}s by computers or turtles, providing methods
* that allow the peripheral call to wait for events before returning, just like in lua. This is very useful if you need * that allow the peripheral call to wait for events before returning, just like in lua. This is very useful if you need
* to signal work to be performed on the main thread, and don't want to return until the work has been completed. * to signal work to be performed on the main thread, and don't want to return until the work has been completed.
*
* This interface mostly exists for integrating with older code. One should use {@link MethodResult} instead, as this
* encourages an asynchronous way of interacting with Lua coroutines.
*/ */
public interface ILuaContext public interface ILuaContext extends ICallContext
{ {
/** /**
* Wait for an event to occur on the computer, suspending the thread until it arises. This method is exactly * Wait for an event to occur on the computer, suspending the thread until it arises. This method is exactly
@ -30,8 +33,10 @@ public interface ILuaContext
* @throws InterruptedException If the user shuts down or reboots the computer while pullEvent() is waiting for an * @throws InterruptedException If the user shuts down or reboots the computer while pullEvent() is waiting for an
* event, InterruptedException will be thrown. This exception must not be caught or * event, InterruptedException will be thrown. This exception must not be caught or
* intercepted, or the computer will leak memory and end up in a broken state. * intercepted, or the computer will leak memory and end up in a broken state.
* @deprecated Use {@link MethodResult#pullEvent(String, ILuaFunction)}
*/ */
@Nonnull @Nonnull
@Deprecated
Object[] pullEvent( @Nullable String filter ) throws LuaException, InterruptedException; Object[] pullEvent( @Nullable String filter ) throws LuaException, InterruptedException;
/** /**
@ -45,8 +50,10 @@ public interface ILuaContext
* an event, InterruptedException will be thrown. This exception must not be caught or * an event, InterruptedException will be thrown. This exception must not be caught or
* intercepted, or the computer will leak memory and end up in a broken state. * intercepted, or the computer will leak memory and end up in a broken state.
* @see #pullEvent(String) * @see #pullEvent(String)
* @deprecated Use {@link MethodResult#pullEventRaw(String, ILuaFunction)}
*/ */
@Nonnull @Nonnull
@Deprecated
Object[] pullEventRaw( @Nullable String filter ) throws InterruptedException; Object[] pullEventRaw( @Nullable String filter ) throws InterruptedException;
/** /**
@ -59,8 +66,10 @@ public interface ILuaContext
* InterruptedException will be thrown. This exception must not be caught or * InterruptedException will be thrown. This exception must not be caught or
* intercepted, or the computer will leak memory and end up in a broken state. * intercepted, or the computer will leak memory and end up in a broken state.
* @see #pullEvent(String) * @see #pullEvent(String)
* @deprecated Use {@link MethodResult#pullEventRaw(ILuaFunction)}
*/ */
@Nonnull @Nonnull
@Deprecated
Object[] yield( @Nullable Object[] arguments ) throws InterruptedException; Object[] yield( @Nullable Object[] arguments ) throws InterruptedException;
/** /**
@ -76,22 +85,9 @@ public interface ILuaContext
* @throws InterruptedException If the user shuts down or reboots the computer the coroutine is suspended, * @throws InterruptedException If the user shuts down or reboots the computer the coroutine is suspended,
* InterruptedException will be thrown. This exception must not be caught or * InterruptedException will be thrown. This exception must not be caught or
* intercepted, or the computer will leak memory and end up in a broken state. * intercepted, or the computer will leak memory and end up in a broken state.
* @deprecated Use {@link MethodResult#onMainThread(ILuaCallable)}
*/ */
@Nullable @Nullable
@Deprecated
Object[] executeMainThreadTask( @Nonnull ILuaTask task ) throws LuaException, InterruptedException; Object[] executeMainThreadTask( @Nonnull ILuaTask task ) throws LuaException, InterruptedException;
/**
* Queue a task to be executed on the main server thread at the beginning of next tick, but do not wait for it to
* complete. This should be used when you need to interact with the world in a thread-safe manner but do not care
* about the result or you wish to run asynchronously.
*
* When the task has finished, it will enqueue a {@code task_completed} event, which takes the task id, a success
* value and the return values, or an error message if it failed. If you need to wait on this event, it may be
* better to use {@link #executeMainThreadTask(ILuaTask)}.
*
* @param task The task to execute on the main thread.
* @return The "id" of the task. This will be the first argument to the {@code task_completed} event.
* @throws LuaException If the task could not be queued.
*/
long issueMainThreadTask( @Nonnull ILuaTask task ) throws LuaException;
} }

View File

@ -0,0 +1,24 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2018. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/
package dan200.computercraft.api.lua;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* A function which executes using a {@link ILuaContext}.
*
* Like {@link ILuaContext}, this is not intended for use in the future - it purely exists as an argument for
* {@link MethodResult#withLuaContext(ILuaContextTask)}.
*/
@FunctionalInterface
public interface ILuaContextTask
{
@Nullable
@Deprecated
Object[] execute( @Nonnull ILuaContext context ) throws LuaException, InterruptedException;
}

View File

@ -0,0 +1,33 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2018. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/
package dan200.computercraft.api.lua;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* A Lua function which consumes some values and returns a result.
*
* @see MethodResult#then(ILuaFunction)
* @see MethodResult#pullEvent(ILuaFunction)
* @see MethodResult#pullEventRaw(String)
*/
@FunctionalInterface
public interface ILuaFunction
{
/**
* Accept the values and return another method result.
*
* @param values The inputs for this function.
* @return The result of executing this function.
* @throws LuaException If you throw any exception from this function, a lua error will be raised with the
* same message as your exception. Use this to throw appropriate errors if the wrong
* arguments are supplied to your method.
*/
@Nonnull
MethodResult call( @Nullable Object[] values ) throws LuaException;
}

View File

@ -1,6 +1,6 @@
/* /*
* This file is part of the public ComputerCraft API - http://www.computercraft.info * This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only. * Copyright Daniel Ratcliffe, 2011-2018. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info. * For help using the API, and posting your mods, visit the forums at computercraft.info.
*/ */
@ -41,16 +41,41 @@ public interface ILuaObject
* wishes to call. The integer indicates the index into the getMethodNames() table * wishes to call. The integer indicates the index into the getMethodNames() table
* that corresponds to the string passed into peripheral.call() * that corresponds to the string passed into peripheral.call()
* @param arguments The arguments for this method. See {@link IPeripheral#callMethod(IComputerAccess, ILuaContext, int, Object[])} * @param arguments The arguments for this method. See {@link IPeripheral#callMethod(IComputerAccess, ILuaContext, int, Object[])}
* the possible values and conversion rules. * for the possible values and conversion rules.
* @return An array of objects, representing the values you wish to return to the Lua program. * @return An array of objects, representing the values you wish to return to the Lua program. See
* See {@link IPeripheral#callMethod(IComputerAccess, ILuaContext, int, Object[])} for the valid values and * {@link MethodResult#of(Object...)} for the valid values and conversion rules.
* conversion rules.
* @throws LuaException If the task could not be queued, or if the task threw an exception. * @throws LuaException If the task could not be queued, or if the task threw an exception.
* @throws InterruptedException If the user shuts down or reboots the computer the coroutine is suspended, * @throws InterruptedException If the user shuts down or reboots the computer the coroutine is suspended,
* InterruptedException will be thrown. This exception must not be caught or * InterruptedException will be thrown. This exception must not be caught or
* intercepted, or the computer will leak memory and end up in a broken state.w * intercepted, or the computer will leak memory and end up in a broken state.w
* @see IPeripheral#callMethod(IComputerAccess, ILuaContext, int, Object[]) * @see IPeripheral#callMethod(IComputerAccess, ILuaContext, int, Object[])
* @deprecated Use {@link #callMethod(ICallContext, int, Object[])} instead.
*/ */
@Nullable @Nullable
@Deprecated
Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException; Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException;
/**
* Called when a user calls one of the methods that this object implements. This works the same as
* {@link IPeripheral#callMethod(IComputerAccess, ICallContext, int, Object[])}}. See that method for detailed
* documentation.
*
* @param context The context of the current call.
* @param method An integer identifying which of the methods from getMethodNames() the computercraft
* wishes to call. The integer indicates the index into the getMethodNames() table
* that corresponds to the string passed into peripheral.call()
* @param arguments The arguments for this method. See {@link IPeripheral#callMethod(IComputerAccess, ICallContext, int, Object[])}
* for the possible values and conversion rules.
* @return The result of calling this method. Use {@link MethodResult#empty()} to return nothing or
* {@link MethodResult#of(Object...)} to return several values.
* @throws LuaException If the task could not be queued, or if the task threw an exception.
* @see IPeripheral#callMethod(IComputerAccess, ICallContext, int, Object[])
* @see MethodResult
*/
@Nonnull
@SuppressWarnings({ "deprecation" })
default MethodResult callMethod( @Nonnull ICallContext context, int method, @Nonnull Object[] arguments ) throws LuaException
{
return MethodResult.withLuaContext( lua -> callMethod( lua, method, arguments ) );
}
} }

View File

@ -0,0 +1,105 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2018. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/
package dan200.computercraft.api.lua;
import javax.annotation.Nonnull;
import java.util.ArrayDeque;
import java.util.Deque;
/**
* Evaluates {@link MethodResult}s within a {@link ILuaContext}.
*
* @see MethodResult#evaluate(ILuaContext)
* @see MethodResult#withLuaContext(ILuaContextTask)
* @deprecated This should not be used except to interface between the two call call systems.
*/
@Deprecated
class LuaContextResultEvaluator
{
@Deprecated
public static Object[] evaluate( @Nonnull ILuaContext context, @Nonnull MethodResult future ) throws LuaException, InterruptedException
{
Deque<ILuaFunction> callbacks = null;
while( true )
{
if( future instanceof MethodResult.AndThen )
{
MethodResult.AndThen then = ((MethodResult.AndThen) future);
// Thens are "unwrapped", being pushed onto a stack
if( callbacks == null ) callbacks = new ArrayDeque<>();
callbacks.addLast( then.getCallback() );
future = then.getPrevious();
if( future == null ) throw new NullPointerException( "Null result from " + then.getCallback() );
}
else if( future instanceof MethodResult.Immediate )
{
Object[] values = ((MethodResult.Immediate) future).getResult();
// Immediate values values will attempt to call the previous "then", or return if nothing
// else needs to be done.
ILuaFunction callback = callbacks == null ? null : callbacks.pollLast();
if( callback == null ) return values;
future = callback.call( values );
if( future == null ) throw new NullPointerException( "Null result from " + callback );
}
else if( future instanceof MethodResult.OnEvent )
{
MethodResult.OnEvent onEvent = (MethodResult.OnEvent) future;
// Poll for an event, and then call the previous "then" or return if nothing else needs
// to be done.
Object[] values = onEvent.isRaw() ? context.pullEventRaw( onEvent.getFilter() ) : context.pullEvent( onEvent.getFilter() );
ILuaFunction callback = callbacks == null ? null : callbacks.pollLast();
if( callback == null ) return values;
future = callback.call( values );
if( future == null ) throw new NullPointerException( "Null result from " + callback );
}
else if( future instanceof MethodResult.OnMainThread )
{
MethodResult.OnMainThread onMainThread = (MethodResult.OnMainThread) future;
// Evaluate our task on the main thread and mark it as the next future to evaluate.
Reference temporary = new Reference();
context.executeMainThreadTask( () -> {
temporary.value = onMainThread.getTask().execute();
return null;
} );
future = temporary.value;
if( future == null ) throw new NullPointerException( "Null result from " + onMainThread.getTask() );
}
else if( future instanceof MethodResult.WithLuaContext )
{
MethodResult.WithLuaContext withContext = (MethodResult.WithLuaContext) future;
// Run the task, and then call the previous "then" or return if nothing else
// needs to be done.
Object[] values = withContext.getConsumer().execute( context );
ILuaFunction callback = callbacks == null ? null : callbacks.pollLast();
if( callback == null ) return values;
future = callback.call( values );
if( future == null ) throw new NullPointerException( "Null result from " + callback );
}
else
{
throw new IllegalStateException( "Unknown MethodResult " + future );
}
}
}
private static class Reference
{
MethodResult value;
}
}

View File

@ -0,0 +1,344 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2018. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/
package dan200.computercraft.api.lua;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.ListenableFuture;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Map;
/**
* The result of calling a method, such as {@link ILuaObject#callMethod(ICallContext, int, Object[])} or
* {@link IPeripheral#callMethod(IComputerAccess, ICallContext, int, Object[])}.
*
* This is non-dissimilar to a promise or {@link ListenableFuture}. One can either return an immediate value through
* {@link #of(Object...)}, wait for an external action with {@link #onMainThread(ILuaCallable)} or {@link #pullEvent()}
* and then act on the result of either of those by using {@link #then(ILuaFunction)}.
*/
public abstract class MethodResult
{
private static MethodResult empty;
MethodResult()
{
}
/**
* A result which returns immediately with no value.
*
* Use {@link #of(Object...)} if you need to return one or more values.
*
* @return The empty method result.
* @see #of(Object...)
*/
@Nonnull
public static MethodResult empty()
{
if( empty == null ) empty = new Immediate( null );
return empty;
}
/**
* A result which returns several values.
*
* @param result The values to return, this may be {@code null}. {@link Number}s, {@link String}s, {@link Boolean}s,
* {@link Map}s, {@link ILuaObject}s, and {@code null} be converted to their corresponding lua type.
* All other types will be converted to nil.
* @return A result which will return these values when evaluated.
* @see #empty()
*/
@Nonnull
public static MethodResult of( Object... result )
{
return result == null ? empty() : new Immediate( result );
}
/**
* Wait for an event to occur on the computer, suspending the coroutine until it arises. This method is equivalent
* to {@code os.pullEvent()} in Lua.
*
* Normally you'll wish to consume the event using {@link #then(ILuaFunction)}. This can be done slightly more
* easily with {@link #pullEvent(ILuaFunction)}.
*
* If you want to listen to a specific event, it's easier to use {@link #pullEvent(String)} rather than
* running until the desired event is found.
*
* @return The constructed method result. This evaluates to the name of the event that occurred, and any event
* parameters.
* @see #pullEvent(ILuaFunction)
* @see #pullEvent(String)
*/
@Nonnull
public static MethodResult pullEvent()
{
return new OnEvent( false, null );
}
/**
* Wait for the specified event to occur on the computer, suspending the coroutine until it arises. This method is
* equivalent to {@code os.pullEvent(event)} in Lua.
*
* Normally you'll wish to consume the event using {@link #then(ILuaFunction)}. This can be done slightly more
* easily with {@link #pullEvent(String, ILuaFunction)}.
*
* @return The constructed method result. This evaluates to the name of the event that occurred, and any event
* parameters.
* @see #pullEvent(String, ILuaFunction)
* @see #pullEvent()
*/
@Nonnull
public static MethodResult pullEvent( @Nonnull String event )
{
Preconditions.checkNotNull( event, "event cannot be null" );
return new OnEvent( false, event );
}
/**
* Wait for an event to occur on the computer, suspending the coroutine until it arises. This method to
* {@link #pullEvent()} and {@link #then(ILuaFunction)}.
*
* If you want to listen to a specific event, it's easier to use {@link #pullEvent(String, ILuaFunction)} rather
* than running until the desired event is found.
*
* @return The constructed method result. This evaluates to the result of the {@code callback}.
* @see #pullEvent()
* @see #pullEvent(String, ILuaFunction)
*/
@Nonnull
public static MethodResult pullEvent( @Nonnull ILuaFunction callback )
{
Preconditions.checkNotNull( callback, "callback cannot be null" );
return new OnEvent( false, null ).then( callback );
}
/**
* Wait for the specified event to occur on the computer, suspending the coroutine until it arises. This method to
* {@link #pullEvent(String)} and {@link #then(ILuaFunction)}.
*
* @return The constructed method result. This evaluates to the result of the {@code callback}.
* @see #pullEvent(String)
* @see #pullEvent(ILuaFunction)
*/
@Nonnull
public static MethodResult pullEvent( @Nullable String filter, @Nonnull ILuaFunction callback )
{
Preconditions.checkNotNull( callback, "callback cannot be null" );
return new OnEvent( false, filter ).then( callback );
}
/**
* The same as {@link #pullEvent()}, except {@code terminated} events are also passed to the callback, instead of
* throwing an error. Only use this if you want to prevent program termination, which is not recommended.
*
* @return The constructed method result. This evaluates to the name of the event that occurred, and any event
* parameters.
*/
@Nonnull
public static MethodResult pullEventRaw()
{
return new OnEvent( true, null );
}
/**
* The same as {@link #pullEvent(String)}, except {@code terminated} events are also passed to the callback, instead
* of throwing an error. Only use this if you want to prevent program termination, which is not recommended.
*
* @return The constructed method result. This evaluates to the name of the event that occurred, and any event
* parameters.
*/
@Nonnull
public static MethodResult pullEventRaw( @Nonnull String event )
{
return new OnEvent( true, event );
}
/**
* The same as {@link #pullEvent(ILuaFunction)}, except {@code terminated} events are also passed to the callback,
* instead of throwing an error. Only use this if you want to prevent program termination, which is not recommended.
*
* @return The constructed method result. This evaluates to the result of the {@code callback}.
*/
@Nonnull
public static MethodResult pullEventRaw( @Nonnull ILuaFunction callback )
{
Preconditions.checkNotNull( callback, "callback cannot be null" );
return new OnEvent( true, null ).then( callback );
}
/**
* The same as {@link #pullEvent(String, ILuaFunction)}, except {@code terminated} events are also passed to the
* callback, instead of throwing an error. Only use this if you want to prevent program termination, which is not
* recommended.
*
* @return The constructed method result. This evaluates to the result of the {@code callback}.
*/
@Nonnull
public static MethodResult pullEventRaw( @Nullable String filter, @Nonnull ILuaFunction callback )
{
Preconditions.checkNotNull( callback, "callback cannot be null" );
return new OnEvent( true, filter ).then( callback );
}
/**
* Queue a task to be executed on the main server thread at the beginning of next tick, waiting for it to complete.
* This should be used when you need to interact with the world in a thread-safe manner.
*
* @param callback The task to execute on the server thread.
* @return The constructed method result, which evaluates to the result of the {@code callback}.
*/
@Nonnull
public static MethodResult onMainThread( @Nonnull ILuaCallable callback )
{
Preconditions.checkNotNull( callback, "callback cannot be null" );
return new OnMainThread( callback );
}
/**
* Consume the result of this {@link MethodResult} and return another result.
*
* Note this does NOT modify the current method result, rather returning a new (wrapped) one. You must return the
* result of this call if you wish to use it.
*
* @param callback The function which consumes the provided values.
* @return The constructed method result.
*/
@Nonnull
public final MethodResult then( @Nonnull ILuaFunction callback )
{
Preconditions.checkNotNull( callback, "callback cannot be null" );
return new AndThen( this, callback );
}
/**
* Execute a blocking task within a {@link ILuaContext} and return its result.
*
* @param consumer The task to execute with the provided Lua context.
* @return The constructed method result.
* @see #evaluate(ILuaContext)
* @deprecated This should not be used except to interface between the two call systems.
*/
@Deprecated
public static MethodResult withLuaContext( @Nonnull ILuaContextTask consumer )
{
Preconditions.checkNotNull( consumer, "consumer cannot be null" );
return new WithLuaContext( consumer );
}
/**
* Evaluate this result task using {@link ILuaContext} and return its result.
*
* @param context The context to execute with.
* @return The resulting values.
* @see #withLuaContext(ILuaContextTask)
* @deprecated This should not be used except to interface between the two call systems.
*/
@Deprecated
public final Object[] evaluate( @Nonnull ILuaContext context ) throws LuaException, InterruptedException
{
return LuaContextResultEvaluator.evaluate( context, this );
}
public static class Immediate extends MethodResult
{
@Nullable
private final Object[] values;
@Nullable
private Immediate( Object[] values )
{
this.values = values;
}
public Object[] getResult()
{
return values;
}
}
public static class OnEvent extends MethodResult
{
private final boolean raw;
private final String filter;
private OnEvent( boolean raw, String filter )
{
this.raw = raw;
this.filter = filter;
}
public boolean isRaw()
{
return raw;
}
@Nullable
public String getFilter()
{
return filter;
}
}
public static class OnMainThread extends MethodResult
{
private final ILuaCallable task;
public OnMainThread( ILuaCallable task )
{
this.task = task;
}
@Nonnull
public ILuaCallable getTask()
{
return task;
}
}
public static class AndThen extends MethodResult
{
private final MethodResult previous;
private final ILuaFunction callback;
private AndThen( MethodResult previous, ILuaFunction callback )
{
this.previous = previous;
this.callback = callback;
}
@Nonnull
public MethodResult getPrevious()
{
return previous;
}
@Nonnull
public ILuaFunction getCallback()
{
return callback;
}
}
public static class WithLuaContext extends MethodResult
{
private final ILuaContextTask consumer;
private WithLuaContext( ILuaContextTask consumer )
{
this.consumer = consumer;
}
@Nonnull
public ILuaContextTask getConsumer()
{
return consumer;
}
}
}

View File

@ -1,13 +1,15 @@
/* /*
* This file is part of the public ComputerCraft API - http://www.computercraft.info * This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only. * Copyright Daniel Ratcliffe, 2011-2018. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info. * For help using the API, and posting your mods, visit the forums at computercraft.info.
*/ */
package dan200.computercraft.api.peripheral; package dan200.computercraft.api.peripheral;
import dan200.computercraft.api.lua.ICallContext;
import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.MethodResult;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -70,10 +72,46 @@ public interface IPeripheral
* InterruptedException will be thrown. This exception must not be caught or * InterruptedException will be thrown. This exception must not be caught or
* intercepted, or the computer will leak memory and end up in a broken state. * intercepted, or the computer will leak memory and end up in a broken state.
* @see #getMethodNames * @see #getMethodNames
* @deprecated Use {@link #callMethod(IComputerAccess, ICallContext, int, Object[])} instead.
*/ */
@Nullable @Nullable
@Deprecated
Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException; Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException;
/**
* This is called when a lua program on an attached computer calls {@code peripheral.call()} with
* one of the methods exposed by {@link #getMethodNames()}.
*
* Be aware that this will be called from the ComputerCraft Lua thread, and must be thread-safe
* when interacting with Minecraft objects.
*
* @param computer The interface to the computer that is making the call. Remember that multiple
* computers can be attached to a peripheral at once.
* @param context The context of the current call.
* @param method An integer identifying which of the methods from getMethodNames() the computercraft
* wishes to call. The integer indicates the index into the getMethodNames() table
* that corresponds to the string passed into peripheral.call()
* @param arguments An array of objects, representing the arguments passed into {@code peripheral.call()}.<br>
* Lua values of type "string" will be represented by Object type String.<br>
* Lua values of type "number" will be represented by Object type Double.<br>
* Lua values of type "boolean" will be represented by Object type Boolean.<br>
* Lua values of type "table" will be represented by Object type Map.<br>
* Lua values of any other type will be represented by a null object.<br>
* This array will be empty if no arguments are passed.
* @return The result of calling this method. Use {@link MethodResult#empty()} to return nothing or
* {@link MethodResult#of(Object...)} to return several values.
* @throws LuaException If you throw any exception from this function, a lua error will be raised with the
* same message as your exception. Use this to throw appropriate errors if the wrong
* arguments are supplied to your method.
* @see #getMethodNames
*/
@Nonnull
@SuppressWarnings({ "deprecation" })
default MethodResult callMethod( @Nonnull IComputerAccess computer, @Nonnull ICallContext context, int method, @Nonnull Object[] arguments ) throws LuaException
{
return MethodResult.withLuaContext( lua -> callMethod( computer, lua, method, arguments ) );
}
/** /**
* Is called when canAttachToSide has returned true, and a computer is attaching to the peripheral. * Is called when canAttachToSide has returned true, and a computer is attaching to the peripheral.
* *

View File

@ -9,6 +9,7 @@ package dan200.computercraft.api.turtle;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
@ -238,10 +239,30 @@ public interface ITurtleAccess
* intercepted, or the computer will leak memory and end up in a broken state. * intercepted, or the computer will leak memory and end up in a broken state.
* @see ITurtleCommand * @see ITurtleCommand
* @see ILuaContext#pullEvent(String) * @see ILuaContext#pullEvent(String)
* @deprecated Use {@link #executeCommand(ITurtleCommand)} instead.
*/ */
@Nonnull @Nonnull
@Deprecated
Object[] executeCommand( @Nonnull ILuaContext context, @Nonnull ITurtleCommand command ) throws LuaException, InterruptedException; Object[] executeCommand( @Nonnull ILuaContext context, @Nonnull ITurtleCommand command ) throws LuaException, InterruptedException;
/**
* Adds a custom command to the turtles command queue. Unlike peripheral methods, these custom commands will be
* executed on the main thread, so are guaranteed to be able to access Minecraft objects safely, and will be queued
* up with the turtles standard movement and tool commands.
*
* An issued command will return an unique integer, which will be supplied as a parameter to a "turtle_response"
* event issued to the turtle after the command has completed. Look at the Lua source code for "rom/apis/turtle" for
* how to build a Lua wrapper around this functionality.
*
* @param command An object which will execute the custom command when its point in the queue is reached
* @return The constructed method result. This evaluates to the result of the provided {@code command}.
* @throws UnsupportedOperationException When attempting to execute a command on the client side.
* @see ITurtleCommand
* @see MethodResult#pullEvent(String)
*/
@Nonnull
MethodResult executeCommand( @Nonnull ITurtleCommand command );
/** /**
* Start playing a specific animation. This will prevent other turtle commands from executing until * Start playing a specific animation. This will prevent other turtle commands from executing until
* it is finished. * it is finished.

View File

@ -133,7 +133,7 @@ public interface ITurtleUpgrade
* @return The model that you wish to be used to render your upgrade, and a transformation to apply to it. Returning * @return The model that you wish to be used to render your upgrade, and a transformation to apply to it. Returning
* a transformation of {@code null} has the same effect as the identify matrix. * a transformation of {@code null} has the same effect as the identify matrix.
*/ */
@SideOnly(Side.CLIENT) @SideOnly( Side.CLIENT )
@Nonnull @Nonnull
Pair<IBakedModel, Matrix4f> getModel( @Nullable ITurtleAccess turtle, @Nonnull TurtleSide side ); Pair<IBakedModel, Matrix4f> getModel( @Nullable ITurtleAccess turtle, @Nonnull TurtleSide side );

View File

@ -6,13 +6,12 @@
package dan200.computercraft.core.apis; package dan200.computercraft.core.apis;
import dan200.computercraft.api.lua.*;
import dan200.computercraft.api.lua.ILuaAPI; import dan200.computercraft.api.lua.ILuaAPI;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.ILuaObject;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.core.terminal.TextBuffer; import dan200.computercraft.core.terminal.TextBuffer;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static dan200.computercraft.core.apis.ArgumentHelper.getString; import static dan200.computercraft.core.apis.ArgumentHelper.getString;
import static dan200.computercraft.core.apis.ArgumentHelper.optInt; import static dan200.computercraft.core.apis.ArgumentHelper.optInt;
@ -41,27 +40,28 @@ public class BufferAPI implements ILuaAPI
}; };
} }
@Nonnull
@Override @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 ) switch( method )
{ {
case 0: case 0:
{ {
// len // len
return new Object[] { m_buffer.length() }; return MethodResult.of( m_buffer.length() );
} }
case 1: case 1:
{ {
// tostring // tostring
return new Object[] { m_buffer.toString() }; return MethodResult.of( m_buffer.toString() );
} }
case 2: case 2:
{ {
// read // read
int start = optInt( arguments, 0, 0 ); int start = optInt( arguments, 0, 0 );
int end = optInt( arguments, 1, m_buffer.length() ); 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: case 3:
{ {
@ -70,7 +70,7 @@ public class BufferAPI implements ILuaAPI
int start = optInt( arguments, 1, 0 ); int start = optInt( arguments, 1, 0 );
int end = optInt( arguments, 2, start + text.length() ); int end = optInt( arguments, 2, start + text.length() );
m_buffer.write( text, start, end ); m_buffer.write( text, start, end );
return null; return MethodResult.empty();
} }
case 4: case 4:
{ {
@ -79,14 +79,22 @@ public class BufferAPI implements ILuaAPI
int start = optInt( arguments, 1, 0 ); int start = optInt( arguments, 1, 0 );
int end = optInt( arguments, 2, m_buffer.length() ); int end = optInt( arguments, 2, m_buffer.length() );
m_buffer.fill( text, start, end ); m_buffer.fill( text, start, end );
return null; return MethodResult.empty();
} }
default: 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 ) public BufferAPI( IAPIEnvironment _env )
@ -110,8 +118,9 @@ public class BufferAPI implements ILuaAPI
}; };
} }
@Nonnull
@Override @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 ) switch( method )
{ {
@ -124,12 +133,20 @@ public class BufferAPI implements ILuaAPI
throw ArgumentHelper.badArgument( 1, "positive number", Integer.toString( repetitions ) ); throw ArgumentHelper.badArgument( 1, "positive number", Integer.toString( repetitions ) );
} }
TextBuffer buffer = new TextBuffer( text, repetitions ); TextBuffer buffer = new TextBuffer( text, repetitions );
return new Object[] { new BufferLuaObject( buffer ) }; return MethodResult.of( new BufferLuaObject( buffer ) );
} }
default: 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 );
}
} }

View File

@ -6,9 +6,8 @@
package dan200.computercraft.core.apis; package dan200.computercraft.core.apis;
import dan200.computercraft.api.lua.*;
import dan200.computercraft.api.lua.ILuaAPI; import dan200.computercraft.api.lua.ILuaAPI;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.core.apis.handles.BinaryInputHandle; import dan200.computercraft.core.apis.handles.BinaryInputHandle;
import dan200.computercraft.core.apis.handles.BinaryOutputHandle; import dan200.computercraft.core.apis.handles.BinaryOutputHandle;
import dan200.computercraft.core.apis.handles.EncodedInputHandle; import dan200.computercraft.core.apis.handles.EncodedInputHandle;
@ -18,6 +17,7 @@ import dan200.computercraft.core.filesystem.FileSystemException;
import dan200.computercraft.core.tracking.TrackingField; import dan200.computercraft.core.tracking.TrackingField;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.HashMap; import java.util.HashMap;
@ -80,8 +80,9 @@ public class FSAPI implements ILuaAPI
}; };
} }
@Nonnull
@Override @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 ) switch( method )
{ {
@ -96,7 +97,7 @@ public class FSAPI implements ILuaAPI
for(int i=0; i<results.length; ++i ) { for(int i=0; i<results.length; ++i ) {
table.put( i+1, results[i] ); table.put( i+1, results[i] );
} }
return new Object[] { table }; return MethodResult.of( table );
} }
catch( FileSystemException e ) catch( FileSystemException e )
{ {
@ -108,13 +109,13 @@ public class FSAPI implements ILuaAPI
// combine // combine
String pathA = getString( args, 0 ); String pathA = getString( args, 0 );
String pathB = getString( args, 1 ); String pathB = getString( args, 1 );
return new Object[] { m_fileSystem.combine( pathA, pathB ) }; return MethodResult.of( m_fileSystem.combine( pathA, pathB ) );
} }
case 2: case 2:
{ {
// getName // getName
String path = getString( args, 0 ); String path = getString( args, 0 );
return new Object[]{ FileSystem.getName( path ) }; return MethodResult.of( FileSystem.getName( path ) );
} }
case 3: case 3:
{ {
@ -122,7 +123,7 @@ public class FSAPI implements ILuaAPI
String path = getString( args, 0 ); String path = getString( args, 0 );
try try
{ {
return new Object[]{ m_fileSystem.getSize( path ) }; return MethodResult.of( m_fileSystem.getSize( path ) );
} }
catch( FileSystemException e ) catch( FileSystemException e )
{ {
@ -134,9 +135,9 @@ public class FSAPI implements ILuaAPI
// exists // exists
String path = getString( args, 0 ); String path = getString( args, 0 );
try { try {
return new Object[]{ m_fileSystem.exists( path ) }; return MethodResult.of( m_fileSystem.exists( path ) );
} catch( FileSystemException e ) { } catch( FileSystemException e ) {
return new Object[]{ false }; return MethodResult.of( false );
} }
} }
case 5: case 5:
@ -144,9 +145,9 @@ public class FSAPI implements ILuaAPI
// isDir // isDir
String path = getString( args, 0 ); String path = getString( args, 0 );
try { try {
return new Object[]{ m_fileSystem.isDir( path ) }; return MethodResult.of( m_fileSystem.isDir( path ) );
} catch( FileSystemException e ) { } catch( FileSystemException e ) {
return new Object[]{ false }; return MethodResult.of( false );
} }
} }
case 6: case 6:
@ -154,9 +155,9 @@ public class FSAPI implements ILuaAPI
// isReadOnly // isReadOnly
String path = getString( args, 0 ); String path = getString( args, 0 );
try { try {
return new Object[]{ m_fileSystem.isReadOnly( path ) }; return MethodResult.of( m_fileSystem.isReadOnly( path ) );
} catch( FileSystemException e ) { } catch( FileSystemException e ) {
return new Object[]{ false }; return MethodResult.of( false );
} }
} }
case 7: case 7:
@ -166,7 +167,7 @@ public class FSAPI implements ILuaAPI
try { try {
m_env.addTrackingChange( TrackingField.FS_OPS ); m_env.addTrackingChange( TrackingField.FS_OPS );
m_fileSystem.makeDir( path ); m_fileSystem.makeDir( path );
return null; return MethodResult.empty();
} catch( FileSystemException e ) { } catch( FileSystemException e ) {
throw new LuaException( e.getMessage() ); throw new LuaException( e.getMessage() );
} }
@ -179,7 +180,7 @@ public class FSAPI implements ILuaAPI
try { try {
m_env.addTrackingChange( TrackingField.FS_OPS ); m_env.addTrackingChange( TrackingField.FS_OPS );
m_fileSystem.move( path, dest ); m_fileSystem.move( path, dest );
return null; return MethodResult.empty();
} catch( FileSystemException e ) { } catch( FileSystemException e ) {
throw new LuaException( e.getMessage() ); throw new LuaException( e.getMessage() );
} }
@ -192,7 +193,7 @@ public class FSAPI implements ILuaAPI
try { try {
m_env.addTrackingChange( TrackingField.FS_OPS ); m_env.addTrackingChange( TrackingField.FS_OPS );
m_fileSystem.copy( path, dest ); m_fileSystem.copy( path, dest );
return null; return MethodResult.empty();
} catch( FileSystemException e ) { } catch( FileSystemException e ) {
throw new LuaException( e.getMessage() ); throw new LuaException( e.getMessage() );
} }
@ -204,7 +205,7 @@ public class FSAPI implements ILuaAPI
try { try {
m_env.addTrackingChange( TrackingField.FS_OPS ); m_env.addTrackingChange( TrackingField.FS_OPS );
m_fileSystem.delete( path ); m_fileSystem.delete( path );
return null; return MethodResult.empty();
} catch( FileSystemException e ) { } catch( FileSystemException e ) {
throw new LuaException( e.getMessage() ); throw new LuaException( e.getMessage() );
} }
@ -222,43 +223,43 @@ public class FSAPI implements ILuaAPI
{ {
// Open the file for reading, then create a wrapper around the reader // Open the file for reading, then create a wrapper around the reader
InputStream reader = m_fileSystem.openForRead( path ); InputStream reader = m_fileSystem.openForRead( path );
return new Object[] { new EncodedInputHandle( reader ) }; return MethodResult.of( new EncodedInputHandle( reader ) );
} }
case "w": case "w":
{ {
// Open the file for writing, then create a wrapper around the writer // Open the file for writing, then create a wrapper around the writer
OutputStream writer = m_fileSystem.openForWrite( path, false ); OutputStream writer = m_fileSystem.openForWrite( path, false );
return new Object[] { new EncodedOutputHandle( writer ) }; return MethodResult.of( new EncodedOutputHandle( writer ) );
} }
case "a": case "a":
{ {
// Open the file for appending, then create a wrapper around the writer // Open the file for appending, then create a wrapper around the writer
OutputStream writer = m_fileSystem.openForWrite( path, true ); OutputStream writer = m_fileSystem.openForWrite( path, true );
return new Object[] { new EncodedOutputHandle( writer ) }; return MethodResult.of( new EncodedOutputHandle( writer ) );
} }
case "rb": case "rb":
{ {
// Open the file for binary reading, then create a wrapper around the reader // Open the file for binary reading, then create a wrapper around the reader
InputStream reader = m_fileSystem.openForRead( path ); InputStream reader = m_fileSystem.openForRead( path );
return new Object[] { new BinaryInputHandle( reader ) }; return MethodResult.of( new BinaryInputHandle( reader ) );
} }
case "wb": case "wb":
{ {
// Open the file for binary writing, then create a wrapper around the writer // Open the file for binary writing, then create a wrapper around the writer
OutputStream writer = m_fileSystem.openForWrite( path, false ); OutputStream writer = m_fileSystem.openForWrite( path, false );
return new Object[] { new BinaryOutputHandle( writer ) }; return MethodResult.of( new BinaryOutputHandle( writer ) );
} }
case "ab": case "ab":
{ {
// Open the file for binary appending, then create a wrapper around the reader // Open the file for binary appending, then create a wrapper around the reader
OutputStream writer = m_fileSystem.openForWrite( path, true ); OutputStream writer = m_fileSystem.openForWrite( path, true );
return new Object[] { new BinaryOutputHandle( writer ) }; return MethodResult.of( new BinaryOutputHandle( writer ) );
} }
default: default:
throw new LuaException( "Unsupported mode" ); throw new LuaException( "Unsupported mode" );
} }
} catch( FileSystemException e ) { } catch( FileSystemException e ) {
return new Object[] { null, e.getMessage() }; return MethodResult.of( null, e.getMessage() );
} }
} }
case 12: case 12:
@ -268,9 +269,9 @@ public class FSAPI implements ILuaAPI
try { try {
if( !m_fileSystem.exists( path ) ) if( !m_fileSystem.exists( path ) )
{ {
return null; return MethodResult.empty();
} }
return new Object[]{ m_fileSystem.getMountLabel( path ) }; return MethodResult.of( m_fileSystem.getMountLabel( path ) );
} catch( FileSystemException e ) { } catch( FileSystemException e ) {
throw new LuaException( e.getMessage() ); throw new LuaException( e.getMessage() );
} }
@ -283,9 +284,9 @@ public class FSAPI implements ILuaAPI
long freeSpace = m_fileSystem.getFreeSpace( path ); long freeSpace = m_fileSystem.getFreeSpace( path );
if( freeSpace >= 0 ) if( freeSpace >= 0 )
{ {
return new Object[]{ freeSpace }; return MethodResult.of( freeSpace );
} }
return new Object[]{ "unlimited" }; return MethodResult.of( "unlimited" );
} catch( FileSystemException e ) { } catch( FileSystemException e ) {
throw new LuaException( e.getMessage() ); throw new LuaException( e.getMessage() );
} }
@ -301,7 +302,7 @@ public class FSAPI implements ILuaAPI
for(int i=0; i<results.length; ++i ) { for(int i=0; i<results.length; ++i ) {
table.put( i+1, results[i] ); table.put( i+1, results[i] );
} }
return new Object[] { table }; return MethodResult.of( table );
} catch( FileSystemException e ) { } catch( FileSystemException e ) {
throw new LuaException( e.getMessage() ); throw new LuaException( e.getMessage() );
} }
@ -310,13 +311,21 @@ public class FSAPI implements ILuaAPI
{ {
// getDir // getDir
String path = getString( args, 0 ); String path = getString( args, 0 );
return new Object[]{ FileSystem.getDirectory( path ) }; return MethodResult.of( FileSystem.getDirectory( path ) );
} }
default: default:
{ {
assert( false ); assert( false );
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 );
}
} }

View File

@ -8,12 +8,12 @@ package dan200.computercraft.core.apis;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import dan200.computercraft.ComputerCraft; import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.lua.*;
import dan200.computercraft.api.lua.ILuaAPI; import dan200.computercraft.api.lua.ILuaAPI;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.core.apis.http.*; import dan200.computercraft.core.apis.http.*;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
@ -102,8 +102,9 @@ public class HTTPAPI implements ILuaAPI
}; };
} }
@Nonnull
@Override @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 ) switch( method )
{ {
@ -164,11 +165,11 @@ public class HTTPAPI implements ILuaAPI
{ {
m_httpTasks.add( HTTPExecutor.EXECUTOR.submit( request ) ); m_httpTasks.add( HTTPExecutor.EXECUTOR.submit( request ) );
} }
return new Object[]{ true }; return MethodResult.of(true);
} }
catch( HTTPRequestException e ) catch( HTTPRequestException e )
{ {
return new Object[]{ false, e.getMessage() }; return MethodResult.of( false, e.getMessage() );
} }
} }
case 1: case 1:
@ -186,11 +187,11 @@ public class HTTPAPI implements ILuaAPI
{ {
m_httpTasks.add( HTTPExecutor.EXECUTOR.submit( check ) ); m_httpTasks.add( HTTPExecutor.EXECUTOR.submit( check ) );
} }
return new Object[]{ true }; return MethodResult.of( true );
} }
catch( HTTPRequestException e ) catch( HTTPRequestException e )
{ {
return new Object[]{ false, e.getMessage() }; return MethodResult.of( false, e.getMessage() );
} }
} }
case 2: // websocket case 2: // websocket
@ -223,20 +224,28 @@ public class HTTPAPI implements ILuaAPI
{ {
m_httpTasks.add( connector ); m_httpTasks.add( connector );
} }
return new Object[]{ true }; return MethodResult.of(true);
} }
catch( HTTPRequestException e ) catch( HTTPRequestException e )
{ {
return new Object[]{ false, e.getMessage() }; return MethodResult.of( false, e.getMessage() );
} }
} }
default: 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 void addCloseable( Closeable closeable ) public void addCloseable( Closeable closeable )
{ {
synchronized( m_closeables ) synchronized( m_closeables )

View File

@ -6,12 +6,12 @@
package dan200.computercraft.core.apis; package dan200.computercraft.core.apis;
import dan200.computercraft.api.lua.*;
import dan200.computercraft.api.lua.ILuaAPI; import dan200.computercraft.api.lua.ILuaAPI;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.shared.util.StringUtil; import dan200.computercraft.shared.util.StringUtil;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.*; import java.util.*;
import static dan200.computercraft.core.apis.ArgumentHelper.*; import static dan200.computercraft.core.apis.ArgumentHelper.*;
@ -221,7 +221,8 @@ public class OSAPI implements ILuaAPI
} }
@Override @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 ) switch( method )
{ {
@ -229,7 +230,7 @@ public class OSAPI implements ILuaAPI
{ {
// queueEvent // queueEvent
queueLuaEvent( getString( args, 0 ), trimArray( args, 1 ) ); queueLuaEvent( getString( args, 0 ), trimArray( args, 1 ) );
return null; return MethodResult.empty();
} }
case 1: case 1:
{ {
@ -238,7 +239,7 @@ public class OSAPI implements ILuaAPI
synchronized( m_timers ) synchronized( m_timers )
{ {
m_timers.put( m_nextTimerToken, new Timer( (int)Math.round( timer / 0.05 ) ) ); 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: case 2:
@ -253,33 +254,33 @@ public class OSAPI implements ILuaAPI
{ {
int day = (time > m_time) ? m_day : (m_day + 1); int day = (time > m_time) ? m_day : (m_day + 1);
m_alarms.put( m_nextAlarmToken, new Alarm( time, day ) ); m_alarms.put( m_nextAlarmToken, new Alarm( time, day ) );
return new Object[] { m_nextAlarmToken++ }; return MethodResult.of( m_nextAlarmToken++ );
} }
} }
case 3: case 3:
{ {
// shutdown // shutdown
m_apiEnvironment.shutdown(); m_apiEnvironment.shutdown();
return null; return MethodResult.empty();
} }
case 4: case 4:
{ {
// reboot // reboot
m_apiEnvironment.reboot(); m_apiEnvironment.reboot();
return null; return MethodResult.empty();
} }
case 5: case 5:
case 6: case 6:
{ {
// computerID/getComputerID // computerID/getComputerID
return new Object[] { getComputerID() }; return MethodResult.of( getComputerID() );
} }
case 7: case 7:
{ {
// setComputerLabel // setComputerLabel
String label = optString( args, 0, null ); String label = optString( args, 0, null );
m_apiEnvironment.setLabel( StringUtil.normaliseLabel( label ) ); m_apiEnvironment.setLabel( StringUtil.normaliseLabel( label ) );
return null; return MethodResult.empty();
} }
case 8: case 8:
case 9: case 9:
@ -288,16 +289,16 @@ public class OSAPI implements ILuaAPI
String label = m_apiEnvironment.getLabel(); String label = m_apiEnvironment.getLabel();
if( label != null ) if( label != null )
{ {
return new Object[] { label }; return MethodResult.of( label );
} }
return null; return MethodResult.empty();
} }
case 10: case 10:
{ {
// clock // clock
synchronized( m_timers ) synchronized( m_timers )
{ {
return new Object[] { m_clock * 0.05 }; return MethodResult.of( m_clock * 0.05 );
} }
} }
case 11: case 11:
@ -310,19 +311,19 @@ public class OSAPI implements ILuaAPI
{ {
// Get Hour of day (UTC) // Get Hour of day (UTC)
Calendar c = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) ); Calendar c = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) );
return new Object[] { getTimeForCalendar( c ) }; return MethodResult.of( getTimeForCalendar( c ) );
} }
case "local": case "local":
{ {
// Get Hour of day (local time) // Get Hour of day (local time)
Calendar c = Calendar.getInstance(); Calendar c = Calendar.getInstance();
return new Object[] { getTimeForCalendar( c ) }; return MethodResult.of( getTimeForCalendar( c ) );
} }
case "ingame": case "ingame":
// Get ingame hour // Get ingame hour
synchronized( m_alarms ) synchronized( m_alarms )
{ {
return new Object[] { m_time }; return MethodResult.of( m_time );
} }
default: default:
throw new LuaException( "Unsupported operation" ); throw new LuaException( "Unsupported operation" );
@ -338,19 +339,19 @@ public class OSAPI implements ILuaAPI
{ {
// Get numbers of days since 1970-01-01 (utc) // Get numbers of days since 1970-01-01 (utc)
Calendar c = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) ); Calendar c = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) );
return new Object[] { getDayForCalendar( c ) }; return MethodResult.of( getDayForCalendar( c ) );
} }
case "local": case "local":
{ {
// Get numbers of days since 1970-01-01 (local time) // Get numbers of days since 1970-01-01 (local time)
Calendar c = Calendar.getInstance(); Calendar c = Calendar.getInstance();
return new Object[] { getDayForCalendar( c ) }; return MethodResult.of( getDayForCalendar( c ) );
} }
case "ingame": case "ingame":
// Get game day // Get game day
synchronized( m_alarms ) synchronized( m_alarms )
{ {
return new Object[] { m_day }; return MethodResult.of( m_day );
} }
default: default:
throw new LuaException( "Unsupported operation" ); throw new LuaException( "Unsupported operation" );
@ -367,7 +368,7 @@ public class OSAPI implements ILuaAPI
m_timers.remove( token ); m_timers.remove( token );
} }
} }
return null; return MethodResult.empty();
} }
case 14: case 14:
{ {
@ -380,7 +381,7 @@ public class OSAPI implements ILuaAPI
m_alarms.remove( token ); m_alarms.remove( token );
} }
} }
return null; return MethodResult.empty();
} }
case 15: case 15:
{ {
@ -392,21 +393,21 @@ public class OSAPI implements ILuaAPI
{ {
// Get utc epoch // Get utc epoch
Calendar c = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) ); Calendar c = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) );
return new Object[] { getEpochForCalendar( c ) }; return MethodResult.of( getEpochForCalendar( c ) );
} }
case "local": case "local":
{ {
// Get local epoch // Get local epoch
Calendar c = Calendar.getInstance(); Calendar c = Calendar.getInstance();
return new Object[] { getEpochForCalendar( c ) }; return MethodResult.of( getEpochForCalendar( c ) );
} }
case "ingame": case "ingame":
// Get in-game epoch // Get in-game epoch
synchronized( m_alarms ) synchronized( m_alarms )
{ {
return new Object[] { return MethodResult.of(
m_day * 86400000 + (int) (m_time * 3600000.0f) m_day * 86400000 + (int) (m_time * 3600000.0f)
}; );
} }
default: default:
throw new LuaException( "Unsupported operation" ); throw new LuaException( "Unsupported operation" );
@ -414,11 +415,18 @@ public class OSAPI implements ILuaAPI
} }
default: 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 methods
private void queueLuaEvent( String event, Object[] args ) private void queueLuaEvent( String event, Object[] args )

View File

@ -8,9 +8,8 @@ package dan200.computercraft.core.apis;
import dan200.computercraft.api.filesystem.IMount; import dan200.computercraft.api.filesystem.IMount;
import dan200.computercraft.api.filesystem.IWritableMount; import dan200.computercraft.api.filesystem.IWritableMount;
import dan200.computercraft.api.lua.*;
import dan200.computercraft.api.lua.ILuaAPI; 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.api.peripheral.IPeripheral;
import dan200.computercraft.core.computer.Computer; import dan200.computercraft.core.computer.Computer;
import dan200.computercraft.core.computer.ComputerThread; import dan200.computercraft.core.computer.ComputerThread;
@ -98,7 +97,7 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
m_attached = false; m_attached = false;
} }
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; int method = -1;
synchronized( this ) synchronized( this )
@ -368,8 +367,9 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
}; };
} }
@Nonnull
@Override @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 ) switch( method )
{ {
@ -389,7 +389,7 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
} }
} }
} }
return new Object[] { present }; return MethodResult.of( present );
} }
case 1: case 1:
{ {
@ -408,10 +408,10 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
} }
if( type != null ) if( type != null )
{ {
return new Object[] { type }; return MethodResult.of( type );
} }
} }
return null; return MethodResult.empty();
} }
case 2: case 2:
{ {
@ -435,9 +435,9 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
for(int i=0; i<methods.length; ++i ) { for(int i=0; i<methods.length; ++i ) {
table.put( i+1, methods[i] ); table.put( i+1, methods[i] );
} }
return new Object[] { table }; return MethodResult.of( table );
} }
return null; return MethodResult.empty();
} }
case 3: case 3:
{ {
@ -462,11 +462,19 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
} }
default: 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 );
}
// Privates // Privates
private int parseSide( Object[] args ) throws LuaException private int parseSide( Object[] args ) throws LuaException

View File

@ -6,12 +6,12 @@
package dan200.computercraft.core.apis; package dan200.computercraft.core.apis;
import dan200.computercraft.api.lua.*;
import dan200.computercraft.api.lua.ILuaAPI; import dan200.computercraft.api.lua.ILuaAPI;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.core.computer.Computer; import dan200.computercraft.core.computer.Computer;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -56,8 +56,9 @@ public class RedstoneAPI implements ILuaAPI
}; };
} }
@Nonnull
@Override @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 ) switch( method )
{ {
@ -69,7 +70,7 @@ public class RedstoneAPI implements ILuaAPI
{ {
table.put( i+1, Computer.s_sideNames[i] ); table.put( i+1, Computer.s_sideNames[i] );
} }
return new Object[] { table }; return MethodResult.of( table );
} }
case 1: case 1:
{ {
@ -77,19 +78,19 @@ public class RedstoneAPI implements ILuaAPI
int side = parseSide( args ); int side = parseSide( args );
boolean output = getBoolean( args, 1 ); boolean output = getBoolean( args, 1 );
m_environment.setOutput( side, output ? 15 : 0 ); m_environment.setOutput( side, output ? 15 : 0 );
return null; return MethodResult.empty();
} }
case 2: case 2:
{ {
// getOutput // getOutput
int side = parseSide( args ); int side = parseSide( args );
return new Object[] { m_environment.getOutput( side ) > 0 }; return MethodResult.of( m_environment.getOutput( side ) > 0 );
} }
case 3: case 3:
{ {
// getInput // getInput
int side = parseSide( args ); int side = parseSide( args );
return new Object[] { m_environment.getInput( side ) > 0 }; return MethodResult.of( m_environment.getInput( side ) > 0 );
} }
case 4: case 4:
{ {
@ -97,19 +98,19 @@ public class RedstoneAPI implements ILuaAPI
int side = parseSide( args ); int side = parseSide( args );
int output = getInt( args, 1 ); int output = getInt( args, 1 );
m_environment.setBundledOutput( side, output ); m_environment.setBundledOutput( side, output );
return null; return MethodResult.empty();
} }
case 5: case 5:
{ {
// getBundledOutput // getBundledOutput
int side = parseSide( args ); int side = parseSide( args );
return new Object[] { m_environment.getBundledOutput( side ) }; return MethodResult.of( m_environment.getBundledOutput( side ) );
} }
case 6: case 6:
{ {
// getBundledInput // getBundledInput
int side = parseSide( args ); int side = parseSide( args );
return new Object[] { m_environment.getBundledInput( side ) }; return MethodResult.of( m_environment.getBundledInput( side ) );
} }
case 7: case 7:
{ {
@ -117,7 +118,7 @@ public class RedstoneAPI implements ILuaAPI
int side = parseSide( args ); int side = parseSide( args );
int mask = getInt( args, 1 ); int mask = getInt( args, 1 );
int input = m_environment.getBundledInput( side ); int input = m_environment.getBundledInput( side );
return new Object[] { ((input & mask) == mask) }; return MethodResult.of( ((input & mask) == mask) );
} }
case 8: case 8:
case 9: case 9:
@ -130,29 +131,37 @@ public class RedstoneAPI implements ILuaAPI
throw new LuaException( "Expected number in range 0-15" ); throw new LuaException( "Expected number in range 0-15" );
} }
m_environment.setOutput( side, output ); m_environment.setOutput( side, output );
return null; return MethodResult.empty();
} }
case 10: case 10:
case 11: case 11:
{ {
// getAnalogOutput/getAnalogueOutput // getAnalogOutput/getAnalogueOutput
int side = parseSide( args ); int side = parseSide( args );
return new Object[] { m_environment.getOutput( side ) }; return MethodResult.of( m_environment.getOutput( side ) );
} }
case 12: case 12:
case 13: case 13:
{ {
// getAnalogInput/getAnalogueInput // getAnalogInput/getAnalogueInput
int side = parseSide( args ); int side = parseSide( args );
return new Object[] { m_environment.getInput( side ) }; return MethodResult.of( m_environment.getInput( side ) );
} }
default: 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 private int parseSide( Object[] args ) throws LuaException
{ {
String side = getString( args, 0 ); String side = getString( args, 0 );

View File

@ -6,15 +6,15 @@
package dan200.computercraft.core.apis; package dan200.computercraft.core.apis;
import dan200.computercraft.api.lua.*;
import dan200.computercraft.api.lua.ILuaAPI; import dan200.computercraft.api.lua.ILuaAPI;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.core.computer.IComputerEnvironment; import dan200.computercraft.core.computer.IComputerEnvironment;
import dan200.computercraft.core.terminal.Terminal; import dan200.computercraft.core.terminal.Terminal;
import dan200.computercraft.shared.util.Palette; import dan200.computercraft.shared.util.Palette;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static dan200.computercraft.core.apis.ArgumentHelper.*; import static dan200.computercraft.core.apis.ArgumentHelper.*;
@ -83,11 +83,9 @@ public class TermAPI implements ILuaAPI
return colour; return colour;
} }
public static Object[] encodeColour( int colour ) throws LuaException public static MethodResult encodeColour( int colour )
{ {
return new Object[] { return MethodResult.of( 1 << colour );
1 << colour
};
} }
public static void setColour( Terminal terminal, int colour, double r, double g, double b ) public static void setColour( Terminal terminal, int colour, double r, double g, double b )
@ -99,8 +97,9 @@ public class TermAPI implements ILuaAPI
} }
} }
@Nonnull
@Override @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 ) switch( method )
{ {
@ -119,7 +118,7 @@ public class TermAPI implements ILuaAPI
m_terminal.write( text ); m_terminal.write( text );
m_terminal.setCursorPos( m_terminal.getCursorX() + text.length(), m_terminal.getCursorY() ); m_terminal.setCursorPos( m_terminal.getCursorX() + text.length(), m_terminal.getCursorY() );
} }
return null; return MethodResult.empty();
} }
case 1: case 1:
{ {
@ -129,7 +128,7 @@ public class TermAPI implements ILuaAPI
{ {
m_terminal.scroll(y); m_terminal.scroll(y);
} }
return null; return MethodResult.empty();
} }
case 2: case 2:
{ {
@ -140,7 +139,7 @@ public class TermAPI implements ILuaAPI
{ {
m_terminal.setCursorPos( x, y ); m_terminal.setCursorPos( x, y );
} }
return null; return MethodResult.empty();
} }
case 3: case 3:
{ {
@ -150,7 +149,7 @@ public class TermAPI implements ILuaAPI
{ {
m_terminal.setCursorBlink( b ); m_terminal.setCursorBlink( b );
} }
return null; return MethodResult.empty();
} }
case 4: case 4:
{ {
@ -161,7 +160,7 @@ public class TermAPI implements ILuaAPI
x = m_terminal.getCursorX(); x = m_terminal.getCursorX();
y = m_terminal.getCursorY(); y = m_terminal.getCursorY();
} }
return new Object[] { x + 1, y + 1 }; return MethodResult.of( x + 1, y + 1 );
} }
case 5: case 5:
{ {
@ -172,7 +171,7 @@ public class TermAPI implements ILuaAPI
width = m_terminal.getWidth(); width = m_terminal.getWidth();
height = m_terminal.getHeight(); height = m_terminal.getHeight();
} }
return new Object[] { width, height }; return MethodResult.of( width, height );
} }
case 6: case 6:
{ {
@ -181,7 +180,7 @@ public class TermAPI implements ILuaAPI
{ {
m_terminal.clear(); m_terminal.clear();
} }
return null; return MethodResult.empty();
} }
case 7: case 7:
{ {
@ -190,7 +189,7 @@ public class TermAPI implements ILuaAPI
{ {
m_terminal.clearLine(); m_terminal.clearLine();
} }
return null; return MethodResult.empty();
} }
case 8: case 8:
case 9: case 9:
@ -201,7 +200,7 @@ public class TermAPI implements ILuaAPI
{ {
m_terminal.setTextColour( colour ); m_terminal.setTextColour( colour );
} }
return null; return MethodResult.empty();
} }
case 10: case 10:
case 11: case 11:
@ -212,13 +211,13 @@ public class TermAPI implements ILuaAPI
{ {
m_terminal.setBackgroundColour( colour ); m_terminal.setBackgroundColour( colour );
} }
return null; return MethodResult.empty();
} }
case 12: case 12:
case 13: case 13:
{ {
// isColour/isColor // isColour/isColor
return new Object[] { m_environment.isColour() }; return MethodResult.of( m_environment.isColour() );
} }
case 14: case 14:
case 15: case 15:
@ -248,7 +247,7 @@ public class TermAPI implements ILuaAPI
m_terminal.blit( text, textColour, backgroundColour ); m_terminal.blit( text, textColour, backgroundColour );
m_terminal.setCursorPos( m_terminal.getCursorX() + text.length(), m_terminal.getCursorY() ); m_terminal.setCursorPos( m_terminal.getCursorX() + text.length(), m_terminal.getCursorY() );
} }
return null; return MethodResult.empty();
} }
case 19: case 19:
case 20: case 20:
@ -268,7 +267,7 @@ public class TermAPI implements ILuaAPI
double b = getReal( args, 3 ); double b = getReal( args, 3 );
setColour( m_terminal, colour, r, g, b ); setColour( m_terminal, colour, r, g, b );
} }
return null; return MethodResult.empty();
} }
case 21: case 21:
case 22: case 22:
@ -279,18 +278,26 @@ public class TermAPI implements ILuaAPI
{ {
if ( m_terminal.getPalette() != null ) 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: 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 ) private static int getHighestBit( int group )
{ {
int bit = 0; int bit = 0;

View File

@ -1,8 +1,9 @@
package dan200.computercraft.core.apis.handles; package dan200.computercraft.core.apis.handles;
import com.google.common.io.ByteStreams; 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.LuaException;
import dan200.computercraft.api.lua.MethodResult;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -35,8 +36,9 @@ public class BinaryInputHandle extends HandleGeneric
}; };
} }
@Nonnull
@Override @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 ) switch( method )
{ {
@ -60,9 +62,9 @@ public class BinaryInputHandle extends HandleGeneric
byte[] bytes = new byte[ count ]; byte[] bytes = new byte[ count ];
int read = m_stream.read( bytes ); int read = m_stream.read( bytes );
if( read < 0 ) return null; if( read < 0 ) return MethodResult.empty();
if( read < count ) bytes = Arrays.copyOf( bytes, read ); if( read < count ) bytes = Arrays.copyOf( bytes, read );
return new Object[] { bytes }; return MethodResult.of( bytes );
} }
else else
{ {
@ -86,18 +88,18 @@ public class BinaryInputHandle extends HandleGeneric
out.write( buffer, 0, read ); out.write( buffer, 0, read );
} }
return new Object[] { out.toByteArray() }; return MethodResult.of( new Object[]{ out.toByteArray() } );
} }
} }
else else
{ {
int b = m_stream.read(); int b = m_stream.read();
return b == -1 ? null : new Object[] { b }; return b == -1 ? MethodResult.empty() : MethodResult.of( b );
} }
} }
catch( IOException e ) catch( IOException e )
{ {
return null; return MethodResult.empty();
} }
case 1: case 1:
// readAll // readAll
@ -105,18 +107,18 @@ public class BinaryInputHandle extends HandleGeneric
try try
{ {
byte[] out = ByteStreams.toByteArray( m_stream ); byte[] out = ByteStreams.toByteArray( m_stream );
return out == null ? null : new Object[] { out }; return out == null ? MethodResult.empty() : MethodResult.of( out );
} }
catch( IOException e ) catch( IOException e )
{ {
return null; return MethodResult.empty();
} }
case 2: case 2:
//close //close
close(); close();
return null; return MethodResult.empty();
default: default:
return null; return MethodResult.empty();
} }
} }
} }

View File

@ -1,7 +1,8 @@
package dan200.computercraft.core.apis.handles; 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.LuaException;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.core.apis.ArgumentHelper; import dan200.computercraft.core.apis.ArgumentHelper;
import dan200.computercraft.shared.util.StringUtil; import dan200.computercraft.shared.util.StringUtil;
@ -30,8 +31,9 @@ public class BinaryOutputHandle extends HandleGeneric
}; };
} }
@Nonnull
@Override @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 ) switch( method )
{ {
@ -54,7 +56,7 @@ public class BinaryOutputHandle extends HandleGeneric
{ {
throw ArgumentHelper.badArgument( 0, "string or number", args.length > 0 ? args[ 0 ] : null ); throw ArgumentHelper.badArgument( 0, "string or number", args.length > 0 ? args[ 0 ] : null );
} }
return null; return MethodResult.empty();
} }
catch( IOException e ) catch( IOException e )
{ {
@ -66,18 +68,18 @@ public class BinaryOutputHandle extends HandleGeneric
try try
{ {
m_writer.flush(); m_writer.flush();
return null; return MethodResult.empty();
} }
catch( IOException e ) catch( IOException e )
{ {
return null; return MethodResult.empty();
} }
case 2: case 2:
//close //close
close(); close();
return null; return MethodResult.empty();
default: default:
return null; return MethodResult.empty();
} }
} }
} }

View File

@ -1,7 +1,8 @@
package dan200.computercraft.core.apis.handles; 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.LuaException;
import dan200.computercraft.api.lua.MethodResult;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.*; import java.io.*;
@ -57,8 +58,9 @@ public class EncodedInputHandle extends HandleGeneric
}; };
} }
@Nonnull
@Override @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 ) switch( method )
{ {
@ -70,16 +72,16 @@ public class EncodedInputHandle extends HandleGeneric
String line = m_reader.readLine(); String line = m_reader.readLine();
if( line != null ) if( line != null )
{ {
return new Object[] { line }; return MethodResult.of( line );
} }
else else
{ {
return null; return MethodResult.empty();
} }
} }
catch( IOException e ) catch( IOException e )
{ {
return null; return MethodResult.empty();
} }
case 1: case 1:
// readAll // readAll
@ -97,16 +99,16 @@ public class EncodedInputHandle extends HandleGeneric
result.append( "\n" ); result.append( "\n" );
} }
} }
return new Object[] { result.toString() }; return MethodResult.of( result.toString() );
} }
catch( IOException e ) catch( IOException e )
{ {
return null; return MethodResult.empty();
} }
case 2: case 2:
// close // close
close(); close();
return null; return MethodResult.empty();
case 3: case 3:
// read // read
checkOpen(); checkOpen();
@ -125,7 +127,7 @@ public class EncodedInputHandle extends HandleGeneric
char[] chars = new char[ count ]; char[] chars = new char[ count ];
int read = m_reader.read( chars ); int read = m_reader.read( chars );
return read < 0 ? null : new Object[] { new String( chars, 0, read ) }; return read < 0 ? MethodResult.empty() : MethodResult.of( new String( chars, 0, read ) );
} }
else else
{ {
@ -150,15 +152,15 @@ public class EncodedInputHandle extends HandleGeneric
out.append( buffer, 0, read ); out.append( buffer, 0, read );
} }
return new Object[] { out.toString() }; return MethodResult.of( out.toString() );
} }
} }
catch( IOException e ) catch( IOException e )
{ {
return null; return MethodResult.empty();
} }
default: default:
return null; return MethodResult.empty();
} }
} }
} }

View File

@ -1,7 +1,8 @@
package dan200.computercraft.core.apis.handles; 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.LuaException;
import dan200.computercraft.api.lua.MethodResult;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.*; import java.io.*;
@ -53,8 +54,9 @@ public class EncodedOutputHandle extends HandleGeneric
}; };
} }
@Nonnull
@Override @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 ) switch( method )
{ {
@ -74,7 +76,7 @@ public class EncodedOutputHandle extends HandleGeneric
try try
{ {
m_writer.write( text, 0, text.length() ); m_writer.write( text, 0, text.length() );
return null; return MethodResult.empty();
} }
catch( IOException e ) catch( IOException e )
{ {
@ -98,7 +100,7 @@ public class EncodedOutputHandle extends HandleGeneric
{ {
m_writer.write( text, 0, text.length() ); m_writer.write( text, 0, text.length() );
m_writer.newLine(); m_writer.newLine();
return null; return MethodResult.empty();
} }
catch( IOException e ) catch( IOException e )
{ {
@ -111,18 +113,18 @@ public class EncodedOutputHandle extends HandleGeneric
try try
{ {
m_writer.flush(); m_writer.flush();
return null; return MethodResult.empty();
} }
catch( IOException e ) catch( IOException e )
{ {
return null; return MethodResult.empty();
} }
case 3: case 3:
// close // close
close(); close();
return null; return MethodResult.empty();
default: default:
return null; return MethodResult.empty();
} }
} }
} }

View File

@ -1,8 +1,12 @@
package dan200.computercraft.core.apis.handles; 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.ILuaObject;
import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaException;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; 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 );
}
} }

View File

@ -9,15 +9,14 @@ package dan200.computercraft.core.apis.http;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import dan200.computercraft.ComputerCraft; import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.*;
import dan200.computercraft.api.lua.ILuaObject;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.core.apis.IAPIEnvironment; import dan200.computercraft.core.apis.IAPIEnvironment;
import dan200.computercraft.core.apis.handles.BinaryInputHandle; import dan200.computercraft.core.apis.handles.BinaryInputHandle;
import dan200.computercraft.core.apis.handles.EncodedInputHandle; import dan200.computercraft.core.apis.handles.EncodedInputHandle;
import dan200.computercraft.core.tracking.TrackingField; import dan200.computercraft.core.tracking.TrackingField;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.util.Arrays; import java.util.Arrays;
@ -236,8 +235,9 @@ public class HTTPRequest implements Runnable
return newMethods; return newMethods;
} }
@Nonnull
@Override @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 ) if( method < methodOffset )
{ {
@ -248,19 +248,27 @@ public class HTTPRequest implements Runnable
case 0: case 0:
{ {
// getResponseCode // getResponseCode
return new Object[]{ responseCode }; return MethodResult.of( responseCode );
} }
case 1: case 1:
{ {
// getResponseHeaders // getResponseHeaders
return new Object[]{ responseHeaders }; return MethodResult.of( responseHeaders );
} }
default: 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 );
}
}; };
} }

View File

@ -7,9 +7,7 @@
package dan200.computercraft.core.apis.http; package dan200.computercraft.core.apis.http;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.*;
import dan200.computercraft.api.lua.ILuaObject;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.core.apis.HTTPAPI; import dan200.computercraft.core.apis.HTTPAPI;
import dan200.computercraft.core.apis.IAPIEnvironment; import dan200.computercraft.core.apis.IAPIEnvironment;
import dan200.computercraft.core.tracking.TrackingField; import dan200.computercraft.core.tracking.TrackingField;
@ -156,33 +154,48 @@ public class WebsocketConnection extends SimpleChannelInboundHandler<Object> imp
@Nullable @Nullable
@Override @Override
@Deprecated
public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException
{
return callMethod( (ICallContext) context, method, arguments ).evaluate( context );
}
@Nonnull
@Override
public MethodResult callMethod( @Nonnull ICallContext context, int method, @Nonnull Object[] arguments ) throws LuaException
{ {
switch( method ) switch( method )
{ {
case 0: case 0:
while( true ) checkOpen();
return MethodResult.pullEvent( MESSAGE_EVENT, new ILuaFunction()
{ {
checkOpen(); @Nonnull
Object[] event = context.pullEvent( MESSAGE_EVENT ); @Override
if( event.length >= 3 && Objects.equal( event[1], url ) ) public MethodResult call( @Nullable Object[] event ) throws LuaException
{ {
return new Object[]{ event[2] }; if( event != null && event.length >= 3 && Objects.equal( event[1], url ) )
{
return MethodResult.of( event[2] );
}
checkOpen();
return MethodResult.pullEvent( MESSAGE_EVENT, this );
} }
} } );
case 1: case 1:
{ {
checkOpen(); checkOpen();
String text = arguments.length > 0 && arguments[0] != null ? arguments[0].toString() : ""; String text = arguments.length > 0 && arguments[0] != null ? arguments[0].toString() : "";
computer.addTrackingChange( TrackingField.WEBSOCKET_OUTGOING, text.length() ); computer.addTrackingChange( TrackingField.WEBSOCKET_OUTGOING, text.length() );
channel.writeAndFlush( new TextWebSocketFrame( text ) ); channel.writeAndFlush( new TextWebSocketFrame( text ) );
return null; return MethodResult.empty();
} }
case 2: case 2:
close( true ); close( true );
return null; return MethodResult.empty();
default: default:
return null; return MethodResult.empty();
} }
} }

View File

@ -265,10 +265,18 @@ public class Computer
@Nullable @Nullable
@Override @Override
@Deprecated
public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException
{ {
return delegate.callMethod( context, method, arguments ); return delegate.callMethod( context, method, arguments );
} }
@Nonnull
@Override
public MethodResult callMethod( @Nonnull ICallContext context, int method, @Nonnull Object[] arguments ) throws LuaException
{
return delegate.callMethod( context, method, arguments );
}
} }
private static IMount s_romMount = null; private static IMount s_romMount = null;

View File

@ -8,9 +8,7 @@ package dan200.computercraft.shared.computer.apis;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import dan200.computercraft.ComputerCraft; import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.lua.ILuaAPI; import dan200.computercraft.api.lua.*;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.shared.computer.blocks.TileCommandComputer; import dan200.computercraft.shared.computer.blocks.TileCommandComputer;
import dan200.computercraft.shared.util.WorldUtil; import dan200.computercraft.shared.util.WorldUtil;
import net.minecraft.block.Block; import net.minecraft.block.Block;
@ -24,6 +22,7 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World; import net.minecraft.world.World;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -82,7 +81,7 @@ public class CommandAPI implements ILuaAPI
sender.clearOutput(); sender.clearOutput();
int result = commandManager.executeCommand( sender, command ); int result = commandManager.executeCommand( sender, command );
return new Object[]{ (result > 0), sender.copyOutput() }; return new Object[] { (result > 0), sender.copyOutput() };
} }
catch( Throwable t ) catch( Throwable t )
{ {
@ -90,7 +89,7 @@ public class CommandAPI implements ILuaAPI
{ {
ComputerCraft.log.error( "Error running command.", t ); 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 else
@ -131,7 +130,8 @@ public class CommandAPI implements ILuaAPI
} }
@Override @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 ) switch( method )
{ {
@ -139,19 +139,19 @@ public class CommandAPI implements ILuaAPI
{ {
// exec // exec
final String command = getString( arguments, 0 ); final String command = getString( arguments, 0 );
return context.executeMainThreadTask( () -> doCommand( command ) ); return MethodResult.onMainThread( () -> MethodResult.of( doCommand( command ) ) );
} }
case 1: case 1:
{ {
// execAsync // execAsync
final String command = getString( arguments, 0 ); final String command = getString( arguments, 0 );
long taskID = context.issueMainThreadTask( () -> doCommand( command ) ); long taskID = context.issueMainThreadTask( () -> doCommand( command ) );
return new Object[] { taskID }; return MethodResult.of( taskID );
} }
case 2: case 2:
{ {
// list // list
return context.executeMainThreadTask( () -> return MethodResult.onMainThread( () ->
{ {
int i = 1; int i = 1;
Map<Object, Object> result = new HashMap<>(); Map<Object, Object> result = new HashMap<>();
@ -182,7 +182,7 @@ public class CommandAPI implements ILuaAPI
} }
} }
} }
return new Object[]{ result }; return MethodResult.of( result );
} ); } );
} }
case 3: case 3:
@ -190,7 +190,7 @@ public class CommandAPI implements ILuaAPI
// getBlockPosition // getBlockPosition
// This is probably safe to do on the Lua thread. Probably. // This is probably safe to do on the Lua thread. Probably.
BlockPos pos = m_computer.getPos(); 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: case 4:
{ {
@ -201,7 +201,7 @@ public class CommandAPI implements ILuaAPI
final int maxx = getInt( arguments, 3 ); final int maxx = getInt( arguments, 3 );
final int maxy = getInt( arguments, 4 ); final int maxy = getInt( arguments, 4 );
final int maxz = getInt( arguments, 5 ); final int maxz = getInt( arguments, 5 );
return context.executeMainThreadTask( () -> return MethodResult.onMainThread( () ->
{ {
// Get the details of the block // Get the details of the block
World world = m_computer.getWorld(); World world = m_computer.getWorld();
@ -236,7 +236,7 @@ public class CommandAPI implements ILuaAPI
} }
} }
} }
return new Object[]{ results }; return MethodResult.of( results );
} ); } );
} }
case 5: case 5:
@ -245,14 +245,14 @@ public class CommandAPI implements ILuaAPI
final int x = getInt( arguments, 0 ); final int x = getInt( arguments, 0 );
final int y = getInt( arguments, 1 ); final int y = getInt( arguments, 1 );
final int z = getInt( arguments, 2 ); final int z = getInt( arguments, 2 );
return context.executeMainThreadTask( () -> return MethodResult.onMainThread( () ->
{ {
// Get the details of the block // Get the details of the block
World world = m_computer.getWorld(); World world = m_computer.getWorld();
BlockPos position = new BlockPos( x, y, z ); BlockPos position = new BlockPos( x, y, z );
if( WorldUtil.isBlockInWorld( world, position ) ) if( WorldUtil.isBlockInWorld( world, position ) )
{ {
return new Object[]{ getBlockInfo( world, position ) }; return MethodResult.of( getBlockInfo( world, position ) );
} }
else else
{ {
@ -262,8 +262,16 @@ public class CommandAPI implements ILuaAPI
} }
default: 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 );
}
} }

View File

@ -6,11 +6,15 @@
package dan200.computercraft.shared.computer.blocks; package dan200.computercraft.shared.computer.blocks;
import dan200.computercraft.api.lua.ICallContext;
import dan200.computercraft.api.lua.ILuaContext; 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.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class ComputerPeripheral public class ComputerPeripheral
implements IPeripheral implements IPeripheral
@ -47,8 +51,9 @@ public class ComputerPeripheral
}; };
} }
@Nonnull
@Override @Override
public Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) public MethodResult callMethod( @Nonnull IComputerAccess computer, @Nonnull ICallContext context, int method, @Nonnull Object[] arguments )
{ {
switch( method ) switch( method )
{ {
@ -56,42 +61,48 @@ public class ComputerPeripheral
{ {
// turnOn // turnOn
m_computer.turnOn(); m_computer.turnOn();
return null; return MethodResult.empty();
} }
case 1: case 1:
{ {
// shutdown // shutdown
m_computer.shutdown(); m_computer.shutdown();
return null; return MethodResult.empty();
} }
case 2: case 2:
{ {
// reboot // reboot
m_computer.reboot(); m_computer.reboot();
return null; return MethodResult.empty();
} }
case 3: case 3:
{ {
// getID // getID
return new Object[] { return MethodResult.of( m_computer.assignID() );
m_computer.assignID()
};
} }
case 4: case 4:
{ {
// isOn // isOn
return new Object[] { m_computer.isOn() }; return MethodResult.of( m_computer.isOn() );
} }
case 5: case 5:
// getLabel // getLabel
return new Object[] { m_computer.getLabel() }; return MethodResult.of( m_computer.getLabel() );
default: 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 @Override
public boolean equals( IPeripheral other ) public boolean equals( IPeripheral other )
{ {

View File

@ -6,14 +6,17 @@
package dan200.computercraft.shared.peripheral.commandblock; package dan200.computercraft.shared.peripheral.commandblock;
import dan200.computercraft.api.lua.ICallContext;
import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.tileentity.TileEntityCommandBlock; import net.minecraft.tileentity.TileEntityCommandBlock;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static dan200.computercraft.core.apis.ArgumentHelper.getString; import static dan200.computercraft.core.apis.ArgumentHelper.getString;
@ -46,17 +49,18 @@ public class CommandBlockPeripheral implements IPeripheral
}; };
} }
@Nonnull
@Override @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: case 0:
{ {
// getCommand // getCommand
return context.executeMainThreadTask( () -> new Object[] { return MethodResult.onMainThread( () ->
m_commandBlock.getCommandBlockLogic().getCommand() MethodResult.of( m_commandBlock.getCommandBlockLogic().getCommand() )
} ); );
} }
case 1: case 1:
{ {
@ -69,27 +73,35 @@ public class CommandBlockPeripheral implements IPeripheral
m_commandBlock.getWorld().markBlockRangeForRenderUpdate( pos, pos ); m_commandBlock.getWorld().markBlockRangeForRenderUpdate( pos, pos );
return null; return null;
} ); } );
return null; return MethodResult.empty();
} }
case 2: case 2:
{ {
// runCommand // runCommand
return context.executeMainThreadTask( () -> return MethodResult.onMainThread( () ->
{ {
m_commandBlock.getCommandBlockLogic().trigger( m_commandBlock.getWorld() ); m_commandBlock.getCommandBlockLogic().trigger( m_commandBlock.getWorld() );
int result = m_commandBlock.getCommandBlockLogic().getSuccessCount(); int result = m_commandBlock.getCommandBlockLogic().getSuccessCount();
if( result > 0 ) if( result > 0 )
{ {
return new Object[] { true }; return MethodResult.of( true );
} }
else 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 @Override

View File

@ -6,8 +6,10 @@
package dan200.computercraft.shared.peripheral.diskdrive; package dan200.computercraft.shared.peripheral.diskdrive;
import dan200.computercraft.api.lua.ICallContext;
import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.api.media.IMedia; import dan200.computercraft.api.media.IMedia;
import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
@ -17,6 +19,7 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static dan200.computercraft.core.apis.ArgumentHelper.optString; import static dan200.computercraft.core.apis.ArgumentHelper.optString;
@ -55,17 +58,18 @@ public class DiskDrivePeripheral implements IPeripheral
}; };
} }
@Nonnull
@Override @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 ) switch( method )
{ {
case 0: case 0:
{ {
// isDiskPresent // isDiskPresent
return new Object[] { return MethodResult.of(
!m_diskDrive.getDiskStack().isEmpty() !m_diskDrive.getDiskStack().isEmpty()
}; );
} }
case 1: case 1:
{ {
@ -73,9 +77,9 @@ public class DiskDrivePeripheral implements IPeripheral
IMedia media = m_diskDrive.getDiskMedia(); IMedia media = m_diskDrive.getDiskMedia();
if( media != null ) 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: case 2:
{ {
@ -96,21 +100,21 @@ public class DiskDrivePeripheral implements IPeripheral
throw new LuaException( "Disk label cannot be changed" ); throw new LuaException( "Disk label cannot be changed" );
} }
} }
return null; return MethodResult.empty();
} }
case 3: case 3:
{ {
// hasData // hasData
return new Object[] { return MethodResult.of(
m_diskDrive.getDiskMountPath( computer ) != null m_diskDrive.getDiskMountPath( computer ) != null
}; );
} }
case 4: case 4:
{ {
// getMountPath // getMountPath
return new Object[] { return MethodResult.of(
m_diskDrive.getDiskMountPath( computer ) m_diskDrive.getDiskMountPath( computer )
}; );
} }
case 5: case 5:
{ {
@ -118,9 +122,9 @@ public class DiskDrivePeripheral implements IPeripheral
IMedia media = m_diskDrive.getDiskMedia(); IMedia media = m_diskDrive.getDiskMedia();
if( media != null ) 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: case 6:
{ {
@ -128,27 +132,27 @@ public class DiskDrivePeripheral implements IPeripheral
IMedia media = m_diskDrive.getDiskMedia(); IMedia media = m_diskDrive.getDiskMedia();
if( media != null ) 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: case 7:
{ {
// playAudio // playAudio
m_diskDrive.playDiskAudio(); m_diskDrive.playDiskAudio();
return null; return MethodResult.empty();
} }
case 8: case 8:
{ {
// stopAudio // stopAudio
m_diskDrive.stopDiskAudio(); m_diskDrive.stopDiskAudio();
return null; return MethodResult.empty();
} }
case 9: case 9:
{ {
// eject // eject
m_diskDrive.ejectDisk(); m_diskDrive.ejectDisk();
return null; return MethodResult.empty();
} }
case 10: case 10:
{ {
@ -159,18 +163,27 @@ public class DiskDrivePeripheral implements IPeripheral
Item item = disk.getItem(); Item item = disk.getItem();
if( item instanceof ItemDiskLegacy ) if( item instanceof ItemDiskLegacy )
{ {
return new Object[] { ((ItemDiskLegacy)item).getDiskID( disk ) }; return MethodResult.of( ((ItemDiskLegacy)item).getDiskID( disk ) );
} }
} }
return null; return MethodResult.empty();
} }
default: 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 @Override
public void attach( @Nonnull IComputerAccess computer ) public void attach( @Nonnull IComputerAccess computer )
{ {

View File

@ -6,8 +6,10 @@
package dan200.computercraft.shared.peripheral.modem; package dan200.computercraft.shared.peripheral.modem;
import dan200.computercraft.api.lua.ICallContext;
import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.api.network.IPacketNetwork; import dan200.computercraft.api.network.IPacketNetwork;
import dan200.computercraft.api.network.IPacketReceiver; import dan200.computercraft.api.network.IPacketReceiver;
import dan200.computercraft.api.network.IPacketSender; import dan200.computercraft.api.network.IPacketSender;
@ -20,6 +22,7 @@ import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World; import net.minecraft.world.World;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static dan200.computercraft.core.apis.ArgumentHelper.getInt; import static dan200.computercraft.core.apis.ArgumentHelper.getInt;
@ -157,8 +160,9 @@ public abstract class ModemPeripheral
return channel; return channel;
} }
@Nonnull
@Override @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 ) switch( method )
{ {
@ -183,7 +187,7 @@ public abstract class ModemPeripheral
} }
} }
} }
return null; return MethodResult.empty();
} }
case 1: case 1:
{ {
@ -192,7 +196,7 @@ public abstract class ModemPeripheral
synchronized( this ) synchronized( this )
{ {
boolean open = m_channels.contains( channel ); boolean open = m_channels.contains( channel );
return new Object[] { open }; return MethodResult.of( open );
} }
} }
case 2: case 2:
@ -210,7 +214,7 @@ public abstract class ModemPeripheral
} }
} }
} }
return null; return MethodResult.empty();
} }
case 3: case 3:
{ {
@ -228,7 +232,7 @@ public abstract class ModemPeripheral
} }
} }
} }
return null; return MethodResult.empty();
} }
case 4: case 4:
{ {
@ -253,7 +257,7 @@ public abstract class ModemPeripheral
} }
} }
} }
return null; return MethodResult.empty();
} }
case 5: case 5:
{ {
@ -262,18 +266,26 @@ public abstract class ModemPeripheral
{ {
if( m_network != null ) 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: 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 @Override
public synchronized void attach( @Nonnull IComputerAccess computer ) public synchronized void attach( @Nonnull IComputerAccess computer )
{ {

View File

@ -3,8 +3,9 @@ package dan200.computercraft.shared.peripheral.modem;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import dan200.computercraft.api.filesystem.IMount; import dan200.computercraft.api.filesystem.IMount;
import dan200.computercraft.api.filesystem.IWritableMount; 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.LuaException;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.api.network.IPacketNetwork; import dan200.computercraft.api.network.IPacketNetwork;
import dan200.computercraft.api.network.wired.IWiredNode; import dan200.computercraft.api.network.wired.IWiredNode;
import dan200.computercraft.api.network.wired.IWiredSender; import dan200.computercraft.api.network.wired.IWiredSender;
@ -79,8 +80,9 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements IW
return newMethods; return newMethods;
} }
@Nonnull
@Override @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(); String[] methods = super.getMethodNames();
switch( method - methods.length ) switch( method - methods.length )
@ -96,14 +98,14 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements IW
{ {
table.put( idx++, name ); table.put( idx++, name );
} }
return new Object[]{ table }; return MethodResult.of( table );
} }
} }
case 1: case 1:
{ {
// isPresentRemote // isPresentRemote
String type = getTypeRemote( getString( arguments, 0 ) ); String type = getTypeRemote( getString( arguments, 0 ) );
return new Object[]{ type != null }; return MethodResult.of( type != null );
} }
case 2: case 2:
{ {
@ -111,9 +113,9 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements IW
String type = getTypeRemote( getString( arguments, 0 ) ); String type = getTypeRemote( getString( arguments, 0 ) );
if( type != null ) if( type != null )
{ {
return new Object[]{ type }; return MethodResult.of( type );
} }
return null; return MethodResult.empty();
} }
case 3: case 3:
{ {
@ -126,9 +128,9 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements IW
{ {
table.put( i + 1, methodNames[i] ); table.put( i + 1, methodNames[i] );
} }
return new Object[]{ table }; return MethodResult.of( table );
} }
return null; return MethodResult.empty();
} }
case 4: case 4:
{ {
@ -143,7 +145,7 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements IW
{ {
// getNameLocal // getNameLocal
String local = getLocalPeripheral().getConnectedName(); String local = getLocalPeripheral().getConnectedName();
return local == null ? null : new Object[]{ local }; return local == null ? MethodResult.empty() : MethodResult.of( local );
} }
default: default:
{ {
@ -261,7 +263,7 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements IW
return null; return null;
} }
private Object[] callMethodRemote( String remoteName, ILuaContext context, String method, Object[] arguments ) throws LuaException, InterruptedException private MethodResult callMethodRemote( String remoteName, ICallContext context, String method, Object[] arguments ) throws LuaException
{ {
RemotePeripheralWrapper wrapper; RemotePeripheralWrapper wrapper;
synchronized( peripheralWrappers ) synchronized( peripheralWrappers )
@ -330,7 +332,7 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements IW
return m_methods; return m_methods;
} }
public Object[] callMethod( ILuaContext context, String methodName, Object[] arguments ) throws LuaException, InterruptedException public MethodResult callMethod( ICallContext context, String methodName, Object[] arguments ) throws LuaException
{ {
if( m_methodMap.containsKey( methodName ) ) if( m_methodMap.containsKey( methodName ) )
{ {

View File

@ -6,8 +6,10 @@
package dan200.computercraft.shared.peripheral.monitor; package dan200.computercraft.shared.peripheral.monitor;
import dan200.computercraft.api.lua.ICallContext;
import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.core.apis.TermAPI; import dan200.computercraft.core.apis.TermAPI;
@ -16,6 +18,7 @@ import dan200.computercraft.shared.util.Palette;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static dan200.computercraft.core.apis.ArgumentHelper.*; import static dan200.computercraft.core.apis.ArgumentHelper.*;
@ -70,8 +73,9 @@ public class MonitorPeripheral implements IPeripheral
}; };
} }
@Nonnull
@Override @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
{ {
ServerMonitor monitor = m_monitor.getCachedServerMonitor(); ServerMonitor monitor = m_monitor.getCachedServerMonitor();
if( monitor == null ) throw new LuaException( "Monitor has been detatched" ); if( monitor == null ) throw new LuaException( "Monitor has been detatched" );
@ -85,21 +89,24 @@ public class MonitorPeripheral implements IPeripheral
{ {
// write // write
String text; String text;
if( args.length > 0 && args[0] != null ) { if( args.length > 0 && args[ 0 ] != null )
text = args[0].toString(); {
} else { text = args[ 0 ].toString();
}
else
{
text = ""; text = "";
} }
terminal.write( text ); terminal.write( text );
terminal.setCursorPos( terminal.getCursorX() + text.length(), terminal.getCursorY() ); terminal.setCursorPos( terminal.getCursorX() + text.length(), terminal.getCursorY() );
return null; return MethodResult.empty();
} }
case 1: case 1:
{ {
// scroll // scroll
int value = getInt( args, 0 ); int value = getInt( args, 0 );
terminal.scroll( value ); terminal.scroll( value );
return null; return MethodResult.empty();
} }
case 2: case 2:
{ {
@ -107,42 +114,42 @@ public class MonitorPeripheral implements IPeripheral
int x = getInt( args, 0 ) - 1; int x = getInt( args, 0 ) - 1;
int y = getInt( args, 1 ) - 1; int y = getInt( args, 1 ) - 1;
terminal.setCursorPos( x, y ); terminal.setCursorPos( x, y );
return null; return MethodResult.empty();
} }
case 3: case 3:
{ {
// setCursorBlink // setCursorBlink
boolean blink = getBoolean( args, 0 ); boolean blink = getBoolean( args, 0 );
terminal.setCursorBlink( blink ); terminal.setCursorBlink( blink );
return null; return MethodResult.empty();
} }
case 4: case 4:
{ {
// getCursorPos // getCursorPos
return new Object[] { return MethodResult.of(
terminal.getCursorX() + 1, terminal.getCursorX() + 1,
terminal.getCursorY() + 1 terminal.getCursorY() + 1
}; );
} }
case 5: case 5:
{ {
// getSize // getSize
return new Object[] { return MethodResult.of(
terminal.getWidth(), terminal.getWidth(),
terminal.getHeight() terminal.getHeight()
}; );
} }
case 6: case 6:
{ {
// clear // clear
terminal.clear(); terminal.clear();
return null; return MethodResult.empty();
} }
case 7: case 7:
{ {
// clearLine // clearLine
terminal.clearLine(); terminal.clearLine();
return null; return MethodResult.empty();
} }
case 8: case 8:
{ {
@ -153,7 +160,7 @@ public class MonitorPeripheral implements IPeripheral
throw new LuaException( "Expected number in range 0.5-5" ); throw new LuaException( "Expected number in range 0.5-5" );
} }
monitor.setTextScale( scale ); monitor.setTextScale( scale );
return null; return MethodResult.empty();
} }
case 9: case 9:
case 10: case 10:
@ -161,7 +168,7 @@ public class MonitorPeripheral implements IPeripheral
// setTextColour/setTextColor // setTextColour/setTextColor
int colour = TermAPI.parseColour( args ); int colour = TermAPI.parseColour( args );
terminal.setTextColour( colour ); terminal.setTextColour( colour );
return null; return MethodResult.empty();
} }
case 11: case 11:
case 12: case 12:
@ -169,15 +176,15 @@ public class MonitorPeripheral implements IPeripheral
// setBackgroundColour/setBackgroundColor // setBackgroundColour/setBackgroundColor
int colour = TermAPI.parseColour( args ); int colour = TermAPI.parseColour( args );
terminal.setBackgroundColour( colour ); terminal.setBackgroundColour( colour );
return null; return MethodResult.empty();
} }
case 13: case 13:
case 14: case 14:
{ {
// isColour/isColor // isColour/isColor
return new Object[] { return MethodResult.of(
monitor.isColour() monitor.isColour()
}; );
} }
case 15: case 15:
case 16: case 16:
@ -204,7 +211,7 @@ public class MonitorPeripheral implements IPeripheral
terminal.blit( text, textColour, backgroundColour ); terminal.blit( text, textColour, backgroundColour );
terminal.setCursorPos( terminal.getCursorX() + text.length(), terminal.getCursorY() ); terminal.setCursorPos( terminal.getCursorX() + text.length(), terminal.getCursorY() );
return null; return MethodResult.empty();
} }
case 20: case 20:
case 21: case 21:
@ -224,7 +231,7 @@ public class MonitorPeripheral implements IPeripheral
double b = getReal( args, 3 ); double b = getReal( args, 3 );
TermAPI.setColour( terminal, colour, r, g, b ); TermAPI.setColour( terminal, colour, r, g, b );
} }
return null; return MethodResult.empty();
} }
case 22: case 22:
case 23: case 23:
@ -236,17 +243,25 @@ public class MonitorPeripheral implements IPeripheral
if( palette != null ) if( palette != null )
{ {
return ArrayUtils.toObject( palette.getColour( colour ) ); return MethodResult.of( (Object[]) ArrayUtils.toObject( palette.getColour( colour ) ) );
} }
return null; return MethodResult.empty();
} }
case 24: case 24:
{ {
// getTextScale // getTextScale
return new Object[] { monitor.getTextScale() / 2.0 }; return MethodResult.of( monitor.getTextScale() / 2.0 );
} }
} }
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 @Override
@ -266,7 +281,7 @@ public class MonitorPeripheral implements IPeripheral
{ {
if( other != null && other instanceof MonitorPeripheral ) if( other != null && other instanceof MonitorPeripheral )
{ {
MonitorPeripheral otherMonitor = (MonitorPeripheral)other; MonitorPeripheral otherMonitor = (MonitorPeripheral) other;
if( otherMonitor.m_monitor == this.m_monitor ) if( otherMonitor.m_monitor == this.m_monitor )
{ {
return true; return true;

View File

@ -6,13 +6,16 @@
package dan200.computercraft.shared.peripheral.printer; package dan200.computercraft.shared.peripheral.printer;
import dan200.computercraft.api.lua.ICallContext;
import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.core.terminal.Terminal; import dan200.computercraft.core.terminal.Terminal;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static dan200.computercraft.core.apis.ArgumentHelper.getInt; import static dan200.computercraft.core.apis.ArgumentHelper.getInt;
import static dan200.computercraft.core.apis.ArgumentHelper.optString; import static dan200.computercraft.core.apis.ArgumentHelper.optString;
@ -50,8 +53,9 @@ public class PrinterPeripheral implements IPeripheral
}; };
} }
@Nonnull
@Override @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 ) switch( method )
{ {
@ -68,7 +72,7 @@ public class PrinterPeripheral implements IPeripheral
Terminal page = getCurrentPage(); Terminal page = getCurrentPage();
page.write( text ); page.write( text );
page.setCursorPos( page.getCursorX() + text.length(), page.getCursorY() ); page.setCursorPos( page.getCursorX() + text.length(), page.getCursorY() );
return null; return MethodResult.empty();
} }
case 1: case 1:
{ {
@ -77,7 +81,7 @@ public class PrinterPeripheral implements IPeripheral
int y = getInt( args, 1 ) - 1; int y = getInt( args, 1 ) - 1;
Terminal page = getCurrentPage(); Terminal page = getCurrentPage();
page.setCursorPos( x, y ); page.setCursorPos( x, y );
return null; return MethodResult.empty();
} }
case 2: case 2:
{ {
@ -85,7 +89,7 @@ public class PrinterPeripheral implements IPeripheral
Terminal page = getCurrentPage(); Terminal page = getCurrentPage();
int x = page.getCursorX(); int x = page.getCursorX();
int y = page.getCursorY(); int y = page.getCursorY();
return new Object[] { x + 1, y + 1 }; return MethodResult.of( x + 1, y + 1 );
} }
case 3: case 3:
{ {
@ -93,23 +97,23 @@ public class PrinterPeripheral implements IPeripheral
Terminal page = getCurrentPage(); Terminal page = getCurrentPage();
int width = page.getWidth(); int width = page.getWidth();
int height = page.getHeight(); int height = page.getHeight();
return new Object[] { width, height }; return MethodResult.of( width, height );
} }
case 4: case 4:
{ {
// newPage // newPage
return new Object[] { m_printer.startNewPage() }; return MethodResult.of( m_printer.startNewPage() );
} }
case 5: case 5:
{ {
// endPage // endPage
getCurrentPage(); getCurrentPage();
return new Object[] { m_printer.endCurrentPage() }; return MethodResult.of( m_printer.endCurrentPage() );
} }
case 6: case 6:
{ {
// getInkLevel // getInkLevel
return new Object[] { m_printer.getInkLevel() }; return MethodResult.of( m_printer.getInkLevel() );
} }
case 7: case 7:
{ {
@ -117,20 +121,28 @@ public class PrinterPeripheral implements IPeripheral
String title = optString( args, 0, "" ); String title = optString( args, 0, "" );
getCurrentPage(); getCurrentPage();
m_printer.setPageTitle( title ); m_printer.setPageTitle( title );
return null; return MethodResult.empty();
} }
case 8: case 8:
{ {
// getPaperLevel // getPaperLevel
return new Object[] { m_printer.getPaperLevel() }; return MethodResult.of( m_printer.getPaperLevel() );
} }
default: 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 @Override
public boolean equals( IPeripheral other ) public boolean equals( IPeripheral other )
{ {

View File

@ -7,9 +7,7 @@
package dan200.computercraft.shared.peripheral.speaker; package dan200.computercraft.shared.peripheral.speaker;
import dan200.computercraft.ComputerCraft; import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.*;
import dan200.computercraft.api.lua.ILuaTask;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
@ -98,21 +96,22 @@ public class SpeakerPeripheral implements IPeripheral {
}; };
} }
@Nonnull
@Override @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 ) switch( methodIndex )
{ {
// playSound // playSound
case 0: case 0:
{ {
return playSound(args, context, false); return MethodResult.of( playSound( args, context, false ) );
} }
// playNote // playNote
case 1: case 1:
{ {
return playNote(args, context); return MethodResult.of( playNote( args, context ) );
} }
default: default:
@ -123,8 +122,16 @@ public class SpeakerPeripheral implements IPeripheral {
} }
} }
@Nonnull @Nullable
private synchronized Object[] playNote( Object[] arguments, ILuaContext context ) throws LuaException @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); String name = getString(arguments, 0);
float volume = (float) optReal( arguments, 1, 1.0 ); float volume = (float) optReal( arguments, 1, 1.0 );
@ -137,7 +144,7 @@ public class SpeakerPeripheral implements IPeripheral {
} }
// If the resource location for note block notes changes, this method call will need to be updated // 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[] { new Object[] {
"block.note." + name, "block.note." + name,
(double)Math.min( volume, 3f ), (double)Math.min( volume, 3f ),
@ -145,16 +152,15 @@ public class SpeakerPeripheral implements IPeripheral {
}, context, true }, context, true
); );
if( returnValue[0] instanceof Boolean && (Boolean) returnValue[0] ) if( success )
{ {
m_notesThisTick.incrementAndGet(); m_notesThisTick.incrementAndGet();
} }
return returnValue; return success;
} }
@Nonnull private synchronized boolean playSound( Object[] arguments, ICallContext context, boolean isNote ) throws LuaException
private synchronized Object[] playSound( Object[] arguments, ILuaContext context, boolean isNote ) throws LuaException
{ {
String name = getString(arguments, 0); String name = getString(arguments, 0);
float volume = (float) optReal( arguments, 1, 1.0 ); float volume = (float) optReal( arguments, 1, 1.0 );
@ -184,16 +190,16 @@ public class SpeakerPeripheral implements IPeripheral {
}); });
m_lastPlayTime = m_clock; m_lastPlayTime = m_clock;
return new Object[]{true}; // Success, return true return true; // Success, return true
} }
else else
{ {
return new Object[]{false}; // Failed - sound not existent, return false return false; // Failed - sound not existent, return false
} }
} }
else else
{ {
return new Object[]{false}; // Failed - rate limited, return false return false; // Failed - rate limited, return false
} }
} }
} }

View File

@ -7,9 +7,7 @@
package dan200.computercraft.shared.pocket.apis; package dan200.computercraft.shared.pocket.apis;
import dan200.computercraft.ComputerCraft; import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.lua.ILuaAPI; import dan200.computercraft.api.lua.*;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.pocket.IPocketUpgrade; import dan200.computercraft.api.pocket.IPocketUpgrade;
import dan200.computercraft.shared.pocket.core.PocketServerComputer; import dan200.computercraft.shared.pocket.core.PocketServerComputer;
import dan200.computercraft.shared.util.InventoryUtil; import dan200.computercraft.shared.util.InventoryUtil;
@ -21,6 +19,7 @@ import net.minecraft.util.NonNullList;
import net.minecraftforge.items.wrapper.PlayerMainInvWrapper; import net.minecraftforge.items.wrapper.PlayerMainInvWrapper;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class PocketAPI implements ILuaAPI public class PocketAPI implements ILuaAPI
{ {
@ -49,14 +48,15 @@ public class PocketAPI implements ILuaAPI
}; };
} }
@Nonnull
@Override @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 ) switch( method )
{ {
case 0: case 0:
// equipBack // equipBack
return context.executeMainThreadTask( () -> return MethodResult.onMainThread( () ->
{ {
if( !(m_computer.getEntity() instanceof EntityPlayer) ) if( !(m_computer.getEntity() instanceof EntityPlayer) )
{ {
@ -94,12 +94,12 @@ public class PocketAPI implements ILuaAPI
// Set the new upgrade // Set the new upgrade
m_computer.setUpgrade( newUpgrade ); m_computer.setUpgrade( newUpgrade );
return null; return MethodResult.empty();
} ); } );
case 1: case 1:
// unequipBack // unequipBack
return context.executeMainThreadTask( () -> return MethodResult.onMainThread( () ->
{ {
if( !(m_computer.getEntity() instanceof EntityPlayer) ) if( !(m_computer.getEntity() instanceof EntityPlayer) )
{ {
@ -125,13 +125,21 @@ public class PocketAPI implements ILuaAPI
} }
} }
return null; return MethodResult.empty();
} ); } );
default: 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<ItemStack> inv, int start, IPocketUpgrade previous ) private static IPocketUpgrade findUpgrade( NonNullList<ItemStack> inv, int start, IPocketUpgrade previous )
{ {
for( int i = 0; i < inv.size(); i++ ) for( int i = 0; i < inv.size(); i++ )

View File

@ -6,9 +6,7 @@
package dan200.computercraft.shared.turtle.apis; package dan200.computercraft.shared.turtle.apis;
import dan200.computercraft.api.lua.ILuaAPI; import dan200.computercraft.api.lua.*;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.turtle.ITurtleAccess; import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleCommand; import dan200.computercraft.api.turtle.ITurtleCommand;
import dan200.computercraft.api.turtle.TurtleSide; import dan200.computercraft.api.turtle.TurtleSide;
@ -22,6 +20,7 @@ import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.MinecraftForge;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
@ -99,9 +98,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 private int parseSlotNumber( Object[] arguments, int index ) throws LuaException
@ -155,7 +154,8 @@ public class TurtleAPI implements ILuaAPI
} }
@Override @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 ) switch( method )
{ {
@ -163,89 +163,89 @@ public class TurtleAPI implements ILuaAPI
{ {
// forward // forward
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleMoveCommand( MoveDirection.Forward ) ); return tryCommand( new TurtleMoveCommand( MoveDirection.Forward ) );
} }
case 1: case 1:
{ {
// back // back
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleMoveCommand( MoveDirection.Back ) ); return tryCommand( new TurtleMoveCommand( MoveDirection.Back ) );
} }
case 2: case 2:
{ {
// up // up
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleMoveCommand( MoveDirection.Up ) ); return tryCommand( new TurtleMoveCommand( MoveDirection.Up ) );
} }
case 3: case 3:
{ {
// down // down
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleMoveCommand( MoveDirection.Down ) ); return tryCommand( new TurtleMoveCommand( MoveDirection.Down ) );
} }
case 4: case 4:
{ {
// turnLeft // turnLeft
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleTurnCommand( TurnDirection.Left ) ); return tryCommand( new TurtleTurnCommand( TurnDirection.Left ) );
} }
case 5: case 5:
{ {
// turnRight // turnRight
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleTurnCommand( TurnDirection.Right ) ); return tryCommand( new TurtleTurnCommand( TurnDirection.Right ) );
} }
case 6: case 6:
{ {
// dig // dig
Optional<TurtleSide> side = parseSide( args, 0 ); Optional<TurtleSide> side = parseSide( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleDigCommand( InteractDirection.Forward, side ) ); return tryCommand( new TurtleDigCommand( InteractDirection.Forward, side ) );
} }
case 7: case 7:
{ {
// digUp // digUp
Optional<TurtleSide> side = parseSide( args, 0 ); Optional<TurtleSide> side = parseSide( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleDigCommand( InteractDirection.Up, side ) ); return tryCommand( new TurtleDigCommand( InteractDirection.Up, side ) );
} }
case 8: case 8:
{ {
// digDown // digDown
Optional<TurtleSide> side = parseSide( args, 0 ); Optional<TurtleSide> side = parseSide( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleDigCommand( InteractDirection.Down, side ) ); return tryCommand( new TurtleDigCommand( InteractDirection.Down, side ) );
} }
case 9: case 9:
{ {
// place // place
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtlePlaceCommand( InteractDirection.Forward, args ) ); return tryCommand( new TurtlePlaceCommand( InteractDirection.Forward, args ) );
} }
case 10: case 10:
{ {
// placeUp // placeUp
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtlePlaceCommand( InteractDirection.Up, args ) ); return tryCommand( new TurtlePlaceCommand( InteractDirection.Up, args ) );
} }
case 11: case 11:
{ {
// placeDown // placeDown
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtlePlaceCommand( InteractDirection.Down, args ) ); return tryCommand( new TurtlePlaceCommand( InteractDirection.Down, args ) );
} }
case 12: case 12:
{ {
// drop // drop
int count = parseCount( args, 0 ); int count = parseCount( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleDropCommand( InteractDirection.Forward, count ) ); return tryCommand( new TurtleDropCommand( InteractDirection.Forward, count ) );
} }
case 13: case 13:
{ {
// select // select
int slot = parseSlotNumber( args, 0 ); int slot = parseSlotNumber( args, 0 );
return tryCommand( context, new TurtleSelectCommand( slot ) ); return tryCommand( new TurtleSelectCommand( slot ) );
} }
case 14: case 14:
{ {
@ -254,11 +254,11 @@ public class TurtleAPI implements ILuaAPI
ItemStack stack = m_turtle.getInventory().getStackInSlot( slot ); ItemStack stack = m_turtle.getInventory().getStackInSlot( slot );
if( !stack.isEmpty() ) if( !stack.isEmpty() )
{ {
return new Object[] { stack.getCount() }; return MethodResult.of( stack.getCount() );
} }
else else
{ {
return new Object[] { 0 }; return MethodResult.of( 0 );
} }
} }
case 15: case 15:
@ -268,172 +268,172 @@ public class TurtleAPI implements ILuaAPI
ItemStack stack = m_turtle.getInventory().getStackInSlot( slot ); ItemStack stack = m_turtle.getInventory().getStackInSlot( slot );
if( !stack.isEmpty() ) if( !stack.isEmpty() )
{ {
return new Object[] { return MethodResult.of(
Math.min( stack.getMaxStackSize(), 64 ) - stack.getCount() Math.min( stack.getMaxStackSize(), 64 ) - stack.getCount()
}; );
} }
return new Object[] { 64 }; return MethodResult.of( 64 );
} }
case 16: case 16:
{ {
// detect // detect
return tryCommand( context, new TurtleDetectCommand( InteractDirection.Forward ) ); return tryCommand( new TurtleDetectCommand( InteractDirection.Forward ) );
} }
case 17: case 17:
{ {
// detectUp // detectUp
return tryCommand( context, new TurtleDetectCommand( InteractDirection.Up ) ); return tryCommand( new TurtleDetectCommand( InteractDirection.Up ) );
} }
case 18: case 18:
{ {
// detectDown // detectDown
return tryCommand( context, new TurtleDetectCommand( InteractDirection.Down ) ); return tryCommand( new TurtleDetectCommand( InteractDirection.Down ) );
} }
case 19: case 19:
{ {
// compare // compare
return tryCommand( context, new TurtleCompareCommand( InteractDirection.Forward ) ); return tryCommand( new TurtleCompareCommand( InteractDirection.Forward ) );
} }
case 20: case 20:
{ {
// compareUp // compareUp
return tryCommand( context, new TurtleCompareCommand( InteractDirection.Up ) ); return tryCommand( new TurtleCompareCommand( InteractDirection.Up ) );
} }
case 21: case 21:
{ {
// compareDown // compareDown
return tryCommand( context, new TurtleCompareCommand( InteractDirection.Down ) ); return tryCommand( new TurtleCompareCommand( InteractDirection.Down ) );
} }
case 22: case 22:
{ {
// attack // attack
Optional<TurtleSide> side = parseSide( args, 0 ); Optional<TurtleSide> side = parseSide( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleAttackCommand( InteractDirection.Forward, side ) ); return tryCommand( new TurtleAttackCommand( InteractDirection.Forward, side ) );
} }
case 23: case 23:
{ {
// attackUp // attackUp
Optional<TurtleSide> side = parseSide( args, 0 ); Optional<TurtleSide> side = parseSide( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleAttackCommand( InteractDirection.Up, side ) ); return tryCommand( new TurtleAttackCommand( InteractDirection.Up, side ) );
} }
case 24: case 24:
{ {
// attackDown // attackDown
Optional<TurtleSide> side = parseSide( args, 0 ); Optional<TurtleSide> side = parseSide( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleAttackCommand( InteractDirection.Down, side ) ); return tryCommand( new TurtleAttackCommand( InteractDirection.Down, side ) );
} }
case 25: case 25:
{ {
// dropUp // dropUp
int count = parseCount( args, 0 ); int count = parseCount( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleDropCommand( InteractDirection.Up, count ) ); return tryCommand( new TurtleDropCommand( InteractDirection.Up, count ) );
} }
case 26: case 26:
{ {
// dropDown // dropDown
int count = parseCount( args, 0 ); int count = parseCount( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleDropCommand( InteractDirection.Down, count ) ); return tryCommand( new TurtleDropCommand( InteractDirection.Down, count ) );
} }
case 27: case 27:
{ {
// suck // suck
int count = parseCount( args, 0 ); int count = parseCount( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleSuckCommand( InteractDirection.Forward, count ) ); return tryCommand( new TurtleSuckCommand( InteractDirection.Forward, count ) );
} }
case 28: case 28:
{ {
// suckUp // suckUp
int count = parseCount( args, 0 ); int count = parseCount( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleSuckCommand( InteractDirection.Up, count ) ); return tryCommand( new TurtleSuckCommand( InteractDirection.Up, count ) );
} }
case 29: case 29:
{ {
// suckDown // suckDown
int count = parseCount( args, 0 ); int count = parseCount( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleSuckCommand( InteractDirection.Down, count ) ); return tryCommand( new TurtleSuckCommand( InteractDirection.Down, count ) );
} }
case 30: case 30:
{ {
// getFuelLevel // getFuelLevel
if( m_turtle.isFuelNeeded() ) if( m_turtle.isFuelNeeded() )
{ {
return new Object[] { m_turtle.getFuelLevel() }; return MethodResult.of( m_turtle.getFuelLevel() );
} }
else else
{ {
return new Object[] { "unlimited" }; return MethodResult.of( "unlimited" );
} }
} }
case 31: case 31:
{ {
// refuel // refuel
int count = parseCount( args, 0 ); int count = parseCount( args, 0 );
return tryCommand( context, new TurtleRefuelCommand( count ) ); return tryCommand( new TurtleRefuelCommand( count ) );
} }
case 32: case 32:
{ {
// compareTo // compareTo
int slot = parseSlotNumber( args, 0 ); int slot = parseSlotNumber( args, 0 );
return tryCommand( context, new TurtleCompareToCommand( slot ) ); return tryCommand( new TurtleCompareToCommand( slot ) );
} }
case 33: case 33:
{ {
// transferTo // transferTo
int slot = parseSlotNumber( args, 0 ); int slot = parseSlotNumber( args, 0 );
int count = parseCount( args, 1 ); int count = parseCount( args, 1 );
return tryCommand( context, new TurtleTransferToCommand( slot, count ) ); return tryCommand( new TurtleTransferToCommand( slot, count ) );
} }
case 34: case 34:
{ {
// getSelectedSlot // getSelectedSlot
return new Object[] { m_turtle.getSelectedSlot() + 1 }; return MethodResult.of( m_turtle.getSelectedSlot() + 1 );
} }
case 35: case 35:
{ {
// getFuelLimit // getFuelLimit
if( m_turtle.isFuelNeeded() ) if( m_turtle.isFuelNeeded() )
{ {
return new Object[] { m_turtle.getFuelLimit() }; return MethodResult.of( m_turtle.getFuelLimit() );
} }
else else
{ {
return new Object[] { "unlimited" }; return MethodResult.of( "unlimited" );
} }
} }
case 36: case 36:
{ {
// equipLeft // equipLeft
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleEquipCommand( TurtleSide.Left ) ); return tryCommand( new TurtleEquipCommand( TurtleSide.Left ) );
} }
case 37: case 37:
{ {
// equipRight // equipRight
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleEquipCommand( TurtleSide.Right ) ); return tryCommand( new TurtleEquipCommand( TurtleSide.Right ) );
} }
case 38: case 38:
{ {
// inspect // inspect
return tryCommand( context, new TurtleInspectCommand( InteractDirection.Forward ) ); return tryCommand( new TurtleInspectCommand( InteractDirection.Forward ) );
} }
case 39: case 39:
{ {
// inspectUp // inspectUp
return tryCommand( context, new TurtleInspectCommand( InteractDirection.Up ) ); return tryCommand( new TurtleInspectCommand( InteractDirection.Up ) );
} }
case 40: case 40:
{ {
// inspectDown // inspectDown
return tryCommand( context, new TurtleInspectCommand( InteractDirection.Down ) ); return tryCommand( new TurtleInspectCommand( InteractDirection.Down ) );
} }
case 41: case 41:
{ {
@ -455,20 +455,28 @@ public class TurtleAPI implements ILuaAPI
TurtleActionEvent event = new TurtleActionEvent( m_turtle, TurtleAction.INSPECT_ITEM ); TurtleActionEvent event = new TurtleActionEvent( m_turtle, TurtleAction.INSPECT_ITEM );
if( MinecraftForge.EVENT_BUS.post( event ) ) if( MinecraftForge.EVENT_BUS.post( event ) )
{ {
return new Object[] { false, event.getFailureMessage() }; return MethodResult.of( false, event.getFailureMessage() );
} }
return new Object[] { table }; return MethodResult.of( table );
} }
else else
{ {
return new Object[] { null }; return MethodResult.of( new Object[] { null } );
} }
} }
default: 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 );
}
} }

View File

@ -10,7 +10,9 @@ import com.google.common.base.Objects;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import dan200.computercraft.ComputerCraft; import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.ILuaFunction;
import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.turtle.*; import dan200.computercraft.api.turtle.*;
import dan200.computercraft.shared.computer.blocks.ComputerProxy; import dan200.computercraft.shared.computer.blocks.ComputerProxy;
@ -761,6 +763,7 @@ public class TurtleBrain implements ITurtleAccess
@Nonnull @Nonnull
@Override @Override
@Deprecated
public Object[] executeCommand( @Nonnull ILuaContext context, @Nonnull ITurtleCommand command ) throws LuaException, InterruptedException public Object[] executeCommand( @Nonnull ILuaContext context, @Nonnull ITurtleCommand command ) throws LuaException, InterruptedException
{ {
if( getWorld().isRemote ) if( getWorld().isRemote )
@ -787,6 +790,38 @@ public class TurtleBrain implements ITurtleAccess
} }
} }
@Nonnull
@Override
public MethodResult executeCommand( @Nonnull ITurtleCommand command )
{
if( getWorld().isRemote )
{
throw new UnsupportedOperationException();
}
// Issue command
int commandID = issueCommand( command );
// Wait for response
return MethodResult.pullEvent( "turtle_response", new ILuaFunction()
{
@Nonnull
@Override
public MethodResult call( Object[] response )
{
if( response.length >= 3 && response[ 1 ] instanceof Number && ((Number) response[ 1 ]).intValue() == commandID
&& response[ 2 ] instanceof Boolean )
{
Object[] returnValues = new Object[ response.length - 2 ];
System.arraycopy( response, 2, returnValues, 0, returnValues.length );
return MethodResult.of( returnValues );
}
return MethodResult.pullEvent( "turtle_response", this );
}
} );
}
@Override @Override
public void playAnimation( @Nonnull TurtleAnimation animation ) public void playAnimation( @Nonnull TurtleAnimation animation )
{ {

View File

@ -6,19 +6,21 @@
package dan200.computercraft.shared.turtle.upgrades; package dan200.computercraft.shared.turtle.upgrades;
import dan200.computercraft.api.lua.ICallContext;
import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.turtle.ITurtleAccess; import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.shared.turtle.core.TurtleCraftCommand; import dan200.computercraft.shared.turtle.core.TurtleCraftCommand;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static dan200.computercraft.core.apis.ArgumentHelper.optInt; import static dan200.computercraft.core.apis.ArgumentHelper.optInt;
public class CraftingTablePeripheral public class CraftingTablePeripheral implements IPeripheral
implements IPeripheral
{ {
private final ITurtleAccess m_turtle; private final ITurtleAccess m_turtle;
@ -56,7 +58,8 @@ public class CraftingTablePeripheral
} }
@Override @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 ) switch( method )
{ {
@ -64,15 +67,24 @@ public class CraftingTablePeripheral
{ {
// craft // craft
final int limit = parseCount( arguments ); final int limit = parseCount( arguments );
return m_turtle.executeCommand( context, new TurtleCraftCommand( limit ) ); return m_turtle.executeCommand( new TurtleCraftCommand( limit ) );
} }
default: 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 @Override
public boolean equals( IPeripheral other ) public boolean equals( IPeripheral other )
{ {