mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-06-26 15:13:01 +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.ITextComponent;
|
||||||
import net.minecraft.util.text.TextComponentString;
|
import net.minecraft.util.text.TextComponentString;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import net.minecraftforge.common.util.FakePlayer;
|
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@ -59,7 +58,7 @@ public final class CommandComputerCraft extends CommandDelegate
|
|||||||
{
|
{
|
||||||
if( arguments.size() == 0 )
|
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() );
|
List<ServerComputer> computers = new ArrayList<>( ComputerCraft.serverComputerRegistry.getComputers() );
|
||||||
|
|
||||||
@ -92,8 +91,7 @@ public final class CommandComputerCraft extends CommandDelegate
|
|||||||
for( ServerComputer computer : computers )
|
for( ServerComputer computer : computers )
|
||||||
{
|
{
|
||||||
table.addRow(
|
table.addRow(
|
||||||
linkComputer( computer ),
|
linkComputer( context, computer, computer.getID() ),
|
||||||
text( Integer.toString( computer.getID() ) ),
|
|
||||||
bool( computer.isOn() ),
|
bool( computer.isOn() ),
|
||||||
linkPosition( context, computer )
|
linkPosition( context, computer )
|
||||||
);
|
);
|
||||||
@ -406,13 +404,46 @@ public final class CommandComputerCraft extends CommandDelegate
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ITextComponent linkComputer( ServerComputer computer )
|
private static ITextComponent linkComputer( CommandContext context, ServerComputer serverComputer, int computerId )
|
||||||
{
|
{
|
||||||
return link(
|
ITextComponent out = new TextComponentString( "" );
|
||||||
text( Integer.toString( computer.getInstanceID() ) ),
|
|
||||||
"/computercraft dump " + computer.getInstanceID(),
|
// 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"
|
"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 )
|
private static ITextComponent linkPosition( CommandContext context, ServerComputer computer )
|
||||||
@ -459,7 +490,6 @@ public final class CommandComputerCraft extends CommandDelegate
|
|||||||
}
|
}
|
||||||
|
|
||||||
ICommandSender sender = context.getSender();
|
ICommandSender sender = context.getSender();
|
||||||
boolean isPlayer = sender instanceof EntityPlayerMP && !(sender instanceof FakePlayer);
|
|
||||||
|
|
||||||
timings.sort( Comparator.<ComputerTracker, Long>comparing( x -> x.get( field ) ).reversed() );
|
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();
|
Computer computer = entry.getComputer();
|
||||||
ServerComputer serverComputer = computer == null ? null : lookup.get( computer );
|
ServerComputer serverComputer = computer == null ? null : lookup.get( computer );
|
||||||
|
|
||||||
ITextComponent computerComponent = new TextComponentString( "" )
|
ITextComponent computerComponent = linkComputer( context, serverComputer, entry.getComputerId() );
|
||||||
.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"
|
|
||||||
) );
|
|
||||||
}
|
|
||||||
|
|
||||||
if( defaultLayout )
|
if( defaultLayout )
|
||||||
{
|
{
|
||||||
|
@ -46,7 +46,7 @@ public final class ComputerSelector
|
|||||||
|
|
||||||
for( int i = 0; i < computers.size(); i++ )
|
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( computers.get( i ).getInstanceID() );
|
||||||
}
|
}
|
||||||
builder.append( ")" );
|
builder.append( ")" );
|
||||||
|
@ -2,7 +2,9 @@ package dan200.computercraft.shared.command.framework;
|
|||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import net.minecraft.command.ICommandSender;
|
import net.minecraft.command.ICommandSender;
|
||||||
|
import net.minecraft.entity.player.EntityPlayerMP;
|
||||||
import net.minecraft.server.MinecraftServer;
|
import net.minecraft.server.MinecraftServer;
|
||||||
|
import net.minecraftforge.common.util.FakePlayer;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -90,4 +92,9 @@ public final class CommandContext
|
|||||||
{
|
{
|
||||||
return sender;
|
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
|
public class TextFormatter
|
||||||
{
|
{
|
||||||
private static final int SPACE_WIDTH = 4;
|
|
||||||
private static final char PADDING_CHAR = '\u02cc';
|
private static final char PADDING_CHAR = '\u02cc';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -54,7 +53,7 @@ public class TextFormatter
|
|||||||
8, 4,
|
8, 4,
|
||||||
};
|
};
|
||||||
|
|
||||||
private static int getWidth( int codePoint )
|
public static int getWidth( int codePoint )
|
||||||
{
|
{
|
||||||
// Escape codes
|
// Escape codes
|
||||||
if( codePoint == 167 ) return -1;
|
if( codePoint == 167 ) return -1;
|
||||||
@ -73,7 +72,7 @@ public class TextFormatter
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int getWidth( ITextComponent component )
|
public static int getWidth( ITextComponent component )
|
||||||
{
|
{
|
||||||
int total = 0;
|
int total = 0;
|
||||||
if( component instanceof TextComponentString )
|
if( component instanceof TextComponentString )
|
||||||
@ -132,10 +131,9 @@ public class TextFormatter
|
|||||||
int width = getWidthFor( entry, sender );
|
int width = getWidthFor( entry, sender );
|
||||||
int delta = maxWidth - width;
|
int delta = maxWidth - width;
|
||||||
|
|
||||||
if( delta > 0 )
|
int spaceWidth = getWidthFor( ' ', sender );
|
||||||
{
|
int spaces = delta / spaceWidth;
|
||||||
int spaces = delta / SPACE_WIDTH;
|
int extra = delta % spaces;
|
||||||
int extra = delta % SPACE_WIDTH;
|
|
||||||
|
|
||||||
// Append a fixed number of spaces
|
// Append a fixed number of spaces
|
||||||
if( spaces > 0 ) out.appendSibling( new TextComponentString( StringUtils.repeat( ' ', spaces ) ) );
|
if( spaces > 0 ) out.appendSibling( new TextComponentString( StringUtils.repeat( ' ', spaces ) ) );
|
||||||
@ -146,5 +144,4 @@ public class TextFormatter
|
|||||||
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;
|
this.columns = header.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TextTable(int id)
|
public TextTable( int id )
|
||||||
{
|
{
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.header = null;
|
this.header = null;
|
||||||
@ -90,14 +90,15 @@ public class TextTable
|
|||||||
ITextComponent[] row = rows.get( y );
|
ITextComponent[] row = rows.get( y );
|
||||||
for( int i = 0; i < row.length; i++ )
|
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;
|
if( width > maxWidths[i] ) maxWidths[i] = width;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a small amount of extra padding. We include this here instead of the separator to allow
|
// Add a small amount of extra padding. This defaults to 3 spaces for players
|
||||||
// for "extra" characters
|
// and 1 for everyone else.
|
||||||
for( int i = 0; i < maxWidths.length; i++ ) maxWidths[i] += 4;
|
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 );
|
int totalWidth = (columns - 1) * getWidthFor( SEPARATOR, sender );
|
||||||
for( int x : maxWidths ) totalWidth += x;
|
for( int x : maxWidths ) totalWidth += x;
|
||||||
@ -161,7 +162,7 @@ public class TextTable
|
|||||||
for( int i = 0; i < out.size(); i++ )
|
for( int i = 0; i < out.size(); i++ )
|
||||||
{
|
{
|
||||||
if( i > 0 ) result.appendSibling( LINE );
|
if( i > 0 ) result.appendSibling( LINE );
|
||||||
result.appendSibling( out.get( 0 ) );
|
result.appendSibling( out.get( i ) );
|
||||||
}
|
}
|
||||||
sender.sendMessage( result );
|
sender.sendMessage( result );
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user