From cb7937f5c6a6677d52efed7caef935dc2e397157 Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Mon, 25 Jul 2022 23:08:13 -0300 Subject: Finish world key machinery API Still need to implement the actual functionality. --- .../friendcode/client/WaitingForFriendCodeScreen.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/main/java/ganarchy/friendcode/client/WaitingForFriendCodeScreen.java') 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; -- cgit 1.4.1