1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-12 11:10: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.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleCommand;
import dan200.computercraft.api.turtle.TurtleCommandResult;
import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.api.turtle.event.TurtleAction;
import dan200.computercraft.api.turtle.event.TurtleActionEvent;
@ -22,9 +23,9 @@ import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
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
{
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;
return parseSlotNumber( arguments, index );
}
private int parseCount( Object[] arguments, int index ) throws LuaException
{
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;
}
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 );
if( side == null )
{
return Optional.empty();
return null;
}
else if( side.equalsIgnoreCase( "left" ) )
{
return Optional.of( TurtleSide.Left );
return TurtleSide.Left;
}
else if( side.equalsIgnoreCase( "right" ) )
{
return Optional.of( TurtleSide.Right );
return TurtleSide.Right;
}
else
{
@ -198,23 +191,23 @@ public class TurtleAPI implements ILuaAPI
case 6:
{
// dig
Optional<TurtleSide> side = parseSide( args, 0 );
TurtleSide side = parseSide( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleDigCommand( InteractDirection.Forward, side ) );
return tryCommand( context, TurtleToolCommand.dig( InteractDirection.Forward, side ) );
}
case 7:
{
// digUp
Optional<TurtleSide> side = parseSide( args, 0 );
TurtleSide side = parseSide( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleDigCommand( InteractDirection.Up, side ) );
return tryCommand( context, TurtleToolCommand.dig( InteractDirection.Up, side ) );
}
case 8:
{
// digDown
Optional<TurtleSide> side = parseSide( args, 0 );
TurtleSide side = parseSide( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleDigCommand( InteractDirection.Down, side ) );
return tryCommand( context, TurtleToolCommand.dig( InteractDirection.Down, side ) );
}
case 9:
{
@ -245,7 +238,10 @@ public class TurtleAPI implements ILuaAPI
{
// select
int slot = parseSlotNumber( args, 0 );
return tryCommand( context, new TurtleSelectCommand( slot ) );
return tryCommand( context, turtle -> {
turtle.setSelectedSlot( slot );
return TurtleCommandResult.success();
} );
}
case 14:
{
@ -307,23 +303,23 @@ public class TurtleAPI implements ILuaAPI
case 22:
{
// attack
Optional<TurtleSide> side = parseSide( args, 0 );
TurtleSide side = parseSide( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleAttackCommand( InteractDirection.Forward, side ) );
return tryCommand( context, TurtleToolCommand.attack( InteractDirection.Forward, side ) );
}
case 23:
{
// attackUp
Optional<TurtleSide> side = parseSide( args, 0 );
TurtleSide side = parseSide( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleAttackCommand( InteractDirection.Up, side ) );
return tryCommand( context, TurtleToolCommand.attack( InteractDirection.Up, side ) );
}
case 24:
{
// attackDown
Optional<TurtleSide> side = parseSide( args, 0 );
TurtleSide side = parseSide( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleAttackCommand( InteractDirection.Down, side ) );
return tryCommand( context, TurtleToolCommand.attack( InteractDirection.Down, side ) );
}
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 javax.annotation.Nonnull;
import java.util.Optional;
import javax.annotation.Nullable;
import java.util.Locale;
public class TurtleToolCommand implements ITurtleCommand
{
private final TurtleVerb m_verb;
private final InteractDirection m_direction;
private final Optional<TurtleSide> m_side;
private final TurtleVerb verb;
private final InteractDirection direction;
private final TurtleSide side;
public TurtleToolCommand( TurtleVerb verb, InteractDirection direction, Optional<TurtleSide> side )
public TurtleToolCommand( TurtleVerb verb, InteractDirection direction, TurtleSide side )
{
m_verb = verb;
m_direction = direction;
m_side = side;
this.verb = verb;
this.direction = direction;
this.side = side;
}
@Nonnull
@ -31,32 +32,26 @@ public class TurtleToolCommand implements ITurtleCommand
TurtleCommandResult firstFailure = null;
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() )
{
TurtleCommandResult result = upgrade.useTool( turtle, side, m_verb, m_direction.toWorldDir( turtle ) );
if( upgrade == null || !upgrade.getType().isTool() ) continue;
TurtleCommandResult result = upgrade.useTool( turtle, side, verb, direction.toWorldDir( turtle ) );
if( result.isSuccess() )
{
switch( side )
{
case Left:
{
turtle.playAnimation( TurtleAnimation.SwingLeftTool );
break;
}
case Right:
{
turtle.playAnimation( TurtleAnimation.SwingRightTool );
break;
}
default:
{
turtle.playAnimation( TurtleAnimation.Wait );
break;
}
}
return result;
}
else if( firstFailure == null )
@ -64,15 +59,17 @@ public class TurtleToolCommand implements ITurtleCommand
firstFailure = result;
}
}
return firstFailure != null ? firstFailure
: TurtleCommandResult.failure( "No tool to " + verb.name().toLowerCase( Locale.ROOT ) + " with" );
}
}
if( firstFailure != null )
public static TurtleToolCommand attack( InteractDirection direction, @Nullable TurtleSide side )
{
return firstFailure;
return new TurtleToolCommand( TurtleVerb.Attack, direction, side );
}
else
public static TurtleToolCommand dig( InteractDirection direction, @Nullable TurtleSide side )
{
return TurtleCommandResult.failure( "No tool to " + m_verb.toString().toLowerCase() + " with" );
}
return new TurtleToolCommand( TurtleVerb.Dig, direction, side );
}
}