summary refs log tree commit diff stats
path: root/src/main/java/ganarchy/friendcode/client/FriendCodeScreen.java
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2022-07-24 21:42:55 -0300
committerSoniEx2 <endermoneymod@gmail.com>2022-07-24 21:42:55 -0300
commit354df6d333ffb7b69e92117406c8ce8d61ea09e0 (patch)
tree976a3a4a153903baf96572033a0f46ab33b48f66 /src/main/java/ganarchy/friendcode/client/FriendCodeScreen.java
parent5200e97d90e2adbeebdf06bd756f66df4e8f1b01 (diff)
Prepare for improved SAM auth and World Codes
Diffstat (limited to 'src/main/java/ganarchy/friendcode/client/FriendCodeScreen.java')
-rw-r--r--src/main/java/ganarchy/friendcode/client/FriendCodeScreen.java51
1 files changed, 41 insertions, 10 deletions
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);