From 25f1a9824bdcba1260f5fff1c1b437825131bd46 Mon Sep 17 00:00:00 2001 From: GeorgeRNG <81434111+GeorgeRNG@users.noreply.github.com> Date: Thu, 25 Jul 2024 19:00:42 +0100 Subject: [PATCH] the api should now correctly handle `/abort` --- .../dev/dfonline/codeclient/Commands.java | 1 + .../codeclient/websocket/SocketHandler.java | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/dev/dfonline/codeclient/Commands.java b/src/main/java/dev/dfonline/codeclient/Commands.java index 001c3dac..09451fcf 100644 --- a/src/main/java/dev/dfonline/codeclient/Commands.java +++ b/src/main/java/dev/dfonline/codeclient/Commands.java @@ -173,6 +173,7 @@ else if(field.getType().isEnum()) { confirm = null; CodeClient.currentAction = new None(); CodeClient.getFeature(Debug.class).ifPresent(Debug::reset); + CodeClient.API.abort(); return 0; })); diff --git a/src/main/java/dev/dfonline/codeclient/websocket/SocketHandler.java b/src/main/java/dev/dfonline/codeclient/websocket/SocketHandler.java index 99eb42d6..f95f424e 100644 --- a/src/main/java/dev/dfonline/codeclient/websocket/SocketHandler.java +++ b/src/main/java/dev/dfonline/codeclient/websocket/SocketHandler.java @@ -3,7 +3,6 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import dev.dfonline.codeclient.Callback; import dev.dfonline.codeclient.CodeClient; -import dev.dfonline.codeclient.Feature; import dev.dfonline.codeclient.Utility; import dev.dfonline.codeclient.action.None; import dev.dfonline.codeclient.action.impl.*; @@ -19,6 +18,7 @@ import net.minecraft.text.Text; import net.minecraft.util.Formatting; import org.java_websocket.WebSocket; +import org.jetbrains.annotations.Nullable; import java.net.InetSocketAddress; import java.util.ArrayList; @@ -31,6 +31,7 @@ public class SocketHandler { public static final int PORT = 31375; private final ArrayList actionQueue = new ArrayList<>(); + @Nullable private WebSocket connection = null; private static final List defaultAuthScopes = List.of(AuthScope.DEFAULT); private List unapprovedAuthScopes = List.of(); @@ -56,6 +57,7 @@ public void stop() { } public void setAcceptedScopes(boolean accepted) { + if(connection == null) return; actionQueue.clear(); if (accepted) { // Add the unapproved scopes & the default scopes @@ -78,6 +80,7 @@ public void setConnection(WebSocket socket) { public void onMessage(String message) { + assert connection != null; String[] arguments = message.split(" "); Action topAction = getTopAction(); if (arguments[0] == null) return; @@ -115,8 +118,9 @@ private Action getTopAction() { return actionQueue.get(actionQueue.size() - 1); } - private void handleScopeRequest(String[] argumets) { - List args = Arrays.asList(argumets).subList(1, argumets.length); + private void handleScopeRequest(String[] arguments) { + assert connection != null; + List args = Arrays.asList(arguments).subList(1, arguments.length); // Send the currently approved scopes if no args are provided if (args.isEmpty()) { @@ -154,7 +158,7 @@ private void handleScopeRequest(String[] argumets) { private void assertScopeLevel(SocketHandler.Action commandClass) { if (!authScopes.contains(commandClass.authScope)) { - connection.send("unauthed"); + if(connection != null) connection.send("unauthed"); return; } actionQueue.add(commandClass); @@ -209,6 +213,11 @@ private void next() { firstAction.start(connection); } + public void abort() { + actionQueue.clear(); + if(connection != null) connection.send("aborted"); + } + private abstract static class Action { public final String name; public final AuthScope authScope; @@ -529,7 +538,7 @@ public void set(WebSocket responder) { @Override public void start(WebSocket responder) { - if (CodeClient.location instanceof Plot && !command.isEmpty()) { + if (CodeClient.location instanceof Plot && !command.isEmpty() && CodeClient.MC.getNetworkHandler() != null) { CodeClient.MC.getNetworkHandler().sendCommand(command); } else { responder.send(CodeClient.location.name());