Skip to content

Commit

Permalink
the api should now correctly handle /abort
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeRNG committed Jul 25, 2024
1 parent 500d6c2 commit 25f1a98
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/main/java/dev/dfonline/codeclient/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}));

Expand Down
19 changes: 14 additions & 5 deletions src/main/java/dev/dfonline/codeclient/websocket/SocketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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;
Expand All @@ -31,6 +31,7 @@
public class SocketHandler {
public static final int PORT = 31375;
private final ArrayList<Action> actionQueue = new ArrayList<>();
@Nullable
private WebSocket connection = null;
private static final List<AuthScope> defaultAuthScopes = List.of(AuthScope.DEFAULT);
private List<AuthScope> unapprovedAuthScopes = List.of();
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -115,8 +118,9 @@ private Action getTopAction() {
return actionQueue.get(actionQueue.size() - 1);
}

private void handleScopeRequest(String[] argumets) {
List<String> args = Arrays.asList(argumets).subList(1, argumets.length);
private void handleScopeRequest(String[] arguments) {
assert connection != null;
List<String> args = Arrays.asList(arguments).subList(1, arguments.length);

// Send the currently approved scopes if no args are provided
if (args.isEmpty()) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 25f1a98

Please sign in to comment.