Fix creating a zero-sized pocket terminal

When the terminal data is not present, width/height are set to 0, rather
than the terminal's width/height. This meant we'd create an empty
terminal, which then crashes when we try to render it.

We now make the terminal nullable and initialise it the first time we
receive the terminal data. To prevent future mistakes, we hide
width/height, and use TerminalState.create everywhere.

Fixes #1765
This commit is contained in:
Jonathan Coates
2024-03-26 21:59:41 +00:00
parent 63580b4acb
commit 6363164f2b
6 changed files with 32 additions and 28 deletions
@@ -11,7 +11,8 @@ import org.junit.jupiter.api.RepeatedTest;
import java.util.Random;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/**
* Tests {@link TerminalState} round tripping works as expected.
@@ -42,6 +43,7 @@ public class TerminalStateTest {
private static void checkEqual(Terminal expected, Terminal actual) {
assertNotNull(expected, "Expected cannot be null");
assertNotNull(actual, "Actual cannot be null");
assertEquals(expected.isColour(), actual.isColour(), "isColour must match");
assertEquals(expected.getHeight(), actual.getHeight(), "Heights must match");
assertEquals(expected.getWidth(), actual.getWidth(), "Widths must match");
@@ -51,13 +53,6 @@ public class TerminalStateTest {
}
private static NetworkedTerminal read(FriendlyByteBuf buffer) {
var state = new TerminalState(buffer);
assertTrue(state.colour);
if (!state.hasTerminal()) return null;
var other = new NetworkedTerminal(state.width, state.height, true);
state.apply(other);
return other;
return new TerminalState(buffer).create();
}
}