summary refs log tree commit diff stats
path: root/src/main/java/ganarchy/friendcode/client/WaitingForFriendCodeScreen.java
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2022-07-25 23:08:13 -0300
committerSoniEx2 <endermoneymod@gmail.com>2022-07-25 23:08:13 -0300
commitcb7937f5c6a6677d52efed7caef935dc2e397157 (patch)
tree4e0f3e051b82ae6d7245adf0e54b99d4f786beaa /src/main/java/ganarchy/friendcode/client/WaitingForFriendCodeScreen.java
parent97c48aa55abca34478f470fa99c3a150c6629f16 (diff)
Finish world key machinery API
Still need to implement the actual functionality.
Diffstat (limited to 'src/main/java/ganarchy/friendcode/client/WaitingForFriendCodeScreen.java')
-rw-r--r--src/main/java/ganarchy/friendcode/client/WaitingForFriendCodeScreen.java16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/main/java/ganarchy/friendcode/client/WaitingForFriendCodeScreen.java b/src/main/java/ganarchy/friendcode/client/WaitingForFriendCodeScreen.java
index 8429a41..79c12f7 100644
--- a/src/main/java/ganarchy/friendcode/client/WaitingForFriendCodeScreen.java
+++ b/src/main/java/ganarchy/friendcode/client/WaitingForFriendCodeScreen.java
@@ -1,5 +1,7 @@
 package ganarchy.friendcode.client;
 
+import ganarchy.friendcode.FriendCode;
+import ganarchy.friendcode.util.KeyUtil;
 import net.minecraft.client.gui.screen.LoadingDisplay;
 import net.minecraft.client.gui.screen.Screen;
 import net.minecraft.client.gui.widget.ButtonWidget;
@@ -8,6 +10,7 @@ import net.minecraft.screen.ScreenTexts;
 import net.minecraft.text.MutableText;
 import net.minecraft.text.Text;
 import net.minecraft.util.Util;
+import net.minecraft.util.WorldSavePath;
 import org.apache.commons.codec.binary.Base32;
 
 import java.nio.charset.StandardCharsets;
@@ -18,9 +21,11 @@ import java.util.Locale;
 
 public class WaitingForFriendCodeScreen extends Screen {
     private final I2PSamPinger samPinger;
+    private final CodeType codeType;
 
-    public WaitingForFriendCodeScreen(I2PSamPinger samPinger) {
+    public WaitingForFriendCodeScreen(CodeType codeType, I2PSamPinger samPinger) {
         super(Text.translatable("friendcode.opening"));
+        this.codeType = codeType;
         this.samPinger = samPinger;
     }
 
@@ -71,8 +76,15 @@ public class WaitingForFriendCodeScreen extends Screen {
                     break;
                 }
                 case RUNNING -> {
+                    if (codeType == CodeType.WORLD) {
+                        var worldDir = client.getServer().submit(() -> client.getServer().getSavePath(WorldSavePath.ROOT)).join();
+                        var keyFile = worldDir.resolve("friendcode.key");
+                        if (!KeyUtil.writeKeyFile(keyFile, samPinger.privateKey())) {
+                            FriendCode.LOGGER.error("Couldn't save world code. It will be regenerated next time.");
+                        }
+                    }
                     // convert from I2P base64 to URL-safe base64 so we don't have to write our own Base64 decoder
-                    final String pubkey_b64 = samPinger.pubkey().replace('~', '_');
+                    final String pubkey_b64 = samPinger.publicKey().replace('~', '_');
                     final byte[] pubkey = Base64.getUrlDecoder().decode(pubkey_b64.getBytes(StandardCharsets.UTF_8));
                     // a .b32.i2p is just a Base32-encoded SHA256
                     final String b32;