summary refs log tree commit diff stats
path: root/src/main/java/ganarchy/friendcode/client/FriendCodeScreen.java
blob: 4bd497103309b0ab5e92c36fa56487b282e60bdb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package ganarchy.friendcode.client;

import ganarchy.friendcode.FriendCode;
import ganarchy.friendcode.mixin.FriendCodeIntegratedServerExt;
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;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.CyclingButtonWidget;
import net.minecraft.client.util.NetworkUtils;
import net.minecraft.client.util.math.MatrixStack;
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 java.io.IOException;

@Environment(EnvType.CLIENT)
public class FriendCodeScreen extends Screen {
    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;

    public FriendCodeScreen(Screen parent) {
        super(Text.translatable("friendcode.title"));
        this.parent = parent;
    }

    @Override
    protected void init() {
        // title button allows going back to LAN screen
        this.addDrawableChild(new ButtonWidget(
            this.width / 2 - 155,
            50,
            310,
            20,
            this.title,
            button -> this.client.setScreen(new OpenToLanScreen(this.parent))
        ));

        // 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;
                }
            )
        );
        this.addDrawableChild(
            CyclingButtonWidget
            .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;

        // start sharing
        this.addDrawableChild(new ButtonWidget(
            this.width / 2 - 155,
            this.height - 28,
            150,
            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");
                this.client.inGameHud.getChatHud().addMessage(text);
                this.client.updateWindowTitle();
                this.client.setScreen(new WaitingForFriendCodeScreen(samPinger));
            }
        ));

        // go back to options
        this.addDrawableChild(new ButtonWidget(
            this.width / 2 + 5,
            this.height - 28,
            150,
            20,
            ScreenTexts.CANCEL,
            button -> this.client.setScreen(this.parent)
        ));
    }

    private static I2PSamPinger openToFriends(MinecraftClient client, CodeType codeType, GameMode gameMode, boolean allowCommands, int port) {
        try {
            String privateKey = null;
            if (codeType == CodeType.WORLD) {
                // FIXME this currently does nothing.
                var worldDir = client.getServer().submit(() -> client.getServer().getSavePath(WorldSavePath.ROOT)).join();
                var keyFile = worldDir.resolve("friendcode.key");
                privateKey = KeyUtil.readKeyFile(keyFile);
            }
            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, privateKey);
            ((FriendCodeIntegratedServerExt) client.getServer()).lanPinger(lanPinger);
            lanPinger.start();
            ((FriendCodeIntegratedServerExt) client.getServer()).forcedGameMode(gameMode);
            client.getServer().getPlayerManager().setCheatsAllowed(allowCommands);
            int i = client.getServer().getPermissionLevel(client.player.getGameProfile());
            client.player.setClientPermissionLevel(i);
            for (ServerPlayerEntity serverPlayerEntity : client.getServer().getPlayerManager().getPlayerList()) {
                client.getServer().getCommandManager().sendCommandTree(serverPlayerEntity);
            }
            return lanPinger;
        }
        catch (IOException iOException) {
            return null;
        }
    }

    @Override
    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);
        super.render(matrices, mouseX, mouseY, delta);
    }
}