2018-12-29 12:18:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
2019-01-01 01:10:18 +00:00
|
|
|
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
2018-12-29 12:18:05 +00:00
|
|
|
* Send enquiries to dratcliffe@gmail.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
package dan200.computercraft.shared.network.client;
|
|
|
|
|
|
|
|
import dan200.computercraft.shared.command.text.TableBuilder;
|
|
|
|
import dan200.computercraft.shared.network.NetworkMessage;
|
|
|
|
import dan200.computercraft.shared.network.NetworkMessages;
|
2018-12-30 16:14:07 +00:00
|
|
|
import dan200.computercraft.shared.util.NBTUtil;
|
2018-12-29 12:18:05 +00:00
|
|
|
import net.minecraft.network.PacketBuffer;
|
|
|
|
import net.minecraft.util.text.ITextComponent;
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
|
|
|
public class ChatTableClientMessage implements NetworkMessage
|
|
|
|
{
|
|
|
|
private TableBuilder table;
|
|
|
|
|
|
|
|
public ChatTableClientMessage( TableBuilder table )
|
|
|
|
{
|
|
|
|
if( table.getColumns() < 0 ) throw new IllegalStateException( "Cannot send an empty table" );
|
|
|
|
this.table = table;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ChatTableClientMessage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getId()
|
|
|
|
{
|
|
|
|
return NetworkMessages.CHAT_TABLE_CLIENT_MESSAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
public TableBuilder getTable()
|
|
|
|
{
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void toBytes( @Nonnull PacketBuffer buf )
|
|
|
|
{
|
|
|
|
buf.writeVarInt( table.getId() );
|
|
|
|
buf.writeVarInt( table.getColumns() );
|
|
|
|
buf.writeBoolean( table.getHeaders() != null );
|
|
|
|
if( table.getHeaders() != null )
|
|
|
|
{
|
|
|
|
for( ITextComponent header : table.getHeaders() ) buf.writeTextComponent( header );
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.writeVarInt( table.getRows().size() );
|
|
|
|
for( ITextComponent[] row : table.getRows() )
|
|
|
|
{
|
|
|
|
for( ITextComponent column : row ) buf.writeTextComponent( column );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fromBytes( @Nonnull PacketBuffer buf )
|
|
|
|
{
|
2018-12-30 16:14:07 +00:00
|
|
|
int id = buf.readVarInt();
|
|
|
|
int columns = buf.readVarInt();
|
|
|
|
TableBuilder table;
|
|
|
|
if( buf.readBoolean() )
|
2018-12-29 12:18:05 +00:00
|
|
|
{
|
2018-12-30 16:14:07 +00:00
|
|
|
ITextComponent[] headers = new ITextComponent[columns];
|
|
|
|
for( int i = 0; i < columns; i++ ) headers[i] = NBTUtil.readTextComponent( buf );
|
|
|
|
table = new TableBuilder( id, headers );
|
2018-12-29 12:18:05 +00:00
|
|
|
}
|
2018-12-30 16:14:07 +00:00
|
|
|
else
|
2018-12-29 12:18:05 +00:00
|
|
|
{
|
2018-12-30 16:14:07 +00:00
|
|
|
table = new TableBuilder( id );
|
2018-12-29 12:18:05 +00:00
|
|
|
}
|
2018-12-30 16:14:07 +00:00
|
|
|
|
|
|
|
int rows = buf.readVarInt();
|
|
|
|
for( int i = 0; i < rows; i++ )
|
|
|
|
{
|
|
|
|
ITextComponent[] row = new ITextComponent[columns];
|
|
|
|
for( int j = 0; j < columns; j++ ) row[j] = NBTUtil.readTextComponent( buf );
|
|
|
|
table.row( row );
|
|
|
|
}
|
|
|
|
this.table = table;
|
2018-12-29 12:18:05 +00:00
|
|
|
}
|
|
|
|
}
|