Created
September 28, 2024 12:53
-
-
Save Densamisten/7bdbbecaa90f6a0f753ee70a48769bf4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package exonihility.client.util.proxy; | |
import exonihility.client.AllyshipClient; | |
import net.minecraft.client.MinecraftClient; | |
import net.minecraft.client.gui.DrawContext; | |
import net.minecraft.client.gui.screen.Screen; | |
import net.minecraft.client.gui.screen.TitleScreen; | |
import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen; | |
import net.minecraft.client.gui.widget.ButtonWidget; | |
import net.minecraft.client.gui.widget.CheckboxWidget; | |
import net.minecraft.client.gui.widget.TextFieldWidget; | |
import net.minecraft.text.Text; | |
import net.minecraft.util.Formatting; | |
import org.apache.commons.lang3.StringUtils; | |
public class GuiProxy extends Screen { | |
private boolean isSocks4 = false; | |
private TextFieldWidget ipPort; | |
private TextFieldWidget username; | |
private TextFieldWidget password; | |
private CheckboxWidget enabledCheck; | |
private ButtonWidget typeButton; | |
private Screen parentScreen; | |
private Proxy.ProxyType currentType = Proxy.ProxyType.SOCKS4; // Default proxy type | |
private String msg = ""; | |
private int[] positionY; | |
private int positionX; | |
private static String text_proxy = Text.translatable("ui.allyshipclient.options.proxy").getString(); | |
public GuiProxy(Screen parentScreen) { | |
super(Text.literal(text_proxy)); | |
this.parentScreen = parentScreen; | |
} | |
private static boolean isValidIpPort(String ipP) { | |
String[] split = ipP.split(":"); | |
if (split.length > 1) { | |
if (!StringUtils.isNumeric(split[1])) return false; | |
int port = Integer.parseInt(split[1]); | |
if (port < 0 || port > 0xFFFF) return false; | |
return true; | |
} else { | |
return false; | |
} | |
} | |
private boolean checkProxy() { | |
if (!isValidIpPort(ipPort.getText())) { | |
msg = Formatting.RED + Text.translatable("ui.allyshipclient.options.invalidIpPort").getString(); | |
this.ipPort.setFocused(true); | |
return false; | |
} | |
return true; | |
} | |
private void centerButtons(int buttonLength) { | |
positionX = (this.width / 2) - (buttonLength / 2); | |
positionY = new int[10]; | |
int center = (this.height + 10 * 26) / 2; | |
int buttonStarts = center - (10 * 26); | |
for (int i = 0; i != 10; i++) { | |
positionY[i] = buttonStarts + (26 * i); | |
} | |
} | |
@Override | |
public boolean keyPressed(int keyCode, int scanCode, int modifiers) { | |
super.keyPressed(keyCode, scanCode, modifiers); | |
msg = ""; | |
return true; | |
} | |
@Override | |
public void render(DrawContext context, int mouseX, int mouseY, float partialTicks) { | |
super.render(context, mouseX, mouseY, partialTicks); | |
if (enabledCheck.isChecked() && !isValidIpPort(ipPort.getText())) { | |
enabledCheck.onPress(); | |
} | |
context.drawTextWithShadow(this.textRenderer, Text.translatable("ui.allyshipclient.options.proxyType").getString(), this.width / 2 - 150, positionY[1] + 5, 10526880); | |
context.drawCenteredTextWithShadow(this.textRenderer, Text.translatable("ui.allyshipclient.options.auth").getString(), this.width / 2, positionY[3] + 8, Formatting.WHITE.getColorValue()); | |
context.drawTextWithShadow(this.textRenderer, Text.translatable("ui.allyshipclient.options.ipPort").getString(), this.width / 2 - 150, positionY[2] + 5, 10526880); | |
this.ipPort.render(context, mouseX, mouseY, partialTicks); | |
if (isSocks4) { | |
context.drawTextWithShadow(this.textRenderer, Text.translatable("ui.allyshipclient.auth.id").getString(), this.width / 2 - 150, positionY[4] + 5, 10526880); | |
this.username.render(context, mouseX, mouseY, partialTicks); | |
} else { | |
context.drawTextWithShadow(this.textRenderer, Text.translatable("ui.allyshipclient.auth.password").getString(), this.width / 2 - 150, positionY[5] + 5, 10526880); | |
context.drawTextWithShadow(this.textRenderer, Text.translatable("ui.allyshipclient.auth.username").getString(), this.width / 2 - 150, positionY[4] + 5, 10526880); | |
this.username.render(context, mouseX, mouseY, partialTicks); | |
this.password.render(context, mouseX, mouseY, partialTicks); | |
} | |
} | |
@Override | |
public void tick() { | |
} | |
@Override | |
public void init() { | |
// MinecraftClient.getInstance().keyboard.setRepeatEvents(true); | |
int buttonLength = 160; | |
centerButtons(buttonLength); | |
isSocks4 = AllyshipClient.proxy.type == Proxy.ProxyType.SOCKS4; | |
ButtonWidget proxyType = ButtonWidget.builder(Text.literal(isSocks4 ? "Socks 4" : "Socks 5"), button -> { | |
isSocks4 = !isSocks4; | |
button.setMessage(Text.literal(isSocks4 ? "Socks 4" : "Socks 5")); | |
}).dimensions(positionX, positionY[1], buttonLength, 20).build(); | |
this.addDrawableChild(proxyType); | |
this.ipPort = new TextFieldWidget(this.textRenderer, positionX, positionY[2], buttonLength, 20, Text.literal("")); | |
this.ipPort.setText(AllyshipClient.proxy.ipPort); | |
this.ipPort.setMaxLength(1024); | |
this.ipPort.setFocused(true); | |
this.addSelectableChild(this.ipPort); | |
this.username = new TextFieldWidget(this.textRenderer, positionX, positionY[4], buttonLength, 20, Text.literal("")); | |
this.username.setMaxLength(255); | |
this.username.setText(AllyshipClient.proxy.username); | |
this.addSelectableChild(this.username); | |
this.password = new TextFieldWidget(this.textRenderer, positionX, positionY[5], buttonLength, 20, Text.literal("")); | |
this.password.setMaxLength(255); | |
this.password.setText(AllyshipClient.proxy.password); | |
this.addSelectableChild(this.password); | |
int posXButtons = (this.width / 2) - (((buttonLength / 2) * 3) / 2); | |
ButtonWidget apply = ButtonWidget.builder(Text.translatable("ui.allyshipclient.options.apply"), button -> { | |
if (checkProxy()) { | |
AllyshipClient.proxy = new Proxy(isSocks4, ipPort.getText(), username.getText(), password.getText()); | |
AllyshipClient.proxyEnabled = enabledCheck.isChecked(); | |
ProxyConfig.setDefaultProxy(AllyshipClient.proxy); | |
ProxyConfig.saveConfig(); | |
MinecraftClient.getInstance().setScreen(new MultiplayerScreen(new TitleScreen())); | |
} | |
}).dimensions(posXButtons, positionY[8], buttonLength / 2 - 3, 20).build(); | |
this.addDrawableChild(apply); | |
CheckboxWidget.Builder checkboxBuilder = CheckboxWidget.builder(Text.translatable("ui.allyshipclient.options.proxyEnabled"), this.textRenderer); | |
checkboxBuilder.pos((this.width / 2) - (15 + textRenderer.getWidth(Text.translatable("ui.allyshipclient.options.proxyEnabled"))) / 2, positionY[7]); | |
if (AllyshipClient.proxyEnabled) { | |
checkboxBuilder.checked(AllyshipClient.proxyEnabled); | |
} | |
this.enabledCheck = checkboxBuilder.build(); | |
this.addDrawableChild(this.enabledCheck); | |
ButtonWidget cancel = ButtonWidget.builder(Text.translatable("ui.allyshipclient.options.cancel"), (button) -> { | |
MinecraftClient.getInstance().setScreen(parentScreen); | |
}).dimensions(posXButtons + (buttonLength / 2 + 3) * 2, positionY[8], buttonLength / 2 - 3, 20).build(); | |
this.addDrawableChild(cancel); | |
} | |
@Override | |
public void close() { | |
msg = ""; | |
// MinecraftClient.getInstance().keyboard.setRepeatEvents(false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment