1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-11-22 08:04:49 +00:00

Merge branch 'mc-1.16.x' into mc-1.17.x

This commit is contained in:
Jonathan Coates
2021-11-25 13:34:19 +00:00
38 changed files with 764 additions and 474 deletions

View File

@@ -40,5 +40,8 @@ public interface ILuaContext
* @throws LuaException If the task could not be queued, or if the task threw an exception.
*/
@Nonnull
MethodResult executeMainThreadTask( @Nonnull ILuaTask task ) throws LuaException;
default MethodResult executeMainThreadTask( @Nonnull ILuaTask task ) throws LuaException
{
return TaskCallback.make( this, task );
}
}

View File

@@ -0,0 +1,53 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2021. 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.Arrays;
final class TaskCallback implements ILuaCallback
{
private final MethodResult pull = MethodResult.pullEvent( "task_complete", this );
private final long task;
private TaskCallback( long task )
{
this.task = task;
}
@Nonnull
@Override
public MethodResult resume( Object[] response ) throws LuaException
{
if( response.length < 3 || !(response[1] instanceof Number) || !(response[2] instanceof Boolean) )
{
return pull;
}
if( ((Number) response[1]).longValue() != task ) return pull;
if( (Boolean) response[2] )
{
// Extract the return values from the event and return them
return MethodResult.of( Arrays.copyOfRange( response, 3, response.length ) );
}
else if( response.length >= 4 && response[3] instanceof String )
{
// Extract the error message from the event and raise it
throw new LuaException( (String) response[3] );
}
else
{
throw new LuaException( "error" );
}
}
static MethodResult make( ILuaContext context, ILuaTask func ) throws LuaException
{
long task = context.issueMainThreadTask( func );
return new TaskCallback( task ).pull;
}
}

View File

@@ -0,0 +1,45 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2021. 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.peripheral;
import dan200.computercraft.api.lua.GenericSource;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.items.IItemHandler;
import javax.annotation.Nonnull;
/**
* A {@link GenericSource} which provides methods for a peripheral.
*
* Unlike a {@link GenericSource}, all methods <strong>should</strong> target the same type, for instance a
* {@link BlockEntity} subclass or a capability interface. This is not currently enforced.
*/
public interface GenericPeripheral extends GenericSource
{
/**
* Get the type of the exposed peripheral.
*
* Unlike normal {@link IPeripheral}s, {@link GenericPeripheral} do not have to have a type. By default, the
* resulting peripheral uses the resource name of the wrapped {@link BlockEntity} (for instance {@literal minecraft:chest}).
*
* However, in some cases it may be more appropriate to specify a more readable name. Overriding this method allows
* you to do so.
*
* When multiple {@link GenericPeripheral}s return a non-empty peripheral type for a single tile entity, the
* lexicographically smallest will be chosen. In order to avoid this conflict, this method should only be
* implemented when your peripheral targets a single tile entity <strong>AND</strong> it's likely that you're the
* only mod to do so. Similarly this should <strong>NOT</strong> be implemented when your methods target a
* capability or other interface (i.e. {@link IItemHandler}).
*
* @return The type of this peripheral or {@link PeripheralType#untyped()}.
* @see IPeripheral#getType()
*/
@Nonnull
default PeripheralType getType()
{
return PeripheralType.untyped();
}
}

View File

@@ -0,0 +1,62 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2021. 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.peripheral;
import com.google.common.base.Strings;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* The type of a {@link GenericPeripheral}.
*
* When determining the final type of the resulting peripheral, the union of all types is taken, with the
* lexicographically smallest non-empty name being chosen.
*/
public final class PeripheralType
{
private static final PeripheralType UNTYPED = new PeripheralType( null );
private final String type;
public PeripheralType( String type )
{
this.type = type;
}
/**
* An empty peripheral type, used when a {@link GenericPeripheral} does not have an explicit type.
*
* @return The empty peripheral type.
*/
public static PeripheralType untyped()
{
return UNTYPED;
}
/**
* Create a new non-empty peripheral type.
*
* @param type The name of the type.
* @return The constructed peripheral type.
*/
public static PeripheralType ofType( @Nonnull String type )
{
if( Strings.isNullOrEmpty( type ) ) throw new IllegalArgumentException( "type cannot be null or empty" );
return new PeripheralType( type );
}
/**
* Get the name of this peripheral type. This may be {@literal null}.
*
* @return The type of this peripheral.
*/
@Nullable
public String getPrimaryType()
{
return type;
}
}