From 4980b7355d72b4d001ef886e5a4cad2950363f6d Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Wed, 24 Apr 2024 21:19:30 +0100 Subject: [PATCH 1/4] Don't share CharsetDecoders across threads Fixes #1803 --- .../core/apis/http/websocket/WebsocketHandle.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/core/src/main/java/dan200/computercraft/core/apis/http/websocket/WebsocketHandle.java b/projects/core/src/main/java/dan200/computercraft/core/apis/http/websocket/WebsocketHandle.java index 407dc4f0c..eafd04df0 100644 --- a/projects/core/src/main/java/dan200/computercraft/core/apis/http/websocket/WebsocketHandle.java +++ b/projects/core/src/main/java/dan200/computercraft/core/apis/http/websocket/WebsocketHandle.java @@ -29,7 +29,7 @@ import static dan200.computercraft.core.apis.http.websocket.WebsocketClient.MESS * @see dan200.computercraft.core.apis.HTTPAPI#websocket On how to open a websocket. */ public class WebsocketHandle { - private static final CharsetDecoder DECODER = StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPLACE); + private static final ThreadLocal DECODER = ThreadLocal.withInitial(() -> StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPLACE)); private final IAPIEnvironment environment; private final String address; @@ -87,7 +87,7 @@ public class WebsocketHandle { websocket.sendBinary(text); } else { try { - websocket.sendText(DECODER.decode(text).toString()); + websocket.sendText(DECODER.get().decode(text).toString()); } catch (CharacterCodingException e) { // This shouldn't happen, but worth mentioning. throw new LuaException("Message is not valid UTF8"); From 929debd3828a2af34420a7fa83c33599873e6f07 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Wed, 24 Apr 2024 21:49:49 +0100 Subject: [PATCH 2/4] Don't build the webside on Windows/Mac It seems to stall on Mac, and unlike Windows, I don't have access to a machine to debug it :/. --- .github/workflows/main-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main-ci.yml b/.github/workflows/main-ci.yml index 18ab88e49..013ec2ad5 100644 --- a/.github/workflows/main-ci.yml +++ b/.github/workflows/main-ci.yml @@ -97,7 +97,7 @@ jobs: - name: ⚒️ Build run: | - ./gradlew --configure-on-demand :core:assemble :web:assemble + ./gradlew --configure-on-demand :core:assemble - name: 🧪 Run tests run: | From 7c1f40031b8ca65a1adb7ace46e91ea8476e1fb6 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Thu, 25 Apr 2024 18:19:34 +0100 Subject: [PATCH 3/4] 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. --- .../client/gui/ClientInputHandler.java | 12 ++-- .../platform/ClientNetworkContextImpl.java | 4 +- .../client/pocket/ClientPocketComputers.java | 2 +- .../client/pocket/PocketComputerData.java | 8 +-- .../computer/terminal/TerminalState.java | 60 +++++++------------ .../network/client/ClientNetworkContext.java | 4 +- .../network/client/MonitorClientMessage.java | 9 +-- .../client/PocketComputerDataMessage.java | 10 ++-- .../network/server/KeyEventServerMessage.java | 20 +++---- .../server/MouseEventServerMessage.java | 25 ++++---- .../peripheral/monitor/ClientMonitor.java | 4 +- .../monitor/MonitorBlockEntity.java | 2 +- .../peripheral/monitor/MonitorWatcher.java | 6 +- .../peripheral/speaker/EncodedAudio.java | 4 +- 14 files changed, 77 insertions(+), 93 deletions(-) diff --git a/projects/common/src/client/java/dan200/computercraft/client/gui/ClientInputHandler.java b/projects/common/src/client/java/dan200/computercraft/client/gui/ClientInputHandler.java index bb6827616..a69fe39c0 100644 --- a/projects/common/src/client/java/dan200/computercraft/client/gui/ClientInputHandler.java +++ b/projects/common/src/client/java/dan200/computercraft/client/gui/ClientInputHandler.java @@ -49,31 +49,31 @@ public final class ClientInputHandler implements InputHandler { @Override public void keyDown(int key, boolean repeat) { - ClientNetworking.sendToServer(new KeyEventServerMessage(menu, repeat ? KeyEventServerMessage.TYPE_REPEAT : KeyEventServerMessage.TYPE_DOWN, key)); + ClientNetworking.sendToServer(new KeyEventServerMessage(menu, repeat ? KeyEventServerMessage.Action.REPEAT : KeyEventServerMessage.Action.DOWN, key)); } @Override public void keyUp(int key) { - ClientNetworking.sendToServer(new KeyEventServerMessage(menu, KeyEventServerMessage.TYPE_UP, key)); + ClientNetworking.sendToServer(new KeyEventServerMessage(menu, KeyEventServerMessage.Action.UP, key)); } @Override public void mouseClick(int button, int x, int y) { - ClientNetworking.sendToServer(new MouseEventServerMessage(menu, MouseEventServerMessage.TYPE_CLICK, button, x, y)); + ClientNetworking.sendToServer(new MouseEventServerMessage(menu, MouseEventServerMessage.Action.CLICK, button, x, y)); } @Override public void mouseUp(int button, int x, int y) { - ClientNetworking.sendToServer(new MouseEventServerMessage(menu, MouseEventServerMessage.TYPE_UP, button, x, y)); + ClientNetworking.sendToServer(new MouseEventServerMessage(menu, MouseEventServerMessage.Action.UP, button, x, y)); } @Override public void mouseDrag(int button, int x, int y) { - ClientNetworking.sendToServer(new MouseEventServerMessage(menu, MouseEventServerMessage.TYPE_DRAG, button, x, y)); + ClientNetworking.sendToServer(new MouseEventServerMessage(menu, MouseEventServerMessage.Action.DRAG, button, x, y)); } @Override public void mouseScroll(int direction, int x, int y) { - ClientNetworking.sendToServer(new MouseEventServerMessage(menu, MouseEventServerMessage.TYPE_SCROLL, direction, x, y)); + ClientNetworking.sendToServer(new MouseEventServerMessage(menu, MouseEventServerMessage.Action.SCROLL, direction, x, y)); } } diff --git a/projects/common/src/client/java/dan200/computercraft/client/platform/ClientNetworkContextImpl.java b/projects/common/src/client/java/dan200/computercraft/client/platform/ClientNetworkContextImpl.java index 2f1708e90..ca3d9f9e7 100644 --- a/projects/common/src/client/java/dan200/computercraft/client/platform/ClientNetworkContextImpl.java +++ b/projects/common/src/client/java/dan200/computercraft/client/platform/ClientNetworkContextImpl.java @@ -49,7 +49,7 @@ public final class ClientNetworkContextImpl implements ClientNetworkContext { } @Override - public void handleMonitorData(BlockPos pos, TerminalState terminal) { + public void handleMonitorData(BlockPos pos, @Nullable TerminalState terminal) { var player = Minecraft.getInstance().player; if (player == null) return; @@ -67,7 +67,7 @@ public final class ClientNetworkContextImpl implements ClientNetworkContext { } @Override - public void handlePocketComputerData(UUID instanceId, ComputerState state, int lightState, TerminalState terminal) { + public void handlePocketComputerData(UUID instanceId, ComputerState state, int lightState, @Nullable TerminalState terminal) { ClientPocketComputers.setState(instanceId, state, lightState, terminal); } diff --git a/projects/common/src/client/java/dan200/computercraft/client/pocket/ClientPocketComputers.java b/projects/common/src/client/java/dan200/computercraft/client/pocket/ClientPocketComputers.java index cdbb9144b..2d4eef3c7 100644 --- a/projects/common/src/client/java/dan200/computercraft/client/pocket/ClientPocketComputers.java +++ b/projects/common/src/client/java/dan200/computercraft/client/pocket/ClientPocketComputers.java @@ -43,7 +43,7 @@ public final class ClientPocketComputers { * @param lightColour The current colour of the modem light. * @param terminalData The current terminal contents. */ - public static void setState(UUID instanceId, ComputerState state, int lightColour, TerminalState terminalData) { + public static void setState(UUID instanceId, ComputerState state, int lightColour, @Nullable TerminalState terminalData) { var computer = instances.get(instanceId); if (computer == null) { instances.put(instanceId, new PocketComputerData(state, lightColour, terminalData)); diff --git a/projects/common/src/client/java/dan200/computercraft/client/pocket/PocketComputerData.java b/projects/common/src/client/java/dan200/computercraft/client/pocket/PocketComputerData.java index d94de5388..d2e5125ef 100644 --- a/projects/common/src/client/java/dan200/computercraft/client/pocket/PocketComputerData.java +++ b/projects/common/src/client/java/dan200/computercraft/client/pocket/PocketComputerData.java @@ -26,10 +26,10 @@ public final class PocketComputerData { private ComputerState state; private int lightColour; - PocketComputerData(ComputerState state, int lightColour, TerminalState terminalData) { + PocketComputerData(ComputerState state, int lightColour, @Nullable TerminalState terminalData) { this.state = state; this.lightColour = lightColour; - if (terminalData.hasTerminal()) terminal = terminalData.create(); + if (terminalData != null) terminal = terminalData.create(); } public int getLightState() { @@ -44,11 +44,11 @@ public final class PocketComputerData { return state; } - void setState(ComputerState state, int lightColour, TerminalState terminalData) { + void setState(ComputerState state, int lightColour, @Nullable TerminalState terminalData) { this.state = state; this.lightColour = lightColour; - if (terminalData.hasTerminal()) { + if (terminalData != null) { if (terminal == null) { terminal = terminalData.create(); } else { diff --git a/projects/common/src/main/java/dan200/computercraft/shared/computer/terminal/TerminalState.java b/projects/common/src/main/java/dan200/computercraft/shared/computer/terminal/TerminalState.java index 91b25a594..13d23561b 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/computer/terminal/TerminalState.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/computer/terminal/TerminalState.java @@ -7,6 +7,7 @@ package dan200.computercraft.shared.computer.terminal; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import net.minecraft.network.FriendlyByteBuf; +import org.jetbrains.annotations.Contract; import javax.annotation.Nullable; @@ -21,68 +22,49 @@ public class TerminalState { private final boolean colour; private final int width; private final int height; - - @Nullable private final ByteBuf buffer; - public TerminalState(@Nullable NetworkedTerminal terminal) { - if (terminal == null) { - colour = false; - width = height = 0; - buffer = null; - } else { - colour = terminal.isColour(); - width = terminal.getWidth(); - height = terminal.getHeight(); + public TerminalState(NetworkedTerminal terminal) { + colour = terminal.isColour(); + width = terminal.getWidth(); + height = terminal.getHeight(); - var buf = buffer = Unpooled.buffer(); - terminal.write(new FriendlyByteBuf(buf)); - } + var buf = buffer = Unpooled.buffer(); + terminal.write(new FriendlyByteBuf(buf)); + } + + @Contract("null -> null; !null -> !null") + public static @Nullable TerminalState create(@Nullable NetworkedTerminal terminal) { + return terminal == null ? null : new TerminalState(terminal); } public TerminalState(FriendlyByteBuf buf) { colour = buf.readBoolean(); + width = buf.readVarInt(); + height = buf.readVarInt(); - if (buf.readBoolean()) { - width = buf.readVarInt(); - height = buf.readVarInt(); - - var length = buf.readVarInt(); - buffer = buf.readBytes(length); - } else { - width = height = 0; - buffer = null; - } + var length = buf.readVarInt(); + buffer = buf.readBytes(length); } public void write(FriendlyByteBuf buf) { buf.writeBoolean(colour); - - buf.writeBoolean(buffer != null); - if (buffer != null) { - buf.writeVarInt(width); - buf.writeVarInt(height); - buf.writeVarInt(buffer.readableBytes()); - buf.writeBytes(buffer, buffer.readerIndex(), buffer.readableBytes()); - } - } - - public boolean hasTerminal() { - return buffer != null; + buf.writeVarInt(width); + buf.writeVarInt(height); + buf.writeVarInt(buffer.readableBytes()); + buf.writeBytes(buffer, buffer.readerIndex(), buffer.readableBytes()); } public int size() { - return buffer == null ? 0 : buffer.readableBytes(); + return buffer.readableBytes(); } public void apply(NetworkedTerminal terminal) { - if (buffer == null) throw new NullPointerException("buffer"); terminal.resize(width, height); terminal.read(new FriendlyByteBuf(buffer)); } public NetworkedTerminal create() { - if (buffer == null) throw new NullPointerException("Terminal does not exist"); var terminal = new NetworkedTerminal(width, height, colour); terminal.read(new FriendlyByteBuf(buffer)); return terminal; diff --git a/projects/common/src/main/java/dan200/computercraft/shared/network/client/ClientNetworkContext.java b/projects/common/src/main/java/dan200/computercraft/shared/network/client/ClientNetworkContext.java index fce3cad03..d5822536d 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/network/client/ClientNetworkContext.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/network/client/ClientNetworkContext.java @@ -26,11 +26,11 @@ public interface ClientNetworkContext { void handleComputerTerminal(int containerId, TerminalState terminal); - void handleMonitorData(BlockPos pos, TerminalState terminal); + void handleMonitorData(BlockPos pos, @Nullable TerminalState terminal); void handlePlayRecord(BlockPos pos, @Nullable SoundEvent sound, @Nullable String name); - void handlePocketComputerData(UUID instanceId, ComputerState state, int lightState, TerminalState terminal); + void handlePocketComputerData(UUID instanceId, ComputerState state, int lightState, @Nullable TerminalState terminal); void handlePocketComputerDeleted(UUID instanceId); diff --git a/projects/common/src/main/java/dan200/computercraft/shared/network/client/MonitorClientMessage.java b/projects/common/src/main/java/dan200/computercraft/shared/network/client/MonitorClientMessage.java index efe0476c8..97f7c73cd 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/network/client/MonitorClientMessage.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/network/client/MonitorClientMessage.java @@ -11,25 +11,26 @@ import dan200.computercraft.shared.network.NetworkMessages; import net.minecraft.core.BlockPos; import net.minecraft.network.FriendlyByteBuf; +import javax.annotation.Nullable; public class MonitorClientMessage implements NetworkMessage { private final BlockPos pos; - private final TerminalState state; + private final @Nullable TerminalState state; - public MonitorClientMessage(BlockPos pos, TerminalState state) { + public MonitorClientMessage(BlockPos pos, @Nullable TerminalState state) { this.pos = pos; this.state = state; } public MonitorClientMessage(FriendlyByteBuf buf) { pos = buf.readBlockPos(); - state = new TerminalState(buf); + state = buf.readNullable(TerminalState::new); } @Override public void write(FriendlyByteBuf buf) { buf.writeBlockPos(pos); - state.write(buf); + buf.writeNullable(state, (b, t) -> t.write(b)); } @Override diff --git a/projects/common/src/main/java/dan200/computercraft/shared/network/client/PocketComputerDataMessage.java b/projects/common/src/main/java/dan200/computercraft/shared/network/client/PocketComputerDataMessage.java index b4be75181..d9396707d 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/network/client/PocketComputerDataMessage.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/network/client/PocketComputerDataMessage.java @@ -5,7 +5,6 @@ package dan200.computercraft.shared.network.client; import dan200.computercraft.shared.computer.core.ComputerState; -import dan200.computercraft.shared.computer.terminal.NetworkedTerminal; import dan200.computercraft.shared.computer.terminal.TerminalState; import dan200.computercraft.shared.network.MessageType; import dan200.computercraft.shared.network.NetworkMessage; @@ -13,6 +12,7 @@ import dan200.computercraft.shared.network.NetworkMessages; import dan200.computercraft.shared.pocket.core.PocketServerComputer; import net.minecraft.network.FriendlyByteBuf; +import javax.annotation.Nullable; import java.util.UUID; /** @@ -22,20 +22,20 @@ public class PocketComputerDataMessage implements NetworkMessage t.write(b)); } @Override diff --git a/projects/common/src/main/java/dan200/computercraft/shared/network/server/KeyEventServerMessage.java b/projects/common/src/main/java/dan200/computercraft/shared/network/server/KeyEventServerMessage.java index addcddc27..97a17ee3a 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/network/server/KeyEventServerMessage.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/network/server/KeyEventServerMessage.java @@ -12,14 +12,10 @@ import net.minecraft.world.inventory.AbstractContainerMenu; public class KeyEventServerMessage extends ComputerServerMessage { - public static final int TYPE_DOWN = 0; - public static final int TYPE_REPEAT = 1; - public static final int TYPE_UP = 2; - - private final int type; + private final Action type; private final int key; - public KeyEventServerMessage(AbstractContainerMenu menu, int type, int key) { + public KeyEventServerMessage(AbstractContainerMenu menu, Action type, int key) { super(menu); this.type = type; this.key = key; @@ -27,24 +23,24 @@ public class KeyEventServerMessage extends ComputerServerMessage { public KeyEventServerMessage(FriendlyByteBuf buf) { super(buf); - type = buf.readByte(); + type = buf.readEnum(Action.class); key = buf.readVarInt(); } @Override public void write(FriendlyByteBuf buf) { super.write(buf); - buf.writeByte(type); + buf.writeEnum(type); buf.writeVarInt(key); } @Override protected void handle(ServerNetworkContext context, ComputerMenu container) { var input = container.getInput(); - if (type == TYPE_UP) { + if (type == Action.UP) { input.keyUp(key); } else { - input.keyDown(key, type == TYPE_REPEAT); + input.keyDown(key, type == Action.REPEAT); } } @@ -52,4 +48,8 @@ public class KeyEventServerMessage extends ComputerServerMessage { public MessageType type() { return NetworkMessages.KEY_EVENT; } + + public enum Action { + DOWN, REPEAT, UP + } } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/network/server/MouseEventServerMessage.java b/projects/common/src/main/java/dan200/computercraft/shared/network/server/MouseEventServerMessage.java index d76f2e6e2..7a8cddc10 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/network/server/MouseEventServerMessage.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/network/server/MouseEventServerMessage.java @@ -12,17 +12,12 @@ import net.minecraft.world.inventory.AbstractContainerMenu; public class MouseEventServerMessage extends ComputerServerMessage { - public static final int TYPE_CLICK = 0; - public static final int TYPE_DRAG = 1; - public static final int TYPE_UP = 2; - public static final int TYPE_SCROLL = 3; - - private final int type; + private final Action type; private final int x; private final int y; private final int arg; - public MouseEventServerMessage(AbstractContainerMenu menu, int type, int arg, int x, int y) { + public MouseEventServerMessage(AbstractContainerMenu menu, Action type, int arg, int x, int y) { super(menu); this.type = type; this.arg = arg; @@ -32,7 +27,7 @@ public class MouseEventServerMessage extends ComputerServerMessage { public MouseEventServerMessage(FriendlyByteBuf buf) { super(buf); - type = buf.readByte(); + type = buf.readEnum(Action.class); arg = buf.readVarInt(); x = buf.readVarInt(); y = buf.readVarInt(); @@ -41,7 +36,7 @@ public class MouseEventServerMessage extends ComputerServerMessage { @Override public void write(FriendlyByteBuf buf) { super.write(buf); - buf.writeByte(type); + buf.writeEnum(type); buf.writeVarInt(arg); buf.writeVarInt(x); buf.writeVarInt(y); @@ -51,10 +46,10 @@ public class MouseEventServerMessage extends ComputerServerMessage { protected void handle(ServerNetworkContext context, ComputerMenu container) { var input = container.getInput(); switch (type) { - case TYPE_CLICK -> input.mouseClick(arg, x, y); - case TYPE_DRAG -> input.mouseDrag(arg, x, y); - case TYPE_UP -> input.mouseUp(arg, x, y); - case TYPE_SCROLL -> input.mouseScroll(arg, x, y); + case CLICK -> input.mouseClick(arg, x, y); + case DRAG -> input.mouseDrag(arg, x, y); + case UP -> input.mouseUp(arg, x, y); + case SCROLL -> input.mouseScroll(arg, x, y); } } @@ -62,4 +57,8 @@ public class MouseEventServerMessage extends ComputerServerMessage { public MessageType type() { return NetworkMessages.MOUSE_EVENT; } + + public enum Action { + CLICK, DRAG, UP, SCROLL, + } } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/monitor/ClientMonitor.java b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/monitor/ClientMonitor.java index d4f553d81..850379096 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/monitor/ClientMonitor.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/monitor/ClientMonitor.java @@ -54,8 +54,8 @@ public final class ClientMonitor { return terminal; } - void read(TerminalState state) { - if (state.hasTerminal()) { + void read(@Nullable TerminalState state) { + if (state != null) { if (terminal == null) { terminal = state.create(); } else { diff --git a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorBlockEntity.java b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorBlockEntity.java index 4442ba324..ef12d415d 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorBlockEntity.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorBlockEntity.java @@ -223,7 +223,7 @@ public class MonitorBlockEntity extends BlockEntity { if (xIndex == 0 && yIndex == 0 && clientMonitor == null) clientMonitor = new ClientMonitor(this); } - public final void read(TerminalState state) { + public final void read(@Nullable TerminalState state) { if (xIndex != 0 || yIndex != 0) { LOG.warn("Receiving monitor state for non-origin terminal at {}", getBlockPos()); return; diff --git a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorWatcher.java b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorWatcher.java index 499c2acf8..2317f9da9 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorWatcher.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorWatcher.java @@ -68,7 +68,7 @@ public final class MonitorWatcher { var state = getState(tile, monitor); ServerNetworking.sendToAllTracking(new MonitorClientMessage(pos, state), chunk); - limit -= state.size(); + limit -= state == null ? 0 : state.size(); } } @@ -76,9 +76,9 @@ public final class MonitorWatcher { return !monitor.isRemoved() && monitor.getXIndex() == 0 && monitor.getYIndex() == 0 ? monitor.getCachedServerMonitor() : null; } - private static TerminalState getState(MonitorBlockEntity tile, ServerMonitor monitor) { + private static @Nullable TerminalState getState(MonitorBlockEntity tile, ServerMonitor monitor) { var state = tile.cached; - if (state == null) state = tile.cached = new TerminalState(monitor.getTerminal()); + if (state == null) state = tile.cached = TerminalState.create(monitor.getTerminal()); return state; } } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/speaker/EncodedAudio.java b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/speaker/EncodedAudio.java index 1e6ce9b5e..35a853920 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/speaker/EncodedAudio.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/speaker/EncodedAudio.java @@ -21,6 +21,7 @@ public record EncodedAudio(int charge, int strength, boolean previousBit, ByteBu buf.writeVarInt(charge()); buf.writeVarInt(strength()); buf.writeBoolean(previousBit()); + buf.writeVarInt(audio.remaining()); buf.writeBytes(audio().duplicate()); } @@ -29,7 +30,8 @@ public record EncodedAudio(int charge, int strength, boolean previousBit, ByteBu var strength = buf.readVarInt(); var previousBit = buf.readBoolean(); - var bytes = new byte[buf.readableBytes()]; + var length = buf.readVarInt(); + var bytes = new byte[length]; buf.readBytes(bytes); return new EncodedAudio(charge, strength, previousBit, ByteBuffer.wrap(bytes)); From 00e2e2bd2decc73a449ef8613c14cfb70de43281 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Thu, 25 Apr 2024 18:21:23 +0100 Subject: [PATCH 4/4] Switch to vanilla's stillValid implementation --- .../common/AbstractContainerBlockEntity.java | 4 +-- .../blocks/AbstractComputerBlockEntity.java | 7 +++-- .../turtle/blocks/TurtleBlockEntity.java | 5 +-- .../shared/util/BlockEntityHelpers.java | 31 ------------------- 4 files changed, 9 insertions(+), 38 deletions(-) diff --git a/projects/common/src/main/java/dan200/computercraft/shared/common/AbstractContainerBlockEntity.java b/projects/common/src/main/java/dan200/computercraft/shared/common/AbstractContainerBlockEntity.java index 825e4bd7c..d4aca0a79 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/common/AbstractContainerBlockEntity.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/common/AbstractContainerBlockEntity.java @@ -5,9 +5,9 @@ package dan200.computercraft.shared.common; import dan200.computercraft.shared.container.BasicContainer; -import dan200.computercraft.shared.util.BlockEntityHelpers; import net.minecraft.core.BlockPos; import net.minecraft.network.chat.Component; +import net.minecraft.world.Container; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.block.entity.BaseContainerBlockEntity; import net.minecraft.world.level.block.entity.BlockEntity; @@ -29,6 +29,6 @@ public abstract class AbstractContainerBlockEntity extends BaseContainerBlockEnt @Override public boolean stillValid(Player player) { - return BlockEntityHelpers.isUsable(this, player, BlockEntityHelpers.DEFAULT_INTERACT_RANGE); + return Container.stillValidBlockEntity(this, player); } } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/computer/blocks/AbstractComputerBlockEntity.java b/projects/common/src/main/java/dan200/computercraft/shared/computer/blocks/AbstractComputerBlockEntity.java index 60f7e0038..1486505cc 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/computer/blocks/AbstractComputerBlockEntity.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/computer/blocks/AbstractComputerBlockEntity.java @@ -24,6 +24,7 @@ import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket; +import net.minecraft.world.Container; import net.minecraft.world.LockCode; import net.minecraft.world.MenuProvider; import net.minecraft.world.Nameable; @@ -75,13 +76,13 @@ public abstract class AbstractComputerBlockEntity extends BlockEntity implements unload(); } - protected double getInteractRange() { - return BlockEntityHelpers.DEFAULT_INTERACT_RANGE; + protected int getInteractRange() { + return Container.DEFAULT_DISTANCE_LIMIT; } public boolean isUsable(Player player) { return BaseContainerBlockEntity.canUnlock(player, lockCode, getDisplayName()) - && BlockEntityHelpers.isUsable(this, player, getInteractRange()); + && Container.stillValidBlockEntity(this, player, getInteractRange()); } protected void serverTick() { diff --git a/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlockEntity.java b/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlockEntity.java index 2fc0c225d..59c306fbf 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlockEntity.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlockEntity.java @@ -26,6 +26,7 @@ import net.minecraft.core.NonNullList; import net.minecraft.nbt.CompoundTag; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.Container; import net.minecraft.world.ContainerHelper; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; @@ -88,8 +89,8 @@ public class TurtleBlockEntity extends AbstractComputerBlockEntity implements Ba } @Override - protected double getInteractRange() { - return 12.0; + protected int getInteractRange() { + return Container.DEFAULT_DISTANCE_LIMIT + 4; } @Override diff --git a/projects/common/src/main/java/dan200/computercraft/shared/util/BlockEntityHelpers.java b/projects/common/src/main/java/dan200/computercraft/shared/util/BlockEntityHelpers.java index e9fee43e4..4e70c55ec 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/util/BlockEntityHelpers.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/util/BlockEntityHelpers.java @@ -4,24 +4,14 @@ package dan200.computercraft.shared.util; -import dan200.computercraft.shared.platform.PlatformHelper; -import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntityTicker; import net.minecraft.world.level.block.entity.BlockEntityType; -import net.minecraft.world.phys.Vec3; import javax.annotation.Nullable; public final class BlockEntityHelpers { - /** - * The maximum limit a player can be away from a block to still have its UI open. - * - * @see #isUsable(BlockEntity, Player, double) - */ - public static final double DEFAULT_INTERACT_RANGE = 8.0; - private BlockEntityHelpers() { } @@ -33,27 +23,6 @@ public final class BlockEntityHelpers { return actualType == expectedType ? (BlockEntityTicker) ticker : null; } - /** - * Determine if a block entity is "usable" by a player. - * - * @param blockEntity The current block entity. - * @param player The player who is trying to interact with the block. - * @param range The default distance the player can be away. This typically defaults to {@link #DEFAULT_INTERACT_RANGE}, - * but a custom value may be used. If {@link PlatformHelper#getReachDistance(Player)} is larger, - * that will be used instead. - * @return Whether this block entity is usable. - */ - public static boolean isUsable(BlockEntity blockEntity, Player player, double range) { - var level = blockEntity.getLevel(); - var pos = blockEntity.getBlockPos(); - - range = Math.max(range, PlatformHelper.get().getReachDistance(player)); - - return player.isAlive() && player.getCommandSenderWorld() == level && - !blockEntity.isRemoved() && level.getBlockEntity(pos) == blockEntity && - player.distanceToSqr(Vec3.atCenterOf(pos)) <= range * range; - } - /** * Update a block entity, marking it as changed and propagating changes to the client. *