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-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/FriendCodeScreen.java
parent97c48aa55abca34478f470fa99c3a150c6629f16 (diff)
Finish world key machinery API
Still need to implement the actual functionality.
Diffstat (limited to 'src/main/java/ganarchy/friendcode/client/FriendCodeScreen.java')
-rw-r--r--src/main/java/ganarchy/friendcode/client/FriendCodeScreen.java152
1 files changed, 87 insertions, 65 deletions
diff --git a/src/main/java/ganarchy/friendcode/client/FriendCodeScreen.java b/src/main/java/ganarchy/friendcode/client/FriendCodeScreen.java
index 4bd4971..9c96bee 100644
--- a/src/main/java/ganarchy/friendcode/client/FriendCodeScreen.java
+++ b/src/main/java/ganarchy/friendcode/client/FriendCodeScreen.java
@@ -2,7 +2,8 @@ package ganarchy.friendcode.client;
 
 import ganarchy.friendcode.FriendCode;
 import ganarchy.friendcode.mixin.FriendCodeIntegratedServerExt;
-import ganarchy.friendcode.util.KeyUtil;import net.fabricmc.api.EnvType;
+import ganarchy.friendcode.util.KeyUtil;
+import net.fabricmc.api.EnvType;
 import net.fabricmc.api.Environment;
 import net.minecraft.client.MinecraftClient;
 import net.minecraft.client.gui.screen.OpenToLanScreen;
@@ -15,7 +16,8 @@ import net.minecraft.screen.ScreenTexts;
 import net.minecraft.server.network.ServerPlayerEntity;
 import net.minecraft.text.MutableText;
 import net.minecraft.text.Text;
-import net.minecraft.util.WorldSavePath;import net.minecraft.world.GameMode;
+import net.minecraft.util.WorldSavePath;
+import net.minecraft.world.GameMode;
 
 import java.io.IOException;
 
@@ -52,57 +54,57 @@ public class FriendCodeScreen extends Screen {
         // game setting buttons
         this.addDrawableChild(
             CyclingButtonWidget
-            .builder(GameMode::getSimpleTranslatableName)
-            .values(
-                GameMode.SURVIVAL,
-                GameMode.SPECTATOR,
-                GameMode.CREATIVE,
-                GameMode.ADVENTURE
-            )
-            .initially(this.gameMode)
-            .build(
-                this.width / 2 - 155,
-                100,
-                150,
-                20,
-                GAME_MODE_TEXT,
-                (button, gameMode) -> {
-                    this.gameMode = gameMode;
-                }
-            )
+                .builder(GameMode::getSimpleTranslatableName)
+                .values(
+                    GameMode.SURVIVAL,
+                    GameMode.SPECTATOR,
+                    GameMode.CREATIVE,
+                    GameMode.ADVENTURE
+                )
+                .initially(this.gameMode)
+                .build(
+                    this.width / 2 - 155,
+                    100,
+                    150,
+                    20,
+                    GAME_MODE_TEXT,
+                    (button, gameMode) -> {
+                        this.gameMode = gameMode;
+                    }
+                )
         );
         this.addDrawableChild(
             CyclingButtonWidget
-            .onOffBuilder(this.allowCommands)
-            .build(
-                this.width / 2 + 5,
-                100,
-                150,
-                20,
-                ALLOW_COMMANDS_TEXT,
-                (button, allowCommands) -> {
-                    this.allowCommands = allowCommands;
-                }
-            )
+                .onOffBuilder(this.allowCommands)
+                .build(
+                    this.width / 2 + 5,
+                    100,
+                    150,
+                    20,
+                    ALLOW_COMMANDS_TEXT,
+                    (button, allowCommands) -> {
+                        this.allowCommands = allowCommands;
+                    }
+                )
         );
 
         // 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;
+                .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;
+                    }
+                )
+        );
 
         // start sharing
         this.addDrawableChild(new ButtonWidget(
@@ -112,13 +114,22 @@ public class FriendCodeScreen extends Screen {
             20,
             START_SHARING,
             button -> {
-                // FIXME
                 int i = NetworkUtils.findLocalPort();
-                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");
+                var samPinger = openToFriends(
+                    this.client, this.codeType, this.gameMode,
+                    this.allowCommands, i
+                );
+                MutableText text;
+                if (samPinger != null) {
+                    text = Text.translatable("commands.publish.started", i);
+                } else {
+                    text = Text.translatable("commands.publish.failed");
+                }
                 this.client.inGameHud.getChatHud().addMessage(text);
                 this.client.updateWindowTitle();
-                this.client.setScreen(new WaitingForFriendCodeScreen(samPinger));
+                this.client.setScreen(new WaitingForFriendCodeScreen(
+                    this.codeType, samPinger
+                ));
             }
         ));
 
@@ -133,41 +144,52 @@ public class FriendCodeScreen extends Screen {
         ));
     }
 
-    private static I2PSamPinger openToFriends(MinecraftClient client, CodeType codeType, GameMode gameMode, boolean allowCommands, int port) {
+    private static I2PSamPinger openToFriends(
+        MinecraftClient client, CodeType codeType, GameMode gameMode,
+        boolean allowCommands, int port
+    ) {
         try {
+            var server = client.getServer();
             String privateKey = null;
             if (codeType == CodeType.WORLD) {
-                // FIXME this currently does nothing.
-                var worldDir = client.getServer().submit(() -> client.getServer().getSavePath(WorldSavePath.ROOT)).join();
+                var worldDir = server.submit(() -> {
+                    return server.getSavePath(WorldSavePath.ROOT);
+                }).join();
                 var keyFile = worldDir.resolve("friendcode.key");
                 privateKey = KeyUtil.readKeyFile(keyFile);
             }
             client.loadBlockList();
-            client.getServer().getNetworkIo().bind(null, port);
+            server.getNetworkIo().bind(null, port);
             FriendCode.LOGGER.info("Started serving on {}", port);
-            ((FriendCodeIntegratedServerExt) client.getServer()).lanPort(port);
+            ((FriendCodeIntegratedServerExt) server).lanPort(port);
             // reuse LAN pinger machinery instead of rolling our own
-            var lanPinger = new I2PSamPinger(client.getServer().getServerMotd(), "" + port, privateKey);
-            ((FriendCodeIntegratedServerExt) client.getServer()).lanPinger(lanPinger);
+            var lanPinger = new I2PSamPinger(
+                server.getServerMotd(), "" + port, privateKey
+            );
+            ((FriendCodeIntegratedServerExt) server).lanPinger(lanPinger);
             lanPinger.start();
-            ((FriendCodeIntegratedServerExt) client.getServer()).forcedGameMode(gameMode);
-            client.getServer().getPlayerManager().setCheatsAllowed(allowCommands);
-            int i = client.getServer().getPermissionLevel(client.player.getGameProfile());
+            ((FriendCodeIntegratedServerExt) server).forcedGameMode(gameMode);
+            server.getPlayerManager().setCheatsAllowed(allowCommands);
+            int i = server.getPermissionLevel(client.player.getGameProfile());
             client.player.setClientPermissionLevel(i);
-            for (ServerPlayerEntity serverPlayerEntity : client.getServer().getPlayerManager().getPlayerList()) {
-                client.getServer().getCommandManager().sendCommandTree(serverPlayerEntity);
+            for (var player : server.getPlayerManager().getPlayerList()) {
+                server.getCommandManager().sendCommandTree(player);
             }
             return lanPinger;
-        }
-        catch (IOException iOException) {
+        } catch (IOException iOException) {
             return null;
         }
     }
 
     @Override
-    public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
+    public void render(
+        MatrixStack matrices, int mouseX, int mouseY, float delta
+    ) {
         this.renderBackground(matrices);
-        FriendCodeScreen.drawCenteredText(matrices, this.textRenderer, OTHER_PLAYERS_TEXT, this.width / 2, 82, 0xFFFFFF);
+        FriendCodeScreen.drawCenteredText(
+            matrices, this.textRenderer, OTHER_PLAYERS_TEXT,
+            this.width / 2, 82, 0xFFFFFF
+        );
         super.render(matrices, mouseX, mouseY, delta);
     }
 }