package ganarchy.friendcode.client; import it.unimi.dsi.fastutil.booleans.BooleanConsumer; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.DirectConnectScreen; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.client.gui.widget.TextFieldWidget; import net.minecraft.client.network.ServerInfo; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.screen.ScreenTexts; import net.minecraft.text.Text; import org.lwjgl.glfw.GLFW; import java.util.regex.Pattern; public class FriendConnectScreen extends Screen { private static final Text ENTER_CODE_TEXT = Text.translatable("friendcode.enter_code"); /** * Pattern for old-style b32 addresses. */ // FIXME? maybe add support for b33 addresses? private static final Pattern B32_PATTERN = Pattern.compile("^[A-Za-z2-7]{52}\\.b32\\.i2p$"); private final Screen parent; private final ServerInfo serverEntry; private final BooleanConsumer callback; private TextFieldWidget addressField; private ButtonWidget selectServerButton; public FriendConnectScreen(Screen parent, BooleanConsumer callback, ServerInfo server) { super(Text.translatable("friendcode.connect")); this.parent = parent; this.serverEntry = server; this.callback = callback; } @Override public void tick() { this.addressField.tick(); } @Override public boolean keyPressed(int keyCode, int scanCode, int modifiers) { if (this.selectServerButton.active && this.getFocused() == this.addressField && (keyCode == GLFW.GLFW_KEY_ENTER || keyCode == GLFW.GLFW_KEY_KP_ENTER)) { this.saveAndClose(); return true; } return super.keyPressed(keyCode, scanCode, modifiers); } @Override protected void init() { this.addDrawableChild(new ButtonWidget( this.width / 2 - 100, 20, 200, 20, this.title, button -> this.client.setScreen(new DirectConnectScreen(this.parent, this.callback, this.serverEntry)) )); this.client.keyboard.setRepeatEvents(true); this.selectServerButton = this.addDrawableChild(new ButtonWidget(this.width / 2 - 100, this.height / 4 + 96 + 12, 200, 20, Text.translatable("friendcode.select"), button -> this.saveAndClose())); this.addDrawableChild(new ButtonWidget(this.width / 2 - 100, this.height / 4 + 120 + 12, 200, 20, ScreenTexts.CANCEL, button -> this.callback.accept(false))); this.addressField = new TextFieldWidget(this.textRenderer, this.width / 2 - 100, 116, 200, 20, ENTER_CODE_TEXT); this.addressField.setMaxLength(128); this.addressField.setTextFieldFocused(true); //this.addressField.setText(this.client.options.lastServer); this.addressField.setChangedListener(text -> this.onAddressFieldChanged()); this.addSelectableChild(this.addressField); this.setInitialFocus(this.addressField); this.onAddressFieldChanged(); } @Override public void resize(MinecraftClient client, int width, int height) { String string = this.addressField.getText(); this.init(client, width, height); this.addressField.setText(string); } private void saveAndClose() { this.client.setScreen(new FriendConnectingScreen(this.serverEntry, this.callback, this.addressField.getText())); // this.serverEntry.address = this.addressField.getText(); // this.callback.accept(true); } @Override public void close() { this.client.setScreen(this.parent); } @Override public void removed() { this.client.keyboard.setRepeatEvents(false); // FIXME store this.addressField.getText() somewhere. // this.client.options.lastServer = this.addressField.getText(); // this.client.options.write(); } private void onAddressFieldChanged() { this.selectServerButton.active = B32_PATTERN.matcher(this.addressField.getText()).matches(); } @Override public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { this.renderBackground(matrices); FriendConnectScreen.drawTextWithShadow(matrices, this.textRenderer, ENTER_CODE_TEXT, this.width / 2 - 100, 100, 0xA0A0A0); this.addressField.render(matrices, mouseX, mouseY, delta); super.render(matrices, mouseX, mouseY, delta); } }