From b3702fed785dda386d8434d1d5d9ceb42aa3ac56 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Sun, 30 Oct 2022 09:20:26 +0000 Subject: [PATCH] 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. --- .../computercraft/ComputerCraftAPIImpl.java | 2 +- .../client/pocket/PocketComputerData.java | 7 +- .../core/computer/ComputerSide.java | 5 +- .../mainthread/MainThreadExecutor.java | 3 +- .../computercraft/core/terminal/Terminal.java | 132 ++---------------- .../computercraft/shared/CommonHooks.java | 2 +- .../computer/core}/ResourceMount.java | 3 +- .../shared/computer/core/ServerComputer.java | 8 +- .../inventory/ContainerComputerBase.java | 5 +- .../shared/computer/menu/ComputerMenu.java | 2 +- .../computer/terminal/NetworkedTerminal.java | 129 +++++++++++++++++ .../terminal}/TerminalState.java | 13 +- .../client/ComputerTerminalClientMessage.java | 1 + .../network/client/MonitorClientMessage.java | 1 + .../client/PocketComputerDataMessage.java | 5 +- .../container/ComputerContainerData.java | 2 +- .../peripheral/monitor/ClientMonitor.java | 7 +- .../peripheral/monitor/MonitorWatcher.java | 2 +- .../peripheral/monitor/ServerMonitor.java | 7 +- .../peripheral/monitor/TileMonitor.java | 2 +- .../peripheral/printer/TilePrinter.java | 3 +- .../core/terminal/TerminalTest.java | 89 ------------ .../computer/core}/ResourceMountTest.java | 2 +- .../terminal/NetworkedTerminalTest.java | 113 +++++++++++++++ .../terminal}/TerminalStateTest.java | 14 +- 25 files changed, 305 insertions(+), 254 deletions(-) rename src/main/java/dan200/computercraft/{core/filesystem => shared/computer/core}/ResourceMount.java (98%) create mode 100644 src/main/java/dan200/computercraft/shared/computer/terminal/NetworkedTerminal.java rename src/main/java/dan200/computercraft/shared/{network/client => computer/terminal}/TerminalState.java (92%) rename src/test/java/dan200/computercraft/{core/filesystem => shared/computer/core}/ResourceMountTest.java (97%) create mode 100644 src/test/java/dan200/computercraft/shared/computer/terminal/NetworkedTerminalTest.java rename src/test/java/dan200/computercraft/shared/{network/client => computer/terminal}/TerminalStateTest.java (84%) diff --git a/src/main/java/dan200/computercraft/ComputerCraftAPIImpl.java b/src/main/java/dan200/computercraft/ComputerCraftAPIImpl.java index ae77168ed..13ce8dcaf 100644 --- a/src/main/java/dan200/computercraft/ComputerCraftAPIImpl.java +++ b/src/main/java/dan200/computercraft/ComputerCraftAPIImpl.java @@ -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; diff --git a/src/main/java/dan200/computercraft/client/pocket/PocketComputerData.java b/src/main/java/dan200/computercraft/client/pocket/PocketComputerData.java index cb3e41294..77ea455b0 100644 --- a/src/main/java/dan200/computercraft/client/pocket/PocketComputerData.java +++ b/src/main/java/dan200/computercraft/client/pocket/PocketComputerData.java @@ -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() diff --git a/src/main/java/dan200/computercraft/core/computer/ComputerSide.java b/src/main/java/dan200/computercraft/core/computer/ComputerSide.java index 85fcfab57..897ae91b9 100644 --- a/src/main/java/dan200/computercraft/core/computer/ComputerSide.java +++ b/src/main/java/dan200/computercraft/core/computer/ComputerSide.java @@ -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 { diff --git a/src/main/java/dan200/computercraft/core/computer/mainthread/MainThreadExecutor.java b/src/main/java/dan200/computercraft/core/computer/mainthread/MainThreadExecutor.java index c70f98cc7..2cccf3933 100644 --- a/src/main/java/dan200/computercraft/core/computer/mainthread/MainThreadExecutor.java +++ b/src/main/java/dan200/computercraft/core/computer/mainthread/MainThreadExecutor.java @@ -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. *

- * 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)}. *

* Now, if anywhere during this period, we use more than our allocated time slice, the executor is marked as diff --git a/src/main/java/dan200/computercraft/core/terminal/Terminal.java b/src/main/java/dan200/computercraft/core/terminal/Terminal.java index a5b532d95..a9271bf83 100644 --- a/src/main/java/dan200/computercraft/core/terminal/Terminal.java +++ b/src/main/java/dan200/computercraft/core/terminal/Terminal.java @@ -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'; diff --git a/src/main/java/dan200/computercraft/shared/CommonHooks.java b/src/main/java/dan200/computercraft/shared/CommonHooks.java index 06b8a7ad4..5467356cf 100644 --- a/src/main/java/dan200/computercraft/shared/CommonHooks.java +++ b/src/main/java/dan200/computercraft/shared/CommonHooks.java @@ -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; diff --git a/src/main/java/dan200/computercraft/core/filesystem/ResourceMount.java b/src/main/java/dan200/computercraft/shared/computer/core/ResourceMount.java similarity index 98% rename from src/main/java/dan200/computercraft/core/filesystem/ResourceMount.java rename to src/main/java/dan200/computercraft/shared/computer/core/ResourceMount.java index 3924ccda2..8a3ec2284 100644 --- a/src/main/java/dan200/computercraft/core/filesystem/ResourceMount.java +++ b/src/main/java/dan200/computercraft/shared/computer/core/ResourceMount.java @@ -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; diff --git a/src/main/java/dan200/computercraft/shared/computer/core/ServerComputer.java b/src/main/java/dan200/computercraft/shared/computer/core/ServerComputer.java index 47fad09fc..0e4927d40 100644 --- a/src/main/java/dan200/computercraft/shared/computer/core/ServerComputer.java +++ b/src/main/java/dan200/computercraft/shared/computer/core/ServerComputer.java @@ -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 ); diff --git a/src/main/java/dan200/computercraft/shared/computer/inventory/ContainerComputerBase.java b/src/main/java/dan200/computercraft/shared/computer/inventory/ContainerComputerBase.java index 6ea17b28b..ae7050d4c 100644 --- a/src/main/java/dan200/computercraft/shared/computer/inventory/ContainerComputerBase.java +++ b/src/main/java/dan200/computercraft/shared/computer/inventory/ContainerComputerBase.java @@ -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 input; - private final @Nullable Terminal terminal; + private final @Nullable NetworkedTerminal terminal; private final ItemStack displayStack; diff --git a/src/main/java/dan200/computercraft/shared/computer/menu/ComputerMenu.java b/src/main/java/dan200/computercraft/shared/computer/menu/ComputerMenu.java index 0711a1bec..f2c7f829a 100644 --- a/src/main/java/dan200/computercraft/shared/computer/menu/ComputerMenu.java +++ b/src/main/java/dan200/computercraft/shared/computer/menu/ComputerMenu.java @@ -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; /** diff --git a/src/main/java/dan200/computercraft/shared/computer/terminal/NetworkedTerminal.java b/src/main/java/dan200/computercraft/shared/computer/terminal/NetworkedTerminal.java new file mode 100644 index 000000000..83298de32 --- /dev/null +++ b/src/main/java/dan200/computercraft/shared/computer/terminal/NetworkedTerminal.java @@ -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(); + } +} diff --git a/src/main/java/dan200/computercraft/shared/network/client/TerminalState.java b/src/main/java/dan200/computercraft/shared/computer/terminal/TerminalState.java similarity index 92% rename from src/main/java/dan200/computercraft/shared/network/client/TerminalState.java rename to src/main/java/dan200/computercraft/shared/computer/terminal/TerminalState.java index fc7f905d4..dd755d819 100644 --- a/src/main/java/dan200/computercraft/shared/network/client/TerminalState.java +++ b/src/main/java/dan200/computercraft/shared/computer/terminal/TerminalState.java @@ -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; } diff --git a/src/main/java/dan200/computercraft/shared/network/client/ComputerTerminalClientMessage.java b/src/main/java/dan200/computercraft/shared/network/client/ComputerTerminalClientMessage.java index bdcedb696..8491ab44e 100644 --- a/src/main/java/dan200/computercraft/shared/network/client/ComputerTerminalClientMessage.java +++ b/src/main/java/dan200/computercraft/shared/network/client/ComputerTerminalClientMessage.java @@ -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; diff --git a/src/main/java/dan200/computercraft/shared/network/client/MonitorClientMessage.java b/src/main/java/dan200/computercraft/shared/network/client/MonitorClientMessage.java index 5aff75662..20fd67b38 100644 --- a/src/main/java/dan200/computercraft/shared/network/client/MonitorClientMessage.java +++ b/src/main/java/dan200/computercraft/shared/network/client/MonitorClientMessage.java @@ -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; diff --git a/src/main/java/dan200/computercraft/shared/network/client/PocketComputerDataMessage.java b/src/main/java/dan200/computercraft/shared/network/client/PocketComputerDataMessage.java index 8e5e8b01b..650af2f72 100644 --- a/src/main/java/dan200/computercraft/shared/network/client/PocketComputerDataMessage.java +++ b/src/main/java/dan200/computercraft/shared/network/client/PocketComputerDataMessage.java @@ -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 ) diff --git a/src/main/java/dan200/computercraft/shared/network/container/ComputerContainerData.java b/src/main/java/dan200/computercraft/shared/network/container/ComputerContainerData.java index 56499ce8e..3c55bbf8b 100644 --- a/src/main/java/dan200/computercraft/shared/network/container/ComputerContainerData.java +++ b/src/main/java/dan200/computercraft/shared/network/container/ComputerContainerData.java @@ -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; diff --git a/src/main/java/dan200/computercraft/shared/peripheral/monitor/ClientMonitor.java b/src/main/java/dan200/computercraft/shared/peripheral/monitor/ClientMonitor.java index 1437bad99..3bf0a3e55 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/monitor/ClientMonitor.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/monitor/ClientMonitor.java @@ -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; } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorWatcher.java b/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorWatcher.java index 9d920a58b..a39fbf310 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorWatcher.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorWatcher.java @@ -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; diff --git a/src/main/java/dan200/computercraft/shared/peripheral/monitor/ServerMonitor.java b/src/main/java/dan200/computercraft/shared/peripheral/monitor/ServerMonitor.java index 2e7efc0de..8a75a3a56 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/monitor/ServerMonitor.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/monitor/ServerMonitor.java @@ -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; } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java b/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java index b89b699ec..cd0874425 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java @@ -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; diff --git a/src/main/java/dan200/computercraft/shared/peripheral/printer/TilePrinter.java b/src/main/java/dan200/computercraft/shared/peripheral/printer/TilePrinter.java index 8e7f2f638..328ec47cc 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/printer/TilePrinter.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/printer/TilePrinter.java @@ -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 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; diff --git a/src/test/java/dan200/computercraft/core/terminal/TerminalTest.java b/src/test/java/dan200/computercraft/core/terminal/TerminalTest.java index fe495a7b4..ddcd50db9 100644 --- a/src/test/java/dan200/computercraft/core/terminal/TerminalTest.java +++ b/src/test/java/dan200/computercraft/core/terminal/TerminalTest.java @@ -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() { diff --git a/src/test/java/dan200/computercraft/core/filesystem/ResourceMountTest.java b/src/test/java/dan200/computercraft/shared/computer/core/ResourceMountTest.java similarity index 97% rename from src/test/java/dan200/computercraft/core/filesystem/ResourceMountTest.java rename to src/test/java/dan200/computercraft/shared/computer/core/ResourceMountTest.java index daf93b599..e88632472 100644 --- a/src/test/java/dan200/computercraft/core/filesystem/ResourceMountTest.java +++ b/src/test/java/dan200/computercraft/shared/computer/core/ResourceMountTest.java @@ -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; diff --git a/src/test/java/dan200/computercraft/shared/computer/terminal/NetworkedTerminalTest.java b/src/test/java/dan200/computercraft/shared/computer/terminal/NetworkedTerminalTest.java new file mode 100644 index 000000000..8614b67c0 --- /dev/null +++ b/src/test/java/dan200/computercraft/shared/computer/terminal/NetworkedTerminalTest.java @@ -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 ) ); + } +} diff --git a/src/test/java/dan200/computercraft/shared/network/client/TerminalStateTest.java b/src/test/java/dan200/computercraft/shared/computer/terminal/TerminalStateTest.java similarity index 84% rename from src/test/java/dan200/computercraft/shared/network/client/TerminalStateTest.java rename to src/test/java/dan200/computercraft/shared/computer/terminal/TerminalStateTest.java index 01a4bd325..3b99ec573 100644 --- a/src/test/java/dan200/computercraft/shared/network/client/TerminalStateTest.java +++ b/src/test/java/dan200/computercraft/shared/computer/terminal/TerminalStateTest.java @@ -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; }