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
|
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);
}
}
|