command completion

This commit is contained in:
Sen 2025-03-17 18:21:41 +01:00
parent 0839beb98e
commit a8f6af2b37
11 changed files with 400 additions and 347 deletions

View file

@ -4,43 +4,58 @@ import java.util.List;
import com.google.common.collect.Lists;
import game.Game;
import game.color.TextColor;
import game.gui.element.ActButton;
import game.gui.element.Fill;
import game.gui.element.Textbox;
import game.gui.element.Textbox.Action;
import game.gui.element.TransparentBox;
import game.log.Log;
import game.network.NetHandlerPlayServer;
import game.packet.CPacketComplete;
import game.util.ExtMath;
import game.vars.BoolVar;
import game.vars.CVar;
import game.window.Keysym;
public class GuiConsole extends Gui implements Textbox.Callback {
public static final GuiConsole INSTANCE = new GuiConsole();
private final List<String> sentMessages = Lists.<String>newArrayList();
private boolean full;
private String historyBuffer = "";
private int sentHistoryCursor = -1;
private boolean playerNamesFound;
private boolean waitingOnAutocomplete;
private boolean reverse;
private String prefixFirst;
private int autocompleteIndex;
private List<String> foundPlayerNames = Lists.<String>newArrayList();
private Textbox inputField;
private TransparentBox logBox;
public GuiConsole setFull(boolean full) {
this.full = full;
return this;
}
public void init(int width, int height) {
this.addSelector("con_autoclose", 0, 0, 160, 24);
this.addSelector("con_timestamps", 160, 0, 160, 24);
this.addSelector("con_loglevel", 320, 0, 160, 24);
this.add(new ActButton(480, 0, 160, 24, new ActButton.Callback() {
public void use(ActButton elem, ActButton.Mode action) {
GuiConsole.this.reset();
GuiConsole.this.setLog(GuiConsole.this.gm.getBuffer());
}
}, "Löschen"));
this.logBox = this.add(new TransparentBox(0, 24, width, height - 48, this.gm.getBuffer()));
this.add(new Fill(640, 0, width - 640, 24));
if(this.full) {
this.addSelector("con_autoclose", 0, 0, 160, 24);
this.addSelector("con_timestamps", 160, 0, 160, 24);
this.addSelector("con_loglevel", 320, 0, 160, 24);
this.add(new ActButton(480, 0, 160, 24, new ActButton.Callback() {
public void use(ActButton elem, ActButton.Mode action) {
GuiConsole.this.reset();
GuiConsole.this.setLog(GuiConsole.this.gm.getBuffer());
}
}, "Löschen"));
}
this.logBox = this.add(new TransparentBox(0, this.full ? 24 : 0, width, height - (this.full ? 48 : 24), this.gm.getBuffer()));
if(this.full)
this.add(new Fill(640, 0, width - 640, 24));
this.inputField = this.add(new Textbox(0, height - 24, width, 24, NetHandlerPlayServer.MAX_CMD_LENGTH, true, this, ""));
this.inputField.setSelected();
this.sentHistoryCursor = this.sentMessages.size();
@ -69,7 +84,7 @@ public class GuiConsole extends Gui implements Textbox.Callback {
public void key(Keysym key, boolean ctrl, boolean shift) {
super.key(key, ctrl, shift);
// this.waitingOnAutocomplete = false;
if(key != Keysym.TAB)
if(key != Keysym.TAB && key != Keysym.LEFT_SHIFT && key != Keysym.RIGHT_SHIFT)
this.playerNamesFound = false;
}
@ -77,8 +92,9 @@ public class GuiConsole extends Gui implements Textbox.Callback {
{
this.waitingOnAutocomplete = false;
if ((value == Action.FORWARD || value == Action.BACKWARD) && this.gm.thePlayer != null)
if (value == Action.FORWARD || value == Action.BACKWARD)
{
this.reverse = value == Action.BACKWARD;
this.autocompletePlayerNames();
}
else
@ -98,13 +114,14 @@ public class GuiConsole extends Gui implements Textbox.Callback {
{
if(this.sentMessages.isEmpty() || !((String)this.sentMessages.get(this.sentMessages.size() - 1)).equals(s))
this.sentMessages.add(s);
this.sentHistoryCursor = this.sentMessages.size();
this.gm.exec(s);
// if(this.gm.thePlayer != null)
// this.gm.thePlayer.sendQueue.addToSendQueue(new CPacketMessage(CPacketMessage.Type.CHAT, s));
}
this.inputField.setText("");
if(this.gm.conAutoclose && this.gm.theWorld != null)
if((this.gm.conAutoclose || !this.full) && this.gm.theWorld != null)
this.gm.displayGuiScreen(null);
}
}
@ -120,6 +137,16 @@ public class GuiConsole extends Gui implements Textbox.Callback {
this.inputField.insertText(newChatText);
}
}
private void addMessage(String msg) {
String buffer = this.gm.getBuffer();
if((buffer.length() + msg.length() + 2) > Game.LOG_BUFFER) {
int offset = (msg.length() + 2) > 1024 ? (msg.length() + 2) : 1024;
int nl = buffer.indexOf('\n', offset);
buffer = nl >= 0 ? buffer.substring(nl + 1) : "";
}
this.setLog(buffer + "\n" + TextColor.RESET + msg);
}
public void autocompletePlayerNames()
{
@ -131,6 +158,10 @@ public class GuiConsole extends Gui implements Textbox.Callback {
{
this.autocompleteIndex = 0;
}
else if (this.autocompleteIndex < 0)
{
this.autocompleteIndex = this.foundPlayerNames.size() - 1;
}
}
else
{
@ -139,7 +170,11 @@ public class GuiConsole extends Gui implements Textbox.Callback {
this.autocompleteIndex = 0;
String s = this.inputField.getText().substring(i).toLowerCase();
String s1 = this.inputField.getText().substring(0, this.inputField.getCursorPosition());
this.sendAutocompleteRequest(s1, s);
String[] localMatches = this.sendAutocompleteRequest(s1, s);
if(localMatches != null) {
this.onAutocompleteResponse(localMatches);
return;
}
if (this.foundPlayerNames.isEmpty())
{
@ -164,13 +199,42 @@ public class GuiConsole extends Gui implements Textbox.Callback {
stringbuilder.append(s2);
}
Log.CONSOLE.user(stringbuilder.toString());
this.addMessage(stringbuilder.toString());
}
this.inputField.insertText((String)this.foundPlayerNames.get(this.autocompleteIndex++));
this.inputField.insertText((String)this.foundPlayerNames.get(this.autocompleteIndex));
this.autocompleteIndex += this.reverse ? -1 : 1;
}
private void sendAutocompleteRequest(String currentText, String newText)
private String[] complete(String s) {
List<String> list = Lists.<String>newArrayList();
String[] argv = s.split(" ", -1);
String pre = argv[0];
s = argv[argv.length - 1];
Iterable<String> res = pre.startsWith("#") ?
(argv.length == 1 ? this.gm.getVars() : (argv.length == 2 ? getVarCompletion(argv[0].substring(1)) : Lists.newArrayList())) :
(this.gm.thePlayer == null ? Lists.newArrayList() : this.gm.thePlayer.sendQueue.getPlayerNames());
if(argv.length == 1 && pre.startsWith("#"))
s = s.substring(1);
for(String s1 : res) {
if(s1.regionMatches(true, 0, s, 0, s.length()))
list.add((argv.length == 1 && pre.startsWith("#") ? "#" : "") + s1);
}
return list.isEmpty() ? null : list.toArray(new String[list.size()]);
}
private List<String> getVarCompletion(String var) {
CVar cv = this.gm.getVar(var);
if(cv == null)
return Lists.newArrayList();
if(cv instanceof BoolVar)
return Boolean.parseBoolean(cv.format()) ? Lists.newArrayList("false", "true") : Lists.newArrayList("true", "false");
else
return Lists.newArrayList(cv.getDefault());
// return Lists.newArrayList();
}
private String[] sendAutocompleteRequest(String currentText, String newText)
{
if (currentText.length() >= 1)
{
@ -185,10 +249,23 @@ public class GuiConsole extends Gui implements Textbox.Callback {
// {
// eid = this.gm.pointed.entity.getId();
// }
this.gm.thePlayer.sendQueue.addToSendQueue(new CPacketComplete(currentText));
this.waitingOnAutocomplete = true;
if(currentText.startsWith("/")) {
if(this.gm.thePlayer != null) {
currentText = currentText.substring(1);
this.prefixFirst = currentText.split(" ", -1).length == 1 ? "/" : null;
this.gm.thePlayer.sendQueue.addToSendQueue(new CPacketComplete(currentText));
this.waitingOnAutocomplete = true;
}
}
else {
String[] comp = this.complete(currentText);
this.prefixFirst = null;
if(comp != null)
this.waitingOnAutocomplete = true;
return comp;
}
}
return null;
}
private void getSentHistory(int msgPos)
@ -224,8 +301,11 @@ public class GuiConsole extends Gui implements Textbox.Callback {
this.playerNamesFound = false;
this.foundPlayerNames.clear();
for (String s : choices)
for (int z = 0; z < choices.length; z++)
{
String s = choices[z];
if(this.prefixFirst != null)
choices[z] = s = this.prefixFirst + s;
if (s.length() > 0)
{
this.foundPlayerNames.add(s);