mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-07-11 22:42:52 +00:00
Prevent computer dump command sending too much information
This commit is contained in:
parent
01d9919a3e
commit
3298efe652
@ -10,11 +10,13 @@ import net.minecraft.command.CommandException;
|
|||||||
import net.minecraft.command.ICommandSender;
|
import net.minecraft.command.ICommandSender;
|
||||||
import net.minecraft.entity.Entity;
|
import net.minecraft.entity.Entity;
|
||||||
import net.minecraft.entity.player.EntityPlayerMP;
|
import net.minecraft.entity.player.EntityPlayerMP;
|
||||||
|
import net.minecraft.server.MinecraftServer;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.util.text.ITextComponent;
|
import net.minecraft.util.text.ITextComponent;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -48,8 +50,35 @@ public final class CommandComputerCraft extends CommandDelegate
|
|||||||
{
|
{
|
||||||
TextTable table = new TextTable( "Instance", "Id", "On", "Position" );
|
TextTable table = new TextTable( "Instance", "Id", "On", "Position" );
|
||||||
|
|
||||||
int max = 50;
|
List<ServerComputer> computers = new ArrayList<>( ComputerCraft.serverComputerRegistry.getComputers() );
|
||||||
for( ServerComputer computer : 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(
|
table.addRow(
|
||||||
linkComputer( computer ),
|
linkComputer( computer ),
|
||||||
@ -57,8 +86,6 @@ public final class CommandComputerCraft extends CommandDelegate
|
|||||||
bool( computer.isOn() ),
|
bool( computer.isOn() ),
|
||||||
linkPosition( context, computer )
|
linkPosition( context, computer )
|
||||||
);
|
);
|
||||||
|
|
||||||
if( max-- < 0 ) break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
table.displayTo( context.getSender() );
|
table.displayTo( context.getSender() );
|
||||||
@ -202,13 +229,16 @@ public final class CommandComputerCraft extends CommandDelegate
|
|||||||
"view", "<id>", "View the terminal of a computer.", UserLevel.OP,
|
"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 " +
|
"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)."
|
"turtle's inventories. You can either specify the computer's instance id (e.g. 123) or computer id (e.g #123)."
|
||||||
) {
|
)
|
||||||
|
{
|
||||||
@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
|
||||||
|
{
|
||||||
if( arguments.size() != 1 ) throw new CommandException( context.getFullUsage() );
|
if( arguments.size() != 1 ) throw new CommandException( context.getFullUsage() );
|
||||||
|
|
||||||
ICommandSender sender = context.getSender();
|
ICommandSender sender = context.getSender();
|
||||||
if (!(sender instanceof EntityPlayerMP)) {
|
if( !(sender instanceof EntityPlayerMP) )
|
||||||
|
{
|
||||||
throw new CommandException( "Cannot open terminal for non-player" );
|
throw new CommandException( "Cannot open terminal for non-player" );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -218,7 +248,8 @@ public final class CommandComputerCraft extends CommandDelegate
|
|||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@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
|
return arguments.size() == 1
|
||||||
? ComputerSelector.completeComputer( arguments.get( 0 ) )
|
? ComputerSelector.completeComputer( arguments.get( 0 ) )
|
||||||
: Collections.emptyList();
|
: Collections.emptyList();
|
||||||
|
@ -3,44 +3,28 @@ package dan200.computercraft.shared.command;
|
|||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
import dan200.computercraft.ComputerCraft;
|
import dan200.computercraft.ComputerCraft;
|
||||||
|
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||||
import net.minecraft.command.CommandException;
|
import net.minecraft.command.CommandException;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.*;
|
||||||
import java.util.Set;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
public final class ComputerSelector
|
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.
|
// We copy it to prevent concurrent modifications.
|
||||||
List<ServerComputer> computers = Lists.newArrayList( ComputerCraft.serverComputerRegistry.getComputers() );
|
List<ServerComputer> computers = Lists.newArrayList( ComputerCraft.serverComputerRegistry.getComputers() );
|
||||||
List<ServerComputer> candidates = Lists.newArrayList();
|
List<ServerComputer> candidates = Lists.newArrayList();
|
||||||
for( ServerComputer searchComputer : computers )
|
for( ServerComputer searchComputer : computers )
|
||||||
{
|
{
|
||||||
if( searchComputer.getID() == id )
|
if( predicate.test( searchComputer ) ) candidates.add( searchComputer );
|
||||||
{
|
|
||||||
candidates.add( searchComputer );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if( candidates.size() == 0 )
|
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 )
|
else if( candidates.size() == 1 )
|
||||||
{
|
{
|
||||||
@ -48,8 +32,8 @@ public final class ComputerSelector
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
StringBuilder builder = new StringBuilder( "Multiple computers with id " )
|
StringBuilder builder = new StringBuilder( "Multiple computers with " )
|
||||||
.append( id ).append( " (instances " );
|
.append( kind ).append( " (instances " );
|
||||||
|
|
||||||
boolean first = true;
|
boolean first = true;
|
||||||
for( ServerComputer computer : candidates )
|
for( ServerComputer computer : candidates )
|
||||||
@ -71,6 +55,35 @@ public final class ComputerSelector
|
|||||||
throw new CommandException( builder.toString() );
|
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
|
else
|
||||||
{
|
{
|
||||||
int instance;
|
int instance;
|
||||||
@ -97,7 +110,7 @@ public final class ComputerSelector
|
|||||||
|
|
||||||
public static List<String> completeComputer( String selector )
|
public static List<String> completeComputer( String selector )
|
||||||
{
|
{
|
||||||
Set<String> options = Sets.newHashSet();
|
TreeSet<String> options = Sets.newTreeSet();
|
||||||
|
|
||||||
// We copy it to prevent concurrent modifications.
|
// We copy it to prevent concurrent modifications.
|
||||||
List<ServerComputer> computers = Lists.newArrayList( ComputerCraft.serverComputerRegistry.getComputers() );
|
List<ServerComputer> computers = Lists.newArrayList( ComputerCraft.serverComputerRegistry.getComputers() );
|
||||||
@ -112,6 +125,26 @@ public final class ComputerSelector
|
|||||||
if( id.startsWith( selector ) ) options.add( "#" + id );
|
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
|
else
|
||||||
{
|
{
|
||||||
for( ServerComputer computer : computers )
|
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 );
|
return Lists.newArrayList( options );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -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++ )
|
for( int i = 0; i < row.length; i++ )
|
||||||
{
|
{
|
||||||
int width = getWidth( row[i], sender );
|
int width = getWidth( row[i], sender );
|
||||||
@ -190,7 +195,7 @@ public class TextTable
|
|||||||
out.appendSibling( LINE );
|
out.appendSibling( LINE );
|
||||||
}
|
}
|
||||||
|
|
||||||
for( int i = 0; i < rows.size(); i++ )
|
for( int i = 0; i < limit; i++ )
|
||||||
{
|
{
|
||||||
ITextComponent[] row = rows.get( i );
|
ITextComponent[] row = rows.get( i );
|
||||||
if( i != 0 ) out.appendSibling( LINE );
|
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 );
|
sender.sendMessage( out );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user