tcr/java/src/game/Temp1.java
2025-03-19 02:08:41 +01:00

387 lines
12 KiB
Java

package game.gui.server;
import game.SKC;
import game.color.TextColor;
import game.gui.GuiScreen;
import game.gui.element.Button;
import game.gui.server.GuiMultiplayer.ServerEntry;
import game.network.NetHandlerPlayServer;
import com.google.common.base.Predicate;
public class GuiServer extends GuiScreen {
private final GuiScreen parentScreen;
private final ServerEntry serverData;
private TextField serverIPField;
private TextField serverNameField;
private TextField serverUserField;
private TextField serverPassField;
private TextField serverAccessField;
public GuiServer(GuiScreen parent, ServerEntry server) {
this.parentScreen = parent;
this.serverData = server;
}
public void initGui() {
this.buttonList.clear();
this.buttonList.add(new Button(0, this.width / 2 - 100, this.height / 4 + 96 + 100 + 18, 200, 20, "Fertig"));
this.buttonList.add(new Button(1, this.width / 2 - 100, this.height / 4 + 120 + 100 + 18, 200, 20, "Abbrechen"));
this.serverNameField = new TextField(this.width / 2 - 100, 66, 200, 20);
this.serverNameField.setFocused(true);
this.serverNameField.setText(this.serverData.name);
this.serverIPField = new TextField(this.width / 2 - 100, 106, 200, 20);
this.serverIPField.setMaxStringLength(128);
this.serverIPField.setText(this.serverData.address);
this.serverUserField = new TextField(this.width / 2 - 100, 146, 200, 20);
this.serverUserField.setMaxStringLength(16);
this.serverUserField.setText(this.serverData.user);
this.serverUserField.setValidator(new Predicate<String>() {
public boolean apply(String name) {
return NetHandlerPlayServer.isValidUser(name);
}
});
this.serverPassField = new TextField(this.width / 2 - 100, 186, 200, 20);
this.serverPassField.setMaxStringLength(64);
this.serverPassField.setText(this.serverData.pass);
this.serverAccessField = new TextField(this.width / 2 - 100, 226, 200, 20);
this.serverAccessField.setMaxStringLength(64);
this.serverAccessField.setText(this.serverData.access);
((Button)this.buttonList.get(0)).enabled = this.serverIPField.getText().length() > 0 && this.serverIPField.getText().split(":").length > 0
&& this.serverNameField.getText().length() > 0 && this.serverUserField.getText().length() > 0;
}
protected void actionPerformed(Button button) {
if(button.enabled) {
if(button.id == 1) {
this.parentScreen.confirmClicked(false);
}
else if(button.id == 0) {
this.serverData.name = this.serverNameField.getText();
this.serverData.address = this.serverIPField.getText();
this.serverData.user = this.serverUserField.getText();
this.serverData.pass = this.serverPassField.getText();
this.serverData.access = this.serverAccessField.getText();
this.parentScreen.confirmClicked(true);
}
}
}
protected void keyTyped(char typedChar, int keyCode) {
if(keyCode == 15) {
int focus = this.serverNameField.isFocused() ? 0 : (this.serverIPField.isFocused() ? 1 : ((this.serverUserField.isFocused() ? 2
: ((this.serverPassField.isFocused() ? 3 : ((this.serverAccessField.isFocused() ? 4 : -1)))))));
focus = focus == -1 ? -1 : ((focus + 5 + (SKC.isShiftKeyDown() ? -1 : 1)) % 5);
this.serverNameField.setFocused(focus == 0);
this.serverIPField.setFocused(focus == 1);
this.serverUserField.setFocused(focus == 2);
this.serverPassField.setFocused(focus == 3);
this.serverAccessField.setFocused(focus == 4);
}
if(keyCode == 28 || keyCode == 156)
this.actionPerformed((Button)this.buttonList.get(0));
((Button)this.buttonList.get(0)).enabled = this.serverIPField.getText().length() > 0 && this.serverIPField.getText().split(":").length > 0
&& this.serverNameField.getText().length() > 0 && this.serverUserField.getText().length() > 0;
}
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
this.drawBackground();
SKC.drawCentered("Serverinformationen bearbeiten", this.width / 2, 17, 16777215);
SKC.drawString((this.serverNameField.getText().isEmpty() ? TextColor.DARK_RED : "") + "Servername", this.width / 2 - 100, 54, 10526880);
SKC.drawString((this.serverIPField.getText().isEmpty() || this.serverIPField.getText().split(":").length == 0
? TextColor.DARK_RED : "") + "Serveradresse", this.width / 2 - 100, 94, 10526880);
SKC.drawString((this.serverUserField.getText().isEmpty() ? TextColor.DARK_RED : "") + "Nutzername", this.width / 2 - 100, 134, 10526880);
SKC.drawString("Passwort", this.width / 2 - 100, 174, 10526880);
SKC.drawString("Zugangspasswort", this.width / 2 - 100, 214, 10526880);
super.drawScreen(mouseX, mouseY, partialTicks);
}
}
private void loadList() {
this.servers.clear();
// List<ServerEntry> list = Lists.newArrayList();
try {
if(!SERVERS_FILE.exists())
return;
List<String> lines = FileUtils.readLines(SERVERS_FILE);
for(String line : lines) {
if(line.isEmpty())
continue;
// ServerData server = ServerData.fromString(line);
String[] svr = line.split("=", 2);
if(svr.length != 2)
continue;
String[] tok = svr[1].split("@", 2);
if(tok.length != 2 || tok[0].startsWith(":"))
continue;
String[] addr = tok[1].split("#", 2);
ServerEntry server = new ServerEntry();
server.name = svr[0];
server.address = addr[0];
if(addr.length == 2)
server.access = addr[1];
String[] login = tok[0].split(":", 2);
if(login.length == 2)
server.pass = login[1];
server.user = login[0];
// return ? null : server;
if(!server.name.isEmpty() && !server.address.isEmpty() && !server.user.isEmpty())
this.servers.add(server);
}
}
catch(Exception e) {
Log.JNI.error(e, "Konnte Serverliste nicht laden");
}
// return list;
}
private void saveList() {
try {
StringBuilder sb = new StringBuilder();
for(ServerEntry server : this.servers) {
sb.append((sb.length() == 0 ? "" : "\n") + server.name + "=" + server.user +
(server.pass.isEmpty() ? "" : (":" + server.pass)) + "@" +
server.address + (server.access.isEmpty() ? "" : ("#" + server.access)));
}
FileUtils.safeWrite(sb.toString(), SERVERS_FILE);
}
catch(Exception e) {
Log.JNI.error(e, "Konnte Serverliste nicht speichern");
}
}
private void drawPing(int width, int xPos, int yPos, int ping) {
GlState.color(1.0F, 1.0F, 1.0F, 1.0F);
this.gm.getTextureManager().bindTexture(icons);
int rectX = 0;
int rectWidth = 0;
if(ping < 0 || ping >= 20000) {
rectWidth = 5;
}
else if(ping < 150) {
rectWidth = 0;
}
else if(ping < 300) {
rectWidth = 1;
}
else if(ping < 600) {
rectWidth = 2;
}
else if(ping < 1000) {
rectWidth = 3;
}
else {
rectWidth = 4;
}
String str = this.formatPing(ping);
this.zLevel += 100.0F;
this.drawTexturedModalRect(xPos + width - 11, yPos, 0 + rectX * 10, 176 + rectWidth * 8, 10, 8);
SKC.drawString(str, xPos + width - (11 + 1 + SKC.getStringWidth(str)), yPos, -1);
this.zLevel -= 100.0F;
}
/*
private final int[] tickTimes = new int[240];
private final long[] frames = new long[240];
private int tickIndex;
private long frameLast;
private long frameWait;
private int lastIndex;
private int frameCounter;
private int frameIndex;
private long startNanoTime = System.nanoTime();
long now = System.nanoTime();
this.addFrame(now - this.startNanoTime);
this.startNanoTime = now;
private void addFrame(long runningTime)
{
this.frames[this.frameIndex] = runningTime;
++this.frameIndex;
if (this.frameIndex == 240)
{
this.frameIndex = 0;
}
if (this.frameCounter < 240)
{
this.lastIndex = 0;
++this.frameCounter;
}
else
{
this.lastIndex = (this.frameIndex + 1) % 240;
}
}
private void updateTick() {
long n = System.nanoTime();
if(n < this.frameLast + this.frameWait)
return;
this.frameWait = 50000000L - ((n - (this.frameLast + this.frameWait)) < 50000000L ? (n - (this.frameLast + this.frameWait)) : 0L);
this.frameLast = n;
this.tickTimes[this.tickIndex++] = this.isServer() ? (int)this.server.getLastTick() : 0;
if(this.tickIndex == 240) {
this.tickIndex = 0;
}
}
private int blendColors(int color1, int color2, float value) {
int i = color1 >> 24 & 255;
int j = color1 >> 16 & 255;
int k = color1 >> 8 & 255;
int l = color1 & 255;
int i1 = color2 >> 24 & 255;
int j1 = color2 >> 16 & 255;
int k1 = color2 >> 8 & 255;
int l1 = color2 & 255;
int i2 = ExtMath.clampi((int)((float)i + (float)(i1 - i) * value), 0, 255);
int j2 = ExtMath.clampi((int)((float)j + (float)(j1 - j) * value), 0, 255);
int k2 = ExtMath.clampi((int)((float)k + (float)(k1 - k) * value), 0, 255);
int l2 = ExtMath.clampi((int)((float)l + (float)(l1 - l) * value), 0, 255);
return i2 << 24 | j2 << 16 | k2 << 8 | l2;
}
private int getFrameColor(int value, int base, int mid, int high) {
value = ExtMath.clampi(value, base, high);
return value < mid ? this.blendColors(-16711936, -256, (float)value / (float)mid)
: this.blendColors(-256, -65536, (float)(value - mid) / (float)(high - mid));
}
private void drawHorizontalLine(int startX, int endX, int y, int color)
{
if (endX < startX)
{
int i = startX;
startX = endX;
endX = i;
}
Gui.drawRect(startX, y, endX + 1, y + 1, color);
}
private void drawVerticalLine(int x, int startY, int endY, int color)
{
if (endY < startY)
{
int i = startY;
startY = endY;
endY = i;
}
Gui.drawRect(x, startY + 1, x + 1, endY, color);
}
private void renderLagometer() {
GlState.disableDepth();
int w = this.displayWidth;
int h = this.displayHeight;
int shifted = this.lastIndex;
int x = 0;
Gui.drawRect(0, h - 74, 240, h - 14, 0x90505050);
drawHorizontalLine(0, 239, h - 44, 0x5fffffff);
drawHorizontalLine(0, 239, h - 74, 0x5fffffff);
while(shifted != this.frameIndex) {
int value = (int) (this.frames[shifted] / 1000000L); // , 30);
int color = this.getFrameColor(value, 0, 17, 67);
if(value > 0)
drawVerticalLine(x, h - 14, h - 14 - value, color & 0xc0ffffff);
++x;
shifted = (shifted + 1) % 240;
}
Gui.drawRect(1, h - 30 - 13, 26, h - 30 - 4, 0x90505050);
FontRenderer.drawString("30ms", 2, h - 30 - 12, 0xE0E0E0);
Gui.drawRect(1, h - 60 - 13, 26, h - 60 - 4, 0x90505050);
FontRenderer.drawString("60ms", 2, h - 60 - 12, 0xE0E0E0);
int width = FontRenderer.getStringWidth("ms/Frame");
Gui.drawRect(238 - width, h - 60 - 13, 239, h - 60 - 4, 0x90505050);
FontRenderer.drawString("ms/Frame", 239 - width, h - 60 - 12, 0xE0E0E0);
if(this.isServer()) {
this.updateTick();
Gui.drawRect(w - 240, h - 74, w, h - 14, 0x90505050);
x = w - 240;
drawHorizontalLine(w - 240, w - 1, h - 44, 0x5fffffff);
drawHorizontalLine(w - 240, w - 1, h - 74, 0x5fffffff);
for (int n = 0; n < 240; ++n)
{
int value = this.tickTimes[(n + this.tickIndex) % 240];
int color = this.getFrameColor(value, 0, 50, 250);
if(value > 0)
drawVerticalLine(x, h - 14, h - 14 - value, color & 0xc0ffffff);
++x;
shifted = (shifted + 1) % 240;
}
width = FontRenderer.getStringWidth("ms/Tick");
Gui.drawRect(w - 240 + 1, h - 30 - 13, w - 240 + 26, h - 30 - 4, 0x90505050);
FontRenderer.drawString("30ms", w - 240 + 2, h - 30 - 12, 0xE0E0E0);
Gui.drawRect(w - 240 + 1, h - 60 - 13, w - 240 + 26, h - 60 - 4, 0x90505050);
FontRenderer.drawString("60ms", w - 240 + 2, h - 60 - 12, 0xE0E0E0);
Gui.drawRect(w - 2 - width, h - 60 - 13, w - 1, h - 60 - 4, 0x90505050);
FontRenderer.drawString("ms/Tick", w - 1 - width, h - 60 - 12, 0xE0E0E0);
}
GlState.enableDepth();
}
*/