1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-02-18 20:10:05 +00:00

Add back executeMainThreadTask

Closes #868
This commit is contained in:
Jonathan Coates 2021-07-24 12:04:21 +01:00
parent 7bcc16bb40
commit 5b57f7509d
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
4 changed files with 47 additions and 1 deletions

View File

@ -27,4 +27,18 @@ public interface ILuaContext
* @see LuaFunction#mainThread() To run functions on the main thread and return their results synchronously.
*/
long issueMainThreadTask( @Nonnull ILuaTask task ) throws LuaException;
/**
* 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.
*
* Note that the return values of your task are handled as events, meaning more complex objects such as maps or
* {@link IDynamicLuaObject} will not preserve their identities.
*
* @param task The task to execute on the main thread.
* @return The objects returned by {@code task}.
* @throws LuaException If the task could not be queued, or if the task threw an exception.
*/
@Nonnull
MethodResult executeMainThreadTask( @Nonnull ILuaTask task ) throws LuaException;
}

View File

@ -9,6 +9,8 @@ import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.ILuaTask;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.core.asm.TaskCallback;
import dan200.computercraft.core.computer.Computer;
import dan200.computercraft.core.computer.MainThread;
@ -66,4 +68,11 @@ class LuaContext implements ILuaContext
throw new LuaException( "Task limit exceeded" );
}
}
@Nonnull
@Override
public MethodResult executeMainThreadTask( @Nonnull ILuaTask task ) throws LuaException
{
return TaskCallback.make( this, task );
}
}

View File

@ -8,6 +8,7 @@ package dan200.computercraft.core.apis;
import dan200.computercraft.api.lua.*;
import dan200.computercraft.core.asm.LuaMethod;
import dan200.computercraft.core.asm.NamedMethod;
import dan200.computercraft.core.asm.TaskCallback;
import javax.annotation.Nonnull;
import java.util.HashMap;
@ -64,4 +65,11 @@ public class ObjectWrapper implements ILuaContext
{
throw new IllegalStateException( "Method should never queue events" );
}
@Nonnull
@Override
public MethodResult executeMainThreadTask( @Nonnull ILuaTask task ) throws LuaException
{
return TaskCallback.make( this, task );
}
}

View File

@ -11,6 +11,7 @@ import dan200.computercraft.core.computer.ComputerSide;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Test;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
@ -248,5 +249,19 @@ public class GeneratorTest
return contramap( is( method ), "name", NamedMethod::getName );
}
private static final ILuaContext CONTEXT = task -> 0;
private static final ILuaContext CONTEXT = new ILuaContext()
{
@Override
public long issueMainThreadTask( @Nonnull ILuaTask task )
{
return 0;
}
@Nonnull
@Override
public MethodResult executeMainThreadTask( @Nonnull ILuaTask task ) throws LuaException
{
return TaskCallback.make( this, task );
}
};
}