1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-07-03 10:32:52 +00:00
Jonathan Coates 7c1f40031b
Some cleanup to network messages
- Use enums for key and mouse actions, rather than integer ids.
 - Change TerminalState to always contain a terminal. We now make
   TerminalState nullable when we want to skip sending anything.
2024-04-25 18:19:34 +01:00

56 lines
1.5 KiB
Java

// SPDX-FileCopyrightText: 2019 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.shared.network.server;
import dan200.computercraft.shared.computer.menu.ComputerMenu;
import dan200.computercraft.shared.network.MessageType;
import dan200.computercraft.shared.network.NetworkMessages;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.inventory.AbstractContainerMenu;
public class KeyEventServerMessage extends ComputerServerMessage {
private final Action type;
private final int key;
public KeyEventServerMessage(AbstractContainerMenu menu, Action type, int key) {
super(menu);
this.type = type;
this.key = key;
}
public KeyEventServerMessage(FriendlyByteBuf buf) {
super(buf);
type = buf.readEnum(Action.class);
key = buf.readVarInt();
}
@Override
public void write(FriendlyByteBuf buf) {
super.write(buf);
buf.writeEnum(type);
buf.writeVarInt(key);
}
@Override
protected void handle(ServerNetworkContext context, ComputerMenu container) {
var input = container.getInput();
if (type == Action.UP) {
input.keyUp(key);
} else {
input.keyDown(key, type == Action.REPEAT);
}
}
@Override
public MessageType<KeyEventServerMessage> type() {
return NetworkMessages.KEY_EVENT;
}
public enum Action {
DOWN, REPEAT, UP
}
}