mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-04-13 14:23:17 +00:00
Improve using /computercraft as a non-player
- Fix text table only showing the first row (fixes #40) - Do not emit alignment characters in monospace environments - Reduce padding in monospace environments Also make the output of dump consistent with that of the profiler: we provide tp and view shortcuts for each computer.
This commit is contained in:
parent
a2083bcff1
commit
81aaead032
@ -20,7 +20,6 @@ import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.TextComponentString;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.util.FakePlayer;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.*;
|
||||
@ -59,7 +58,7 @@ public final class CommandComputerCraft extends CommandDelegate
|
||||
{
|
||||
if( arguments.size() == 0 )
|
||||
{
|
||||
TextTable table = new TextTable( DUMP_LIST_ID, "Instance", "Id", "On", "Position" );
|
||||
TextTable table = new TextTable( DUMP_LIST_ID, "Computer", "On", "Position" );
|
||||
|
||||
List<ServerComputer> computers = new ArrayList<>( ComputerCraft.serverComputerRegistry.getComputers() );
|
||||
|
||||
@ -92,8 +91,7 @@ public final class CommandComputerCraft extends CommandDelegate
|
||||
for( ServerComputer computer : computers )
|
||||
{
|
||||
table.addRow(
|
||||
linkComputer( computer ),
|
||||
text( Integer.toString( computer.getID() ) ),
|
||||
linkComputer( context, computer, computer.getID() ),
|
||||
bool( computer.isOn() ),
|
||||
linkPosition( context, computer )
|
||||
);
|
||||
@ -406,13 +404,46 @@ public final class CommandComputerCraft extends CommandDelegate
|
||||
return root;
|
||||
}
|
||||
|
||||
private static ITextComponent linkComputer( ServerComputer computer )
|
||||
private static ITextComponent linkComputer( CommandContext context, ServerComputer serverComputer, int computerId )
|
||||
{
|
||||
return link(
|
||||
text( Integer.toString( computer.getInstanceID() ) ),
|
||||
"/computercraft dump " + computer.getInstanceID(),
|
||||
"View more info about this computer"
|
||||
);
|
||||
ITextComponent out = new TextComponentString( "" );
|
||||
|
||||
// Append the computer instance
|
||||
if( serverComputer == null )
|
||||
{
|
||||
out.appendSibling( text( "?" ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
out.appendSibling( link(
|
||||
text( Integer.toString( serverComputer.getInstanceID() ) ),
|
||||
"/computercraft dump " + serverComputer.getInstanceID(),
|
||||
"View more info about this computer"
|
||||
) );
|
||||
}
|
||||
|
||||
// And ID
|
||||
out.appendText( " (id " + computerId + ")" );
|
||||
|
||||
// And, if we're a player, some useful links
|
||||
if( serverComputer != null && UserLevel.OP.canExecute( context ) && context.fromPlayer() )
|
||||
{
|
||||
out
|
||||
.appendText( " " )
|
||||
.appendSibling( link(
|
||||
text( "\u261b" ),
|
||||
"/computercraft tp " + serverComputer.getInstanceID(),
|
||||
"Teleport to this computer"
|
||||
) )
|
||||
.appendText( " " )
|
||||
.appendSibling( link(
|
||||
text( "\u20e2" ),
|
||||
"/computercraft view " + serverComputer.getInstanceID(),
|
||||
"View this computer"
|
||||
) );
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
private static ITextComponent linkPosition( CommandContext context, ServerComputer computer )
|
||||
@ -459,7 +490,6 @@ public final class CommandComputerCraft extends CommandDelegate
|
||||
}
|
||||
|
||||
ICommandSender sender = context.getSender();
|
||||
boolean isPlayer = sender instanceof EntityPlayerMP && !(sender instanceof FakePlayer);
|
||||
|
||||
timings.sort( Comparator.<ComputerTracker, Long>comparing( x -> x.get( field ) ).reversed() );
|
||||
|
||||
@ -476,26 +506,7 @@ public final class CommandComputerCraft extends CommandDelegate
|
||||
Computer computer = entry.getComputer();
|
||||
ServerComputer serverComputer = computer == null ? null : lookup.get( computer );
|
||||
|
||||
ITextComponent computerComponent = new TextComponentString( "" )
|
||||
.appendSibling( serverComputer == null ? text( "?" ) : linkComputer( serverComputer ) )
|
||||
.appendText( " (id " + entry.getComputerId() + ")" );
|
||||
|
||||
if( serverComputer != null && UserLevel.OP.canExecute( context ) && isPlayer )
|
||||
{
|
||||
computerComponent
|
||||
.appendText( " " )
|
||||
.appendSibling( link(
|
||||
text( "\u261b" ),
|
||||
"/computercraft tp " + serverComputer.getInstanceID(),
|
||||
"Teleport to this computer"
|
||||
) )
|
||||
.appendText( " " )
|
||||
.appendSibling( link(
|
||||
text( "\u20e2" ),
|
||||
"/computercraft view " + serverComputer.getInstanceID(),
|
||||
"View this computer"
|
||||
) );
|
||||
}
|
||||
ITextComponent computerComponent = linkComputer( context, serverComputer, entry.getComputerId() );
|
||||
|
||||
if( defaultLayout )
|
||||
{
|
||||
|
@ -46,7 +46,7 @@ public final class ComputerSelector
|
||||
|
||||
for( int i = 0; i < computers.size(); i++ )
|
||||
{
|
||||
if( i > 1 ) builder.append( ", " );
|
||||
if( i > 0 ) builder.append( ", " );
|
||||
builder.append( computers.get( i ).getInstanceID() );
|
||||
}
|
||||
builder.append( ")" );
|
||||
|
@ -2,7 +2,9 @@ package dan200.computercraft.shared.command.framework;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import net.minecraft.command.ICommandSender;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraftforge.common.util.FakePlayer;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@ -90,4 +92,9 @@ public final class CommandContext
|
||||
{
|
||||
return sender;
|
||||
}
|
||||
|
||||
public boolean fromPlayer()
|
||||
{
|
||||
return sender instanceof EntityPlayerMP && !(sender instanceof FakePlayer);
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,6 @@ import static dan200.computercraft.shared.command.framework.ChatHelpers.coloured
|
||||
*/
|
||||
public class TextFormatter
|
||||
{
|
||||
private static final int SPACE_WIDTH = 4;
|
||||
private static final char PADDING_CHAR = '\u02cc';
|
||||
|
||||
/**
|
||||
@ -54,7 +53,7 @@ public class TextFormatter
|
||||
8, 4,
|
||||
};
|
||||
|
||||
private static int getWidth( int codePoint )
|
||||
public static int getWidth( int codePoint )
|
||||
{
|
||||
// Escape codes
|
||||
if( codePoint == 167 ) return -1;
|
||||
@ -73,7 +72,7 @@ public class TextFormatter
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static int getWidth( ITextComponent component )
|
||||
public static int getWidth( ITextComponent component )
|
||||
{
|
||||
int total = 0;
|
||||
if( component instanceof TextComponentString )
|
||||
@ -132,19 +131,17 @@ public class TextFormatter
|
||||
int width = getWidthFor( entry, sender );
|
||||
int delta = maxWidth - width;
|
||||
|
||||
if( delta > 0 )
|
||||
int spaceWidth = getWidthFor( ' ', sender );
|
||||
int spaces = delta / spaceWidth;
|
||||
int extra = delta % spaces;
|
||||
|
||||
// Append a fixed number of spaces
|
||||
if( spaces > 0 ) out.appendSibling( new TextComponentString( StringUtils.repeat( ' ', spaces ) ) );
|
||||
|
||||
// Append several minor characters to pad to a full string
|
||||
if( extra > 0 )
|
||||
{
|
||||
int spaces = delta / SPACE_WIDTH;
|
||||
int extra = delta % SPACE_WIDTH;
|
||||
|
||||
// Append a fixed number of spaces
|
||||
if( spaces > 0 ) out.appendSibling( new TextComponentString( StringUtils.repeat( ' ', spaces ) ) );
|
||||
|
||||
// Append several minor characters to pad to a full string
|
||||
if( extra > 0 )
|
||||
{
|
||||
out.appendSibling( coloured( StringUtils.repeat( PADDING_CHAR, extra ), TextFormatting.GRAY ) );
|
||||
}
|
||||
out.appendSibling( coloured( StringUtils.repeat( PADDING_CHAR, extra ), TextFormatting.GRAY ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ public class TextTable
|
||||
this.columns = header.length;
|
||||
}
|
||||
|
||||
public TextTable(int id)
|
||||
public TextTable( int id )
|
||||
{
|
||||
this.id = id;
|
||||
this.header = null;
|
||||
@ -90,14 +90,15 @@ public class TextTable
|
||||
ITextComponent[] row = rows.get( y );
|
||||
for( int i = 0; i < row.length; i++ )
|
||||
{
|
||||
int width = getWidthFor( row[i], sender ) + 3;
|
||||
int width = getWidthFor( row[i], sender );
|
||||
if( width > maxWidths[i] ) maxWidths[i] = width;
|
||||
}
|
||||
}
|
||||
|
||||
// Add a small amount of extra padding. We include this here instead of the separator to allow
|
||||
// for "extra" characters
|
||||
for( int i = 0; i < maxWidths.length; i++ ) maxWidths[i] += 4;
|
||||
// Add a small amount of extra padding. This defaults to 3 spaces for players
|
||||
// and 1 for everyone else.
|
||||
int padding = isPlayer( sender ) ? getWidth( ' ' ) * 3 : 1;
|
||||
for( int i = 0; i < maxWidths.length; i++ ) maxWidths[i] += padding;
|
||||
|
||||
int totalWidth = (columns - 1) * getWidthFor( SEPARATOR, sender );
|
||||
for( int x : maxWidths ) totalWidth += x;
|
||||
@ -161,7 +162,7 @@ public class TextTable
|
||||
for( int i = 0; i < out.size(); i++ )
|
||||
{
|
||||
if( i > 0 ) result.appendSibling( LINE );
|
||||
result.appendSibling( out.get( 0 ) );
|
||||
result.appendSibling( out.get( i ) );
|
||||
}
|
||||
sender.sendMessage( result );
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user