2023-03-15 21:52:13 +00:00
|
|
|
// SPDX-FileCopyrightText: 2020 The CC: Tweaked Developers
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-10-30 09:20:26 +00:00
|
|
|
package dan200.computercraft.shared.computer.terminal;
|
2020-05-20 07:44:44 +00:00
|
|
|
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.buffer.Unpooled;
|
2021-08-03 20:46:53 +00:00
|
|
|
import net.minecraft.network.FriendlyByteBuf;
|
2024-04-25 19:17:43 +00:00
|
|
|
import net.minecraft.network.codec.StreamCodec;
|
2024-04-25 17:19:34 +00:00
|
|
|
import org.jetbrains.annotations.Contract;
|
2020-05-20 07:44:44 +00:00
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A snapshot of a terminal's state.
|
2022-10-21 22:10:18 +00:00
|
|
|
* <p>
|
2020-05-20 07:44:44 +00:00
|
|
|
* This is somewhat memory inefficient (we build a buffer, only to write it elsewhere), however it means we get a
|
|
|
|
* complete and accurate description of a terminal, which avoids a lot of complexities with resizing terminals, dirty
|
|
|
|
* states, etc...
|
|
|
|
*/
|
|
|
|
public class TerminalState {
|
2024-04-25 19:17:43 +00:00
|
|
|
public static final StreamCodec<FriendlyByteBuf, TerminalState> STREAM_CODEC = StreamCodec.ofMember(TerminalState::write, TerminalState::new);
|
|
|
|
|
2024-03-26 21:54:48 +00:00
|
|
|
private final boolean colour;
|
|
|
|
private final int width;
|
|
|
|
private final int height;
|
2020-05-20 07:44:44 +00:00
|
|
|
private final ByteBuf buffer;
|
|
|
|
|
2024-04-25 17:19:34 +00:00
|
|
|
public TerminalState(NetworkedTerminal terminal) {
|
|
|
|
colour = terminal.isColour();
|
|
|
|
width = terminal.getWidth();
|
|
|
|
height = terminal.getHeight();
|
|
|
|
|
|
|
|
var buf = buffer = Unpooled.buffer();
|
|
|
|
terminal.write(new FriendlyByteBuf(buf));
|
|
|
|
}
|
2020-05-20 07:44:44 +00:00
|
|
|
|
2024-04-25 17:19:34 +00:00
|
|
|
@Contract("null -> null; !null -> !null")
|
|
|
|
public static @Nullable TerminalState create(@Nullable NetworkedTerminal terminal) {
|
|
|
|
return terminal == null ? null : new TerminalState(terminal);
|
2020-05-20 07:44:44 +00:00
|
|
|
}
|
|
|
|
|
2024-04-25 19:17:43 +00:00
|
|
|
private TerminalState(FriendlyByteBuf buf) {
|
2021-06-12 21:18:35 +00:00
|
|
|
colour = buf.readBoolean();
|
2024-04-25 17:19:34 +00:00
|
|
|
width = buf.readVarInt();
|
|
|
|
height = buf.readVarInt();
|
2020-05-20 07:44:44 +00:00
|
|
|
|
2024-04-25 17:19:34 +00:00
|
|
|
var length = buf.readVarInt();
|
|
|
|
buffer = buf.readBytes(length);
|
2020-05-20 07:44:44 +00:00
|
|
|
}
|
|
|
|
|
2024-04-25 19:17:43 +00:00
|
|
|
private void write(FriendlyByteBuf buf) {
|
2020-05-20 07:44:44 +00:00
|
|
|
buf.writeBoolean(colour);
|
2024-04-25 17:19:34 +00:00
|
|
|
buf.writeVarInt(width);
|
|
|
|
buf.writeVarInt(height);
|
|
|
|
buf.writeVarInt(buffer.readableBytes());
|
|
|
|
buf.writeBytes(buffer, buffer.readerIndex(), buffer.readableBytes());
|
2020-05-20 07:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int size() {
|
2024-04-25 17:19:34 +00:00
|
|
|
return buffer.readableBytes();
|
2020-05-20 07:44:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 09:20:26 +00:00
|
|
|
public void apply(NetworkedTerminal terminal) {
|
2022-10-21 18:00:29 +00:00
|
|
|
terminal.resize(width, height);
|
2021-08-03 20:46:53 +00:00
|
|
|
terminal.read(new FriendlyByteBuf(buffer));
|
2020-05-20 07:44:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 09:20:26 +00:00
|
|
|
public NetworkedTerminal create() {
|
|
|
|
var terminal = new NetworkedTerminal(width, height, colour);
|
2022-10-25 21:36:21 +00:00
|
|
|
terminal.read(new FriendlyByteBuf(buffer));
|
Remove ClientComputer
Historically CC has maintained two computer registries; one on the
server (which runs the actual computer) and one on the client (which
stores the terminal and some small bits of additional data).
This means when a user opens the computer UI, we send the terminal
contents and store it in the client computer registry. We then send the
instance id alongside the "open container" packet, which is used to look
up the client computer (and thus terminal) in our client-side registry.
This patch makes the computer menu syncing behaviour more consistent
with vanilla. The initial terminal contents is sent alongside the "open
container" packet, and subsequent terminal changes apply /just/ to the
open container. Computer on/off state is synced via a vanilla
ContainerData/IIntArray.
Likewise, sending user input to the server now targets the open
container, rather than an arbitrary instance id.
The one remaining usage of ClientComputer is for pocket computers. For
these, we still need to sync the current on/off/blinking state and the
pocket computer light.
We don't need the full ClientComputer interface for this case (after
all, you can't send input to a pocket computer someone else is
holding!). This means we can tear out ClientComputer and
ClientComputerRegistry, replacing it with a much simpler
ClientPocketComputers store.
This in turn allows the following changes:
- Remove IComputer, as we no longer need to abstract over client and
server computers.
- Likewise, we can merge ComputerRegistry into the server
registry. This commit also cleans up the handling of instance IDs a
little bit: ServerComputers are now responsible for generating their
ID and adding/removing themselves from the registry.
- As the client-side terminal will never be null, we can remove a whole
bunch of null checks throughout the codebase.
- As the terminal is available immediately, we don't need to explicitly
pass in terminal sizes to the computer GUIs. This means we're no
longer reliant on those config values on the client side!
- Remove the "request computer state" packet. Pocket computers now
store which players need to know the computer state, automatically
sending data when a new player starts tracking the computer.
2022-10-21 17:17:42 +00:00
|
|
|
return terminal;
|
|
|
|
}
|
2020-05-20 07:44:44 +00:00
|
|
|
}
|