diff --git a/src/main/java/dan200/computercraft/client/pocket/PocketComputerData.java b/src/main/java/dan200/computercraft/client/pocket/PocketComputerData.java index de188d727..cb3e41294 100644 --- a/src/main/java/dan200/computercraft/client/pocket/PocketComputerData.java +++ b/src/main/java/dan200/computercraft/client/pocket/PocketComputerData.java @@ -25,7 +25,7 @@ import javax.annotation.Nonnull; */ public class PocketComputerData { - private Terminal terminal; + private final Terminal terminal; private ComputerState state = ComputerState.OFF; private int lightColour = -1; @@ -58,13 +58,6 @@ public class PocketComputerData public void setTerminal( TerminalState state ) { - if( state.width != terminal.getWidth() || state.height != terminal.getHeight() || state.colour != terminal.isColour() ) - { - terminal = state.create(); - } - else - { - state.apply( terminal ); - } + state.apply( terminal ); } } diff --git a/src/main/java/dan200/computercraft/shared/common/ClientTerminal.java b/src/main/java/dan200/computercraft/shared/common/ClientTerminal.java deleted file mode 100644 index 6a5f73650..000000000 --- a/src/main/java/dan200/computercraft/shared/common/ClientTerminal.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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.common; - -import dan200.computercraft.core.terminal.Terminal; -import dan200.computercraft.shared.network.client.TerminalState; - -public class ClientTerminal -{ - private boolean colour; - private Terminal terminal; - private boolean terminalChanged; - - public ClientTerminal( boolean colour ) - { - this.colour = colour; - terminal = null; - terminalChanged = false; - } - - public boolean pollTerminalChanged() - { - boolean changed = terminalChanged; - terminalChanged = false; - return changed; - } - - public Terminal getTerminal() - { - return terminal; - } - - public void read( TerminalState state ) - { - colour = state.colour; - if( state.hasTerminal() ) - { - resizeTerminal( state.width, state.height ); - state.apply( terminal ); - } - else - { - deleteTerminal(); - } - } - - private void resizeTerminal( int width, int height ) - { - if( terminal == null ) - { - terminal = new Terminal( width, height, colour, () -> terminalChanged = true ); - terminalChanged = true; - } - else - { - terminal.resize( width, height ); - } - } - - private void deleteTerminal() - { - if( terminal != null ) - { - terminal = null; - terminalChanged = true; - } - } -} diff --git a/src/main/java/dan200/computercraft/shared/common/ServerTerminal.java b/src/main/java/dan200/computercraft/shared/common/ServerTerminal.java deleted file mode 100644 index 84a90c311..000000000 --- a/src/main/java/dan200/computercraft/shared/common/ServerTerminal.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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.common; - -import dan200.computercraft.core.terminal.Terminal; -import dan200.computercraft.shared.network.client.TerminalState; - -import javax.annotation.Nullable; -import java.util.concurrent.atomic.AtomicBoolean; - -public class ServerTerminal -{ - private final boolean colour; - private @Nullable Terminal terminal; - private final AtomicBoolean terminalChanged = new AtomicBoolean( false ); - private boolean terminalChangedLastFrame = false; - - public ServerTerminal( boolean colour ) - { - this.colour = colour; - } - - protected void resize( int width, int height ) - { - if( terminal == null ) - { - terminal = new Terminal( width, height, colour, this::markTerminalChanged ); - markTerminalChanged(); - } - else - { - terminal.resize( width, height ); - } - } - - public void delete() - { - if( terminal != null ) - { - terminal = null; - markTerminalChanged(); - } - } - - protected void markTerminalChanged() - { - terminalChanged.set( true ); - } - - public void tickServer() - { - terminalChangedLastFrame = terminalChanged.getAndSet( false ); - } - - public boolean hasTerminalChanged() - { - return terminalChangedLastFrame; - } - - public Terminal getTerminal() - { - return terminal; - } - - public TerminalState write() - { - return new TerminalState( terminal ); - } -} diff --git a/src/main/java/dan200/computercraft/shared/network/client/TerminalState.java b/src/main/java/dan200/computercraft/shared/network/client/TerminalState.java index 39b87d240..d31e19347 100644 --- a/src/main/java/dan200/computercraft/shared/network/client/TerminalState.java +++ b/src/main/java/dan200/computercraft/shared/network/client/TerminalState.java @@ -118,6 +118,7 @@ public class TerminalState public void apply( Terminal terminal ) { if( buffer == null ) throw new NullPointerException( "buffer" ); + terminal.resize( width, height ); terminal.read( new PacketBuffer( buffer ) ); } 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 186d63c76..03df2a7aa 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 com.mojang.blaze3d.systems.RenderSystem; import dan200.computercraft.client.util.DirectBuffers; import dan200.computercraft.client.util.DirectVertexBuffer; -import dan200.computercraft.shared.common.ClientTerminal; +import dan200.computercraft.core.terminal.Terminal; +import dan200.computercraft.shared.network.client.TerminalState; import net.minecraft.util.math.BlockPos; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; @@ -22,7 +23,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.Set; -public final class ClientMonitor extends ClientTerminal +public final class ClientMonitor { private static final Set allMonitors = new HashSet<>(); @@ -35,10 +36,11 @@ public final class ClientMonitor extends ClientTerminal public int tboTexture; public int tboUniform; public DirectVertexBuffer buffer; + private Terminal terminal; + private boolean terminalChanged; - public ClientMonitor( boolean colour, TileMonitor origin ) + public ClientMonitor( TileMonitor origin ) { - super( colour ); this.origin = origin; } @@ -156,4 +158,34 @@ public final class ClientMonitor extends ClientTerminal } } } + + public boolean pollTerminalChanged() + { + boolean changed = terminalChanged; + terminalChanged = false; + return changed; + } + + public Terminal getTerminal() + { + return terminal; + } + + void read( TerminalState state ) + { + if( state.hasTerminal() ) + { + if( terminal == null ) terminal = new Terminal( state.width, state.height, state.colour ); + state.apply( terminal ); + terminalChanged = true; + } + else + { + if( terminal != null ) + { + terminal = null; + 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 c390d7b30..bbbf81991 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorWatcher.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorWatcher.java @@ -123,7 +123,7 @@ public final class MonitorWatcher private static TerminalState getState( TileMonitor tile, ServerMonitor monitor ) { TerminalState state = tile.cached; - if( state == null ) state = tile.cached = monitor.write(); + if( state == null ) state = tile.cached = new TerminalState( monitor.getTerminal() ); return state; } 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 669c2e1e1..056786755 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/monitor/ServerMonitor.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/monitor/ServerMonitor.java @@ -5,26 +5,30 @@ */ package dan200.computercraft.shared.peripheral.monitor; +import com.google.common.annotations.VisibleForTesting; import dan200.computercraft.core.terminal.Terminal; -import dan200.computercraft.shared.common.ServerTerminal; import dan200.computercraft.shared.util.TickScheduler; +import javax.annotation.Nullable; import java.util.concurrent.atomic.AtomicBoolean; -public class ServerMonitor extends ServerTerminal +public class ServerMonitor { private final TileMonitor origin; + + private final boolean colour; private int textScale = 2; + private @Nullable Terminal terminal; private final AtomicBoolean resized = new AtomicBoolean( false ); private final AtomicBoolean changed = new AtomicBoolean( false ); - public ServerMonitor( boolean colour, TileMonitor origin ) + ServerMonitor( boolean colour, TileMonitor origin ) { - super( colour ); + this.colour = colour; this.origin = origin; } - public synchronized void rebuild() + synchronized void rebuild() { Terminal oldTerm = getTerminal(); int oldWidth = oldTerm == null ? -1 : oldTerm.getWidth(); @@ -40,52 +44,55 @@ public class ServerMonitor extends ServerTerminal 1.0 ); - resize( termWidth, termHeight ); + if( terminal == null ) + { + terminal = new Terminal( termWidth, termHeight, colour, this::markChanged ); + markChanged(); + } + else + { + terminal.resize( termWidth, termHeight ); + } + if( oldWidth != termWidth || oldHeight != termHeight ) { - getTerminal().clear(); + terminal.clear(); resized.set( true ); markChanged(); } } - @Override - protected void markTerminalChanged() - { - super.markTerminalChanged(); - markChanged(); - } - private void markChanged() { if( !changed.getAndSet( true ) ) TickScheduler.schedule( origin ); } - protected void clearChanged() - { - changed.set( false ); - } - - public int getTextScale() + int getTextScale() { return textScale; } - public synchronized void setTextScale( int textScale ) + synchronized void setTextScale( int textScale ) { if( this.textScale == textScale ) return; this.textScale = textScale; rebuild(); } - public boolean pollResized() + boolean pollResized() { return resized.getAndSet( false ); } - public boolean pollTerminalChanged() + boolean pollTerminalChanged() { - tickServer(); - return hasTerminalChanged(); + return changed.getAndSet( false ); + } + + @Nullable + @VisibleForTesting + public Terminal 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 706850e83..307ad4a8b 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java @@ -5,11 +5,11 @@ */ package dan200.computercraft.shared.peripheral.monitor; +import com.google.common.annotations.VisibleForTesting; import dan200.computercraft.ComputerCraft; import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.core.terminal.Terminal; -import dan200.computercraft.shared.common.ServerTerminal; import dan200.computercraft.shared.common.TileGeneric; import dan200.computercraft.shared.network.client.TerminalState; import dan200.computercraft.shared.util.CapabilityUtil; @@ -171,8 +171,6 @@ public class TileMonitor extends TileGeneric if( xIndex != 0 || yIndex != 0 || serverMonitor == null ) return; - serverMonitor.clearChanged(); - if( serverMonitor.pollResized() ) eachComputer( c -> c.queueEvent( "monitor_resize", c.getAttachmentName() ) ); if( serverMonitor.pollTerminalChanged() ) MonitorWatcher.enqueue( this ); } @@ -199,6 +197,7 @@ public class TileMonitor extends TileGeneric } @Nullable + @VisibleForTesting public ServerMonitor getCachedServerMonitor() { return serverMonitor; @@ -305,7 +304,7 @@ public class TileMonitor extends TileGeneric if( xIndex == 0 && yIndex == 0 ) { // If we're the origin terminal then create it. - if( clientMonitor == null ) clientMonitor = new ClientMonitor( advanced, this ); + if( clientMonitor == null ) clientMonitor = new ClientMonitor( this ); } } @@ -317,7 +316,7 @@ public class TileMonitor extends TileGeneric return; } - if( clientMonitor == null ) clientMonitor = new ClientMonitor( advanced, this ); + if( clientMonitor == null ) clientMonitor = new ClientMonitor( this ); clientMonitor.read( state ); } @@ -588,7 +587,7 @@ public class TileMonitor extends TileGeneric return; } - ServerTerminal serverTerminal = getServerMonitor(); + ServerMonitor serverTerminal = getServerMonitor(); if( serverTerminal == null ) return; Terminal originTerminal = serverTerminal.getTerminal(); diff --git a/src/testMod/java/dan200/computercraft/ingame/MonitorTest.kt b/src/testMod/java/dan200/computercraft/ingame/MonitorTest.kt index bd7cc1308..4f291b53c 100644 --- a/src/testMod/java/dan200/computercraft/ingame/MonitorTest.kt +++ b/src/testMod/java/dan200/computercraft/ingame/MonitorTest.kt @@ -55,7 +55,7 @@ class Monitor_Test { val monitor = helper.getBlockEntity(BlockPos(2, 2, 3)) as TileMonitor monitor.getCapability(Capabilities.CAPABILITY_PERIPHERAL) - val terminal = monitor.cachedServerMonitor!!.terminal + val terminal = monitor.cachedServerMonitor!!.terminal!! terminal.write("Hello, world!") terminal.setCursorPos(1, 2) terminal.textColour = 2