Error when missing computers after executing the command

This allows you to automate running various commands and still have them
"work" if some computers are not loaded.

Closes #47
This commit is contained in:
SquidDev 2018-08-04 10:39:44 +01:00
parent a3d1cff298
commit f40733e9a6
2 changed files with 54 additions and 66 deletions

View File

@ -23,6 +23,7 @@
import javax.annotation.Nonnull;
import java.util.*;
import java.util.function.Consumer;
import static dan200.computercraft.shared.command.framework.ChatHelpers.*;
@ -147,26 +148,15 @@ public List<String> getCompletion( @Nonnull CommandContext context, @Nonnull Lis
@Override
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
{
Set<ServerComputer> computers = Sets.newHashSet();
if( arguments.size() > 0 )
{
for( String arg : arguments )
withComputers( arguments, computers -> {
int shutdown = 0;
for( ServerComputer computer : computers )
{
computers.addAll( ComputerSelector.getComputers( arg ) );
if( computer.isOn() ) shutdown++;
computer.unload();
}
}
else
{
computers.addAll( ComputerCraft.serverComputerRegistry.getComputers() );
}
int shutdown = 0;
for( ServerComputer computer : computers )
{
if( computer.isOn() ) shutdown++;
computer.unload();
}
context.getSender().sendMessage( text( "Shutdown " + shutdown + " / " + computers.size() + " computers" ) );
context.getSender().sendMessage( text( "Shutdown " + shutdown + " / " + computers.size() + " computers" ) );
} );
}
@Nonnull
@ -188,26 +178,15 @@ public List<String> getCompletion( @Nonnull CommandContext context, @Nonnull Lis
@Override
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
{
Set<ServerComputer> computers = Sets.newHashSet();
if( arguments.size() > 0 )
{
for( String arg : arguments )
withComputers( arguments, computers -> {
int on = 0;
for( ServerComputer computer : computers )
{
computers.addAll( ComputerSelector.getComputers( arg ) );
if( !computer.isOn() ) on++;
computer.turnOn();
}
}
else
{
computers.addAll( ComputerCraft.serverComputerRegistry.getComputers() );
}
int on = 0;
for( ServerComputer computer : computers )
{
if( !computer.isOn() ) on++;
computer.turnOn();
}
context.getSender().sendMessage( text( "Turned on " + on + " / " + computers.size() + " computers" ) );
context.getSender().sendMessage( text( "Turned on " + on + " / " + computers.size() + " computers" ) );
} );
}
@Nonnull
@ -489,8 +468,6 @@ private static void displayTimings( CommandContext context, List<ComputerTracker
if( server.getID() > maxId ) maxId = server.getID();
}
ICommandSender sender = context.getSender();
timings.sort( Comparator.<ComputerTracker, Long>comparing( x -> x.get( field ) ).reversed() );
boolean defaultLayout = field == TrackingField.TASKS || field == TrackingField.TOTAL_TIME
@ -526,4 +503,30 @@ private static void displayTimings( CommandContext context, List<ComputerTracker
table.displayTo( context.getSender() );
}
private static void withComputers( List<String> selectors, Consumer<Collection<ServerComputer>> action ) throws CommandException
{
Set<ServerComputer> computers = Sets.newHashSet();
List<String> failed = new ArrayList<>();
if( selectors.isEmpty() )
{
computers.addAll( ComputerCraft.serverComputerRegistry.getComputers() );
}
else
{
for( String selector : selectors )
{
List<ServerComputer> selected = ComputerSelector.getComputers( selector );
computers.addAll( selected );
if( selected.isEmpty() ) failed.add( selector );
}
}
action.accept( computers );
if( !failed.isEmpty() )
{
throw new CommandException( "Could not find computers matching " + String.join( ", ", failed ) );
}
}
}

View File

@ -12,30 +12,22 @@
public final class ComputerSelector
{
private static List<ServerComputer> getComputers( Predicate<ServerComputer> predicate, String selector ) throws CommandException
private static List<ServerComputer> getComputers( Predicate<ServerComputer> predicate ) throws CommandException
{
// We copy it to prevent concurrent modifications.
List<ServerComputer> computers = Lists.newArrayList( ComputerCraft.serverComputerRegistry.getComputers() );
List<ServerComputer> candidates = Lists.newArrayList();
for( ServerComputer searchComputer : computers )
{
if( predicate.test( searchComputer ) ) candidates.add( searchComputer );
}
if( candidates.isEmpty() )
{
throw new CommandException( "No computer matching " + selector );
}
else
{
return candidates;
}
ArrayList<ServerComputer> computers = new ArrayList<>( ComputerCraft.serverComputerRegistry.getComputers() );
computers.removeIf( predicate.negate() );
return computers;
}
public static ServerComputer getComputer( String selector ) throws CommandException
{
List<ServerComputer> computers = getComputers( selector );
if( computers.size() == 1 )
if( computers.size() == 0 )
{
throw new CommandException( "No computer matching " + selector );
}
else if( computers.size() == 1 )
{
return computers.get( 0 );
}
@ -71,17 +63,17 @@ public static List<ServerComputer> getComputers( String selector ) throws Comman
throw new CommandException( "'" + selector + "' is not a valid number" );
}
return getComputers( x -> x.getID() == id, selector );
return getComputers( x -> x.getID() == id );
}
else if( selector.length() > 0 && selector.charAt( 0 ) == '@' )
{
String label = selector.substring( 1 );
return getComputers( x -> Objects.equals( label, x.getLabel() ), selector );
return getComputers( x -> Objects.equals( label, x.getLabel() ) );
}
else if( selector.length() > 0 && selector.charAt( 0 ) == '~' )
{
String familyName = selector.substring( 1 );
return getComputers( x -> x.getFamily().name().equalsIgnoreCase( familyName ), selector );
return getComputers( x -> x.getFamily().name().equalsIgnoreCase( familyName ) );
}
else
{
@ -96,14 +88,7 @@ else if( selector.length() > 0 && selector.charAt( 0 ) == '~' )
}
ServerComputer computer = ComputerCraft.serverComputerRegistry.get( instance );
if( computer == null )
{
throw new CommandException( "No such computer for instance id " + instance );
}
else
{
return Collections.singletonList( computer );
}
return computer == null ? Collections.emptyList() : Collections.singletonList( computer );
}
}