Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3b4e1ab4cb | ||
|
|
8cb458c9bd | ||
|
|
9e3bf14ffa | ||
|
|
2cfd93c197 | ||
|
|
1ddfd9d024 | ||
|
|
5e44a71054 | ||
|
|
5dd4b17db5 | ||
|
|
3379acef80 | ||
|
|
f07750cdcb |
+1
-1
@@ -7,5 +7,5 @@ java {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation "com.github.selimaj-dev:session-java:0.1.6"
|
implementation "dev.selimaj:session-java:0.2.0"
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,11 @@
|
|||||||
package org.saturnclient.client;
|
package org.saturnclient.client;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.CompletionException;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
import org.saturnclient.client.player.SaturnPlayer;
|
import org.saturnclient.client.player.SaturnPlayer;
|
||||||
import org.saturnclient.common.provider.Providers;
|
import org.saturnclient.common.provider.Providers;
|
||||||
@@ -10,154 +14,278 @@ import org.saturnclient.cosmetics.Cloaks;
|
|||||||
import org.saturnclient.cosmetics.Hats;
|
import org.saturnclient.cosmetics.Hats;
|
||||||
|
|
||||||
import dev.selimaj.session.Session;
|
import dev.selimaj.session.Session;
|
||||||
|
import dev.selimaj.session.types.SessionErrorException;
|
||||||
|
|
||||||
public class ServiceClient {
|
public class ServiceClient {
|
||||||
private static Session session;
|
/** Override with -Dsaturn.serverUri=ws://127.0.0.1:8080 to use a local server. */
|
||||||
public static UUID uuid;
|
private static final String SERVER_URI = System.getProperty("saturn.serverUri", "wss://saturn-server.selimaj.dev");
|
||||||
|
private static final long CONNECT_TIMEOUT_SECONDS = 10;
|
||||||
|
private static final Duration REQUEST_TIMEOUT = Duration.ofSeconds(10);
|
||||||
|
private static final long MIN_RECONNECT_DELAY_MS = 1_000;
|
||||||
|
private static final long MAX_RECONNECT_DELAY_MS = 60_000;
|
||||||
|
|
||||||
public static boolean connectTimeout() {
|
/** The authenticated session, or null while disconnected or connecting. */
|
||||||
|
private static volatile Session session;
|
||||||
|
public static volatile UUID uuid;
|
||||||
|
|
||||||
|
private static final AtomicBoolean CONNECTING = new AtomicBoolean();
|
||||||
|
private static volatile boolean stopping = false;
|
||||||
|
|
||||||
|
public static void initialize() {
|
||||||
|
Providers.saturn.getClient().onClientStopping(() -> {
|
||||||
|
stopping = true;
|
||||||
|
Session current = session;
|
||||||
|
session = null;
|
||||||
|
|
||||||
|
if (current != null) {
|
||||||
try {
|
try {
|
||||||
session = Session.connect("wss://saturn-server.selimaj.dev", 10, TimeUnit.SECONDS);
|
current.close();
|
||||||
return true;
|
|
||||||
} catch (Exception e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean authenticate() {
|
|
||||||
MinecraftClientRef client = Providers.saturn.getClient();
|
|
||||||
|
|
||||||
client.onClientStopping(() -> {
|
|
||||||
if (session != null) {
|
|
||||||
try {
|
|
||||||
session.close();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Providers.saturn.logError("Unable to close Saturn session", e);
|
Providers.saturn.logError("Unable to close Saturn session", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
connectInBackground();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isConnected() {
|
||||||
|
return session != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Starts the connect loop unless one is already running. */
|
||||||
|
private static void connectInBackground() {
|
||||||
|
if (stopping || !CONNECTING.compareAndSet(false, true)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Thread thread = new Thread(() -> {
|
||||||
|
boolean connected = false;
|
||||||
try {
|
try {
|
||||||
|
connected = connectLoop();
|
||||||
|
} finally {
|
||||||
|
CONNECTING.set(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// A drop between connecting and clearing CONNECTING couldn't start a new loop.
|
||||||
|
if (connected && session == null) {
|
||||||
|
connectInBackground();
|
||||||
|
}
|
||||||
|
}, "Saturn-Connect");
|
||||||
|
thread.setDaemon(true);
|
||||||
|
thread.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connects and authenticates, retrying with exponential backoff. Returns
|
||||||
|
* whether it connected.
|
||||||
|
*/
|
||||||
|
private static boolean connectLoop() {
|
||||||
|
long delay = MIN_RECONNECT_DELAY_MS;
|
||||||
|
|
||||||
|
while (!stopping) {
|
||||||
|
switch (connectOnce()) {
|
||||||
|
case CONNECTED -> {
|
||||||
|
Providers.saturn.logInfo("Saturn Session initialization complete");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case GIVE_UP -> {
|
||||||
|
Providers.saturn.logError("Failed to authenticate with the server");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
case RETRY -> {
|
||||||
|
Providers.saturn.logError("Saturn server unavailable, retrying in " + delay / 1000 + "s");
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(delay);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
delay = Math.min(delay * 2, MAX_RECONNECT_DELAY_MS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum ConnectResult {
|
||||||
|
CONNECTED,
|
||||||
|
/** Retrying cannot help, e.g. no Minecraft session or the token was rejected. */
|
||||||
|
GIVE_UP,
|
||||||
|
RETRY
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ConnectResult connectOnce() {
|
||||||
|
MinecraftClientRef client = Providers.saturn.getClient();
|
||||||
|
|
||||||
String accessToken = client.getAccessToken();
|
String accessToken = client.getAccessToken();
|
||||||
uuid = client.getUuid();
|
UUID playerUuid = client.getUuid();
|
||||||
String username = client.getUsername();
|
String username = client.getUsername();
|
||||||
|
|
||||||
if (accessToken == null || uuid == null || username == null) {
|
if (accessToken == null || playerUuid == null || username == null) {
|
||||||
Providers.saturn.logError("No active Minecraft session found");
|
Providers.saturn.logError("No active Minecraft session found");
|
||||||
return false;
|
return ConnectResult.GIVE_UP;
|
||||||
}
|
}
|
||||||
|
|
||||||
Providers.saturn.logInfo("Authenticating with UUID: " + uuid);
|
uuid = playerUuid;
|
||||||
|
Providers.saturn.logInfo("Authenticating with UUID: " + playerUuid);
|
||||||
|
|
||||||
if (!connectTimeout()) {
|
Session s;
|
||||||
Providers.saturn.logError("Unable to authenticate: Session Server Timeout");
|
try {
|
||||||
return false;
|
s = Session.connect(SERVER_URI, CONNECT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
||||||
}
|
|
||||||
|
|
||||||
ServiceMethods.Types.Player response = session.request(ServiceMethods.Authenticate, accessToken)
|
|
||||||
.get();
|
|
||||||
|
|
||||||
for (String availableCloak : response.cloaks()) {
|
|
||||||
Cloaks.availableCloaks.add(availableCloak);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (String availableHat : response.hats()) {
|
|
||||||
Hats.availableHats.add(availableHat);
|
|
||||||
}
|
|
||||||
|
|
||||||
eventHandlers();
|
|
||||||
|
|
||||||
SaturnPlayer.set(new SaturnPlayer(uuid, username, response.cloak(), response.hat()));
|
|
||||||
|
|
||||||
return true;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Providers.saturn.logError("Authentication failed", e);
|
Providers.saturn.logError("Unable to connect to the Saturn server: " + rootMessage(e));
|
||||||
return false;
|
return ConnectResult.RETRY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ServiceMethods.Types.Player response;
|
||||||
|
try {
|
||||||
|
response = s.request(ServiceMethods.Authenticate, accessToken, REQUEST_TIMEOUT).get();
|
||||||
|
} catch (ExecutionException e) {
|
||||||
|
s.close();
|
||||||
|
|
||||||
|
if (e.getCause() instanceof SessionErrorException rejected) {
|
||||||
|
Providers.saturn.logError("Saturn server rejected authentication: " + rejected.getMessage());
|
||||||
|
return ConnectResult.GIVE_UP;
|
||||||
|
}
|
||||||
|
|
||||||
|
Providers.saturn.logError("Authentication failed: " + rootMessage(e));
|
||||||
|
return ConnectResult.RETRY;
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
s.close();
|
||||||
|
return ConnectResult.GIVE_UP;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cloaks.availableCloaks.clear();
|
||||||
|
Cloaks.availableCloaks.addAll(java.util.List.of(response.cloaks()));
|
||||||
|
Cloaks.initialize();
|
||||||
|
|
||||||
|
Hats.availableHats.clear();
|
||||||
|
Hats.availableHats.addAll(java.util.List.of(response.hats()));
|
||||||
|
Hats.initialize();
|
||||||
|
|
||||||
|
// Other players may have changed cosmetics while we were disconnected.
|
||||||
|
SaturnPlayer.clear();
|
||||||
|
SaturnPlayer.set(new SaturnPlayer(playerUuid, username, response.cloak(), response.hat()));
|
||||||
|
|
||||||
|
eventHandlers(s);
|
||||||
|
|
||||||
|
if (stopping) {
|
||||||
|
s.close();
|
||||||
|
return ConnectResult.GIVE_UP;
|
||||||
|
}
|
||||||
|
|
||||||
|
session = s;
|
||||||
|
// Registered after publishing so an immediate drop still clears it.
|
||||||
|
s.onClose(() -> onDisconnected(s));
|
||||||
|
return ConnectResult.CONNECTED;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void onDisconnected(Session closed) {
|
||||||
|
if (session != closed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
session = null;
|
||||||
|
|
||||||
|
if (!stopping) {
|
||||||
|
Providers.saturn.logError("Disconnected from the Saturn server, reconnecting");
|
||||||
|
connectInBackground();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String rootMessage(Throwable e) {
|
||||||
|
while ((e instanceof CompletionException || e instanceof ExecutionException) && e.getCause() != null) {
|
||||||
|
e = e.getCause();
|
||||||
|
}
|
||||||
|
return e.getClass().getSimpleName() + ": " + e.getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setCloak(String itemId) {
|
public static void setCloak(String itemId) {
|
||||||
try {
|
Session s = session;
|
||||||
session.request(ServiceMethods.SetCloak, itemId).whenComplete((msg, throwable) -> {
|
if (s == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
s.request(ServiceMethods.SetCloak, itemId, REQUEST_TIMEOUT).whenComplete((msg, throwable) -> {
|
||||||
if (throwable != null) {
|
if (throwable != null) {
|
||||||
throwable.printStackTrace();
|
Providers.saturn.logError("Failed to set cloak (service): " + rootMessage(throwable));
|
||||||
} else {
|
} else {
|
||||||
try {
|
sendPlayer(s);
|
||||||
session.request(ServiceMethods.SendPlayer,
|
|
||||||
new ServiceMethods.Types.SendPlayerRequest(SaturnPlayer.getExternalUUIDAsString()));
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (Exception e) {
|
|
||||||
Providers.saturn.logError("Failed to set cloak (service): ", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setHat(String itemId) {
|
public static void setHat(String itemId) {
|
||||||
try {
|
Session s = session;
|
||||||
session.request(ServiceMethods.SetHat, itemId).whenComplete((msg, throwable) -> {
|
if (s == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
s.request(ServiceMethods.SetHat, itemId, REQUEST_TIMEOUT).whenComplete((msg, throwable) -> {
|
||||||
if (throwable != null) {
|
if (throwable != null) {
|
||||||
throwable.printStackTrace();
|
Providers.saturn.logError("Failed to set hat (service): " + rootMessage(throwable));
|
||||||
} else {
|
} else {
|
||||||
try {
|
sendPlayer(s);
|
||||||
session.request(ServiceMethods.SendPlayer,
|
|
||||||
new ServiceMethods.Types.SendPlayerRequest(SaturnPlayer.getExternalUUIDAsString()));
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (Exception e) {
|
|
||||||
Providers.saturn.logError("Failed to set hat (service): ", e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Tells nearby Saturn players about our current cosmetics. */
|
||||||
|
private static void sendPlayer(Session s) {
|
||||||
|
s.request(ServiceMethods.SendPlayer,
|
||||||
|
new ServiceMethods.Types.SendPlayerRequest(SaturnPlayer.getExternalUUIDAsString()),
|
||||||
|
REQUEST_TIMEOUT).whenComplete((msg, throwable) -> {
|
||||||
|
if (throwable != null) {
|
||||||
|
Providers.saturn.logError("Failed to send player (service): " + rootMessage(throwable));
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void buyCloak(String itemId) {
|
public static void buyCloak(String itemId) {
|
||||||
try {
|
Session s = session;
|
||||||
session.request(ServiceMethods.BuyCloak, itemId).whenComplete((msg, throwable) -> {
|
if (s == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
s.request(ServiceMethods.BuyCloak, itemId, REQUEST_TIMEOUT).whenComplete((msg, throwable) -> {
|
||||||
if (throwable != null) {
|
if (throwable != null) {
|
||||||
throwable.printStackTrace();
|
Providers.saturn.logError("Failed to buy cloak (service): " + rootMessage(throwable));
|
||||||
} else {
|
} else if (!Cloaks.availableCloaks.contains(itemId)) {
|
||||||
Cloaks.availableCloaks.add(itemId);
|
Cloaks.availableCloaks.add(itemId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (Exception e) {
|
|
||||||
Providers.saturn.logError("Failed to buy cloak (service): ", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void buyHat(String itemId) {
|
public static void buyHat(String itemId) {
|
||||||
try {
|
Session s = session;
|
||||||
session.request(ServiceMethods.BuyHat, itemId).whenComplete((msg, throwable) -> {
|
if (s == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
s.request(ServiceMethods.BuyHat, itemId, REQUEST_TIMEOUT).whenComplete((msg, throwable) -> {
|
||||||
if (throwable != null) {
|
if (throwable != null) {
|
||||||
throwable.printStackTrace();
|
Providers.saturn.logError("Failed to buy hat (service): " + rootMessage(throwable));
|
||||||
} else {
|
} else if (!Hats.availableHats.contains(itemId)) {
|
||||||
Hats.availableHats.add(itemId);
|
Hats.availableHats.add(itemId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (Exception e) {
|
|
||||||
Providers.saturn.logError("Failed to buy hat (service): ", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void emote(String emote) {
|
public static void emote(String emote) {
|
||||||
try {
|
Session s = session;
|
||||||
session.request(ServiceMethods.Emote,
|
if (s == null)
|
||||||
new ServiceMethods.Types.EmoteRequest(emote, SaturnPlayer.getExternalUUIDAsString()))
|
return;
|
||||||
.whenComplete((msg, throwable) -> {
|
|
||||||
|
s.request(ServiceMethods.Emote,
|
||||||
|
new ServiceMethods.Types.EmoteRequest(emote, SaturnPlayer.getExternalUUIDAsString()),
|
||||||
|
REQUEST_TIMEOUT).whenComplete((msg, throwable) -> {
|
||||||
if (throwable != null) {
|
if (throwable != null) {
|
||||||
throwable.printStackTrace();
|
Providers.saturn.logError("Failed to emote (service): " + rootMessage(throwable));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (Exception e) {
|
|
||||||
Providers.saturn.logError("Failed to emote (service): ", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void eventHandlers() {
|
private static void eventHandlers(Session s) {
|
||||||
session.onNotification(ServiceMethods.EmoteEvent, (data) -> {
|
s.onNotification(ServiceMethods.EmoteEvent, (data) -> {
|
||||||
if (data == null) {
|
if (data == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -168,31 +296,34 @@ public class ServiceClient {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.emote() != null && !data.emote().isEmpty()) {
|
String emote = data.emote() != null && !data.emote().isEmpty() ? data.emote() : null;
|
||||||
Providers.saturn.playEmote(from, data.emote());
|
|
||||||
} else {
|
// Notifications arrive on a background thread.
|
||||||
Providers.saturn.playEmote(from, null);
|
Providers.saturn.getClient().executeOnThread(() -> Providers.saturn.playEmote(from, emote));
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
session.onNotification(ServiceMethods.Player, (player) -> {
|
s.onNotification(ServiceMethods.Player, (player) -> {
|
||||||
System.out.println(player);
|
|
||||||
SaturnPlayer.set(player.toSaturnPlayer());
|
SaturnPlayer.set(player.toSaturnPlayer());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SaturnPlayer getPlayer(UUID uuid, String name) {
|
/**
|
||||||
try {
|
* Returns the player's Saturn data, or null if the server says they aren't
|
||||||
ServiceMethods.Types.Player player = session.request(ServiceMethods.GetPlayer, uuid.toString()).join();
|
* using Saturn. Throws if the lookup itself fails or times out, so callers
|
||||||
|
* can retry.
|
||||||
|
*/
|
||||||
|
public static SaturnPlayer getPlayer(UUID uuid, String name) throws Exception {
|
||||||
|
Session s = session;
|
||||||
|
|
||||||
|
if (s == null)
|
||||||
|
throw new IllegalStateException("Not connected to the Saturn server");
|
||||||
|
|
||||||
|
ServiceMethods.Types.Player player = s.request(ServiceMethods.GetPlayer, uuid.toString(), REQUEST_TIMEOUT)
|
||||||
|
.join();
|
||||||
|
|
||||||
if (player == null)
|
if (player == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
return player.toSaturnPlayer(uuid, name);
|
return player.toSaturnPlayer(uuid, name);
|
||||||
} catch (Exception e) {
|
|
||||||
Providers.saturn.logError("Failed to get player", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +1,39 @@
|
|||||||
package org.saturnclient.client.player;
|
package org.saturnclient.client.player;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Queue;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
import org.saturnclient.client.ServiceClient;
|
import org.saturnclient.client.ServiceClient;
|
||||||
import org.saturnclient.common.provider.Providers;
|
import org.saturnclient.common.provider.Providers;
|
||||||
|
|
||||||
public class SaturnPlayer {
|
public class SaturnPlayer {
|
||||||
private static final Map<UUID, SaturnPlayer> PLAYERS = new HashMap<>();
|
/** Players known to be using Saturn. */
|
||||||
|
private static final Map<UUID, SaturnPlayer> PLAYERS = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private static final Queue<UUID> QUEUE = new ConcurrentLinkedQueue<>();
|
/**
|
||||||
|
* Earliest time (ms) a player without a Saturn entry may be looked up again,
|
||||||
|
* so non-Saturn players aren't re-requested every frame.
|
||||||
|
*/
|
||||||
|
private static final Map<UUID, Long> NEXT_LOOKUP = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private static volatile boolean RUNNING = false;
|
/** Players queued or being looked up. */
|
||||||
private static Thread WORKER;
|
private static final Set<UUID> PENDING = ConcurrentHashMap.newKeySet();
|
||||||
|
|
||||||
public String cloak = "";
|
private static final long NOT_SATURN_RECHECK_MS = 60_000;
|
||||||
public String hat = "";
|
private static final long FAILED_LOOKUP_RETRY_MS = 5_000;
|
||||||
|
|
||||||
|
private static final ExecutorService LOOKUPS = Executors.newSingleThreadExecutor(runnable -> {
|
||||||
|
Thread thread = new Thread(runnable, "SaturnPlayer-Worker");
|
||||||
|
thread.setDaemon(true);
|
||||||
|
return thread;
|
||||||
|
});
|
||||||
|
|
||||||
|
public volatile String cloak = "";
|
||||||
|
public volatile String hat = "";
|
||||||
public final UUID uuid;
|
public final UUID uuid;
|
||||||
public final String name;
|
public final String name;
|
||||||
|
|
||||||
@@ -30,12 +45,18 @@ public class SaturnPlayer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static SaturnPlayer get() {
|
public static SaturnPlayer get() {
|
||||||
if (ServiceClient.uuid == null) {
|
UUID self = ServiceClient.uuid;
|
||||||
|
if (self == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return PLAYERS.get(ServiceClient.uuid);
|
return PLAYERS.get(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the player if they are known to use Saturn, otherwise null. An
|
||||||
|
* unknown player is looked up in the background, at most once at a time and
|
||||||
|
* no more often than {@link #NEXT_LOOKUP} allows. Safe to call every frame.
|
||||||
|
*/
|
||||||
public static SaturnPlayer get(String name, UUID uuid) {
|
public static SaturnPlayer get(String name, UUID uuid) {
|
||||||
if (uuid == null)
|
if (uuid == null)
|
||||||
return null;
|
return null;
|
||||||
@@ -43,9 +64,7 @@ public class SaturnPlayer {
|
|||||||
SaturnPlayer player = PLAYERS.get(uuid);
|
SaturnPlayer player = PLAYERS.get(uuid);
|
||||||
|
|
||||||
if (player == null) {
|
if (player == null) {
|
||||||
PLAYERS.put(uuid, null);
|
requestLookup(name, uuid);
|
||||||
QUEUE.add(uuid);
|
|
||||||
startPlayerThread();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return player;
|
return player;
|
||||||
@@ -59,60 +78,54 @@ public class SaturnPlayer {
|
|||||||
return get(name, Providers.saturn.getClient().getPlayerListEntryUUID(name));
|
return get(name, Providers.saturn.getClient().getPlayerListEntryUUID(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Forgets every cached player, e.g. after reconnecting to the server. */
|
||||||
|
public static void clear() {
|
||||||
|
PLAYERS.clear();
|
||||||
|
NEXT_LOOKUP.clear();
|
||||||
|
}
|
||||||
|
|
||||||
public static void set(SaturnPlayer player) {
|
public static void set(SaturnPlayer player) {
|
||||||
PLAYERS.put(player.uuid, player);
|
PLAYERS.put(player.uuid, player);
|
||||||
|
NEXT_LOOKUP.remove(player.uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String[] getExternalUUIDAsString() {
|
public static String[] getExternalUUIDAsString() {
|
||||||
return PLAYERS.keySet().stream().filter(id -> ServiceClient.uuid == null || !id.equals(ServiceClient.uuid))
|
UUID self = ServiceClient.uuid;
|
||||||
|
|
||||||
|
return PLAYERS.keySet().stream().filter(id -> self == null || !id.equals(self))
|
||||||
.map(UUID::toString)
|
.map(UUID::toString)
|
||||||
.toArray(String[]::new);
|
.toArray(String[]::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized void startPlayerThread() {
|
private static void requestLookup(String name, UUID uuid) {
|
||||||
if (RUNNING)
|
if (!ServiceClient.isConnected()) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
RUNNING = true;
|
Long next = NEXT_LOOKUP.get(uuid);
|
||||||
|
if (next != null && System.currentTimeMillis() < next) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
WORKER = new Thread(() -> {
|
if (!PENDING.add(uuid)) {
|
||||||
long lastWorkTime = System.currentTimeMillis();
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
while (true) {
|
LOOKUPS.execute(() -> {
|
||||||
UUID uuid = QUEUE.poll();
|
|
||||||
|
|
||||||
if (uuid != null) {
|
|
||||||
try {
|
try {
|
||||||
String name = Providers.saturn.getClient().getPlayerListEntry(uuid);
|
|
||||||
|
|
||||||
SaturnPlayer player = ServiceClient.getPlayer(uuid, name);
|
SaturnPlayer player = ServiceClient.getPlayer(uuid, name);
|
||||||
|
|
||||||
// Apply on main thread
|
if (player != null) {
|
||||||
Providers.saturn.getClient().executeOnThread(() -> {
|
set(player);
|
||||||
PLAYERS.put(uuid, player);
|
} else {
|
||||||
});
|
NEXT_LOOKUP.put(uuid, System.currentTimeMillis() + NOT_SATURN_RECHECK_MS);
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Providers.saturn.logError("Failed to fetch player " + uuid, e);
|
Providers.saturn.logError("Failed to fetch player " + uuid, e);
|
||||||
|
NEXT_LOOKUP.put(uuid, System.currentTimeMillis() + FAILED_LOOKUP_RETRY_MS);
|
||||||
|
} finally {
|
||||||
|
PENDING.remove(uuid);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
lastWorkTime = System.currentTimeMillis();
|
|
||||||
} else {
|
|
||||||
if (System.currentTimeMillis() - lastWorkTime > 5000) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
Thread.sleep(50);
|
|
||||||
} catch (InterruptedException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
RUNNING = false;
|
|
||||||
}, "SaturnPlayer-Worker");
|
|
||||||
|
|
||||||
WORKER.setDaemon(true);
|
|
||||||
WORKER.start();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.saturnclient.common.ref.game;
|
package org.saturnclient.common.ref.game;
|
||||||
|
|
||||||
|
import org.saturnclient.common.ref.asset.IdentifierRef;
|
||||||
import org.saturnclient.common.ref.asset.SpriteRef;
|
import org.saturnclient.common.ref.asset.SpriteRef;
|
||||||
|
|
||||||
public interface EffectRef {
|
public interface EffectRef {
|
||||||
@@ -7,6 +8,10 @@ public interface EffectRef {
|
|||||||
|
|
||||||
SpriteRef getIcon();
|
SpriteRef getIcon();
|
||||||
|
|
||||||
|
default IdentifierRef getIconId() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
boolean isInfinite();
|
boolean isInfinite();
|
||||||
|
|
||||||
String getInfiniteText();
|
String getInfiniteText();
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import org.saturnclient.client.player.SaturnPlayer;
|
|||||||
import org.saturnclient.common.ref.asset.IdentifierRef;
|
import org.saturnclient.common.ref.asset.IdentifierRef;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
|
||||||
public class Cloaks {
|
public class Cloaks {
|
||||||
public static final String[] ALL_CLOAKS = {
|
public static final String[] ALL_CLOAKS = {
|
||||||
@@ -30,7 +31,8 @@ public class Cloaks {
|
|||||||
"black_hole_flame",
|
"black_hole_flame",
|
||||||
"black_hole_white" };
|
"black_hole_white" };
|
||||||
|
|
||||||
public static final List<String> availableCloaks = new ArrayList<>();
|
// Written by network callbacks, read by the UI thread.
|
||||||
|
public static final List<String> availableCloaks = new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
public static void initialize() {
|
public static void initialize() {
|
||||||
availableCloaks.add(0, "");
|
availableCloaks.add(0, "");
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package org.saturnclient.cosmetics;
|
package org.saturnclient.cosmetics;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
|
||||||
import org.saturnclient.client.ServiceClient;
|
import org.saturnclient.client.ServiceClient;
|
||||||
import org.saturnclient.client.player.SaturnPlayer;
|
import org.saturnclient.client.player.SaturnPlayer;
|
||||||
@@ -9,7 +9,8 @@ import org.saturnclient.client.player.SaturnPlayer;
|
|||||||
public class Hats {
|
public class Hats {
|
||||||
public static final String[] ALL_HATS = { "horns_black", "horns_white", "halo_white", "halo_black", "horns_end",
|
public static final String[] ALL_HATS = { "horns_black", "horns_white", "halo_white", "halo_black", "horns_end",
|
||||||
"halo_end", "bucket_black", "bucket_end" };
|
"halo_end", "bucket_black", "bucket_end" };
|
||||||
public static List<String> availableHats = new ArrayList<>();
|
// Written by network callbacks, read by the UI thread.
|
||||||
|
public static List<String> availableHats = new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
public static void initialize() {
|
public static void initialize() {
|
||||||
availableHats.add(0, "");
|
availableHats.add(0, "");
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import java.util.List;
|
|||||||
|
|
||||||
import org.saturnclient.common.provider.FeatureProvider;
|
import org.saturnclient.common.provider.FeatureProvider;
|
||||||
import org.saturnclient.common.provider.Providers;
|
import org.saturnclient.common.provider.Providers;
|
||||||
|
import org.saturnclient.common.ref.asset.SpriteRef;
|
||||||
import org.saturnclient.common.ref.game.EffectRef;
|
import org.saturnclient.common.ref.game.EffectRef;
|
||||||
import org.saturnclient.config.property.BoolProperty;
|
import org.saturnclient.config.property.BoolProperty;
|
||||||
import org.saturnclient.config.property.Property;
|
import org.saturnclient.config.property.Property;
|
||||||
@@ -63,7 +64,12 @@ public class StatusEffectsMod extends Mod implements HudMod {
|
|||||||
if (!effect.shouldShowIcon())
|
if (!effect.shouldShowIcon())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
scope.drawSpriteStretched(effect.getIcon(), 0, 18 * row, 16, 16);
|
SpriteRef sprite = effect.getIcon();
|
||||||
|
if (sprite == null) {
|
||||||
|
scope.drawTexture(effect.getIconId(), 0, 18 * row, 0, 0, 16, 16);
|
||||||
|
} else {
|
||||||
|
scope.drawSpriteStretched(sprite, 0, 18 * row, 16, 16);
|
||||||
|
}
|
||||||
scope.drawText(0.5f, durationString(effect),
|
scope.drawText(0.5f, durationString(effect),
|
||||||
18, 18 * row + 3,
|
18, 18 * row + 3,
|
||||||
layout.font.value, -1);
|
layout.font.value, -1);
|
||||||
|
|||||||
@@ -64,19 +64,18 @@ public abstract class Input extends Element {
|
|||||||
public void render(RenderScope renderScope, ElementContext ctx) {
|
public void render(RenderScope renderScope, ElementContext ctx) {
|
||||||
checkReset();
|
checkReset();
|
||||||
|
|
||||||
renderScope.drawRoundedRectangle(0, 0, width, height, 10, 0xFF000000);
|
renderScope.drawRoundedRectangle(0, 0, width, height, 10, Theme.PRIMARY.value);
|
||||||
|
|
||||||
int scrollOffset = getScrollOffset();
|
int scrollOffset = getScrollOffset();
|
||||||
String visibleText = getVisibleText(scrollOffset);
|
String visibleText = getVisibleText(scrollOffset);
|
||||||
int textColor = focused ? 0xFFFFFF : 0xAAAAAA;
|
renderScope.drawText(0.6f, visibleText, 4, 4, Theme.FONT.value, Theme.PRIMARY_FG.value);
|
||||||
renderScope.drawText(0.6f, visibleText, 4, 4, Theme.FONT.value, textColor);
|
|
||||||
|
|
||||||
if (focused) {
|
if (focused) {
|
||||||
int cursorX = 4 +
|
int cursorX = 4 +
|
||||||
(int) (Fonts.getWidth(visibleText.substring(0, cursorPosition - scrollOffset), Theme.FONT.value)
|
(int) (Fonts.getWidth(visibleText.substring(0, cursorPosition - scrollOffset), Theme.FONT.value)
|
||||||
* 0.6f);
|
* 0.6f);
|
||||||
|
|
||||||
renderScope.drawRectangle(cursorX, 2, 1, height - 4, 0xFFFFFFFF);
|
renderScope.drawRectangle(cursorX, 2, 1, height - 4, Theme.ACCENT.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user