CC-Tweaked/src/main/java/dan200/computercraft/shared/turtle/apis/TurtleAPI.java

451 lines
15 KiB
Java
Raw Normal View History

/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.turtle.apis;
import dan200.computercraft.api.lua.ILuaAPI;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleCommand;
import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.api.turtle.event.TurtleAction;
import dan200.computercraft.api.turtle.event.TurtleActionEvent;
import dan200.computercraft.core.apis.IAPIEnvironment;
import dan200.computercraft.shared.turtle.core.*;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
2017-05-06 23:07:42 +00:00
import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import static dan200.computercraft.core.apis.ArgumentHelper.*;
public class TurtleAPI implements ILuaAPI
{
private IAPIEnvironment m_environment;
2017-05-01 14:48:44 +00:00
private ITurtleAccess m_turtle;
public TurtleAPI( IAPIEnvironment environment, ITurtleAccess turtle )
{
m_environment = environment;
2017-05-01 14:48:44 +00:00
m_turtle = turtle;
}
2017-05-01 14:48:44 +00:00
// ILuaAPI implementation
2017-05-01 14:48:44 +00:00
@Override
public String[] getNames()
{
2017-05-01 14:48:44 +00:00
return new String[] {
"turtle"
};
}
2017-05-01 14:48:44 +00:00
2017-05-06 23:07:42 +00:00
@Nonnull
2017-05-01 14:48:44 +00:00
@Override
public String[] getMethodNames()
{
2017-05-01 14:48:44 +00:00
return new String[] {
"forward",
"back",
"up",
"down",
"turnLeft",
"turnRight",
"dig",
"digUp",
"digDown",
"place",
"placeUp",
"placeDown",
"drop",
"select",
"getItemCount",
"getItemSpace",
"detect",
"detectUp",
"detectDown",
"compare",
"compareUp",
"compareDown",
"attack",
"attackUp",
"attackDown",
"dropUp",
"dropDown",
"suck",
"suckUp",
"suckDown",
"getFuelLevel",
"refuel",
"compareTo",
"transferTo",
"getSelectedSlot",
"getFuelLimit",
"equipLeft",
"equipRight",
"inspect",
"inspectUp",
"inspectDown",
"getItemDetail",
2017-05-01 14:48:44 +00:00
};
}
private Object[] tryCommand( ILuaContext context, ITurtleCommand command ) throws LuaException, InterruptedException
{
return m_turtle.executeCommand( context, command );
}
private int parseSlotNumber( Object[] arguments, int index ) throws LuaException
{
int slot = getInt( arguments, index );
if( slot < 1 || slot > 16 ) throw new LuaException( "Slot number " + slot + " out of range" );
return slot - 1;
}
private int parseOptionalSlotNumber( Object[] arguments, int index, int fallback ) throws LuaException
{
if( index >= arguments.length || arguments[ index ] == null ) return fallback;
int slot = getInt( arguments, index );
if( slot < 1 || slot > 16 ) throw new LuaException( "Slot number " + slot + " out of range" );
return slot - 1;
}
private int parseCount( Object[] arguments, int index ) throws LuaException
{
int count = optInt( arguments, index, 64 );
2017-05-01 14:48:44 +00:00
if( count >= 0 && count <= 64 )
{
return count;
2017-05-01 14:48:44 +00:00
}
else
{
throw new LuaException( "Item count " + count + " out of range" );
}
}
private Optional<TurtleSide> parseSide( Object[] arguments, int index ) throws LuaException
{
String side = optString( arguments, index, null );
if( side == null )
{
return Optional.empty();
}
else if( side.equalsIgnoreCase( "left" ) )
{
return Optional.of( TurtleSide.Left );
}
else if( side.equalsIgnoreCase( "right" ) )
{
return Optional.of( TurtleSide.Right );
}
else
{
throw new LuaException( "Invalid side" );
}
}
2017-05-01 14:48:44 +00:00
@Override
2017-05-06 23:07:42 +00:00
public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException, InterruptedException
{
2017-05-01 14:48:44 +00:00
switch( method )
{
case 0:
{
// forward
return tryCommand( context, new TurtleMoveCommand( MoveDirection.Forward ) );
}
case 1:
{
// back
return tryCommand( context, new TurtleMoveCommand( MoveDirection.Back ) );
2017-05-01 14:48:44 +00:00
}
case 2:
{
// up
return tryCommand( context, new TurtleMoveCommand( MoveDirection.Up ) );
}
case 3:
{
// down
return tryCommand( context, new TurtleMoveCommand( MoveDirection.Down ) );
}
case 4:
{
// turnLeft
return tryCommand( context, new TurtleTurnCommand( TurnDirection.Left ) );
}
case 5:
{
// turnRight
return tryCommand( context, new TurtleTurnCommand( TurnDirection.Right ) );
}
case 6:
{
// dig
Optional<TurtleSide> side = parseSide( args, 0 );
2017-05-01 14:48:44 +00:00
return tryCommand( context, new TurtleDigCommand( InteractDirection.Forward, side ) );
}
case 7:
{
// digUp
Optional<TurtleSide> side = parseSide( args, 0 );
return tryCommand( context, new TurtleDigCommand( InteractDirection.Up, side ) );
2017-05-01 14:48:44 +00:00
}
case 8:
{
// digDown
Optional<TurtleSide> side = parseSide( args, 0 );
return tryCommand( context, new TurtleDigCommand( InteractDirection.Down, side ) );
2017-05-01 14:48:44 +00:00
}
case 9:
{
// place
return tryCommand( context, new TurtlePlaceCommand( InteractDirection.Forward, args ) );
2017-05-01 14:48:44 +00:00
}
case 10:
{
// placeUp
return tryCommand( context, new TurtlePlaceCommand( InteractDirection.Up, args ) );
2017-05-01 14:48:44 +00:00
}
case 11:
{
// placeDown
return tryCommand( context, new TurtlePlaceCommand( InteractDirection.Down, args ) );
2017-05-01 14:48:44 +00:00
}
case 12:
{
// drop
int count = parseCount( args, 0 );
return tryCommand( context, new TurtleDropCommand( InteractDirection.Forward, count ) );
2017-05-01 14:48:44 +00:00
}
case 13:
{
// select
int slot = parseSlotNumber( args, 0 );
return tryCommand( context, new TurtleSelectCommand( slot ) );
2017-05-01 14:48:44 +00:00
}
case 14:
{
// getItemCount
int slot = parseOptionalSlotNumber( args, 0, m_turtle.getSelectedSlot() );
ItemStack stack = m_turtle.getInventory().getStackInSlot( slot );
2017-05-11 00:08:26 +00:00
if( !stack.isEmpty() )
{
2017-05-11 00:08:26 +00:00
return new Object[] { stack.getCount() };
}
else
{
return new Object[] { 0 };
}
2017-05-01 14:48:44 +00:00
}
case 15:
{
// getItemSpace
int slot = parseOptionalSlotNumber( args, 0, m_turtle.getSelectedSlot() );
ItemStack stack = m_turtle.getInventory().getStackInSlot( slot );
2017-05-11 00:08:26 +00:00
if( !stack.isEmpty() )
{
return new Object[] {
2017-05-11 00:08:26 +00:00
Math.min( stack.getMaxStackSize(), 64 ) - stack.getCount()
};
}
return new Object[] { 64 };
2017-05-01 14:48:44 +00:00
}
case 16:
{
// detect
return tryCommand( context, new TurtleDetectCommand( InteractDirection.Forward ) );
2017-05-01 14:48:44 +00:00
}
case 17:
{
// detectUp
return tryCommand( context, new TurtleDetectCommand( InteractDirection.Up ) );
2017-05-01 14:48:44 +00:00
}
case 18:
{
// detectDown
return tryCommand( context, new TurtleDetectCommand( InteractDirection.Down ) );
2017-05-01 14:48:44 +00:00
}
case 19:
{
// compare
return tryCommand( context, new TurtleCompareCommand( InteractDirection.Forward ) );
2017-05-01 14:48:44 +00:00
}
case 20:
{
// compareUp
return tryCommand( context, new TurtleCompareCommand( InteractDirection.Up ) );
2017-05-01 14:48:44 +00:00
}
case 21:
{
// compareDown
return tryCommand( context, new TurtleCompareCommand( InteractDirection.Down ) );
2017-05-01 14:48:44 +00:00
}
case 22:
{
// attack
Optional<TurtleSide> side = parseSide( args, 0 );
return tryCommand( context, new TurtleAttackCommand( InteractDirection.Forward, side ) );
2017-05-01 14:48:44 +00:00
}
case 23:
{
// attackUp
Optional<TurtleSide> side = parseSide( args, 0 );
return tryCommand( context, new TurtleAttackCommand( InteractDirection.Up, side ) );
2017-05-01 14:48:44 +00:00
}
case 24:
{
// attackDown
Optional<TurtleSide> side = parseSide( args, 0 );
return tryCommand( context, new TurtleAttackCommand( InteractDirection.Down, side ) );
2017-05-01 14:48:44 +00:00
}
case 25:
{
// dropUp
int count = parseCount( args, 0 );
return tryCommand( context, new TurtleDropCommand( InteractDirection.Up, count ) );
2017-05-01 14:48:44 +00:00
}
case 26:
{
// dropDown
int count = parseCount( args, 0 );
return tryCommand( context, new TurtleDropCommand( InteractDirection.Down, count ) );
2017-05-01 14:48:44 +00:00
}
case 27:
{
// suck
int count = parseCount( args, 0 );
2017-05-01 14:48:44 +00:00
return tryCommand( context, new TurtleSuckCommand( InteractDirection.Forward, count ) );
}
case 28:
{
// suckUp
int count = parseCount( args, 0 );
return tryCommand( context, new TurtleSuckCommand( InteractDirection.Up, count ) );
2017-05-01 14:48:44 +00:00
}
case 29:
{
// suckDown
int count = parseCount( args, 0 );
return tryCommand( context, new TurtleSuckCommand( InteractDirection.Down, count ) );
2017-05-01 14:48:44 +00:00
}
case 30:
{
// getFuelLevel
if( m_turtle.isFuelNeeded() )
{
return new Object[] { m_turtle.getFuelLevel() };
}
else
{
2017-05-01 14:48:44 +00:00
return new Object[] { "unlimited" };
}
2017-05-01 14:48:44 +00:00
}
case 31:
{
// refuel
int count = parseCount( args, 0 );
return tryCommand( context, new TurtleRefuelCommand( count ) );
2017-05-01 14:48:44 +00:00
}
case 32:
{
// compareTo
int slot = parseSlotNumber( args, 0 );
return tryCommand( context, new TurtleCompareToCommand( slot ) );
}
2017-05-01 14:48:44 +00:00
case 33:
{
// transferTo
int slot = parseSlotNumber( args, 0 );
int count = parseCount( args, 1 );
2017-05-01 14:48:44 +00:00
return tryCommand( context, new TurtleTransferToCommand( slot, count ) );
}
case 34:
{
// getSelectedSlot
return new Object[] { m_turtle.getSelectedSlot() + 1 };
}
case 35:
{
// getFuelLimit
if( m_turtle.isFuelNeeded() )
{
return new Object[] { m_turtle.getFuelLimit() };
}
else
{
return new Object[] { "unlimited" };
}
}
case 36:
{
// equipLeft
return tryCommand( context, new TurtleEquipCommand( TurtleSide.Left ) );
}
case 37:
{
// equipRight
return tryCommand( context, new TurtleEquipCommand( TurtleSide.Right ) );
}
case 38:
{
// inspect
return tryCommand( context, new TurtleInspectCommand( InteractDirection.Forward ) );
}
case 39:
{
// inspectUp
return tryCommand( context, new TurtleInspectCommand( InteractDirection.Up ) );
}
case 40:
{
// inspectDown
return tryCommand( context, new TurtleInspectCommand( InteractDirection.Down ) );
}
case 41:
{
// getItemDetail
int slot = parseOptionalSlotNumber( args, 0, m_turtle.getSelectedSlot() );
ItemStack stack = m_turtle.getInventory().getStackInSlot( slot );
2017-05-11 00:08:26 +00:00
if( !stack.isEmpty() )
{
Item item = stack.getItem();
String name = Item.REGISTRY.getNameForObject( item ).toString();
int damage = stack.getItemDamage();
2017-05-11 00:08:26 +00:00
int count = stack.getCount();
Map<Object, Object> table = new HashMap<>();
table.put( "name", name );
table.put( "damage", damage );
table.put( "count", count );
TurtleActionEvent event = new TurtleActionEvent( m_turtle, TurtleAction.INSPECT_ITEM );
if( MinecraftForge.EVENT_BUS.post( event ) )
{
return new Object[] { false, event.getFailureMessage() };
}
return new Object[] { table };
}
else
{
return new Object[] { null };
}
}
2017-05-01 14:48:44 +00:00
default:
{
return null;
}
}
}
}