1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-12 19:20:29 +00:00

Remove a couple of redundant commands

- Remove the two redstone commands. These are not used (maybe only by
   CCEdu), and so are somewhat redundant.
 - Inline the select command, converting it to a lambda.
 - Inline the attack/dig commands, as they did little more than wrap the
   tool command.
This commit is contained in:
SquidDev 2018-12-26 09:21:33 +00:00
parent 4ead319092
commit 031f17c98e
7 changed files with 69 additions and 268 deletions

View File

@ -11,6 +11,7 @@ import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException; 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.TurtleCommandResult;
import dan200.computercraft.api.turtle.TurtleSide; import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.api.turtle.event.TurtleAction; import dan200.computercraft.api.turtle.event.TurtleAction;
import dan200.computercraft.api.turtle.event.TurtleActionEvent; import dan200.computercraft.api.turtle.event.TurtleActionEvent;
@ -22,9 +23,9 @@ 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 static dan200.computercraft.core.apis.ArgumentHelper.*; import static dan200.computercraft.core.apis.ArgumentHelper.*;
@ -114,39 +115,31 @@ public class TurtleAPI implements ILuaAPI
private int parseOptionalSlotNumber( Object[] arguments, int index, int fallback ) throws LuaException private int parseOptionalSlotNumber( Object[] arguments, int index, int fallback ) throws LuaException
{ {
if( index >= arguments.length || arguments[index] == null ) return fallback; if( index >= arguments.length || arguments[index] == null ) return fallback;
return parseSlotNumber( arguments, index );
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 private int parseCount( Object[] arguments, int index ) throws LuaException
{ {
int count = optInt( arguments, index, 64 ); int count = optInt( arguments, index, 64 );
if( count >= 0 && count <= 64 ) if( count < 0 || count > 64 ) throw new LuaException( "Item count " + count + " out of range" );
{ return count;
return count;
}
else
{
throw new LuaException( "Item count " + count + " out of range" );
}
} }
private Optional<TurtleSide> parseSide( Object[] arguments, int index ) throws LuaException @Nullable
private TurtleSide parseSide( Object[] arguments, int index ) throws LuaException
{ {
String side = optString( arguments, index, null ); String side = optString( arguments, index, null );
if( side == null ) if( side == null )
{ {
return Optional.empty(); return null;
} }
else if( side.equalsIgnoreCase( "left" ) ) else if( side.equalsIgnoreCase( "left" ) )
{ {
return Optional.of( TurtleSide.Left ); return TurtleSide.Left;
} }
else if( side.equalsIgnoreCase( "right" ) ) else if( side.equalsIgnoreCase( "right" ) )
{ {
return Optional.of( TurtleSide.Right ); return TurtleSide.Right;
} }
else else
{ {
@ -198,23 +191,23 @@ public class TurtleAPI implements ILuaAPI
case 6: case 6:
{ {
// dig // dig
Optional<TurtleSide> side = parseSide( args, 0 ); 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( context, TurtleToolCommand.dig( InteractDirection.Forward, side ) );
} }
case 7: case 7:
{ {
// digUp // digUp
Optional<TurtleSide> side = parseSide( args, 0 ); 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( context, TurtleToolCommand.dig( InteractDirection.Up, side ) );
} }
case 8: case 8:
{ {
// digDown // digDown
Optional<TurtleSide> side = parseSide( args, 0 ); 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( context, TurtleToolCommand.dig( InteractDirection.Down, side ) );
} }
case 9: case 9:
{ {
@ -245,7 +238,10 @@ public class TurtleAPI implements ILuaAPI
{ {
// select // select
int slot = parseSlotNumber( args, 0 ); int slot = parseSlotNumber( args, 0 );
return tryCommand( context, new TurtleSelectCommand( slot ) ); return tryCommand( context, turtle -> {
turtle.setSelectedSlot( slot );
return TurtleCommandResult.success();
} );
} }
case 14: case 14:
{ {
@ -307,23 +303,23 @@ public class TurtleAPI implements ILuaAPI
case 22: case 22:
{ {
// attack // attack
Optional<TurtleSide> side = parseSide( args, 0 ); 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( context, TurtleToolCommand.attack( InteractDirection.Forward, side ) );
} }
case 23: case 23:
{ {
// attackUp // attackUp
Optional<TurtleSide> side = parseSide( args, 0 ); 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( context, TurtleToolCommand.attack( InteractDirection.Up, side ) );
} }
case 24: case 24:
{ {
// attackDown // attackDown
Optional<TurtleSide> side = parseSide( args, 0 ); 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( context, TurtleToolCommand.attack( InteractDirection.Down, side ) );
} }
case 25: case 25:
{ {

View File

@ -1,20 +0,0 @@
/*
* 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.core;
import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.api.turtle.TurtleVerb;
import java.util.Optional;
public class TurtleAttackCommand extends TurtleToolCommand
{
public TurtleAttackCommand( InteractDirection direction, Optional<TurtleSide> side )
{
super( TurtleVerb.Attack, direction, side );
}
}

View File

@ -1,62 +0,0 @@
/*
* 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.core;
import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleCommand;
import dan200.computercraft.api.turtle.TurtleCommandResult;
import dan200.computercraft.core.apis.IAPIEnvironment;
import javax.annotation.Nonnull;
public class TurtleCheckRedstoneCommand implements ITurtleCommand
{
private final IAPIEnvironment m_environment;
private final InteractDirection m_direction;
public TurtleCheckRedstoneCommand( IAPIEnvironment environment, InteractDirection direction )
{
m_environment = environment;
m_direction = direction;
}
@Nonnull
@Override
public TurtleCommandResult execute( @Nonnull ITurtleAccess turtle )
{
// Do the checking
int redstoneSide;
switch( m_direction )
{
case Forward:
default:
{
redstoneSide = 3;
break;
}
case Up:
{
redstoneSide = 1;
break;
}
case Down:
{
redstoneSide = 2;
break;
}
}
int power = m_environment.getInput( redstoneSide );
if( power > 0 )
{
return TurtleCommandResult.success();
}
else
{
return TurtleCommandResult.failure();
}
}
}

View File

@ -1,20 +0,0 @@
/*
* 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.core;
import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.api.turtle.TurtleVerb;
import java.util.Optional;
public class TurtleDigCommand extends TurtleToolCommand
{
public TurtleDigCommand( InteractDirection direction, Optional<TurtleSide> side )
{
super( TurtleVerb.Dig, direction, side );
}
}

View File

@ -1,31 +0,0 @@
/*
* 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.core;
import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleCommand;
import dan200.computercraft.api.turtle.TurtleCommandResult;
import javax.annotation.Nonnull;
public class TurtleSelectCommand implements ITurtleCommand
{
private final int m_slot;
public TurtleSelectCommand( int slot )
{
m_slot = slot;
}
@Nonnull
@Override
public TurtleCommandResult execute( @Nonnull ITurtleAccess turtle )
{
turtle.setSelectedSlot( m_slot );
return TurtleCommandResult.success();
}
}

View File

@ -1,59 +0,0 @@
/*
* 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.core;
import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleCommand;
import dan200.computercraft.api.turtle.TurtleAnimation;
import dan200.computercraft.api.turtle.TurtleCommandResult;
import dan200.computercraft.core.apis.IAPIEnvironment;
import javax.annotation.Nonnull;
public class TurtleSetRedstoneCommand implements ITurtleCommand
{
private final IAPIEnvironment m_environment;
private final InteractDirection m_direction;
private final int m_value;
public TurtleSetRedstoneCommand( IAPIEnvironment environment, InteractDirection direction, int value )
{
m_environment = environment;
m_direction = direction;
m_value = value;
}
@Nonnull
@Override
public TurtleCommandResult execute( @Nonnull ITurtleAccess turtle )
{
// Do the setting
int redstoneSide;
switch( m_direction )
{
case Forward:
default:
{
redstoneSide = 3;
break;
}
case Up:
{
redstoneSide = 1;
break;
}
case Down:
{
redstoneSide = 2;
break;
}
}
m_environment.setOutput( redstoneSide, m_value );
turtle.playAnimation( TurtleAnimation.ShortWait );
return TurtleCommandResult.success();
}
}

View File

@ -9,19 +9,20 @@ package dan200.computercraft.shared.turtle.core;
import dan200.computercraft.api.turtle.*; import dan200.computercraft.api.turtle.*;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.Optional; import javax.annotation.Nullable;
import java.util.Locale;
public class TurtleToolCommand implements ITurtleCommand public class TurtleToolCommand implements ITurtleCommand
{ {
private final TurtleVerb m_verb; private final TurtleVerb verb;
private final InteractDirection m_direction; private final InteractDirection direction;
private final Optional<TurtleSide> m_side; private final TurtleSide side;
public TurtleToolCommand( TurtleVerb verb, InteractDirection direction, Optional<TurtleSide> side ) public TurtleToolCommand( TurtleVerb verb, InteractDirection direction, TurtleSide side )
{ {
m_verb = verb; this.verb = verb;
m_direction = direction; this.direction = direction;
m_side = side; this.side = side;
} }
@Nonnull @Nonnull
@ -31,48 +32,44 @@ public class TurtleToolCommand implements ITurtleCommand
TurtleCommandResult firstFailure = null; TurtleCommandResult firstFailure = null;
for( TurtleSide side : TurtleSide.values() ) for( TurtleSide side : TurtleSide.values() )
{ {
if( !m_side.isPresent() || m_side.get() == side ) if( this.side != null && this.side != side ) continue;
ITurtleUpgrade upgrade = turtle.getUpgrade( side );
if( upgrade == null || !upgrade.getType().isTool() ) continue;
TurtleCommandResult result = upgrade.useTool( turtle, side, verb, direction.toWorldDir( turtle ) );
if( result.isSuccess() )
{ {
ITurtleUpgrade upgrade = turtle.getUpgrade( side ); switch( side )
if( upgrade != null && upgrade.getType().isTool() )
{ {
TurtleCommandResult result = upgrade.useTool( turtle, side, m_verb, m_direction.toWorldDir( turtle ) ); case Left:
if( result.isSuccess() ) turtle.playAnimation( TurtleAnimation.SwingLeftTool );
{ break;
switch( side ) case Right:
{ turtle.playAnimation( TurtleAnimation.SwingRightTool );
case Left: break;
{ default:
turtle.playAnimation( TurtleAnimation.SwingLeftTool ); turtle.playAnimation( TurtleAnimation.Wait );
break; break;
}
case Right:
{
turtle.playAnimation( TurtleAnimation.SwingRightTool );
break;
}
default:
{
turtle.playAnimation( TurtleAnimation.Wait );
break;
}
}
return result;
}
else if( firstFailure == null )
{
firstFailure = result;
}
} }
return result;
}
else if( firstFailure == null )
{
firstFailure = result;
} }
} }
if( firstFailure != null ) return firstFailure != null ? firstFailure
{ : TurtleCommandResult.failure( "No tool to " + verb.name().toLowerCase( Locale.ROOT ) + " with" );
return firstFailure; }
}
else public static TurtleToolCommand attack( InteractDirection direction, @Nullable TurtleSide side )
{ {
return TurtleCommandResult.failure( "No tool to " + m_verb.toString().toLowerCase() + " with" ); return new TurtleToolCommand( TurtleVerb.Attack, direction, side );
} }
public static TurtleToolCommand dig( InteractDirection direction, @Nullable TurtleSide side )
{
return new TurtleToolCommand( TurtleVerb.Dig, direction, side );
} }
} }