sounds WAV -> OPUS

This commit is contained in:
Sen 2025-03-21 13:08:55 +01:00
parent 103e4d35f5
commit 93f98f9984
11 changed files with 367 additions and 554 deletions

View file

@ -63,6 +63,7 @@ import game.entity.npc.EntityNPC;
import game.entity.types.EntityLiving; import game.entity.types.EntityLiving;
import game.gui.Font; import game.gui.Font;
import game.gui.Gui; import game.gui.Gui;
import game.gui.GuiConnect;
import game.gui.GuiConsole; import game.gui.GuiConsole;
import game.gui.GuiGameOver; import game.gui.GuiGameOver;
import game.gui.GuiInfo; import game.gui.GuiInfo;
@ -119,6 +120,7 @@ import game.renderer.texture.EntityTexManager;
import game.renderer.texture.TextureManager; import game.renderer.texture.TextureManager;
import game.renderer.texture.TextureMap; import game.renderer.texture.TextureMap;
import game.rng.Random; import game.rng.Random;
import game.util.CharValidator;
import game.util.ExtMath; import game.util.ExtMath;
import game.util.FileCallback; import game.util.FileCallback;
import game.util.FileUtils; import game.util.FileUtils;
@ -137,6 +139,8 @@ import game.vars.FloatVar;
import game.vars.FloatVar.FloatFunction; import game.vars.FloatVar.FloatFunction;
import game.vars.IntVar; import game.vars.IntVar;
import game.vars.IntVar.IntFunction; import game.vars.IntVar.IntFunction;
import game.vars.StringVar;
import game.vars.StringVar.StringFunction;
import game.vars.Variable; import game.vars.Variable;
import game.vars.Variable.IntType; import game.vars.Variable.IntType;
import game.window.Bind; import game.window.Bind;
@ -294,13 +298,13 @@ public class Game implements IThreadListener {
public EntityNPC thePlayer; public EntityNPC thePlayer;
public HitPosition pointed; public HitPosition pointed;
@Variable(name = "chunk_view_distance", category = CVarCategory.WORLD, min = 2, max = 16 /* 128 */, callback = DistanceFunction.class, display = "Sichtweite", unit = "Chunks") @Variable(name = "chunk_view_distance", category = CVarCategory.RENDER, min = 2, max = 16 /* 128 */, callback = DistanceFunction.class, display = "Sichtweite", unit = "Chunks")
public int renderDistance = 8; public int renderDistance = 8;
@Variable(name = "chunk_build_time", category = CVarCategory.WORLD, min = 1, max = 100, display = "Max. Zeit für Chunk-Bau pro Frame", unit = "ms") @Variable(name = "chunk_build_time", category = CVarCategory.RENDER, min = 1, max = 100, display = "Max. Zeit für Chunk-Bau pro Frame", unit = "ms")
public int maxBuildTime = 8; public int maxBuildTime = 8;
@Variable(name = "gl_fov", category = CVarCategory.RENDER, min = 1.0f, max = 179.0f, display = "Sichtfeld (FOV)", unit = "°", precision = 1) @Variable(name = "gl_fov", category = CVarCategory.RENDER, min = 1.0f, max = 179.0f, display = "Sichtfeld (FOV)", unit = "°", precision = 1)
public float fov = 70.0f; public float fov = 70.0f;
@Variable(name = "gl_wireframe", category = CVarCategory.RENDER) @Variable(name = "gl_wireframe", category = CVarCategory.RENDER, display = "Gitter-Render-Modus")
public boolean wireframe = false; public boolean wireframe = false;
@Variable(name = "con_timestamps", category = CVarCategory.CONSOLE, display = "Zeiten") @Variable(name = "con_timestamps", category = CVarCategory.CONSOLE, display = "Zeiten")
@ -308,32 +312,32 @@ public class Game implements IThreadListener {
@Variable(name = "con_loglevel", category = CVarCategory.CONSOLE, display = "Ausgabe") @Variable(name = "con_loglevel", category = CVarCategory.CONSOLE, display = "Ausgabe")
public LogLevel level = LogLevel.INFO; public LogLevel level = LogLevel.INFO;
@Variable(name = "win_sync", category = CVarCategory.WINDOW, min = -1, max = 16384, callback = SyncFunction.class) @Variable(name = "win_sync", category = CVarCategory.WINDOW, min = -1, max = 16384, callback = SyncFunction.class, display = "Maximale Bildrate")
public int sync = 0; public int sync = 0;
public int width; public int width;
public int height; public int height;
public int mouse_x; public int mouse_x;
public int mouse_y; public int mouse_y;
@Variable(name = "win_width", category = CVarCategory.WINDOW, min = 1, max = 65536) @Variable(name = "win_width", category = CVarCategory.WINDOW, min = 1, max = 65536, display = "Fensterbreite")
public int xsize = 1280; public int xsize = 1280;
@Variable(name = "win_height", category = CVarCategory.WINDOW, min = 1, max = 65536) @Variable(name = "win_height", category = CVarCategory.WINDOW, min = 1, max = 65536, display = "Fensterhöhe")
public int ysize = 800; public int ysize = 800;
@Variable(name = "win_pos_x", category = CVarCategory.WINDOW, min = -65536, max = 65536) @Variable(name = "win_pos_x", category = CVarCategory.WINDOW, min = -65536, max = 65536, display = "Fenster X-Position")
public int saved_xpos = 0x80000000; public int saved_xpos = 0x80000000;
@Variable(name = "win_pos_y", category = CVarCategory.WINDOW, min = -65536, max = 65536) @Variable(name = "win_pos_y", category = CVarCategory.WINDOW, min = -65536, max = 65536, display = "Fenster Y-Position")
public int saved_ypos = 0x80000000; public int saved_ypos = 0x80000000;
public int fb_x; public int fb_x;
public int fb_y; public int fb_y;
@Variable(name = "phy_sensitivity", category = CVarCategory.PHYSICS, min = 0.01f, max = 10.0f, display = "Mausempfindlichkeit", precision = 2, unit = "%") @Variable(name = "phy_sensitivity", category = CVarCategory.INPUT, min = 0.01f, max = 10.0f, display = "Mausempfindlichkeit", precision = 2, unit = "%")
public float sensitivity = 1.0f; public float sensitivity = 1.0f;
public boolean fullscreen; public boolean fullscreen;
public long syncLimit; public long syncLimit;
public boolean mouseFirst; public boolean mouseFirst;
public boolean nograb = System.getProperty("mouse.nograb") != null; public boolean nograb = System.getProperty("mouse.nograb") != null;
public DisplayMode vidMode; public DisplayMode vidMode;
@Variable(name = "gui_dclick_delay", category = CVarCategory.GUI, min = 150, max = 750, display = "Doppelklick bei", unit = "ms") @Variable(name = "gui_dclick_delay", category = CVarCategory.INPUT, min = 150, max = 750, display = "Doppelklick bei", unit = "ms")
public int dclickDelay = 250; public int dclickDelay = 250;
@Variable(name = "con_size", category = CVarCategory.CONSOLE, min = 0, max = 128, callback = ConFunction.class, display = "Nachrichten im Overlay") @Variable(name = "con_size", category = CVarCategory.CONSOLE, min = 0, max = 128, callback = ConFunction.class, display = "Nachrichten im Overlay")
public int hudSize = 32; public int hudSize = 32;
@ -341,7 +345,7 @@ public class Game implements IThreadListener {
public int hudFadeout = 5000; public int hudFadeout = 5000;
@Variable(name = "con_overlay", category = CVarCategory.CONSOLE, display = "Konsolen-Overlay") @Variable(name = "con_overlay", category = CVarCategory.CONSOLE, display = "Konsolen-Overlay")
public boolean hudOverlay = true; public boolean hudOverlay = true;
@Variable(name = "con_position", category = CVarCategory.CONSOLE, display = "Position") @Variable(name = "con_position", category = CVarCategory.CONSOLE, display = "Position des Overlays")
public ConsolePos hudPos = ConsolePos.TOP; public ConsolePos hudPos = ConsolePos.TOP;
@Variable(name = "con_opacity", category = CVarCategory.CONSOLE, min = 0x00, max = 0xff, display = "Deckkraft Hintergrund") @Variable(name = "con_opacity", category = CVarCategory.CONSOLE, min = 0x00, max = 0xff, display = "Deckkraft Hintergrund")
public int hudOpacity = 0x40; public int hudOpacity = 0x40;
@ -358,20 +362,20 @@ public class Game implements IThreadListener {
public boolean interrupted; public boolean interrupted;
public List<DisplayMode> vid_modes = Lists.newArrayList(); public List<DisplayMode> vid_modes = Lists.newArrayList();
public List<Message> messages = Lists.newArrayList(); public List<Message> messages = Lists.newArrayList();
@Variable(name = "gui_theme", category = CVarCategory.STYLE) @Variable(name = "gui_theme", category = CVarCategory.GUI, display = "Oberflächen-Design")
public Style style = Style.DEFAULT; public Style style = Style.DEFAULT;
public List<Style> themes; public List<Style> themes;
@Variable(name = "gui_scroll_lines", category = CVarCategory.GUI, min = 1, max = 10, display = "Scrollbreite", unit = "Zeilen#") @Variable(name = "gui_scroll_lines", category = CVarCategory.GUI, min = 1, max = 10, display = "Scrollbreite", unit = "Zeilen")
public int scrollLines = 3; public int scrollLines = 3;
@Variable(name = "tic_target", category = CVarCategory.PHYSICS, min = 1.0f, max = 1200.0f, callback = TickFunction.class) @Variable(name = "tic_target", category = CVarCategory.SYSTEM, min = 1.0f, max = 1200.0f, callback = TickFunction.class, display = "Tickrate")
public float tpsTarget = 20.0f; public float tpsTarget = 20.0f;
@Variable(name = "tic_timeout", category = CVarCategory.PHYSICS, min = 1, max = 60000) @Variable(name = "tic_timeout", category = CVarCategory.SYSTEM, min = 1, max = 60000, display = "Tick-Timeout-Zeit")
public int tickTimeout = 2000; public int tickTimeout = 2000;
@Variable(name = "srv_port", category = CVarCategory.WORLD, min = 0, max = 65535) @Variable(name = "srv_port", category = CVarCategory.SYSTEM, min = 0, max = 65535, display = "Standard Hosting-Port")
public int port = Config.PORT; public int port = Config.PORT;
public int bind = -1; public int bind = -1;
@ -389,6 +393,16 @@ public class Game implements IThreadListener {
private String buffer = ""; private String buffer = "";
private boolean crashed; private boolean crashed;
private final int[] tickTimes = new int[240];
private final long[] frames = new long[240];
private int tickIndex;
private int lastIndex;
private int frameCounter;
private int frameIndex;
private long frameLast;
private long frameWait;
private long startNanoTime = System.nanoTime();
private static final Game INSTANCE = new Game(); private static final Game INSTANCE = new Game();
private final Bind[] keyBindsHotbar = new Bind[] { private final Bind[] keyBindsHotbar = new Bind[] {
@ -945,6 +959,7 @@ public class Game implements IThreadListener {
if(this.drawFps) { // && !(this.open instanceof GuiConsole) if(this.drawFps) { // && !(this.open instanceof GuiConsole)
if(this.drawDebug && this.open == null) { if(this.drawDebug && this.open == null) {
this.renderStats(); this.renderStats();
this.renderLagometer();
} }
else { else {
Drawing.drawText(String.format("%s%.2f", framecode(), Timing.framerate), 0, 0, 0xffffffff); Drawing.drawText(String.format("%s%.2f", framecode(), Timing.framerate), 0, 0, 0xffffffff);
@ -2001,6 +2016,39 @@ public class Game implements IThreadListener {
GL11.glTranslatef(0.0F, 0.0F, -2000.0F); GL11.glTranslatef(0.0F, 0.0F, -2000.0F);
} }
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.server != null ? (int)this.server.getLastTick() : 0;
if(this.tickIndex == 240) {
this.tickIndex = 0;
}
}
public void run() { public void run() {
start = Window.getTime(); start = Window.getTime();
Log.SYSTEM.info("Java " + System.getProperty("java.version")); Log.SYSTEM.info("Java " + System.getProperty("java.version"));
@ -2020,11 +2068,12 @@ public class Game implements IThreadListener {
GlState.enableBlend(); GlState.enableBlend();
GlState.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); GlState.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
this.initConsole(); this.initConsole();
this.startSound(true);
this.vidMode = Window.getDisplayMode(); this.vidMode = Window.getDisplayMode();
Window.initWindow(this.saved_xpos, this.saved_ypos, this.xsize, this.ysize); Window.initWindow(this.saved_xpos, this.saved_ypos, this.xsize, this.ysize);
Window.setIcon(genTriwave(64, 64, 0x00000000, 0xffff0000, 0xff00ff00, 0xff0000ff, 0xff7f00ff, 0xff000000), 64, 64); Window.setIcon(genTriwave(64, 64, 0x00000000, 0xffff0000, 0xff00ff00, 0xff0000ff, 0xff7f00ff, 0xff000000), 64, 64);
this.sync(this.sync); this.sync(this.sync);
this.startSound(true); // this.startSound(true);
this.getVar("tic_target").setDefault(); this.getVar("tic_target").setDefault();
this.displayGuiScreen(GuiMenu.INSTANCE); this.displayGuiScreen(GuiMenu.INSTANCE);
@ -2067,6 +2116,9 @@ public class Game implements IThreadListener {
Log.flushLog(); Log.flushLog();
this.poll(); this.poll();
PerfSection.WAIT.enter(); PerfSection.WAIT.enter();
long now = System.nanoTime();
this.addFrame(now - this.startNanoTime);
this.startNanoTime = now;
while(this.syncLimited && (rtime() - Timing.tmr_current) < (1000000L / this.syncLimit)) { while(this.syncLimited && (rtime() - Timing.tmr_current) < (1000000L / this.syncLimit)) {
; ;
} }
@ -2753,6 +2805,7 @@ public class Game implements IThreadListener {
Variable value = field.getAnnotation(Variable.class); Variable value = field.getAnnotation(Variable.class);
if(value.name().isEmpty()) if(value.name().isEmpty())
throw new IllegalArgumentException("Variablenname von " + field + " kann nicht leer sein!"); throw new IllegalArgumentException("Variablenname von " + field + " kann nicht leer sein!");
field.setAccessible(true);
CVar cv; CVar cv;
VarFunction func = null; VarFunction func = null;
if(value.callback() != VarFunction.class) { if(value.callback() != VarFunction.class) {
@ -2764,6 +2817,16 @@ public class Game implements IThreadListener {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
CharValidator validator = null;
if(value.validator() != CharValidator.class) {
try {
validator = value.validator().getConstructor().newInstance();
}
catch(InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
if(field.getType() == int.class) if(field.getType() == int.class)
cv = value.type() != IntType.INT ? new ColorVar(value.name(), value.display(), field, object, value.category(), value.type() == IntType.ALPHA, (IntFunction)func) : cv = value.type() != IntType.INT ? new ColorVar(value.name(), value.display(), field, object, value.category(), value.type() == IntType.ALPHA, (IntFunction)func) :
new IntVar(value.name(), value.display(), field, object, value.category(), (int)value.min(), (int)value.max(), (IntFunction)func, value.unit(), value.precision()); new IntVar(value.name(), value.display(), field, object, value.category(), (int)value.min(), (int)value.max(), (IntFunction)func, value.unit(), value.precision());
@ -2771,6 +2834,8 @@ public class Game implements IThreadListener {
cv = new FloatVar(value.name(), value.display(), field, object, value.category(), value.min(), value.max(), (FloatFunction)func, value.unit(), value.precision()); cv = new FloatVar(value.name(), value.display(), field, object, value.category(), value.min(), value.max(), (FloatFunction)func, value.unit(), value.precision());
else if(field.getType() == boolean.class) else if(field.getType() == boolean.class)
cv = new BoolVar(value.name(), value.display(), field, object, value.category(), (BoolFunction)func); cv = new BoolVar(value.name(), value.display(), field, object, value.category(), (BoolFunction)func);
else if(field.getType() == String.class)
cv = new StringVar(value.name(), value.display(), field, object, value.category(), (int)value.max(), (StringFunction)func, validator, (int)value.min() <= 0);
else if(field.getType().isEnum()) else if(field.getType().isEnum())
cv = new EnumVar(value.name(), value.display(), field, object, value.category(), (EnumFunction)func); cv = new EnumVar(value.name(), value.display(), field, object, value.category(), (EnumFunction)func);
else else
@ -2798,6 +2863,7 @@ public class Game implements IThreadListener {
} }
regVars(this); regVars(this);
regVars(Style.CUSTOM); regVars(Style.CUSTOM);
regVars(GuiConnect.INSTANCE);
if(!config.exists()) if(!config.exists())
return; return;
@ -2964,6 +3030,40 @@ public class Game implements IThreadListener {
return base + (ping < 10000 ? String.format("%d", ping) + "ms" : String.format("%.1f", ((float)ping) / 1000.0f) + "s"); return base + (ping < 10000 ? String.format("%d", ping) + "ms" : String.format("%.1f", ((float)ping) / 1000.0f) + "s");
} }
/*
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;
}
*/
public void drawInfo() { public void drawInfo() {
NetHandlerPlayClient netHandler = this.getNetHandler(); NetHandlerPlayClient netHandler = this.getNetHandler();
if(netHandler != null) { if(netHandler != null) {
@ -3010,6 +3110,78 @@ public class Game implements IThreadListener {
GlState.disableBlend(); GlState.disableBlend();
} }
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 renderLagometer() {
// GlState.disableDepth();
int w = this.fb_x;
int h = this.fb_y;
int shifted = this.lastIndex;
int x = 0;
Drawing.drawRect(0, h - 74, 240, 60, 0x90505050);
Drawing.drawRect(0, h - 44, 240, 1, 0x5fffffff);
Drawing.drawRect(0, h - 74, 240, 1, 0x5fffffff);
while(shifted != this.frameIndex) {
int value = (int) (this.frames[shifted] / 1000000L); // , 30);
int color = this.getFrameColor(value, 0, 17, 67);
if(value > 0)
Drawing.drawRect(x, h - 14 - value, 1, value, color & 0xc0ffffff);
++x;
shifted = (shifted + 1) % 240;
}
Drawing.drawText("30ms", 2, h - 30 - 14, 0xffE0E0E0);
Drawing.drawText("60ms", 2, h - 60 - 14, 0xffE0E0E0);
Drawing.drawTextRight("ms/Frame", 238, h - 60 - 14, 0xffE0E0E0);
if(this.server != null) {
this.updateTick();
Drawing.drawRect(w - 240, h - 74, 240, 60, 0x90505050);
x = w - 240;
Drawing.drawRect(w - 240, h - 44, 240, 1, 0x5fffffff);
Drawing.drawRect(w - 240, h - 74, 240, 1, 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)
Drawing.drawRect(x, h - 14 - value, 1, value, color & 0xc0ffffff);
++x;
shifted = (shifted + 1) % 240;
}
Drawing.drawText("30ms", w - 240 + 2, h - 30 - 14, 0xffE0E0E0);
Drawing.drawText("60ms", w - 240 + 2, h - 60 - 14, 0xffE0E0E0);
Drawing.drawTextRight("ms/Tick", w - 2, h - 60 - 14, 0xffE0E0E0);
}
// GlState.enableDepth();
}
public void showDirDialog(final String title, final String def, final FileCallback callback) { public void showDirDialog(final String title, final String def, final FileCallback callback) {
new Thread(new Runnable() { new Thread(new Runnable() {
public void run() { public void run() {

View file

@ -1,391 +0,0 @@
package game;
/*
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();
}
*/
class Temp1 {
}

View file

@ -7,6 +7,8 @@ import game.gui.element.Textbox;
import game.gui.element.Textbox.Action; import game.gui.element.Textbox.Action;
import game.init.Config; import game.init.Config;
import game.network.NetHandlerPlayServer; import game.network.NetHandlerPlayServer;
import game.vars.CVarCategory;
import game.vars.Variable;
public class GuiConnect extends Gui implements Textbox.Callback { public class GuiConnect extends Gui implements Textbox.Callback {
public static final GuiConnect INSTANCE = new GuiConnect(); public static final GuiConnect INSTANCE = new GuiConnect();
@ -24,10 +26,16 @@ public class GuiConnect extends Gui implements Textbox.Callback {
private Label userLabel; private Label userLabel;
private Label passLabel; private Label passLabel;
private Label accLabel; private Label accLabel;
@Variable(name = "srv_last_address", category = CVarCategory.SYSTEM, min = 1, max = 128, display = "Letzte Server-Adresse")
private String lastAddr = ""; private String lastAddr = "";
@Variable(name = "srv_last_port", category = CVarCategory.SYSTEM, min = 0, max = 65535, display = "Letzter Server-Port")
private int lastPort = Config.PORT; private int lastPort = Config.PORT;
@Variable(name = "srv_last_user", category = CVarCategory.SYSTEM, max = NetHandlerPlayServer.MAX_USER_LENGTH, display = "Letzter Server-Nutzer", validator = NetHandlerPlayServer.UserValidator.class)
private String lastUser = ""; private String lastUser = "";
@Variable(name = "srv_last_password", category = CVarCategory.SYSTEM, max = NetHandlerPlayServer.MAX_PASS_LENGTH, display = "Letztes Server-Passwort")
private String lastPass = ""; private String lastPass = "";
@Variable(name = "srv_last_access", category = CVarCategory.SYSTEM, max = NetHandlerPlayServer.MAX_PASS_LENGTH, display = "Letzter Server-Zugang")
private String lastAcc = ""; private String lastAcc = "";
public void init(int width, int height) { public void init(int width, int height) {
@ -94,6 +102,7 @@ public class GuiConnect extends Gui implements Textbox.Callback {
this.lastUser = user; this.lastUser = user;
this.lastPass = pass; this.lastPass = pass;
this.lastAcc = acc; this.lastAcc = acc;
this.gm.setDirty();
this.gm.connect(addr, port, user, pass, acc); this.gm.connect(addr, port, user, pass, acc);
} }

View file

@ -86,7 +86,7 @@ public class GuiStyle extends GuiOptions implements Dropdown.Callback<String>, A
GuiStyle.this.gm.displayGuiScreen(GuiStyle.this); GuiStyle.this.gm.displayGuiScreen(GuiStyle.this);
} }
} }
}, "In angepassten Stil kopieren")); }, "In angepasstes Design kopieren"));
} }
else { else {
this.add(new ActButton(200, 100 + 3 * 50, 560, 24, new ActButton.Callback() { this.add(new ActButton(200, 100 + 3 * 50, 560, 24, new ActButton.Callback() {
@ -98,7 +98,7 @@ public class GuiStyle extends GuiOptions implements Dropdown.Callback<String>, A
GuiStyle.this.gm.setDirty(); GuiStyle.this.gm.setDirty();
GuiStyle.this.gm.displayGuiScreen(GuiStyle.this); GuiStyle.this.gm.displayGuiScreen(GuiStyle.this);
} }
}, "Angepassten Stil zurücksetzen")); }, "Angepasstes Design zurücksetzen"));
} }
super.init(width, height); super.init(width, height);
} }

View file

@ -17,25 +17,25 @@ public enum Style implements IStringSerializable, Displayable {
this.name = name; this.name = name;
} }
@Variable(type = IntType.COLOR, name = "color_border_top", category = CVarCategory.STYLE, display = "Umrahmung oben / l.") @Variable(type = IntType.COLOR, name = "color_border_top", category = CVarCategory.GUI, display = "Umrahmung oben / l.")
public int brdr_top; public int brdr_top;
@Variable(type = IntType.COLOR, name = "color_border_btm", category = CVarCategory.STYLE, display = "Umrahmung unten / r.") @Variable(type = IntType.COLOR, name = "color_border_btm", category = CVarCategory.GUI, display = "Umrahmung unten / r.")
public int brdr_btm; public int brdr_btm;
@Variable(type = IntType.COLOR, name = "color_button_top", category = CVarCategory.STYLE, display = "Knopf oben") @Variable(type = IntType.COLOR, name = "color_button_top", category = CVarCategory.GUI, display = "Knopf oben")
public int fill_top; public int fill_top;
@Variable(type = IntType.COLOR, name = "color_button_btm", category = CVarCategory.STYLE, display = "Knopf unten") @Variable(type = IntType.COLOR, name = "color_button_btm", category = CVarCategory.GUI, display = "Knopf unten")
public int fill_btm; public int fill_btm;
@Variable(type = IntType.COLOR, name = "color_textbox_top", category = CVarCategory.STYLE, display = "Textfeld oben") @Variable(type = IntType.COLOR, name = "color_textbox_top", category = CVarCategory.GUI, display = "Textfeld oben")
public int field_top; public int field_top;
@Variable(type = IntType.COLOR, name = "color_textbox_btm", category = CVarCategory.STYLE, display = "Textfeld unten") @Variable(type = IntType.COLOR, name = "color_textbox_btm", category = CVarCategory.GUI, display = "Textfeld unten")
public int field_btm; public int field_btm;
@Variable(type = IntType.COLOR, name = "color_label_text", category = CVarCategory.STYLE, display = "Beschriftung") @Variable(type = IntType.COLOR, name = "color_label_text", category = CVarCategory.GUI, display = "Beschriftung")
public int text_label; public int text_label;
@Variable(type = IntType.COLOR, name = "color_button_text", category = CVarCategory.STYLE, display = "Knopf Text") @Variable(type = IntType.COLOR, name = "color_button_text", category = CVarCategory.GUI, display = "Knopf Text")
public int text_base; public int text_base;
@Variable(type = IntType.COLOR, name = "color_textbox_text", category = CVarCategory.STYLE, display = "Textfeld Text") @Variable(type = IntType.COLOR, name = "color_textbox_text", category = CVarCategory.GUI, display = "Textfeld Text")
public int text_field; public int text_field;
static { static {

View file

@ -4,6 +4,11 @@ import java.io.BufferedInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ShortBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.util.opus.OpusFile;
import game.log.Log; import game.log.Log;
import game.rng.Random; import game.rng.Random;
@ -124,10 +129,6 @@ public enum SoundEvent {
SLIME_SMALL("slime_small1", "slime_small2", "slime_small3", "slime_small4", "slime_small5"); SLIME_SMALL("slime_small1", "slime_small2", "slime_small3", "slime_small4", "slime_small5");
private static final Random RANDOM = new Random(); private static final Random RANDOM = new Random();
private static final byte[] WAV_HDR = "RIFF".getBytes();
private static final byte[] WAV_HDR_FMT = "fmt ".getBytes();
private static final byte[] WAV_HDR_DATA = "data".getBytes();
private static final byte[] WAV_FORMAT = "WAVE".getBytes();
private final String[] sounds; private final String[] sounds;
private final short[][] buffers; private final short[][] buffers;
@ -138,130 +139,68 @@ public enum SoundEvent {
for(int z = 0; z < entry.sounds.length; z++) { for(int z = 0; z < entry.sounds.length; z++) {
String sound = entry.sounds[z]; String sound = entry.sounds[z];
Log.SOUND.trace("Lade Sound %s", sound); Log.SOUND.trace("Lade Sound %s", sound);
entry.buffers[z] = readWav("sounds/" + sound + ".wav", 48000, (short)1); entry.buffers[z] = readOpus("sounds/" + sound + ".opus");
} }
} }
} }
private static int read32(byte[] data, int off) { private static short[] readOpus(String filename) {
int value = 0; InputStream in = null;
for(int h = 0; h < 4; h++) { byte[] data;
value |= (int)((data[h + off]) & 0xff) << (8*h);
}
return value;
}
private static short read16(byte[] data, int off) {
short value = 0;
for(int h = 0; h < 2; h++) {
value |= (short)((data[h + off]) & 0xff) << (8*h);
}
return value;
}
private static boolean memcmp(byte[] d1, int off, byte[] d2, int size) {
for(int z = 0; z < size; z++) {
if(d1[z + off] != d2[z])
return true;
}
return false;
}
private static int readWavHeader(byte[] data, int samplerate, short channels, short bytes) {
if(memcmp(data, 0, WAV_HDR, 4)) {
Log.IO.debug("base header");
return 0;
}
int len = read32(data, 4);
if(memcmp(data, 8, WAV_FORMAT, 4)) {
Log.IO.debug("wave header");
return 0;
}
if(memcmp(data, 12, WAV_HDR_FMT, 4)) {
Log.IO.debug("fmt header");
return 0;
}
if(read32(data, 16) != 16) {
Log.IO.debug("header size");
return 0;
}
if(read16(data, 20) != 1) {
Log.IO.debug("pcm");
return 0;
}
if(read16(data, 22) != channels) {
Log.IO.debug("channels");
return 0;
}
if(read32(data, 24) != samplerate) {
Log.IO.debug("sample rate");
return 0;
}
if(read32(data, 28) != (samplerate * ((int)channels) * ((int)bytes))) {
Log.IO.debug("bitrate");
return 0;
}
if(read16(data, 32) != (channels * bytes)) {
Log.IO.debug("sample align");
return 0;
}
if(read16(data, 34) != (8 * bytes)) {
Log.IO.debug("sample size");
return 0;
}
return len;
}
private static short[] readWav(String filename, int samplerate, short channels) {
try { try {
InputStream fd = new BufferedInputStream(FileUtils.getResource(filename)); in = new BufferedInputStream(FileUtils.getResource(filename));
byte[] hdr = new byte[36]; data = FileUtils.readBytes(in);
if(fd.read(hdr, 0, 36) != 36) {
Log.IO.error("Fehler beim Lesen von WAV-Datei '%s': E/A-Fehler", filename);
fd.close();
return null;
}
if(readWavHeader(hdr, samplerate, channels, (short)2) == 0) {
Log.IO.error("Fehler beim Lesen von WAV-Datei '%s': Falsches oder fehlerhaftes Format", filename);
fd.close();
return null;
}
int len = 0;
do {
if(len != 0 && fd.skip(len) != len) {
Log.IO.error("Fehler beim Lesen von WAV-Datei '%s': E/A-Fehler", filename);
fd.close();
return null;
}
if(fd.read(hdr, 0, 8) != 8) {
Log.IO.error("Fehler beim Lesen von WAV-Datei '%s': E/A-Fehler", filename);
fd.close();
return null;
}
len = read32(hdr, 4);
}
while(memcmp(hdr, 0, WAV_HDR_DATA, 4));
int samples = len / ((int)channels * 2);
byte[] buf = new byte[samples * channels * 2];
if(fd.read(buf, 0, samples * channels * 2) != samples * channels * 2) {
Log.IO.error("Fehler beim Lesen von WAV-Datei '%s': E/A-Fehler", filename);
fd.close();
return null;
}
fd.close();
short[] sbuf = new short[samples * channels];
for(int z = 0; z < samples * channels; z++) {
sbuf[z] = read16(buf, z << 1);
}
return sbuf;
} }
catch(FileNotFoundException e) { catch(FileNotFoundException e) {
Log.IO.error("Fehler beim Öffnen von WAV-Datei '%s': Datei nicht gefunden", filename); Log.IO.error("Fehler beim Lesen von OPUS-Datei '%s': Datei nicht gefunden", filename);
return null;
} }
catch(IOException e) { catch(IOException e) {
Log.IO.error("Fehler beim Öffnen von WAV-Datei '%s': %s", filename, e.getMessage()); Log.IO.error("Fehler beim Lesen von OPUS-Datei '%s': %s", filename, e.getMessage());
return null;
} }
return null; finally {
try {
if(in != null)
in.close();
}
catch(IOException e) {
}
}
ByteBuffer buf = BufferUtils.createByteBuffer(data.length);
buf.put(data);
buf.flip();
long fd = OpusFile.op_open_memory(buf, null);
if(fd == 0L) {
Log.IO.error("Fehler beim Lesen von OPUS-Datei '%s': Falsches oder fehlerhaftes Format", filename);
return null;
}
if(OpusFile.op_channel_count(fd, -1) != 1) {
Log.IO.error("Fehler beim Lesen von OPUS-Datei '%s': Falsches Anzahl von Kanälen (!= 1)", filename);
OpusFile.op_free(fd);
return null;
}
long samples = OpusFile.op_pcm_total(fd, -1);
if(samples < 0L) {
Log.IO.error("Fehler beim Lesen von OPUS-Datei '%s': E/A-Fehler", filename);
OpusFile.op_free(fd);
return null;
}
ShortBuffer pcm = BufferUtils.createShortBuffer((int)samples);
int state;
while((state = OpusFile.op_read(fd, pcm, null)) > 0L) {
pcm.position(pcm.position() + state);
}
if(state < 0L) {
Log.IO.error("Fehler beim Lesen von OPUS-Datei '%s': E/A-Fehler", filename);
OpusFile.op_free(fd);
return null;
}
OpusFile.op_free(fd);
short[] decoded = new short[(int)samples];
pcm.rewind();
pcm.get(decoded);
return decoded;
} }
private SoundEvent(String ... sounds) { private SoundEvent(String ... sounds) {

View file

@ -140,22 +140,25 @@ public class NetHandlerPlayServer extends NetHandler implements ICrafting, Scrip
} }
} }
public static class UserValidator implements CharValidator {
public boolean valid(char ch) {
return /* (ch >= 'A' && ch <= 'Z') || */ (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == '-';
}
}
public static class NickValidator implements CharValidator {
public boolean valid(char ch) {
return (ch <= 0xff && Character.isLetterOrDigit(ch)) || (ch >= 32 && ch < 126);
}
}
public static final int MAX_USER_LENGTH = 16; public static final int MAX_USER_LENGTH = 16;
public static final int MAX_NICK_LENGTH = 32; public static final int MAX_NICK_LENGTH = 32;
public static final int MAX_PASS_LENGTH = 64; public static final int MAX_PASS_LENGTH = 64;
public static final int MAX_CMD_LENGTH = 1024; public static final int MAX_CMD_LENGTH = 1024;
public static final CharValidator VALID_USER = new CharValidator() { public static final CharValidator VALID_USER = new UserValidator();
public boolean valid(char ch) { public static final CharValidator VALID_NICK = new NickValidator();
return /* (ch >= 'A' && ch <= 'Z') || */ (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == '-';
}
};
public static final CharValidator VALID_NICK = new CharValidator() {
public boolean valid(char ch) {
return (ch <= 0xff && Character.isLetterOrDigit(ch)) || (ch >= 32 && ch < 126);
}
};
private final NetConnection connection; private final NetConnection connection;
private final Server server; private final Server server;

View file

@ -6,12 +6,9 @@ public enum CVarCategory {
SYSTEM(TextColor.RED + "system"), SYSTEM(TextColor.RED + "system"),
WINDOW(TextColor.BLUE + "window"), WINDOW(TextColor.BLUE + "window"),
GUI(TextColor.GREEN + "gui"), GUI(TextColor.GREEN + "gui"),
STYLE(TextColor.VIOLET + "style"),
RENDER(TextColor.NEON + "render"), RENDER(TextColor.NEON + "render"),
BIND(TextColor.ORANGE + "bind"), INPUT(TextColor.ORANGE + "input"),
CONSOLE(TextColor.YELLOW + "console"), CONSOLE(TextColor.YELLOW + "console"),
PHYSICS(TextColor.CYAN + "physics"),
WORLD(TextColor.MAGENTA + "world"),
SOUND(TextColor.CRIMSON + "sound"); SOUND(TextColor.CRIMSON + "sound");
private final String name; private final String name;

View file

@ -0,0 +1,82 @@
package game.vars;
import java.lang.reflect.Field;
import game.color.TextColor;
import game.gui.element.Textbox;
import game.gui.element.Textbox.Action;
import game.util.CharValidator;
public class StringVar extends BaseVar {
public static interface StringFunction extends VarFunction {
void apply(StringVar cv, String value);
}
private final StringFunction func;
private final CharValidator validator;
private final String def;
private final int maxLength;
private final boolean allowEmpty;
public StringVar(String name, String display, Field field, Object object, CVarCategory category, int maxLength, StringFunction func, CharValidator validator, boolean allowEmpty) {
super(name, display, field, object, category);
this.func = func;
this.maxLength = maxLength;
this.validator = validator;
this.allowEmpty = allowEmpty;
try {
this.def = (String)field.get(object);
}
catch(IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public String getType() {
return TextColor.YELLOW + "float";
}
public boolean parse(String str) {
if(this.validator != null)
str = this.validator.filter(str);
if(!this.allowEmpty && str.isEmpty())
return false;
if(str.length() > this.maxLength)
str = str.substring(0, this.maxLength);
try {
this.field.set(this.object, str);
}
catch(IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
}
if(this.func != null)
this.func.apply(this, str);
return true;
}
public String format() {
try {
return (String)this.field.get(this.object);
}
catch(IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public String getDefault() {
return this.def;
}
public Textbox selector(int x, int y, int w, int h) {
return new Textbox(x, y, w, h, this.maxLength, true, new Textbox.Callback() {
public void use(Textbox elem, Textbox.Action value) {
if(value == Action.SEND || value == Action.UNFOCUS) {
if(!StringVar.this.allowEmpty && elem.getText().isEmpty())
elem.setText(StringVar.this.format());
else
StringVar.this.parse(elem.getText());
}
}
}, this.validator, this.format());
}
}

View file

@ -6,6 +6,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import game.util.CharValidator;
import game.vars.BaseVar.VarFunction; import game.vars.BaseVar.VarFunction;
@Target(FIELD) @Target(FIELD)
@ -16,7 +17,7 @@ public @interface Variable {
} }
String name(); String name();
String display() default ""; String display();
CVarCategory category(); CVarCategory category();
float min() default (float)Integer.MIN_VALUE; float min() default (float)Integer.MIN_VALUE;
float max() default (float)Integer.MAX_VALUE; float max() default (float)Integer.MAX_VALUE;
@ -24,4 +25,5 @@ public @interface Variable {
int precision() default 0; int precision() default 0;
String unit() default ""; String unit() default "";
Class<? extends VarFunction> callback() default VarFunction.class; Class<? extends VarFunction> callback() default VarFunction.class;
Class<? extends CharValidator> validator() default CharValidator.class;
} }

View file

@ -182,7 +182,7 @@ public enum Bind implements IStringSerializable, CVar {
} }
public CVarCategory getCategory() { public CVarCategory getCategory() {
return CVarCategory.BIND; return CVarCategory.INPUT;
} }
public boolean parse(String str) { public boolean parse(String str) {