1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-07-07 20:42:53 +00:00

Prevent computer dump command sending too much information

This commit is contained in:
SquidDev 2018-01-20 11:07:09 +00:00
parent 01d9919a3e
commit 3298efe652
3 changed files with 167 additions and 78 deletions

View File

@ -10,11 +10,13 @@ import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.world.World;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -48,8 +50,35 @@ public final class CommandComputerCraft extends CommandDelegate
{
TextTable table = new TextTable( "Instance", "Id", "On", "Position" );
int max = 50;
for( ServerComputer computer : ComputerCraft.serverComputerRegistry.getComputers() )
List<ServerComputer> computers = new ArrayList<>( ComputerCraft.serverComputerRegistry.getComputers() );
// Unless we're on a server, limit the number of rows we can send.
if( !(context.getSender() instanceof MinecraftServer) )
{
World world = context.getSender().getEntityWorld();
BlockPos pos = context.getSender().getPosition();
computers.sort( ( a, b ) -> {
if( a.getWorld() == b.getWorld() && a.getWorld() == world )
{
return Double.compare( a.getPosition().distanceSq( pos ), b.getPosition().distanceSq( pos ) );
}
else if( a.getWorld() == world )
{
return -1;
}
else if( b.getWorld() == world )
{
return 1;
}
else
{
return Integer.compare( a.getInstanceID(), b.getInstanceID() );
}
} );
}
for( ServerComputer computer : computers )
{
table.addRow(
linkComputer( computer ),
@ -57,8 +86,6 @@ public final class CommandComputerCraft extends CommandDelegate
bool( computer.isOn() ),
linkPosition( context, computer )
);
if( max-- < 0 ) break;
}
table.displayTo( context.getSender() );
@ -202,13 +229,16 @@ public final class CommandComputerCraft extends CommandDelegate
"view", "<id>", "View the terminal of a computer.", UserLevel.OP,
"Open the terminal of a computer, allowing remote control of a computer. This does not provide access to " +
"turtle's inventories. You can either specify the computer's instance id (e.g. 123) or computer id (e.g #123)."
) {
)
{
@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
{
if( arguments.size() != 1 ) throw new CommandException( context.getFullUsage() );
ICommandSender sender = context.getSender();
if (!(sender instanceof EntityPlayerMP)) {
if( !(sender instanceof EntityPlayerMP) )
{
throw new CommandException( "Cannot open terminal for non-player" );
}
@ -218,7 +248,8 @@ public final class CommandComputerCraft extends CommandDelegate
@Nonnull
@Override
public List<String> getCompletion(@Nonnull CommandContext context, @Nonnull List<String> arguments) {
public List<String> getCompletion( @Nonnull CommandContext context, @Nonnull List<String> arguments )
{
return arguments.size() == 1
? ComputerSelector.completeComputer( arguments.get( 0 ) )
: Collections.emptyList();

View File

@ -3,44 +3,28 @@ package dan200.computercraft.shared.command;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.core.ServerComputer;
import net.minecraft.command.CommandException;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.function.Predicate;
public final class ComputerSelector
{
public static ServerComputer getComputer( String selector ) throws CommandException
private static ServerComputer getComputer( Predicate<ServerComputer> predicate, String kind ) throws CommandException
{
if( selector.length() > 0 && selector.charAt( 0 ) == '#' )
{
selector = selector.substring( 1 );
int id;
try
{
id = Integer.parseInt( selector );
}
catch( NumberFormatException e )
{
throw new CommandException( "'" + selector + "' is not a valid number" );
}
// 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( searchComputer.getID() == id )
{
candidates.add( searchComputer );
}
if( predicate.test( searchComputer ) ) candidates.add( searchComputer );
}
if( candidates.size() == 0 )
{
throw new CommandException( "No such computer for id " + id );
throw new CommandException( "No such computer for " + kind );
}
else if( candidates.size() == 1 )
{
@ -48,8 +32,8 @@ public final class ComputerSelector
}
else
{
StringBuilder builder = new StringBuilder( "Multiple computers with id " )
.append( id ).append( " (instances " );
StringBuilder builder = new StringBuilder( "Multiple computers with " )
.append( kind ).append( " (instances " );
boolean first = true;
for( ServerComputer computer : candidates )
@ -71,6 +55,35 @@ public final class ComputerSelector
throw new CommandException( builder.toString() );
}
}
public static ServerComputer getComputer( String selector ) throws CommandException
{
if( selector.length() > 0 && selector.charAt( 0 ) == '#' )
{
selector = selector.substring( 1 );
int id;
try
{
id = Integer.parseInt( selector );
}
catch( NumberFormatException e )
{
throw new CommandException( "'" + selector + "' is not a valid number" );
}
return getComputer( x -> x.getID() == id, "id " + id );
}
else if( selector.length() > 0 && selector.charAt( 0 ) == '@' )
{
String label = selector.substring( 1 );
return getComputer( x -> Objects.equals( label, x.getLabel() ), "label '" + label + "'" );
}
else if( selector.length() > 0 && selector.charAt( 0 ) == '~' )
{
String familyName = selector.substring( 1 );
return getComputer( x -> x.getFamily().name().equalsIgnoreCase( familyName ), "family '" + familyName + "'" );
}
else
{
int instance;
@ -97,7 +110,7 @@ public final class ComputerSelector
public static List<String> completeComputer( String selector )
{
Set<String> options = Sets.newHashSet();
TreeSet<String> options = Sets.newTreeSet();
// We copy it to prevent concurrent modifications.
List<ServerComputer> computers = Lists.newArrayList( ComputerCraft.serverComputerRegistry.getComputers() );
@ -112,6 +125,26 @@ public final class ComputerSelector
if( id.startsWith( selector ) ) options.add( "#" + id );
}
}
else if( selector.length() > 0 && selector.charAt( 0 ) == '@' )
{
String label = selector.substring( 1 );
for( ServerComputer computer : computers )
{
String thisLabel = computer.getLabel();
if( thisLabel != null && thisLabel.startsWith( label ) ) options.add( "@" + thisLabel );
}
}
else if( selector.length() > 0 && selector.charAt( 0 ) == '~' )
{
String familyName = selector.substring( 1 ).toLowerCase( Locale.ENGLISH );
for( ComputerFamily family : ComputerFamily.values() )
{
if( family.name().toLowerCase( Locale.ENGLISH ).startsWith( familyName ) )
{
options.add( "~" + family.name() );
}
}
}
else
{
for( ServerComputer computer : computers )
@ -121,6 +154,20 @@ public final class ComputerSelector
}
}
if( options.size() > 100 )
{
ArrayList<String> result = Lists.newArrayListWithCapacity( 100 );
for( String element : options )
{
if( result.size() > 100 ) break;
result.add( element );
}
return result;
}
else
{
return Lists.newArrayList( options );
}
}
}

View File

@ -148,8 +148,13 @@ public class TextTable
}
}
for( ITextComponent[] row : rows )
// Limit the number of rows to something sensible.
int limit = isPlayer( sender ) ? 30 : 100;
if( limit > rows.size() ) limit = rows.size();
for( int y = 0; y < limit; y++ )
{
ITextComponent[] row = rows.get( y );
for( int i = 0; i < row.length; i++ )
{
int width = getWidth( row[i], sender );
@ -190,7 +195,7 @@ public class TextTable
out.appendSibling( LINE );
}
for( int i = 0; i < rows.size(); i++ )
for( int i = 0; i < limit; i++ )
{
ITextComponent[] row = rows.get( i );
if( i != 0 ) out.appendSibling( LINE );
@ -201,6 +206,12 @@ public class TextTable
}
}
if( limit != rows.size() )
{
out.appendSibling( LINE );
out.appendSibling( coloured( (rows.size() - limit) + " additional rows...", TextFormatting.AQUA ) );
}
sender.sendMessage( out );
}