Compare commits
8
Commits
d73895ac7c
...
8cb458c9bd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8cb458c9bd | ||
|
|
9e3bf14ffa | ||
|
|
2cfd93c197 | ||
|
|
1ddfd9d024 | ||
|
|
5e44a71054 | ||
|
|
5dd4b17db5 | ||
|
|
3379acef80 | ||
|
|
f07750cdcb |
@@ -2,6 +2,7 @@ package org.saturnclient.client;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.saturnclient.client.player.SaturnPlayer;
|
||||
import org.saturnclient.common.provider.Providers;
|
||||
@@ -12,18 +13,55 @@ import org.saturnclient.cosmetics.Hats;
|
||||
import dev.selimaj.session.Session;
|
||||
|
||||
public class ServiceClient {
|
||||
private static Session session;
|
||||
public static UUID uuid;
|
||||
private static volatile Session session;
|
||||
public static volatile UUID uuid;
|
||||
|
||||
public static void initialize() {
|
||||
new Thread(() -> {
|
||||
if (authenticate()) {
|
||||
Cloaks.initialize();
|
||||
Hats.initialize();
|
||||
Providers.saturn.logInfo("Saturn Session initialization complete");
|
||||
} else {
|
||||
Providers.saturn.logError("Failed to authenticate with the server");
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
public static boolean isConnected() {
|
||||
return session != null;
|
||||
}
|
||||
|
||||
public static boolean connectTimeout() {
|
||||
try {
|
||||
session = Session.connect("wss://saturn-server.selimaj.dev", 10, TimeUnit.SECONDS);
|
||||
session = Session.connect("wss://saturn-server.selimaj.dev", 2, TimeUnit.MINUTES);
|
||||
return true;
|
||||
} catch (TimeoutException e) {
|
||||
System.out.println("Session Server timeout");
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean connectMultiTimeout() {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
if (connectTimeout()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Providers.saturn.logError("Failed to authenticate (attempt " + (i + 1) + ")");
|
||||
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean authenticate() {
|
||||
MinecraftClientRef client = Providers.saturn.getClient();
|
||||
|
||||
@@ -49,7 +87,7 @@ public class ServiceClient {
|
||||
|
||||
Providers.saturn.logInfo("Authenticating with UUID: " + uuid);
|
||||
|
||||
if (!connectTimeout()) {
|
||||
if (!connectMultiTimeout()) {
|
||||
Providers.saturn.logError("Unable to authenticate: Session Server Timeout");
|
||||
return false;
|
||||
}
|
||||
@@ -77,6 +115,9 @@ public class ServiceClient {
|
||||
}
|
||||
|
||||
public static void setCloak(String itemId) {
|
||||
if (session == null)
|
||||
return;
|
||||
|
||||
try {
|
||||
session.request(ServiceMethods.SetCloak, itemId).whenComplete((msg, throwable) -> {
|
||||
if (throwable != null) {
|
||||
@@ -96,6 +137,9 @@ public class ServiceClient {
|
||||
}
|
||||
|
||||
public static void setHat(String itemId) {
|
||||
if (session == null)
|
||||
return;
|
||||
|
||||
try {
|
||||
session.request(ServiceMethods.SetHat, itemId).whenComplete((msg, throwable) -> {
|
||||
if (throwable != null) {
|
||||
@@ -115,6 +159,9 @@ public class ServiceClient {
|
||||
}
|
||||
|
||||
public static void buyCloak(String itemId) {
|
||||
if (session == null)
|
||||
return;
|
||||
|
||||
try {
|
||||
session.request(ServiceMethods.BuyCloak, itemId).whenComplete((msg, throwable) -> {
|
||||
if (throwable != null) {
|
||||
@@ -129,6 +176,9 @@ public class ServiceClient {
|
||||
}
|
||||
|
||||
public static void buyHat(String itemId) {
|
||||
if (session == null)
|
||||
return;
|
||||
|
||||
try {
|
||||
session.request(ServiceMethods.BuyHat, itemId).whenComplete((msg, throwable) -> {
|
||||
if (throwable != null) {
|
||||
@@ -143,6 +193,9 @@ public class ServiceClient {
|
||||
}
|
||||
|
||||
public static void emote(String emote) {
|
||||
if (session == null)
|
||||
return;
|
||||
|
||||
try {
|
||||
session.request(ServiceMethods.Emote,
|
||||
new ServiceMethods.Types.EmoteRequest(emote, SaturnPlayer.getExternalUUIDAsString()))
|
||||
@@ -157,6 +210,9 @@ public class ServiceClient {
|
||||
}
|
||||
|
||||
public static void eventHandlers() {
|
||||
if (session == null)
|
||||
return;
|
||||
|
||||
session.onNotification(ServiceMethods.EmoteEvent, (data) -> {
|
||||
if (data == null) {
|
||||
return;
|
||||
@@ -168,11 +224,10 @@ public class ServiceClient {
|
||||
return;
|
||||
}
|
||||
|
||||
if (data.emote() != null && !data.emote().isEmpty()) {
|
||||
Providers.saturn.playEmote(from, data.emote());
|
||||
} else {
|
||||
Providers.saturn.playEmote(from, null);
|
||||
}
|
||||
String emote = data.emote() != null && !data.emote().isEmpty() ? data.emote() : null;
|
||||
|
||||
// Notifications arrive on a background thread.
|
||||
Providers.saturn.getClient().executeOnThread(() -> Providers.saturn.playEmote(from, emote));
|
||||
});
|
||||
|
||||
session.onNotification(ServiceMethods.Player, (player) -> {
|
||||
@@ -181,18 +236,21 @@ public class ServiceClient {
|
||||
});
|
||||
}
|
||||
|
||||
public static SaturnPlayer getPlayer(UUID uuid, String name) {
|
||||
try {
|
||||
/**
|
||||
* Returns the player's Saturn data, or null if the server says they aren't
|
||||
* using Saturn. Throws if the lookup itself fails, so callers can retry.
|
||||
*/
|
||||
public static SaturnPlayer getPlayer(UUID uuid, String name) throws Exception {
|
||||
Session session = ServiceClient.session;
|
||||
|
||||
if (session == null)
|
||||
throw new IllegalStateException("Not connected to the Saturn server");
|
||||
|
||||
ServiceMethods.Types.Player player = session.request(ServiceMethods.GetPlayer, uuid.toString()).join();
|
||||
|
||||
if (player == null)
|
||||
return null;
|
||||
|
||||
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;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
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.common.provider.Providers;
|
||||
|
||||
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;
|
||||
private static Thread WORKER;
|
||||
/** Players queued or being looked up. */
|
||||
private static final Set<UUID> PENDING = ConcurrentHashMap.newKeySet();
|
||||
|
||||
public String cloak = "";
|
||||
public String hat = "";
|
||||
private static final long NOT_SATURN_RECHECK_MS = 60_000;
|
||||
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 String name;
|
||||
|
||||
@@ -30,12 +45,18 @@ public class SaturnPlayer {
|
||||
}
|
||||
|
||||
public static SaturnPlayer get() {
|
||||
if (ServiceClient.uuid == null) {
|
||||
UUID self = ServiceClient.uuid;
|
||||
if (self == 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) {
|
||||
if (uuid == null)
|
||||
return null;
|
||||
@@ -43,9 +64,7 @@ public class SaturnPlayer {
|
||||
SaturnPlayer player = PLAYERS.get(uuid);
|
||||
|
||||
if (player == null) {
|
||||
PLAYERS.put(uuid, null);
|
||||
QUEUE.add(uuid);
|
||||
startPlayerThread();
|
||||
requestLookup(name, uuid);
|
||||
}
|
||||
|
||||
return player;
|
||||
@@ -61,58 +80,46 @@ public class SaturnPlayer {
|
||||
|
||||
public static void set(SaturnPlayer player) {
|
||||
PLAYERS.put(player.uuid, player);
|
||||
NEXT_LOOKUP.remove(player.uuid);
|
||||
}
|
||||
|
||||
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)
|
||||
.toArray(String[]::new);
|
||||
}
|
||||
|
||||
public static synchronized void startPlayerThread() {
|
||||
if (RUNNING)
|
||||
private static void requestLookup(String name, UUID uuid) {
|
||||
if (!ServiceClient.isConnected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
RUNNING = true;
|
||||
Long next = NEXT_LOOKUP.get(uuid);
|
||||
if (next != null && System.currentTimeMillis() < next) {
|
||||
return;
|
||||
}
|
||||
|
||||
WORKER = new Thread(() -> {
|
||||
long lastWorkTime = System.currentTimeMillis();
|
||||
if (!PENDING.add(uuid)) {
|
||||
return;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
UUID uuid = QUEUE.poll();
|
||||
|
||||
if (uuid != null) {
|
||||
LOOKUPS.execute(() -> {
|
||||
try {
|
||||
String name = Providers.saturn.getClient().getPlayerListEntry(uuid);
|
||||
|
||||
SaturnPlayer player = ServiceClient.getPlayer(uuid, name);
|
||||
|
||||
// Apply on main thread
|
||||
Providers.saturn.getClient().executeOnThread(() -> {
|
||||
PLAYERS.put(uuid, player);
|
||||
});
|
||||
|
||||
if (player != null) {
|
||||
set(player);
|
||||
} else {
|
||||
NEXT_LOOKUP.put(uuid, System.currentTimeMillis() + NOT_SATURN_RECHECK_MS);
|
||||
}
|
||||
} catch (Exception 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;
|
||||
|
||||
import org.saturnclient.common.ref.asset.IdentifierRef;
|
||||
import org.saturnclient.common.ref.asset.SpriteRef;
|
||||
|
||||
public interface EffectRef {
|
||||
@@ -7,6 +8,10 @@ public interface EffectRef {
|
||||
|
||||
SpriteRef getIcon();
|
||||
|
||||
default IdentifierRef getIconId() {
|
||||
return null;
|
||||
}
|
||||
|
||||
boolean isInfinite();
|
||||
|
||||
String getInfiniteText();
|
||||
|
||||
@@ -5,6 +5,7 @@ import org.saturnclient.client.player.SaturnPlayer;
|
||||
import org.saturnclient.common.ref.asset.IdentifierRef;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
public class Cloaks {
|
||||
public static final String[] ALL_CLOAKS = {
|
||||
@@ -30,7 +31,8 @@ public class Cloaks {
|
||||
"black_hole_flame",
|
||||
"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() {
|
||||
availableCloaks.add(0, "");
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.saturnclient.cosmetics;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import org.saturnclient.client.ServiceClient;
|
||||
import org.saturnclient.client.player.SaturnPlayer;
|
||||
@@ -9,7 +9,8 @@ import org.saturnclient.client.player.SaturnPlayer;
|
||||
public class Hats {
|
||||
public static final String[] ALL_HATS = { "horns_black", "horns_white", "halo_white", "halo_black", "horns_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() {
|
||||
availableHats.add(0, "");
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.util.List;
|
||||
|
||||
import org.saturnclient.common.provider.FeatureProvider;
|
||||
import org.saturnclient.common.provider.Providers;
|
||||
import org.saturnclient.common.ref.asset.SpriteRef;
|
||||
import org.saturnclient.common.ref.game.EffectRef;
|
||||
import org.saturnclient.config.property.BoolProperty;
|
||||
import org.saturnclient.config.property.Property;
|
||||
@@ -63,7 +64,12 @@ public class StatusEffectsMod extends Mod implements HudMod {
|
||||
if (!effect.shouldShowIcon())
|
||||
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),
|
||||
18, 18 * row + 3,
|
||||
layout.font.value, -1);
|
||||
|
||||
@@ -64,19 +64,18 @@ public abstract class Input extends Element {
|
||||
public void render(RenderScope renderScope, ElementContext ctx) {
|
||||
checkReset();
|
||||
|
||||
renderScope.drawRoundedRectangle(0, 0, width, height, 10, 0xFF000000);
|
||||
renderScope.drawRoundedRectangle(0, 0, width, height, 10, Theme.PRIMARY.value);
|
||||
|
||||
int scrollOffset = getScrollOffset();
|
||||
String visibleText = getVisibleText(scrollOffset);
|
||||
int textColor = focused ? 0xFFFFFF : 0xAAAAAA;
|
||||
renderScope.drawText(0.6f, visibleText, 4, 4, Theme.FONT.value, textColor);
|
||||
renderScope.drawText(0.6f, visibleText, 4, 4, Theme.FONT.value, Theme.PRIMARY_FG.value);
|
||||
|
||||
if (focused) {
|
||||
int cursorX = 4 +
|
||||
(int) (Fonts.getWidth(visibleText.substring(0, cursorPosition - scrollOffset), Theme.FONT.value)
|
||||
* 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