diff options
Diffstat (limited to 'src/main/java/ganarchy/friendcode/client/FriendCodeScreen.java')
-rw-r--r-- | src/main/java/ganarchy/friendcode/client/FriendCodeScreen.java | 51 |
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); |