mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-11-03 23:22:59 +00:00 
			
		
		
		
	Remove all Minecraft references from the core package
This is an initial step before refactoring this into a separate module. It's definitely not complete - there's a lot of work needed to remove referneces to the main ComputerCraft class for instance - but is a useful first step.
This commit is contained in:
		@@ -22,7 +22,7 @@ import dan200.computercraft.api.redstone.IBundledRedstoneProvider;
 | 
			
		||||
import dan200.computercraft.core.apis.ApiFactories;
 | 
			
		||||
import dan200.computercraft.core.asm.GenericMethod;
 | 
			
		||||
import dan200.computercraft.core.filesystem.FileMount;
 | 
			
		||||
import dan200.computercraft.core.filesystem.ResourceMount;
 | 
			
		||||
import dan200.computercraft.shared.computer.core.ResourceMount;
 | 
			
		||||
import dan200.computercraft.impl.ComputerCraftAPIService;
 | 
			
		||||
import dan200.computercraft.impl.detail.DetailRegistryImpl;
 | 
			
		||||
import dan200.computercraft.shared.BundledRedstone;
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,8 @@ package dan200.computercraft.client.pocket;
 | 
			
		||||
import dan200.computercraft.ComputerCraft;
 | 
			
		||||
import dan200.computercraft.core.terminal.Terminal;
 | 
			
		||||
import dan200.computercraft.shared.computer.core.ComputerState;
 | 
			
		||||
import dan200.computercraft.shared.network.client.TerminalState;
 | 
			
		||||
import dan200.computercraft.shared.computer.terminal.NetworkedTerminal;
 | 
			
		||||
import dan200.computercraft.shared.computer.terminal.TerminalState;
 | 
			
		||||
import dan200.computercraft.shared.pocket.core.PocketServerComputer;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
@@ -25,13 +26,13 @@ import javax.annotation.Nonnull;
 | 
			
		||||
 */
 | 
			
		||||
public class PocketComputerData
 | 
			
		||||
{
 | 
			
		||||
    private final Terminal terminal;
 | 
			
		||||
    private final NetworkedTerminal terminal;
 | 
			
		||||
    private ComputerState state = ComputerState.OFF;
 | 
			
		||||
    private int lightColour = -1;
 | 
			
		||||
 | 
			
		||||
    public PocketComputerData( boolean colour )
 | 
			
		||||
    {
 | 
			
		||||
        terminal = new Terminal( ComputerCraft.pocketTermWidth, ComputerCraft.pocketTermHeight, colour );
 | 
			
		||||
        terminal = new NetworkedTerminal( ComputerCraft.pocketTermWidth, ComputerCraft.pocketTermHeight, colour );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getLightState()
 | 
			
		||||
 
 | 
			
		||||
@@ -5,14 +5,11 @@
 | 
			
		||||
 */
 | 
			
		||||
package dan200.computercraft.core.computer;
 | 
			
		||||
 | 
			
		||||
import net.minecraft.core.Direction;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
import javax.annotation.Nullable;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * A side on a computer. Unlike {@link Direction}, this is relative to the direction the computer is
 | 
			
		||||
 * facing..
 | 
			
		||||
 * A side on a computer. This is relative to the direction the computer is facing.
 | 
			
		||||
 */
 | 
			
		||||
public enum ComputerSide
 | 
			
		||||
{
 | 
			
		||||
 
 | 
			
		||||
@@ -10,7 +10,6 @@ import dan200.computercraft.api.peripheral.IWorkMonitor;
 | 
			
		||||
import dan200.computercraft.core.computer.Computer;
 | 
			
		||||
import dan200.computercraft.core.metrics.Metrics;
 | 
			
		||||
import dan200.computercraft.core.metrics.MetricsObserver;
 | 
			
		||||
import net.minecraft.world.level.block.entity.BlockEntity;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayDeque;
 | 
			
		||||
import java.util.Queue;
 | 
			
		||||
@@ -28,7 +27,7 @@ import java.util.concurrent.TimeUnit;
 | 
			
		||||
 * this tick. At the beginning of the tick, we execute as many {@link MainThread} tasks as possible, until our
 | 
			
		||||
 * time-frame or the global time frame has expired.
 | 
			
		||||
 * <p>
 | 
			
		||||
 * Then, when other objects (such as {@link BlockEntity}) are ticked, we update how much time we've used using
 | 
			
		||||
 * Then, when other objects (such as block entities or entities) are ticked, we update how much time we've used via
 | 
			
		||||
 * {@link IWorkMonitor#trackWork(long, TimeUnit)}.
 | 
			
		||||
 * <p>
 | 
			
		||||
 * Now, if anywhere during this period, we use more than our allocated time slice, the executor is marked as
 | 
			
		||||
 
 | 
			
		||||
@@ -7,8 +7,6 @@ package dan200.computercraft.core.terminal;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.shared.util.Colour;
 | 
			
		||||
import dan200.computercraft.shared.util.Palette;
 | 
			
		||||
import net.minecraft.nbt.CompoundTag;
 | 
			
		||||
import net.minecraft.network.FriendlyByteBuf;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
import javax.annotation.Nullable;
 | 
			
		||||
@@ -16,23 +14,23 @@ import java.nio.ByteBuffer;
 | 
			
		||||
 | 
			
		||||
public class Terminal
 | 
			
		||||
{
 | 
			
		||||
    private static final String BASE_16 = "0123456789abcdef";
 | 
			
		||||
    protected static final String BASE_16 = "0123456789abcdef";
 | 
			
		||||
 | 
			
		||||
    private int width;
 | 
			
		||||
    private int height;
 | 
			
		||||
    private final boolean colour;
 | 
			
		||||
    protected int width;
 | 
			
		||||
    protected int height;
 | 
			
		||||
    protected final boolean colour;
 | 
			
		||||
 | 
			
		||||
    private int cursorX = 0;
 | 
			
		||||
    private int cursorY = 0;
 | 
			
		||||
    private boolean cursorBlink = false;
 | 
			
		||||
    private int cursorColour = 0;
 | 
			
		||||
    private int cursorBackgroundColour = 15;
 | 
			
		||||
    protected int cursorX = 0;
 | 
			
		||||
    protected int cursorY = 0;
 | 
			
		||||
    protected boolean cursorBlink = false;
 | 
			
		||||
    protected int cursorColour = 0;
 | 
			
		||||
    protected int cursorBackgroundColour = 15;
 | 
			
		||||
 | 
			
		||||
    private TextBuffer[] text;
 | 
			
		||||
    private TextBuffer[] textColour;
 | 
			
		||||
    private TextBuffer[] backgroundColour;
 | 
			
		||||
    protected TextBuffer[] text;
 | 
			
		||||
    protected TextBuffer[] textColour;
 | 
			
		||||
    protected TextBuffer[] backgroundColour;
 | 
			
		||||
 | 
			
		||||
    private final Palette palette;
 | 
			
		||||
    protected final Palette palette;
 | 
			
		||||
 | 
			
		||||
    private final @Nullable Runnable onChanged;
 | 
			
		||||
 | 
			
		||||
@@ -320,110 +318,6 @@ public class Terminal
 | 
			
		||||
        if( onChanged != null ) onChanged.run();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public synchronized void write( FriendlyByteBuf buffer )
 | 
			
		||||
    {
 | 
			
		||||
        buffer.writeInt( cursorX );
 | 
			
		||||
        buffer.writeInt( cursorY );
 | 
			
		||||
        buffer.writeBoolean( cursorBlink );
 | 
			
		||||
        buffer.writeByte( cursorBackgroundColour << 4 | cursorColour );
 | 
			
		||||
 | 
			
		||||
        for( int y = 0; y < height; y++ )
 | 
			
		||||
        {
 | 
			
		||||
            TextBuffer text = this.text[y];
 | 
			
		||||
            TextBuffer textColour = this.textColour[y];
 | 
			
		||||
            TextBuffer backColour = backgroundColour[y];
 | 
			
		||||
 | 
			
		||||
            for( int x = 0; x < width; x++ ) buffer.writeByte( text.charAt( x ) & 0xFF );
 | 
			
		||||
            for( int x = 0; x < width; x++ )
 | 
			
		||||
            {
 | 
			
		||||
                buffer.writeByte( getColour(
 | 
			
		||||
                    backColour.charAt( x ), Colour.BLACK ) << 4 |
 | 
			
		||||
                    getColour( textColour.charAt( x ), Colour.WHITE )
 | 
			
		||||
                );
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        palette.write( buffer );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public synchronized void read( FriendlyByteBuf buffer )
 | 
			
		||||
    {
 | 
			
		||||
        cursorX = buffer.readInt();
 | 
			
		||||
        cursorY = buffer.readInt();
 | 
			
		||||
        cursorBlink = buffer.readBoolean();
 | 
			
		||||
 | 
			
		||||
        byte cursorColour = buffer.readByte();
 | 
			
		||||
        cursorBackgroundColour = (cursorColour >> 4) & 0xF;
 | 
			
		||||
        this.cursorColour = cursorColour & 0xF;
 | 
			
		||||
 | 
			
		||||
        for( int y = 0; y < height; y++ )
 | 
			
		||||
        {
 | 
			
		||||
            TextBuffer text = this.text[y];
 | 
			
		||||
            TextBuffer textColour = this.textColour[y];
 | 
			
		||||
            TextBuffer backColour = backgroundColour[y];
 | 
			
		||||
 | 
			
		||||
            for( int x = 0; x < width; x++ ) text.setChar( x, (char) (buffer.readByte() & 0xFF) );
 | 
			
		||||
            for( int x = 0; x < width; x++ )
 | 
			
		||||
            {
 | 
			
		||||
                byte colour = buffer.readByte();
 | 
			
		||||
                backColour.setChar( x, BASE_16.charAt( (colour >> 4) & 0xF ) );
 | 
			
		||||
                textColour.setChar( x, BASE_16.charAt( colour & 0xF ) );
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        palette.read( buffer );
 | 
			
		||||
        setChanged();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public synchronized CompoundTag writeToNBT( CompoundTag nbt )
 | 
			
		||||
    {
 | 
			
		||||
        nbt.putInt( "term_cursorX", cursorX );
 | 
			
		||||
        nbt.putInt( "term_cursorY", cursorY );
 | 
			
		||||
        nbt.putBoolean( "term_cursorBlink", cursorBlink );
 | 
			
		||||
        nbt.putInt( "term_textColour", cursorColour );
 | 
			
		||||
        nbt.putInt( "term_bgColour", cursorBackgroundColour );
 | 
			
		||||
        for( int n = 0; n < height; n++ )
 | 
			
		||||
        {
 | 
			
		||||
            nbt.putString( "term_text_" + n, text[n].toString() );
 | 
			
		||||
            nbt.putString( "term_textColour_" + n, textColour[n].toString() );
 | 
			
		||||
            nbt.putString( "term_textBgColour_" + n, backgroundColour[n].toString() );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        palette.writeToNBT( nbt );
 | 
			
		||||
        return nbt;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public synchronized void readFromNBT( CompoundTag nbt )
 | 
			
		||||
    {
 | 
			
		||||
        cursorX = nbt.getInt( "term_cursorX" );
 | 
			
		||||
        cursorY = nbt.getInt( "term_cursorY" );
 | 
			
		||||
        cursorBlink = nbt.getBoolean( "term_cursorBlink" );
 | 
			
		||||
        cursorColour = nbt.getInt( "term_textColour" );
 | 
			
		||||
        cursorBackgroundColour = nbt.getInt( "term_bgColour" );
 | 
			
		||||
 | 
			
		||||
        for( int n = 0; n < height; n++ )
 | 
			
		||||
        {
 | 
			
		||||
            text[n].fill( ' ' );
 | 
			
		||||
            if( nbt.contains( "term_text_" + n ) )
 | 
			
		||||
            {
 | 
			
		||||
                text[n].write( nbt.getString( "term_text_" + n ) );
 | 
			
		||||
            }
 | 
			
		||||
            textColour[n].fill( BASE_16.charAt( cursorColour ) );
 | 
			
		||||
            if( nbt.contains( "term_textColour_" + n ) )
 | 
			
		||||
            {
 | 
			
		||||
                textColour[n].write( nbt.getString( "term_textColour_" + n ) );
 | 
			
		||||
            }
 | 
			
		||||
            backgroundColour[n].fill( BASE_16.charAt( cursorBackgroundColour ) );
 | 
			
		||||
            if( nbt.contains( "term_textBgColour_" + n ) )
 | 
			
		||||
            {
 | 
			
		||||
                backgroundColour[n].write( nbt.getString( "term_textBgColour_" + n ) );
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        palette.readFromNBT( nbt );
 | 
			
		||||
        setChanged();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static int getColour( char c, Colour def )
 | 
			
		||||
    {
 | 
			
		||||
        if( c >= '0' && c <= '9' ) return c - '0';
 | 
			
		||||
 
 | 
			
		||||
@@ -7,7 +7,7 @@ package dan200.computercraft.shared;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.ComputerCraft;
 | 
			
		||||
import dan200.computercraft.core.apis.http.NetworkUtils;
 | 
			
		||||
import dan200.computercraft.core.filesystem.ResourceMount;
 | 
			
		||||
import dan200.computercraft.shared.computer.core.ResourceMount;
 | 
			
		||||
import dan200.computercraft.shared.command.CommandComputerCraft;
 | 
			
		||||
import dan200.computercraft.shared.computer.core.ServerContext;
 | 
			
		||||
import dan200.computercraft.shared.computer.metrics.ComputerMBean;
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,7 @@
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
package dan200.computercraft.core.filesystem;
 | 
			
		||||
package dan200.computercraft.shared.computer.core;
 | 
			
		||||
 | 
			
		||||
import com.google.common.cache.Cache;
 | 
			
		||||
import com.google.common.cache.CacheBuilder;
 | 
			
		||||
@@ -11,6 +11,7 @@ import com.google.common.io.ByteStreams;
 | 
			
		||||
import dan200.computercraft.ComputerCraft;
 | 
			
		||||
import dan200.computercraft.api.filesystem.IMount;
 | 
			
		||||
import dan200.computercraft.core.apis.handles.ArrayByteChannel;
 | 
			
		||||
import dan200.computercraft.core.filesystem.FileSystem;
 | 
			
		||||
import dan200.computercraft.shared.util.IoUtil;
 | 
			
		||||
import net.minecraft.ResourceLocationException;
 | 
			
		||||
import net.minecraft.resources.ResourceLocation;
 | 
			
		||||
@@ -15,12 +15,12 @@ import dan200.computercraft.core.computer.Computer;
 | 
			
		||||
import dan200.computercraft.core.computer.ComputerEnvironment;
 | 
			
		||||
import dan200.computercraft.core.computer.ComputerSide;
 | 
			
		||||
import dan200.computercraft.core.metrics.MetricsObserver;
 | 
			
		||||
import dan200.computercraft.core.terminal.Terminal;
 | 
			
		||||
import dan200.computercraft.shared.computer.menu.ComputerMenu;
 | 
			
		||||
import dan200.computercraft.shared.computer.terminal.NetworkedTerminal;
 | 
			
		||||
import dan200.computercraft.shared.computer.terminal.TerminalState;
 | 
			
		||||
import dan200.computercraft.shared.network.NetworkHandler;
 | 
			
		||||
import dan200.computercraft.shared.network.NetworkMessage;
 | 
			
		||||
import dan200.computercraft.shared.network.client.ComputerTerminalClientMessage;
 | 
			
		||||
import dan200.computercraft.shared.network.client.TerminalState;
 | 
			
		||||
import net.minecraft.core.BlockPos;
 | 
			
		||||
import net.minecraft.server.MinecraftServer;
 | 
			
		||||
import net.minecraft.server.level.ServerLevel;
 | 
			
		||||
@@ -41,7 +41,7 @@ public class ServerComputer implements InputHandler, ComputerEnvironment
 | 
			
		||||
    private final MetricsObserver metrics;
 | 
			
		||||
    private final Computer computer;
 | 
			
		||||
 | 
			
		||||
    private final Terminal terminal;
 | 
			
		||||
    private final NetworkedTerminal terminal;
 | 
			
		||||
    private final AtomicBoolean terminalChanged = new AtomicBoolean( false );
 | 
			
		||||
 | 
			
		||||
    private boolean changedLastFrame;
 | 
			
		||||
@@ -54,7 +54,7 @@ public class ServerComputer implements InputHandler, ComputerEnvironment
 | 
			
		||||
 | 
			
		||||
        ServerContext context = ServerContext.get( level.getServer() );
 | 
			
		||||
        instanceID = context.registry().getUnusedInstanceID();
 | 
			
		||||
        terminal = new Terminal( terminalWidth, terminalHeight, family != ComputerFamily.NORMAL, this::markTerminalChanged );
 | 
			
		||||
        terminal = new NetworkedTerminal( terminalWidth, terminalHeight, family != ComputerFamily.NORMAL, this::markTerminalChanged );
 | 
			
		||||
        metrics = context.metrics().createMetricObserver( this );
 | 
			
		||||
 | 
			
		||||
        computer = new Computer( context.computerContext(), this, terminal, computerID );
 | 
			
		||||
 
 | 
			
		||||
@@ -11,7 +11,8 @@ import dan200.computercraft.shared.computer.core.ServerComputer;
 | 
			
		||||
import dan200.computercraft.shared.computer.menu.ComputerMenu;
 | 
			
		||||
import dan200.computercraft.shared.computer.menu.ServerInputHandler;
 | 
			
		||||
import dan200.computercraft.shared.computer.menu.ServerInputState;
 | 
			
		||||
import dan200.computercraft.shared.network.client.TerminalState;
 | 
			
		||||
import dan200.computercraft.shared.computer.terminal.NetworkedTerminal;
 | 
			
		||||
import dan200.computercraft.shared.computer.terminal.TerminalState;
 | 
			
		||||
import dan200.computercraft.shared.network.container.ComputerContainerData;
 | 
			
		||||
import dan200.computercraft.shared.util.SingleIntArray;
 | 
			
		||||
import net.minecraft.world.entity.player.Player;
 | 
			
		||||
@@ -34,7 +35,7 @@ public abstract class ContainerComputerBase extends AbstractContainerMenu implem
 | 
			
		||||
    private final @Nullable ServerComputer computer;
 | 
			
		||||
    private final @Nullable ServerInputState<ContainerComputerBase> input;
 | 
			
		||||
 | 
			
		||||
    private final @Nullable Terminal terminal;
 | 
			
		||||
    private final @Nullable NetworkedTerminal terminal;
 | 
			
		||||
 | 
			
		||||
    private final ItemStack displayStack;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -6,7 +6,7 @@
 | 
			
		||||
package dan200.computercraft.shared.computer.menu;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.shared.computer.core.ServerComputer;
 | 
			
		||||
import dan200.computercraft.shared.network.client.TerminalState;
 | 
			
		||||
import dan200.computercraft.shared.computer.terminal.TerminalState;
 | 
			
		||||
import net.minecraft.world.inventory.AbstractContainerMenu;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,129 @@
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of ComputerCraft - http://www.computercraft.info
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
package dan200.computercraft.shared.computer.terminal;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.core.terminal.Terminal;
 | 
			
		||||
import dan200.computercraft.core.terminal.TextBuffer;
 | 
			
		||||
import dan200.computercraft.shared.util.Colour;
 | 
			
		||||
import net.minecraft.nbt.CompoundTag;
 | 
			
		||||
import net.minecraft.network.FriendlyByteBuf;
 | 
			
		||||
 | 
			
		||||
public class NetworkedTerminal extends Terminal
 | 
			
		||||
{
 | 
			
		||||
    public NetworkedTerminal( int width, int height, boolean colour )
 | 
			
		||||
    {
 | 
			
		||||
        super( width, height, colour );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public NetworkedTerminal( int width, int height, boolean colour, Runnable changedCallback )
 | 
			
		||||
    {
 | 
			
		||||
        super( width, height, colour, changedCallback );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public synchronized void write( FriendlyByteBuf buffer )
 | 
			
		||||
    {
 | 
			
		||||
        buffer.writeInt( cursorX );
 | 
			
		||||
        buffer.writeInt( cursorY );
 | 
			
		||||
        buffer.writeBoolean( cursorBlink );
 | 
			
		||||
        buffer.writeByte( cursorBackgroundColour << 4 | cursorColour );
 | 
			
		||||
 | 
			
		||||
        for( int y = 0; y < height; y++ )
 | 
			
		||||
        {
 | 
			
		||||
            TextBuffer text = this.text[y];
 | 
			
		||||
            TextBuffer textColour = this.textColour[y];
 | 
			
		||||
            TextBuffer backColour = backgroundColour[y];
 | 
			
		||||
 | 
			
		||||
            for( int x = 0; x < width; x++ ) buffer.writeByte( text.charAt( x ) & 0xFF );
 | 
			
		||||
            for( int x = 0; x < width; x++ )
 | 
			
		||||
            {
 | 
			
		||||
                buffer.writeByte( getColour(
 | 
			
		||||
                    backColour.charAt( x ), Colour.BLACK ) << 4 |
 | 
			
		||||
                    getColour( textColour.charAt( x ), Colour.WHITE )
 | 
			
		||||
                );
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        palette.write( buffer );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public synchronized void read( FriendlyByteBuf buffer )
 | 
			
		||||
    {
 | 
			
		||||
        cursorX = buffer.readInt();
 | 
			
		||||
        cursorY = buffer.readInt();
 | 
			
		||||
        cursorBlink = buffer.readBoolean();
 | 
			
		||||
 | 
			
		||||
        byte cursorColour = buffer.readByte();
 | 
			
		||||
        cursorBackgroundColour = (cursorColour >> 4) & 0xF;
 | 
			
		||||
        this.cursorColour = cursorColour & 0xF;
 | 
			
		||||
 | 
			
		||||
        for( int y = 0; y < height; y++ )
 | 
			
		||||
        {
 | 
			
		||||
            TextBuffer text = this.text[y];
 | 
			
		||||
            TextBuffer textColour = this.textColour[y];
 | 
			
		||||
            TextBuffer backColour = backgroundColour[y];
 | 
			
		||||
 | 
			
		||||
            for( int x = 0; x < width; x++ ) text.setChar( x, (char) (buffer.readByte() & 0xFF) );
 | 
			
		||||
            for( int x = 0; x < width; x++ )
 | 
			
		||||
            {
 | 
			
		||||
                byte colour = buffer.readByte();
 | 
			
		||||
                backColour.setChar( x, BASE_16.charAt( (colour >> 4) & 0xF ) );
 | 
			
		||||
                textColour.setChar( x, BASE_16.charAt( colour & 0xF ) );
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        palette.read( buffer );
 | 
			
		||||
        setChanged();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public synchronized CompoundTag writeToNBT( CompoundTag nbt )
 | 
			
		||||
    {
 | 
			
		||||
        nbt.putInt( "term_cursorX", cursorX );
 | 
			
		||||
        nbt.putInt( "term_cursorY", cursorY );
 | 
			
		||||
        nbt.putBoolean( "term_cursorBlink", cursorBlink );
 | 
			
		||||
        nbt.putInt( "term_textColour", cursorColour );
 | 
			
		||||
        nbt.putInt( "term_bgColour", cursorBackgroundColour );
 | 
			
		||||
        for( int n = 0; n < height; n++ )
 | 
			
		||||
        {
 | 
			
		||||
            nbt.putString( "term_text_" + n, text[n].toString() );
 | 
			
		||||
            nbt.putString( "term_textColour_" + n, textColour[n].toString() );
 | 
			
		||||
            nbt.putString( "term_textBgColour_" + n, backgroundColour[n].toString() );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        palette.writeToNBT( nbt );
 | 
			
		||||
        return nbt;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public synchronized void readFromNBT( CompoundTag nbt )
 | 
			
		||||
    {
 | 
			
		||||
        cursorX = nbt.getInt( "term_cursorX" );
 | 
			
		||||
        cursorY = nbt.getInt( "term_cursorY" );
 | 
			
		||||
        cursorBlink = nbt.getBoolean( "term_cursorBlink" );
 | 
			
		||||
        cursorColour = nbt.getInt( "term_textColour" );
 | 
			
		||||
        cursorBackgroundColour = nbt.getInt( "term_bgColour" );
 | 
			
		||||
 | 
			
		||||
        for( int n = 0; n < height; n++ )
 | 
			
		||||
        {
 | 
			
		||||
            text[n].fill( ' ' );
 | 
			
		||||
            if( nbt.contains( "term_text_" + n ) )
 | 
			
		||||
            {
 | 
			
		||||
                text[n].write( nbt.getString( "term_text_" + n ) );
 | 
			
		||||
            }
 | 
			
		||||
            textColour[n].fill( BASE_16.charAt( cursorColour ) );
 | 
			
		||||
            if( nbt.contains( "term_textColour_" + n ) )
 | 
			
		||||
            {
 | 
			
		||||
                textColour[n].write( nbt.getString( "term_textColour_" + n ) );
 | 
			
		||||
            }
 | 
			
		||||
            backgroundColour[n].fill( BASE_16.charAt( cursorBackgroundColour ) );
 | 
			
		||||
            if( nbt.contains( "term_textBgColour_" + n ) )
 | 
			
		||||
            {
 | 
			
		||||
                backgroundColour[n].write( nbt.getString( "term_textBgColour_" + n ) );
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        palette.readFromNBT( nbt );
 | 
			
		||||
        setChanged();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -3,9 +3,8 @@
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
package dan200.computercraft.shared.network.client;
 | 
			
		||||
package dan200.computercraft.shared.computer.terminal;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.core.terminal.Terminal;
 | 
			
		||||
import dan200.computercraft.shared.util.IoUtil;
 | 
			
		||||
import io.netty.buffer.ByteBuf;
 | 
			
		||||
import io.netty.buffer.ByteBufInputStream;
 | 
			
		||||
@@ -42,12 +41,12 @@ public class TerminalState
 | 
			
		||||
 | 
			
		||||
    private ByteBuf compressed;
 | 
			
		||||
 | 
			
		||||
    public TerminalState( @Nullable Terminal terminal )
 | 
			
		||||
    public TerminalState( @Nullable NetworkedTerminal terminal )
 | 
			
		||||
    {
 | 
			
		||||
        this( terminal, true );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public TerminalState( @Nullable Terminal terminal, boolean compress )
 | 
			
		||||
    public TerminalState( @Nullable NetworkedTerminal terminal, boolean compress )
 | 
			
		||||
    {
 | 
			
		||||
        this.compress = compress;
 | 
			
		||||
 | 
			
		||||
@@ -115,17 +114,17 @@ public class TerminalState
 | 
			
		||||
        return buffer == null ? 0 : buffer.readableBytes();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void apply( Terminal terminal )
 | 
			
		||||
    public void apply( NetworkedTerminal terminal )
 | 
			
		||||
    {
 | 
			
		||||
        if( buffer == null ) throw new NullPointerException( "buffer" );
 | 
			
		||||
        terminal.resize( width, height );
 | 
			
		||||
        terminal.read( new FriendlyByteBuf( buffer ) );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Terminal create()
 | 
			
		||||
    public NetworkedTerminal create()
 | 
			
		||||
    {
 | 
			
		||||
        if( buffer == null ) throw new NullPointerException( "Terminal does not exist" );
 | 
			
		||||
        Terminal terminal = new Terminal( width, height, colour );
 | 
			
		||||
        NetworkedTerminal terminal = new NetworkedTerminal( width, height, colour );
 | 
			
		||||
        terminal.read( new FriendlyByteBuf( buffer ) );
 | 
			
		||||
        return terminal;
 | 
			
		||||
    }
 | 
			
		||||
@@ -6,6 +6,7 @@
 | 
			
		||||
package dan200.computercraft.shared.network.client;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.shared.computer.menu.ComputerMenu;
 | 
			
		||||
import dan200.computercraft.shared.computer.terminal.TerminalState;
 | 
			
		||||
import dan200.computercraft.shared.network.NetworkMessage;
 | 
			
		||||
import net.minecraft.client.Minecraft;
 | 
			
		||||
import net.minecraft.network.FriendlyByteBuf;
 | 
			
		||||
 
 | 
			
		||||
@@ -5,6 +5,7 @@
 | 
			
		||||
 */
 | 
			
		||||
package dan200.computercraft.shared.network.client;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.shared.computer.terminal.TerminalState;
 | 
			
		||||
import dan200.computercraft.shared.network.NetworkMessage;
 | 
			
		||||
import dan200.computercraft.shared.peripheral.monitor.TileMonitor;
 | 
			
		||||
import net.minecraft.client.Minecraft;
 | 
			
		||||
 
 | 
			
		||||
@@ -7,8 +7,9 @@ package dan200.computercraft.shared.network.client;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.client.pocket.ClientPocketComputers;
 | 
			
		||||
import dan200.computercraft.client.pocket.PocketComputerData;
 | 
			
		||||
import dan200.computercraft.core.terminal.Terminal;
 | 
			
		||||
import dan200.computercraft.shared.computer.core.ComputerState;
 | 
			
		||||
import dan200.computercraft.shared.computer.terminal.NetworkedTerminal;
 | 
			
		||||
import dan200.computercraft.shared.computer.terminal.TerminalState;
 | 
			
		||||
import dan200.computercraft.shared.network.NetworkMessage;
 | 
			
		||||
import dan200.computercraft.shared.pocket.core.PocketServerComputer;
 | 
			
		||||
import net.minecraft.network.FriendlyByteBuf;
 | 
			
		||||
@@ -29,7 +30,7 @@ public class PocketComputerDataMessage implements NetworkMessage
 | 
			
		||||
        instanceId = computer.getInstanceID();
 | 
			
		||||
        state = computer.getState();
 | 
			
		||||
        lightState = computer.getLight();
 | 
			
		||||
        terminal = sendTerminal ? computer.getTerminalState() : new TerminalState( (Terminal) null );
 | 
			
		||||
        terminal = sendTerminal ? computer.getTerminalState() : new TerminalState( (NetworkedTerminal) null );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public PocketComputerDataMessage( FriendlyByteBuf buf )
 | 
			
		||||
 
 | 
			
		||||
@@ -7,7 +7,7 @@ package dan200.computercraft.shared.network.container;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
 | 
			
		||||
import dan200.computercraft.shared.computer.core.ServerComputer;
 | 
			
		||||
import dan200.computercraft.shared.network.client.TerminalState;
 | 
			
		||||
import dan200.computercraft.shared.computer.terminal.TerminalState;
 | 
			
		||||
import net.minecraft.network.FriendlyByteBuf;
 | 
			
		||||
import net.minecraft.world.item.ItemStack;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -9,7 +9,8 @@ import com.mojang.blaze3d.platform.GlStateManager;
 | 
			
		||||
import dan200.computercraft.client.util.DirectBuffers;
 | 
			
		||||
import dan200.computercraft.client.util.DirectVertexBuffer;
 | 
			
		||||
import dan200.computercraft.core.terminal.Terminal;
 | 
			
		||||
import dan200.computercraft.shared.network.client.TerminalState;
 | 
			
		||||
import dan200.computercraft.shared.computer.terminal.NetworkedTerminal;
 | 
			
		||||
import dan200.computercraft.shared.computer.terminal.TerminalState;
 | 
			
		||||
import net.minecraft.core.BlockPos;
 | 
			
		||||
import net.minecraftforge.api.distmarker.Dist;
 | 
			
		||||
import net.minecraftforge.api.distmarker.OnlyIn;
 | 
			
		||||
@@ -36,7 +37,7 @@ public final class ClientMonitor
 | 
			
		||||
    public int tboUniform;
 | 
			
		||||
    public DirectVertexBuffer backgroundBuffer;
 | 
			
		||||
    public DirectVertexBuffer foregroundBuffer;
 | 
			
		||||
    private Terminal terminal;
 | 
			
		||||
    private NetworkedTerminal terminal;
 | 
			
		||||
    private boolean terminalChanged;
 | 
			
		||||
 | 
			
		||||
    public ClientMonitor( TileMonitor origin )
 | 
			
		||||
@@ -182,7 +183,7 @@ public final class ClientMonitor
 | 
			
		||||
    {
 | 
			
		||||
        if( state.hasTerminal() )
 | 
			
		||||
        {
 | 
			
		||||
            if( terminal == null ) terminal = new Terminal( state.width, state.height, state.colour );
 | 
			
		||||
            if( terminal == null ) terminal = new NetworkedTerminal( state.width, state.height, state.colour );
 | 
			
		||||
            state.apply( terminal );
 | 
			
		||||
            terminalChanged = true;
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,7 @@ package dan200.computercraft.shared.peripheral.monitor;
 | 
			
		||||
import dan200.computercraft.ComputerCraft;
 | 
			
		||||
import dan200.computercraft.shared.network.NetworkHandler;
 | 
			
		||||
import dan200.computercraft.shared.network.client.MonitorClientMessage;
 | 
			
		||||
import dan200.computercraft.shared.network.client.TerminalState;
 | 
			
		||||
import dan200.computercraft.shared.computer.terminal.TerminalState;
 | 
			
		||||
import net.minecraft.core.BlockPos;
 | 
			
		||||
import net.minecraft.server.level.ServerLevel;
 | 
			
		||||
import net.minecraft.server.level.ServerPlayer;
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,7 @@ package dan200.computercraft.shared.peripheral.monitor;
 | 
			
		||||
 | 
			
		||||
import com.google.common.annotations.VisibleForTesting;
 | 
			
		||||
import dan200.computercraft.core.terminal.Terminal;
 | 
			
		||||
import dan200.computercraft.shared.computer.terminal.NetworkedTerminal;
 | 
			
		||||
import dan200.computercraft.shared.util.TickScheduler;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nullable;
 | 
			
		||||
@@ -18,7 +19,7 @@ public class ServerMonitor
 | 
			
		||||
 | 
			
		||||
    private final boolean colour;
 | 
			
		||||
    private int textScale = 2;
 | 
			
		||||
    private @Nullable Terminal terminal;
 | 
			
		||||
    private @Nullable NetworkedTerminal terminal;
 | 
			
		||||
    private final AtomicBoolean resized = new AtomicBoolean( false );
 | 
			
		||||
    private final AtomicBoolean changed = new AtomicBoolean( false );
 | 
			
		||||
 | 
			
		||||
@@ -46,7 +47,7 @@ public class ServerMonitor
 | 
			
		||||
 | 
			
		||||
        if( terminal == null )
 | 
			
		||||
        {
 | 
			
		||||
            terminal = new Terminal( termWidth, termHeight, colour, this::markChanged );
 | 
			
		||||
            terminal = new NetworkedTerminal( termWidth, termHeight, colour, this::markChanged );
 | 
			
		||||
            markChanged();
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
@@ -91,7 +92,7 @@ public class ServerMonitor
 | 
			
		||||
 | 
			
		||||
    @Nullable
 | 
			
		||||
    @VisibleForTesting
 | 
			
		||||
    public Terminal getTerminal()
 | 
			
		||||
    public NetworkedTerminal getTerminal()
 | 
			
		||||
    {
 | 
			
		||||
        return terminal;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -11,7 +11,7 @@ import dan200.computercraft.api.peripheral.IComputerAccess;
 | 
			
		||||
import dan200.computercraft.api.peripheral.IPeripheral;
 | 
			
		||||
import dan200.computercraft.core.terminal.Terminal;
 | 
			
		||||
import dan200.computercraft.shared.common.TileGeneric;
 | 
			
		||||
import dan200.computercraft.shared.network.client.TerminalState;
 | 
			
		||||
import dan200.computercraft.shared.computer.terminal.TerminalState;
 | 
			
		||||
import dan200.computercraft.shared.util.CapabilityUtil;
 | 
			
		||||
import dan200.computercraft.shared.util.TickScheduler;
 | 
			
		||||
import net.minecraft.core.BlockPos;
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,7 @@ package dan200.computercraft.shared.peripheral.printer;
 | 
			
		||||
import dan200.computercraft.api.peripheral.IPeripheral;
 | 
			
		||||
import dan200.computercraft.core.terminal.Terminal;
 | 
			
		||||
import dan200.computercraft.shared.common.TileGeneric;
 | 
			
		||||
import dan200.computercraft.shared.computer.terminal.NetworkedTerminal;
 | 
			
		||||
import dan200.computercraft.shared.media.items.ItemPrintout;
 | 
			
		||||
import dan200.computercraft.shared.util.*;
 | 
			
		||||
import net.minecraft.core.BlockPos;
 | 
			
		||||
@@ -63,7 +64,7 @@ public final class TilePrinter extends TileGeneric implements DefaultSidedInvent
 | 
			
		||||
        SidedCaps.ofNullable( facing -> facing == null ? new InvWrapper( this ) : new SidedInvWrapper( this, facing ) );
 | 
			
		||||
    private LazyOptional<IPeripheral> peripheralCap;
 | 
			
		||||
 | 
			
		||||
    private final Terminal page = new Terminal( ItemPrintout.LINE_MAX_LENGTH, ItemPrintout.LINES_PER_PAGE, true );
 | 
			
		||||
    private final NetworkedTerminal page = new NetworkedTerminal( ItemPrintout.LINE_MAX_LENGTH, ItemPrintout.LINES_PER_PAGE, true );
 | 
			
		||||
    private String pageTitle = "";
 | 
			
		||||
    private boolean printing = false;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -9,9 +9,6 @@ import dan200.computercraft.api.lua.LuaValues;
 | 
			
		||||
import dan200.computercraft.shared.util.Colour;
 | 
			
		||||
import dan200.computercraft.test.core.CallCounter;
 | 
			
		||||
import dan200.computercraft.test.core.terminal.TerminalMatchers;
 | 
			
		||||
import io.netty.buffer.Unpooled;
 | 
			
		||||
import net.minecraft.nbt.CompoundTag;
 | 
			
		||||
import net.minecraft.network.FriendlyByteBuf;
 | 
			
		||||
import org.hamcrest.Matcher;
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
 | 
			
		||||
@@ -597,92 +594,6 @@ class TerminalTest
 | 
			
		||||
        callCounter.assertNotCalled();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    void testPacketBufferRoundtrip()
 | 
			
		||||
    {
 | 
			
		||||
        Terminal writeTerminal = new Terminal( 2, 1, true );
 | 
			
		||||
 | 
			
		||||
        blit( writeTerminal, "hi", "11", "ee" );
 | 
			
		||||
        writeTerminal.setCursorPos( 2, 5 );
 | 
			
		||||
        writeTerminal.setTextColour( 3 );
 | 
			
		||||
        writeTerminal.setBackgroundColour( 5 );
 | 
			
		||||
 | 
			
		||||
        FriendlyByteBuf packetBuffer = new FriendlyByteBuf( Unpooled.buffer() );
 | 
			
		||||
        writeTerminal.write( packetBuffer );
 | 
			
		||||
 | 
			
		||||
        CallCounter callCounter = new CallCounter();
 | 
			
		||||
        Terminal readTerminal = new Terminal( 2, 1, true, callCounter );
 | 
			
		||||
        packetBuffer.writeBytes( packetBuffer );
 | 
			
		||||
        readTerminal.read( packetBuffer );
 | 
			
		||||
 | 
			
		||||
        assertThat( readTerminal, allOf(
 | 
			
		||||
            textMatches( new String[] { "hi", } ),
 | 
			
		||||
            textColourMatches( new String[] { "11", } ),
 | 
			
		||||
            backgroundColourMatches( new String[] { "ee", } )
 | 
			
		||||
        ) );
 | 
			
		||||
 | 
			
		||||
        assertEquals( 2, readTerminal.getCursorX() );
 | 
			
		||||
        assertEquals( 5, readTerminal.getCursorY() );
 | 
			
		||||
        assertEquals( 3, readTerminal.getTextColour() );
 | 
			
		||||
        assertEquals( 5, readTerminal.getBackgroundColour() );
 | 
			
		||||
        callCounter.assertCalledTimes( 1 );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    void testNbtRoundtrip()
 | 
			
		||||
    {
 | 
			
		||||
        Terminal writeTerminal = new Terminal( 10, 5, true );
 | 
			
		||||
        blit( writeTerminal, "hi", "11", "ee" );
 | 
			
		||||
        writeTerminal.setCursorPos( 2, 5 );
 | 
			
		||||
        writeTerminal.setTextColour( 3 );
 | 
			
		||||
        writeTerminal.setBackgroundColour( 5 );
 | 
			
		||||
 | 
			
		||||
        CompoundTag nbt = new CompoundTag();
 | 
			
		||||
        writeTerminal.writeToNBT( nbt );
 | 
			
		||||
 | 
			
		||||
        CallCounter callCounter = new CallCounter();
 | 
			
		||||
        Terminal readTerminal = new Terminal( 2, 1, true, callCounter );
 | 
			
		||||
 | 
			
		||||
        readTerminal.readFromNBT( nbt );
 | 
			
		||||
 | 
			
		||||
        assertThat( readTerminal, allOf(
 | 
			
		||||
            textMatches( new String[] { "hi", } ),
 | 
			
		||||
            textColourMatches( new String[] { "11", } ),
 | 
			
		||||
            backgroundColourMatches( new String[] { "ee", } )
 | 
			
		||||
        ) );
 | 
			
		||||
 | 
			
		||||
        assertEquals( 2, readTerminal.getCursorX() );
 | 
			
		||||
        assertEquals( 5, readTerminal.getCursorY() );
 | 
			
		||||
        assertEquals( 3, readTerminal.getTextColour() );
 | 
			
		||||
        assertEquals( 5, readTerminal.getBackgroundColour() );
 | 
			
		||||
        callCounter.assertCalledTimes( 1 );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    void testReadWriteNBTEmpty()
 | 
			
		||||
    {
 | 
			
		||||
        Terminal terminal = new Terminal( 0, 0, true );
 | 
			
		||||
 | 
			
		||||
        CompoundTag nbt = new CompoundTag();
 | 
			
		||||
        terminal.writeToNBT( nbt );
 | 
			
		||||
 | 
			
		||||
        CallCounter callCounter = new CallCounter();
 | 
			
		||||
        terminal = new Terminal( 0, 1, true, callCounter );
 | 
			
		||||
        terminal.readFromNBT( nbt );
 | 
			
		||||
 | 
			
		||||
        assertThat( terminal, allOf(
 | 
			
		||||
            textMatches( new String[] { "", } ),
 | 
			
		||||
            textColourMatches( new String[] { "", } ),
 | 
			
		||||
            backgroundColourMatches( new String[] { "", } )
 | 
			
		||||
        ) );
 | 
			
		||||
 | 
			
		||||
        assertEquals( 0, terminal.getCursorX() );
 | 
			
		||||
        assertEquals( 0, terminal.getCursorY() );
 | 
			
		||||
        assertEquals( 0, terminal.getTextColour() );
 | 
			
		||||
        assertEquals( 15, terminal.getBackgroundColour() );
 | 
			
		||||
        callCounter.assertCalledTimes( 1 );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    void testGetColour()
 | 
			
		||||
    {
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,7 @@
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
package dan200.computercraft.core.filesystem;
 | 
			
		||||
package dan200.computercraft.shared.computer.core;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.api.filesystem.IMount;
 | 
			
		||||
import net.minecraft.Util;
 | 
			
		||||
@@ -0,0 +1,113 @@
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of ComputerCraft - http://www.computercraft.info
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
package dan200.computercraft.shared.computer.terminal;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.api.lua.LuaValues;
 | 
			
		||||
import dan200.computercraft.core.terminal.Terminal;
 | 
			
		||||
import dan200.computercraft.test.core.CallCounter;
 | 
			
		||||
import io.netty.buffer.Unpooled;
 | 
			
		||||
import net.minecraft.nbt.CompoundTag;
 | 
			
		||||
import net.minecraft.network.FriendlyByteBuf;
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
 | 
			
		||||
import static dan200.computercraft.test.core.terminal.TerminalMatchers.*;
 | 
			
		||||
import static org.hamcrest.MatcherAssert.assertThat;
 | 
			
		||||
import static org.hamcrest.Matchers.allOf;
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.assertEquals;
 | 
			
		||||
 | 
			
		||||
class NetworkedTerminalTest
 | 
			
		||||
{
 | 
			
		||||
    @Test
 | 
			
		||||
    void testPacketBufferRoundtrip()
 | 
			
		||||
    {
 | 
			
		||||
        var writeTerminal = new NetworkedTerminal( 2, 1, true );
 | 
			
		||||
 | 
			
		||||
        blit( writeTerminal, "hi", "11", "ee" );
 | 
			
		||||
        writeTerminal.setCursorPos( 2, 5 );
 | 
			
		||||
        writeTerminal.setTextColour( 3 );
 | 
			
		||||
        writeTerminal.setBackgroundColour( 5 );
 | 
			
		||||
 | 
			
		||||
        FriendlyByteBuf packetBuffer = new FriendlyByteBuf( Unpooled.buffer() );
 | 
			
		||||
        writeTerminal.write( packetBuffer );
 | 
			
		||||
 | 
			
		||||
        CallCounter callCounter = new CallCounter();
 | 
			
		||||
        var readTerminal = new NetworkedTerminal( 2, 1, true, callCounter );
 | 
			
		||||
        packetBuffer.writeBytes( packetBuffer );
 | 
			
		||||
        readTerminal.read( packetBuffer );
 | 
			
		||||
 | 
			
		||||
        assertThat( readTerminal, allOf(
 | 
			
		||||
            textMatches( new String[] { "hi", } ),
 | 
			
		||||
            textColourMatches( new String[] { "11", } ),
 | 
			
		||||
            backgroundColourMatches( new String[] { "ee", } )
 | 
			
		||||
        ) );
 | 
			
		||||
 | 
			
		||||
        assertEquals( 2, readTerminal.getCursorX() );
 | 
			
		||||
        assertEquals( 5, readTerminal.getCursorY() );
 | 
			
		||||
        assertEquals( 3, readTerminal.getTextColour() );
 | 
			
		||||
        assertEquals( 5, readTerminal.getBackgroundColour() );
 | 
			
		||||
        callCounter.assertCalledTimes( 1 );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    void testNbtRoundtrip()
 | 
			
		||||
    {
 | 
			
		||||
        var writeTerminal = new NetworkedTerminal( 10, 5, true );
 | 
			
		||||
        blit( writeTerminal, "hi", "11", "ee" );
 | 
			
		||||
        writeTerminal.setCursorPos( 2, 5 );
 | 
			
		||||
        writeTerminal.setTextColour( 3 );
 | 
			
		||||
        writeTerminal.setBackgroundColour( 5 );
 | 
			
		||||
 | 
			
		||||
        CompoundTag nbt = new CompoundTag();
 | 
			
		||||
        writeTerminal.writeToNBT( nbt );
 | 
			
		||||
 | 
			
		||||
        CallCounter callCounter = new CallCounter();
 | 
			
		||||
        var readTerminal = new NetworkedTerminal( 2, 1, true, callCounter );
 | 
			
		||||
 | 
			
		||||
        readTerminal.readFromNBT( nbt );
 | 
			
		||||
 | 
			
		||||
        assertThat( readTerminal, allOf(
 | 
			
		||||
            textMatches( new String[] { "hi", } ),
 | 
			
		||||
            textColourMatches( new String[] { "11", } ),
 | 
			
		||||
            backgroundColourMatches( new String[] { "ee", } )
 | 
			
		||||
        ) );
 | 
			
		||||
 | 
			
		||||
        assertEquals( 2, readTerminal.getCursorX() );
 | 
			
		||||
        assertEquals( 5, readTerminal.getCursorY() );
 | 
			
		||||
        assertEquals( 3, readTerminal.getTextColour() );
 | 
			
		||||
        assertEquals( 5, readTerminal.getBackgroundColour() );
 | 
			
		||||
        callCounter.assertCalledTimes( 1 );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    void testReadWriteNBTEmpty()
 | 
			
		||||
    {
 | 
			
		||||
        var terminal = new NetworkedTerminal( 0, 0, true );
 | 
			
		||||
 | 
			
		||||
        CompoundTag nbt = new CompoundTag();
 | 
			
		||||
        terminal.writeToNBT( nbt );
 | 
			
		||||
 | 
			
		||||
        CallCounter callCounter = new CallCounter();
 | 
			
		||||
        terminal = new NetworkedTerminal( 0, 1, true, callCounter );
 | 
			
		||||
        terminal.readFromNBT( nbt );
 | 
			
		||||
 | 
			
		||||
        assertThat( terminal, allOf(
 | 
			
		||||
            textMatches( new String[] { "", } ),
 | 
			
		||||
            textColourMatches( new String[] { "", } ),
 | 
			
		||||
            backgroundColourMatches( new String[] { "", } )
 | 
			
		||||
        ) );
 | 
			
		||||
 | 
			
		||||
        assertEquals( 0, terminal.getCursorX() );
 | 
			
		||||
        assertEquals( 0, terminal.getCursorY() );
 | 
			
		||||
        assertEquals( 0, terminal.getTextColour() );
 | 
			
		||||
        assertEquals( 15, terminal.getBackgroundColour() );
 | 
			
		||||
        callCounter.assertCalledTimes( 1 );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static void blit( Terminal terminal, String text, String fg, String bg )
 | 
			
		||||
    {
 | 
			
		||||
        terminal.blit( LuaValues.encode( text ), LuaValues.encode( fg ), LuaValues.encode( bg ) );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -3,7 +3,7 @@
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
package dan200.computercraft.shared.network.client;
 | 
			
		||||
package dan200.computercraft.shared.computer.terminal;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.core.terminal.Terminal;
 | 
			
		||||
import dan200.computercraft.core.terminal.TextBuffer;
 | 
			
		||||
@@ -23,7 +23,7 @@ public class TerminalStateTest
 | 
			
		||||
    @RepeatedTest( 5 )
 | 
			
		||||
    public void testCompressed()
 | 
			
		||||
    {
 | 
			
		||||
        Terminal terminal = randomTerminal();
 | 
			
		||||
        var terminal = randomTerminal();
 | 
			
		||||
 | 
			
		||||
        FriendlyByteBuf buffer = new FriendlyByteBuf( Unpooled.directBuffer() );
 | 
			
		||||
        new TerminalState( terminal, true ).write( buffer );
 | 
			
		||||
@@ -35,7 +35,7 @@ public class TerminalStateTest
 | 
			
		||||
    @RepeatedTest( 5 )
 | 
			
		||||
    public void testUncompressed()
 | 
			
		||||
    {
 | 
			
		||||
        Terminal terminal = randomTerminal();
 | 
			
		||||
        var terminal = randomTerminal();
 | 
			
		||||
 | 
			
		||||
        FriendlyByteBuf buffer = new FriendlyByteBuf( Unpooled.directBuffer() );
 | 
			
		||||
        new TerminalState( terminal, false ).write( buffer );
 | 
			
		||||
@@ -44,10 +44,10 @@ public class TerminalStateTest
 | 
			
		||||
        assertEquals( 0, buffer.readableBytes() );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static Terminal randomTerminal()
 | 
			
		||||
    private static NetworkedTerminal randomTerminal()
 | 
			
		||||
    {
 | 
			
		||||
        Random random = new Random();
 | 
			
		||||
        Terminal terminal = new Terminal( 10, 5, true );
 | 
			
		||||
        NetworkedTerminal terminal = new NetworkedTerminal( 10, 5, true );
 | 
			
		||||
        for( int y = 0; y < terminal.getHeight(); y++ )
 | 
			
		||||
        {
 | 
			
		||||
            TextBuffer buffer = terminal.getLine( y );
 | 
			
		||||
@@ -70,14 +70,14 @@ public class TerminalStateTest
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static Terminal read( FriendlyByteBuf buffer )
 | 
			
		||||
    private static NetworkedTerminal read( FriendlyByteBuf buffer )
 | 
			
		||||
    {
 | 
			
		||||
        TerminalState state = new TerminalState( buffer );
 | 
			
		||||
        assertTrue( state.colour );
 | 
			
		||||
 | 
			
		||||
        if( !state.hasTerminal() ) return null;
 | 
			
		||||
 | 
			
		||||
        Terminal other = new Terminal( state.width, state.height, true );
 | 
			
		||||
        var other = new NetworkedTerminal( state.width, state.height, true );
 | 
			
		||||
        state.apply( other );
 | 
			
		||||
        return other;
 | 
			
		||||
    }
 | 
			
		||||
		Reference in New Issue
	
	Block a user