mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-27 09:24:47 +00:00
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:
parent
a3d1cff298
commit
f40733e9a6
@ -23,6 +23,7 @@ import net.minecraft.world.World;
|
|||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import static dan200.computercraft.shared.command.framework.ChatHelpers.*;
|
import static dan200.computercraft.shared.command.framework.ChatHelpers.*;
|
||||||
|
|
||||||
@ -147,19 +148,7 @@ public final class CommandComputerCraft extends CommandDelegate
|
|||||||
@Override
|
@Override
|
||||||
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
||||||
{
|
{
|
||||||
Set<ServerComputer> computers = Sets.newHashSet();
|
withComputers( arguments, computers -> {
|
||||||
if( arguments.size() > 0 )
|
|
||||||
{
|
|
||||||
for( String arg : arguments )
|
|
||||||
{
|
|
||||||
computers.addAll( ComputerSelector.getComputers( arg ) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
computers.addAll( ComputerCraft.serverComputerRegistry.getComputers() );
|
|
||||||
}
|
|
||||||
|
|
||||||
int shutdown = 0;
|
int shutdown = 0;
|
||||||
for( ServerComputer computer : computers )
|
for( ServerComputer computer : computers )
|
||||||
{
|
{
|
||||||
@ -167,6 +156,7 @@ public final class CommandComputerCraft extends CommandDelegate
|
|||||||
computer.unload();
|
computer.unload();
|
||||||
}
|
}
|
||||||
context.getSender().sendMessage( text( "Shutdown " + shutdown + " / " + computers.size() + " computers" ) );
|
context.getSender().sendMessage( text( "Shutdown " + shutdown + " / " + computers.size() + " computers" ) );
|
||||||
|
} );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@ -188,19 +178,7 @@ public final class CommandComputerCraft extends CommandDelegate
|
|||||||
@Override
|
@Override
|
||||||
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
||||||
{
|
{
|
||||||
Set<ServerComputer> computers = Sets.newHashSet();
|
withComputers( arguments, computers -> {
|
||||||
if( arguments.size() > 0 )
|
|
||||||
{
|
|
||||||
for( String arg : arguments )
|
|
||||||
{
|
|
||||||
computers.addAll( ComputerSelector.getComputers( arg ) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
computers.addAll( ComputerCraft.serverComputerRegistry.getComputers() );
|
|
||||||
}
|
|
||||||
|
|
||||||
int on = 0;
|
int on = 0;
|
||||||
for( ServerComputer computer : computers )
|
for( ServerComputer computer : computers )
|
||||||
{
|
{
|
||||||
@ -208,6 +186,7 @@ public final class CommandComputerCraft extends CommandDelegate
|
|||||||
computer.turnOn();
|
computer.turnOn();
|
||||||
}
|
}
|
||||||
context.getSender().sendMessage( text( "Turned on " + on + " / " + computers.size() + " computers" ) );
|
context.getSender().sendMessage( text( "Turned on " + on + " / " + computers.size() + " computers" ) );
|
||||||
|
} );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@ -489,8 +468,6 @@ public final class CommandComputerCraft extends CommandDelegate
|
|||||||
if( server.getID() > maxId ) maxId = server.getID();
|
if( server.getID() > maxId ) maxId = server.getID();
|
||||||
}
|
}
|
||||||
|
|
||||||
ICommandSender sender = context.getSender();
|
|
||||||
|
|
||||||
timings.sort( Comparator.<ComputerTracker, Long>comparing( x -> x.get( field ) ).reversed() );
|
timings.sort( Comparator.<ComputerTracker, Long>comparing( x -> x.get( field ) ).reversed() );
|
||||||
|
|
||||||
boolean defaultLayout = field == TrackingField.TASKS || field == TrackingField.TOTAL_TIME
|
boolean defaultLayout = field == TrackingField.TASKS || field == TrackingField.TOTAL_TIME
|
||||||
@ -526,4 +503,30 @@ public final class CommandComputerCraft extends CommandDelegate
|
|||||||
|
|
||||||
table.displayTo( context.getSender() );
|
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 ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,30 +12,22 @@ import java.util.function.Predicate;
|
|||||||
|
|
||||||
public final class ComputerSelector
|
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.
|
// We copy it to prevent concurrent modifications.
|
||||||
List<ServerComputer> computers = Lists.newArrayList( ComputerCraft.serverComputerRegistry.getComputers() );
|
ArrayList<ServerComputer> computers = new ArrayList<>( ComputerCraft.serverComputerRegistry.getComputers() );
|
||||||
List<ServerComputer> candidates = Lists.newArrayList();
|
computers.removeIf( predicate.negate() );
|
||||||
for( ServerComputer searchComputer : computers )
|
return computers;
|
||||||
{
|
|
||||||
if( predicate.test( searchComputer ) ) candidates.add( searchComputer );
|
|
||||||
}
|
|
||||||
|
|
||||||
if( candidates.isEmpty() )
|
|
||||||
{
|
|
||||||
throw new CommandException( "No computer matching " + selector );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return candidates;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ServerComputer getComputer( String selector ) throws CommandException
|
public static ServerComputer getComputer( String selector ) throws CommandException
|
||||||
{
|
{
|
||||||
List<ServerComputer> computers = getComputers( selector );
|
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 );
|
return computers.get( 0 );
|
||||||
}
|
}
|
||||||
@ -71,17 +63,17 @@ public final class ComputerSelector
|
|||||||
throw new CommandException( "'" + selector + "' is not a valid number" );
|
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 ) == '@' )
|
else if( selector.length() > 0 && selector.charAt( 0 ) == '@' )
|
||||||
{
|
{
|
||||||
String label = selector.substring( 1 );
|
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 ) == '~' )
|
else if( selector.length() > 0 && selector.charAt( 0 ) == '~' )
|
||||||
{
|
{
|
||||||
String familyName = selector.substring( 1 );
|
String familyName = selector.substring( 1 );
|
||||||
return getComputers( x -> x.getFamily().name().equalsIgnoreCase( familyName ), selector );
|
return getComputers( x -> x.getFamily().name().equalsIgnoreCase( familyName ) );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -96,14 +88,7 @@ public final class ComputerSelector
|
|||||||
}
|
}
|
||||||
|
|
||||||
ServerComputer computer = ComputerCraft.serverComputerRegistry.get( instance );
|
ServerComputer computer = ComputerCraft.serverComputerRegistry.get( instance );
|
||||||
if( computer == null )
|
return computer == null ? Collections.emptyList() : Collections.singletonList( computer );
|
||||||
{
|
|
||||||
throw new CommandException( "No such computer for instance id " + instance );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return Collections.singletonList( computer );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user