Before switching session versions
This commit is contained in:
@@ -1,11 +1,14 @@
|
|||||||
package org.saturnclient.saturnclient.client;
|
package org.saturnclient.saturnclient.client;
|
||||||
|
|
||||||
import java.net.http.WebSocket;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
import org.saturnclient.saturnclient.SaturnClient;
|
import org.saturnclient.saturnclient.SaturnClient;
|
||||||
import org.saturnclient.saturnclient.cosmetics.Hats;
|
|
||||||
import org.saturnclient.saturnclient.cosmetics.cloaks.Cloaks;
|
|
||||||
|
|
||||||
import dev.selimaj.session.Session;
|
import dev.selimaj.session.Session;
|
||||||
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
|
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
|
||||||
@@ -14,6 +17,29 @@ public class ServiceClient {
|
|||||||
private static Session session;
|
private static Session session;
|
||||||
public static UUID uuid;
|
public static UUID uuid;
|
||||||
|
|
||||||
|
public static boolean connectTimeout() {
|
||||||
|
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||||
|
|
||||||
|
Callable<Session> task = () -> Session.connect("ws://localhost:3000");
|
||||||
|
|
||||||
|
Future<Session> future = executor.submit(task);
|
||||||
|
|
||||||
|
try {
|
||||||
|
session = future.get(10, TimeUnit.SECONDS);
|
||||||
|
System.out.println("Connected!");
|
||||||
|
return true;
|
||||||
|
} catch (TimeoutException e) {
|
||||||
|
System.out.println("Connection timed out!");
|
||||||
|
future.cancel(true);
|
||||||
|
return false;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return false;
|
||||||
|
} finally {
|
||||||
|
executor.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean authenticate() {
|
public static boolean authenticate() {
|
||||||
ClientLifecycleEvents.CLIENT_STOPPING.register(_o -> {
|
ClientLifecycleEvents.CLIENT_STOPPING.register(_o -> {
|
||||||
if (session != null)
|
if (session != null)
|
||||||
@@ -37,6 +63,10 @@ public class ServiceClient {
|
|||||||
|
|
||||||
SaturnClient.LOGGER.info("Authenticating with UUID: " + accessToken);
|
SaturnClient.LOGGER.info("Authenticating with UUID: " + accessToken);
|
||||||
|
|
||||||
|
if (!connectTimeout()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// String cloak = parser.getString("cloak");
|
// String cloak = parser.getString("cloak");
|
||||||
// String hat = parser.getString("hat");
|
// String hat = parser.getString("hat");
|
||||||
|
|
||||||
@@ -52,13 +82,6 @@ public class ServiceClient {
|
|||||||
|
|
||||||
// Cloaks.loadCloak(uuid);
|
// Cloaks.loadCloak(uuid);
|
||||||
|
|
||||||
// playerNames.put(SaturnClient.client.getSession().getUsername(), uuid);
|
|
||||||
|
|
||||||
// afterAuth();
|
|
||||||
|
|
||||||
// startListenerThread();
|
|
||||||
// startPingThread();
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
SaturnClient.LOGGER.error("Authentication failed", e);
|
SaturnClient.LOGGER.error("Authentication failed", e);
|
||||||
|
|||||||
@@ -1,5 +1,36 @@
|
|||||||
package org.saturnclient.saturnclient.client;
|
package org.saturnclient.saturnclient.client;
|
||||||
|
|
||||||
public class ServiceMethods {
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.node.JsonNodeType;
|
||||||
|
import com.fasterxml.jackson.databind.node.TextNode;
|
||||||
|
|
||||||
|
import dev.selimaj.session.types.Method;
|
||||||
|
|
||||||
|
public class ServiceMethods {
|
||||||
|
public static class AuthResponse extends JsonNode {
|
||||||
|
@JsonProperty(required = true)
|
||||||
|
private String token;
|
||||||
|
|
||||||
|
@JsonProperty(required = true)
|
||||||
|
private String userId;
|
||||||
|
|
||||||
|
// Constructor enforcing required fields
|
||||||
|
public AuthResponse(String token, String userId) {
|
||||||
|
this.token = token;
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default constructor needed for Jackson deserialization
|
||||||
|
public AuthResponse() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JsonNodeType getNodeType() {
|
||||||
|
return JsonNodeType.OBJECT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final Method<TextNode, AuthResponse, TextNode> Authenticate = new Method<>("auth",
|
||||||
|
TextNode.class, TextNode.class, TextNode.class);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user