diff options
author | SoniEx2 <endermoneymod@gmail.com> | 2022-07-03 23:12:06 -0300 |
---|---|---|
committer | SoniEx2 <endermoneymod@gmail.com> | 2022-07-03 23:12:06 -0300 |
commit | bfb981cd49a6bbcd15482dceeb4ab121c0408157 (patch) | |
tree | 54f1fca2884e68f5a8e6205bf34a01cc62c8a953 /src/main/java/ganarchy/friendcode/client/FriendConnectScreen.java | |
parent | 0c1c11065062c745ce49529e9bee48b05aa4bc41 (diff) |
[Project] Friend Code
A Minecraft mod which adds friend codes, an easy way to play in the same world with remote friends.
Diffstat (limited to 'src/main/java/ganarchy/friendcode/client/FriendConnectScreen.java')
-rw-r--r-- | src/main/java/ganarchy/friendcode/client/FriendConnectScreen.java | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/src/main/java/ganarchy/friendcode/client/FriendConnectScreen.java b/src/main/java/ganarchy/friendcode/client/FriendConnectScreen.java new file mode 100644 index 0000000..53282d7 --- /dev/null +++ b/src/main/java/ganarchy/friendcode/client/FriendConnectScreen.java @@ -0,0 +1,111 @@ +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); + } +} |