diff --git a/src/main/java/dan200/computercraft/core/apis/TermMethods.java b/src/main/java/dan200/computercraft/core/apis/TermMethods.java index 84c104e34..5692065a6 100644 --- a/src/main/java/dan200/computercraft/core/apis/TermMethods.java +++ b/src/main/java/dan200/computercraft/core/apis/TermMethods.java @@ -14,6 +14,7 @@ import dan200.computercraft.shared.util.StringUtil; import org.apache.commons.lang3.ArrayUtils; import javax.annotation.Nonnull; +import java.nio.ByteBuffer; /** * A base class for all objects which interact with a terminal. Namely the {@link TermAPI} and monitors. @@ -283,9 +284,9 @@ public abstract class TermMethods * } */ @LuaFunction - public final void blit( String text, String textColour, String backgroundColour ) throws LuaException + public final void blit( ByteBuffer text, ByteBuffer textColour, ByteBuffer backgroundColour ) throws LuaException { - if( textColour.length() != text.length() || backgroundColour.length() != text.length() ) + if( textColour.remaining() != text.remaining() || backgroundColour.remaining() != text.remaining() ) { throw new LuaException( "Arguments must be the same length" ); } @@ -294,7 +295,7 @@ public abstract class TermMethods synchronized( terminal ) { terminal.blit( text, textColour, backgroundColour ); - terminal.setCursorPos( terminal.getCursorX() + text.length(), terminal.getCursorY() ); + terminal.setCursorPos( terminal.getCursorX() + text.remaining(), terminal.getCursorY() ); } } diff --git a/src/main/java/dan200/computercraft/core/terminal/Terminal.java b/src/main/java/dan200/computercraft/core/terminal/Terminal.java index b8c2b8411..6385a84dc 100644 --- a/src/main/java/dan200/computercraft/core/terminal/Terminal.java +++ b/src/main/java/dan200/computercraft/core/terminal/Terminal.java @@ -11,6 +11,7 @@ import net.minecraft.nbt.CompoundNBT; import net.minecraft.network.PacketBuffer; import javax.annotation.Nonnull; +import java.nio.ByteBuffer; public class Terminal { @@ -191,7 +192,7 @@ public class Terminal return palette; } - public synchronized void blit( String text, String textColour, String backgroundColour ) + public synchronized void blit( ByteBuffer text, ByteBuffer textColour, ByteBuffer backgroundColour ) { int x = cursorX; int y = cursorY; diff --git a/src/main/java/dan200/computercraft/core/terminal/TextBuffer.java b/src/main/java/dan200/computercraft/core/terminal/TextBuffer.java index e1211639b..2809546e0 100644 --- a/src/main/java/dan200/computercraft/core/terminal/TextBuffer.java +++ b/src/main/java/dan200/computercraft/core/terminal/TextBuffer.java @@ -5,6 +5,8 @@ */ package dan200.computercraft.core.terminal; +import java.nio.ByteBuffer; + public class TextBuffer { private final char[] text; @@ -42,6 +44,19 @@ public class TextBuffer } } + public void write( ByteBuffer text, int start ) + { + int pos = start; + start = Math.max( start, 0 ); + int length = text.remaining(); + int end = Math.min( start + length, pos + length ); + end = Math.min( end, this.text.length ); + for( int i = start; i < end; i++ ) + { + this.text[i] = (char) (text.get( i - pos ) & 0xFF); + } + } + public void write( TextBuffer text ) { int end = Math.min( text.length(), this.text.length ); diff --git a/src/test/java/dan200/computercraft/core/terminal/TerminalTest.java b/src/test/java/dan200/computercraft/core/terminal/TerminalTest.java index 37e033150..a68087980 100644 --- a/src/test/java/dan200/computercraft/core/terminal/TerminalTest.java +++ b/src/test/java/dan200/computercraft/core/terminal/TerminalTest.java @@ -5,6 +5,7 @@ */ package dan200.computercraft.core.terminal; +import dan200.computercraft.api.lua.LuaValues; import dan200.computercraft.shared.util.Colour; import dan200.computercraft.utils.CallCounter; import io.netty.buffer.Unpooled; @@ -293,7 +294,7 @@ class TerminalTest CallCounter callCounter = new CallCounter(); Terminal terminal = new Terminal( 4, 3, callCounter ); - terminal.blit( "test", "1234", "abcd" ); + blit( terminal, "test", "1234", "abcd" ); assertThat( terminal, allOf( textMatches( new String[] { @@ -322,7 +323,7 @@ class TerminalTest terminal.setCursorPos( 2, 1 ); callCounter.reset(); - terminal.blit( "hi", "11", "ee" ); + blit( terminal, "hi", "11", "ee" ); assertThat( terminal, allOf( textMatches( new String[] { @@ -354,13 +355,13 @@ class TerminalTest terminal.setCursorPos( 2, -5 ); callCounter.reset(); - terminal.blit( "hi", "11", "ee" ); + blit( terminal, "hi", "11", "ee" ); assertThat( terminal, old.matches() ); callCounter.assertNotCalled(); terminal.setCursorPos( 2, 5 ); callCounter.reset(); - terminal.blit( "hi", "11", "ee" ); + blit( terminal, "hi", "11", "ee" ); assertThat( terminal, old.matches() ); callCounter.assertNotCalled(); } @@ -584,7 +585,7 @@ class TerminalTest { Terminal writeTerminal = new Terminal( 2, 1 ); - writeTerminal.blit( "hi", "11", "ee" ); + blit( writeTerminal, "hi", "11", "ee" ); writeTerminal.setCursorPos( 2, 5 ); writeTerminal.setTextColour( 3 ); writeTerminal.setBackgroundColour( 5 ); @@ -614,7 +615,7 @@ class TerminalTest void testNbtRoundtrip() { Terminal writeTerminal = new Terminal( 10, 5 ); - writeTerminal.blit( "hi", "11", "ee" ); + blit( writeTerminal, "hi", "11", "ee" ); writeTerminal.setCursorPos( 2, 5 ); writeTerminal.setTextColour( 3 ); writeTerminal.setBackgroundColour( 5 ); @@ -687,6 +688,11 @@ class TerminalTest assertEquals( 5, Terminal.getColour( 'Z', Colour.LIME ) ); } + private static void blit( Terminal terminal, String text, String fg, String bg ) + { + terminal.blit( LuaValues.encode( text ), LuaValues.encode( fg ), LuaValues.encode( bg ) ); + } + private static final class TerminalBufferSnapshot { final String[] textLines;