From 3c468536f448b3c2cec4bd8701c1047989dc8e59 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sun, 13 Apr 2025 13:30:45 +0200 Subject: [PATCH] networking fix --- .../saturnclient/saturnclient/auth/Auth.java | 4 +- .../saturnclient/auth/Network.java | 15 ++- .../saturnclient/cosmetics/cloaks/Cloaks.java | 95 ++++++++++++------- 3 files changed, 71 insertions(+), 43 deletions(-) diff --git a/src/main/java/org/saturnclient/saturnclient/auth/Auth.java b/src/main/java/org/saturnclient/saturnclient/auth/Auth.java index eadd851..753bd15 100644 --- a/src/main/java/org/saturnclient/saturnclient/auth/Auth.java +++ b/src/main/java/org/saturnclient/saturnclient/auth/Auth.java @@ -53,6 +53,8 @@ public class Auth { players.put(uuid, new SaturnPlayer(cloak, hat)); + Cloaks.loadCloak(uuid); + playerNames.put(client.getSession().getUsername(), uuid); afterAuth(); @@ -90,7 +92,7 @@ public class Auth { String cloak = parser.getString("cloak"); String hat = parser.getString("hat"); players.put(uuid, new SaturnPlayer(cloak, hat)); - Cloaks.setCloakSilent(uuid, cloak); + Cloaks.loadCloak(uuid); } else { playerNames.entrySet().removeIf(entry -> entry.getValue().equals(uuid)); } diff --git a/src/main/java/org/saturnclient/saturnclient/auth/Network.java b/src/main/java/org/saturnclient/saturnclient/auth/Network.java index 33cc245..4a61515 100644 --- a/src/main/java/org/saturnclient/saturnclient/auth/Network.java +++ b/src/main/java/org/saturnclient/saturnclient/auth/Network.java @@ -11,7 +11,7 @@ import java.security.spec.X509EncodedKeySpec; public class Network { static Socket socket; - static DataOutputStream out; + static PrintWriter out; static BufferedReader in; static byte[] key = new byte[32]; @@ -37,8 +37,9 @@ public class Network { // Generate AES key new SecureRandom().nextBytes(key); - socket = new Socket("localhost", 8080); - out = new DataOutputStream(socket.getOutputStream()); + socket = new Socket("77.247.92.168", 8080); + // out = new DataOutputStream(socket.getOutputStream()); + out = new PrintWriter(socket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); StringBuilder publicKeyBuilder = new StringBuilder(); @@ -62,16 +63,14 @@ public class Network { rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedKey = rsaCipher.doFinal(key); - out.write(Base64.getEncoder().encode(encryptedKey)); - out.write('\n'); + out.println(Base64.getEncoder().encodeToString(encryptedKey)); } public static void write(String message) { try { - out.write(Base64.getEncoder().encode(AES.encryptAES256(message, key))); - out.write('\n'); - out.flush(); + out.println(Base64.getEncoder().encodeToString(AES.encryptAES256(message, key))); } catch (Exception e) { + throw new RuntimeException("Failed to write to server: " + e.getMessage()); } } diff --git a/src/main/java/org/saturnclient/saturnclient/cosmetics/cloaks/Cloaks.java b/src/main/java/org/saturnclient/saturnclient/cosmetics/cloaks/Cloaks.java index 0c3b791..08e3e76 100644 --- a/src/main/java/org/saturnclient/saturnclient/cosmetics/cloaks/Cloaks.java +++ b/src/main/java/org/saturnclient/saturnclient/cosmetics/cloaks/Cloaks.java @@ -71,6 +71,65 @@ public class Cloaks { } } + /** + * Handles loading and caching of a new cloak texture, without connecting to the + * server. + * + * @param cloakName Name of the cloak file to load + */ + public static void loadCloak(String uuid) { + SaturnPlayer player = Auth.players.get(uuid); + if (player != null && player.cloak != null) { + if (!player.cloak.isEmpty()) { + if (Arrays.asList(ANIMATED_CLOAKS).contains(player.cloak)) { + // Load animated cloak data in background thread + new Thread(() -> { + try { + String fileName = player.cloak + ".gif"; + String resourcePath = CLOAKS_RESOURCE_PATH + fileName; + InputStream inputStream = Cloaks.class.getClassLoader().getResourceAsStream(resourcePath); + + if (inputStream != null) { + byte[] data = inputStream.readAllBytes(); + GifDecoder.GifImage gif = GifDecoder.read(data); + List frames = new ArrayList<>(); + List delays = new ArrayList<>(); + int frameCount = gif.getFrameCount(); + + for (int i = 0; i < frameCount; i++) { + frames.add(gif.getFrame(i)); + delays.add(gif.getDelay(i) * 10); + } + + // Register textures on main thread + net.minecraft.client.MinecraftClient.getInstance().execute(() -> { + try { + List animatedFrames = new ArrayList<>(); + for (int i = 0; i < frameCount; i++) { + String frameId = fileName.replace(".gif", "") + "_frame_" + i; + Identifier frameIdentifier = Identifier.of(SaturnClient.MOD_ID, "cloaks_" + frameId); + IdentifierUtils.registerBufferedImageTexture(frameIdentifier, frames.get(i)); + animatedFrames.add(new AnimatedCloakData(frameIdentifier, delays.get(i))); + } + animatedCloaks.put(uuid, animatedFrames); + lastFrameTime.put(uuid, System.currentTimeMillis()); + SaturnClient.LOGGER.info("Loaded " + frames.size() + " frames for animated cloak: " + fileName); + } catch (Exception e) { + SaturnClient.LOGGER.error("Failed to register animated cloak textures: " + fileName, e); + } + }); + } + } catch (IOException e) { + SaturnClient.LOGGER.error("Failed to load animated cloak from resources: " + player.cloak + ".gif", e); + } + }).start(); + } else { + net.minecraft.client.MinecraftClient.getInstance().execute(() -> loadStaticCloak(player.cloak + ".png")); + } + } + } + } + /** * Loads and processes a static cloak from a PNG file in resources. * @@ -100,40 +159,8 @@ public class Cloaks { } private static void loadAnimatedCloak(String uuid, String fileName) { - try { - String resourcePath = CLOAKS_RESOURCE_PATH + fileName; - InputStream inputStream = Cloaks.class.getClassLoader().getResourceAsStream(resourcePath); - - if (inputStream != null) { - // Read all bytes from input stream - byte[] data = inputStream.readAllBytes(); - - // Use our custom GifDecoder - GifDecoder.GifImage gif = GifDecoder.read(data); - - List frames = new ArrayList<>(); - int frameCount = gif.getFrameCount(); - - for (int i = 0; i < frameCount; i++) { - BufferedImage frame = gif.getFrame(i); - // Convert from hundredths of a second to milliseconds (multiply by 10) - int delay = gif.getDelay(i) * 10; - - String frameId = fileName.replace(".gif", "") + "_frame_" + i; - Identifier frameIdentifier = Identifier.of(SaturnClient.MOD_ID, "cloaks_" + frameId); - IdentifierUtils.registerBufferedImageTexture(frameIdentifier, frame); - - frames.add(new AnimatedCloakData(frameIdentifier, delay)); - } - - animatedCloaks.put(uuid, frames); - lastFrameTime.put(uuid, System.currentTimeMillis()); - - SaturnClient.LOGGER.info("Loaded " + frames.size() + " frames for animated cloak: " + fileName); - } - } catch (IOException e) { - SaturnClient.LOGGER.error("Failed to load animated cloak from resources: " + fileName, e); - } + // This method is now handled directly in loadCloak + throw new UnsupportedOperationException("This method should not be called directly. Use loadCloak instead."); } public static Identifier getCurrentCloakTexture(String uuid) {