summary refs log tree commit diff stats
path: root/src/main/java/ganarchy/friendcode/client
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/ganarchy/friendcode/client')
-rw-r--r--src/main/java/ganarchy/friendcode/client/CodeType.java16
-rw-r--r--src/main/java/ganarchy/friendcode/client/FriendCodeScreen.java51
-rw-r--r--src/main/java/ganarchy/friendcode/client/I2PSamPinger.java6
-rw-r--r--src/main/java/ganarchy/friendcode/client/SamProxyThread.java2
4 files changed, 61 insertions, 14 deletions
diff --git a/src/main/java/ganarchy/friendcode/client/CodeType.java b/src/main/java/ganarchy/friendcode/client/CodeType.java
new file mode 100644
index 0000000..e9e5c57
--- /dev/null
+++ b/src/main/java/ganarchy/friendcode/client/CodeType.java
@@ -0,0 +1,16 @@
+package ganarchy.friendcode.client;
+
+import net.minecraft.text.Text;public enum CodeType {
+    SESSION("friendcode.code_type.session"),
+    WORLD("friendcode.code_type.world");
+
+    private final Text translatableText;
+
+    CodeType(String textKey) {
+        this.translatableText = Text.translatable(textKey);
+    }
+
+    public Text getSimpleTranslatableName(CodeType this) {
+        return this.translatableText;
+    }
+}
diff --git a/src/main/java/ganarchy/friendcode/client/FriendCodeScreen.java b/src/main/java/ganarchy/friendcode/client/FriendCodeScreen.java
index 38703a5..9496942 100644
--- a/src/main/java/ganarchy/friendcode/client/FriendCodeScreen.java
+++ b/src/main/java/ganarchy/friendcode/client/FriendCodeScreen.java
@@ -15,21 +15,23 @@ import net.minecraft.screen.ScreenTexts;
 import net.minecraft.server.network.ServerPlayerEntity;
 import net.minecraft.text.MutableText;
 import net.minecraft.text.Text;
-import net.minecraft.world.GameMode;
+import net.minecraft.util.WorldSavePath;import net.minecraft.world.GameMode;
 
 import java.io.IOException;
 
 @Environment(EnvType.CLIENT)
 public class FriendCodeScreen extends Screen {
-    private static final Text ALLOW_COMMANDS_TEXT = Text.translatable("selectWorld.allowCommands");
-    private static final Text GAME_MODE_TEXT = Text.translatable("selectWorld.gameMode");
-    private static final Text OTHER_PLAYERS_TEXT = Text.translatable("lanServer.otherPlayers");
+    private static final Text
+        ALLOW_COMMANDS_TEXT = Text.translatable("selectWorld.allowCommands"),
+        GAME_MODE_TEXT = Text.translatable("selectWorld.gameMode"),
+        CODE_TYPE_TEXT = Text.translatable("friendcode.code_type"),
+        OTHER_PLAYERS_TEXT = Text.translatable("lanServer.otherPlayers"),
+        START_SHARING = Text.translatable("friendcode.start");
     private final Screen parent;
+    private CodeType codeType = CodeType.SESSION;
     private GameMode gameMode = GameMode.SURVIVAL;
     private boolean allowCommands;
 
-    private static final Text START_SHARING = Text.translatable("friendcode.start");
-
     public FriendCodeScreen(Screen parent) {
         super(Text.translatable("friendcode.title"));
         this.parent = parent;
@@ -51,7 +53,12 @@ public class FriendCodeScreen extends Screen {
         this.addDrawableChild(
             CyclingButtonWidget
             .builder(GameMode::getSimpleTranslatableName)
-            .values(GameMode.SURVIVAL, GameMode.SPECTATOR, GameMode.CREATIVE, GameMode.ADVENTURE)
+            .values(
+                GameMode.SURVIVAL,
+                GameMode.SPECTATOR,
+                GameMode.CREATIVE,
+                GameMode.ADVENTURE
+            )
             .initially(this.gameMode)
             .build(
                 this.width / 2 - 155,
@@ -79,6 +86,24 @@ public class FriendCodeScreen extends Screen {
             )
         );
 
+        // friend code type button
+        this.addDrawableChild(
+            CyclingButtonWidget
+            .builder(CodeType::getSimpleTranslatableName)
+            .values(CodeType.SESSION, CodeType.WORLD)
+            .initially(this.codeType)
+            .build(
+                this.width / 2 - 155,
+                125,
+                310,
+                20,
+                CODE_TYPE_TEXT,
+                (button, codeType) -> {
+                    this.codeType = codeType;
+                }
+            )
+        ).active = false;
+
         // start sharing
         this.addDrawableChild(new ButtonWidget(
             this.width / 2 - 155,
@@ -89,7 +114,7 @@ public class FriendCodeScreen extends Screen {
             button -> {
                 // FIXME
                 int i = NetworkUtils.findLocalPort();
-                var samPinger = openToFriends(this.client, this.gameMode, this.allowCommands, i);
+                var samPinger = openToFriends(this.client, this.codeType, this.gameMode, this.allowCommands, i);
                 MutableText text = samPinger != null ? Text.translatable("commands.publish.started", i) : Text.translatable("commands.publish.failed");
                 this.client.inGameHud.getChatHud().addMessage(text);
                 this.client.updateWindowTitle();
@@ -108,14 +133,20 @@ public class FriendCodeScreen extends Screen {
         ));
     }
 
-    private static I2PSamPinger openToFriends(MinecraftClient client, GameMode gameMode, boolean allowCommands, int port) {
+    private static I2PSamPinger openToFriends(MinecraftClient client, CodeType codeType, GameMode gameMode, boolean allowCommands, int port) {
         try {
+            String privateKey = null;
+            if (codeType == CodeType.WORLD) {
+                // FIXME
+                var worldDir = client.getServer().submit(() -> client.getServer().getSavePath(WorldSavePath.ROOT)).join();
+                var keyFile = worldDir.resolve("friendcode.key");
+            }
             client.loadBlockList();
             client.getServer().getNetworkIo().bind(null, port);
             FriendCode.LOGGER.info("Started serving on {}", port);
             ((FriendCodeIntegratedServerExt) client.getServer()).lanPort(port);
             // reuse LAN pinger machinery instead of rolling our own
-            var lanPinger = new I2PSamPinger(client.getServer().getServerMotd(), "" + port);
+            var lanPinger = new I2PSamPinger(client.getServer().getServerMotd(), "" + port, privateKey);
             ((FriendCodeIntegratedServerExt) client.getServer()).lanPinger(lanPinger);
             lanPinger.start();
             ((FriendCodeIntegratedServerExt) client.getServer()).forcedGameMode(gameMode);
diff --git a/src/main/java/ganarchy/friendcode/client/I2PSamPinger.java b/src/main/java/ganarchy/friendcode/client/I2PSamPinger.java
index 0476c61..e0824b9 100644
--- a/src/main/java/ganarchy/friendcode/client/I2PSamPinger.java
+++ b/src/main/java/ganarchy/friendcode/client/I2PSamPinger.java
@@ -14,11 +14,11 @@ public class I2PSamPinger extends LanServerPinger implements LanSendPing {
     private volatile Status status = Status.IDLE;
     private volatile boolean stopSam;
 
-    public I2PSamPinger(String motd, String port) throws IOException {
+    public I2PSamPinger(String motd, String port, String privateKey) throws IOException {
         super(motd, port);
         // cba to mixin
         this.port = port;
-        this.sam = new I2PSamControl(true);
+        this.sam = new I2PSamControl(true, privateKey);
     }
 
     @Override
@@ -96,7 +96,7 @@ public class I2PSamPinger extends LanServerPinger implements LanSendPing {
     }
 
     public String pubkey() {
-        return this.sam.pubkey();
+        return this.sam.publicKey();
     }
 
     public void stopSam() {
diff --git a/src/main/java/ganarchy/friendcode/client/SamProxyThread.java b/src/main/java/ganarchy/friendcode/client/SamProxyThread.java
index c7db41f..2938455 100644
--- a/src/main/java/ganarchy/friendcode/client/SamProxyThread.java
+++ b/src/main/java/ganarchy/friendcode/client/SamProxyThread.java
@@ -38,7 +38,7 @@ public class SamProxyThread extends Thread {
         // who cares if you have 3 threads just to connect to a minecraft server/friend code
         // it's only 3 threads
         // the "server" is far more efficient, only requiring one
-        try (final I2PSamControl control = new I2PSamControl(zeroHop)) {
+        try (final I2PSamControl control = new I2PSamControl(zeroHop, null)) {
             if (!control.connect()) {
                 this.running = false;
                 this.status = Status.CONNECTION_FAILED;