Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Console! #778

Closed
wants to merge 1 commit into from

6 participants

@kylecbrodie

A functioning console with a few predefined commands

@kylecbrodie kylecbrodie Console!
A functioning console with a few predefined commands
1c0596e
@Mebibyte

+1

Although, I used /quit 1, and I got an error in Eclipse.

Exception in thread "Thread-3" java.lang.NullPointerException
at paulscode.sound.SoundSystem.setListenerPosition(SoundSystem.java:1282)
at com.mojang.mojam.sound.SoundPlayer.setListenerPosition(SoundPlayer.java:149)
at com.mojang.mojam.MojamComponent.render(MojamComponent.java:432)
at com.mojang.mojam.MojamComponent.run(MojamComponent.java:389)
at java.lang.Thread.run(Unknown Source)

@mkalam-alami
Collaborator

What about #703?

@master-lincoln
Collaborator

+1 I like the implementation...

@Maescool
Owner

@Mebibyte That's a soundsystem error, not rellevant with this code.
@KyleBrodie please sync up, we else can't merge

@Maescool
Owner

anybody prefer this or #703?

@kylecbrodie

I prefer this :P I believe that #703 lacked a good abstraction for commands. The commands should be self-contained and work on objects rather than commands being completed by objects.
@Mebibyte I knew about that but I didn't include it because the /exit was an example command and I just wanted to include the functioning console in this merge

@Maescool I think I am synced. Using GitHub for Mac and did a sync cycle

@Maescool
Owner

@KyleBrodie you might be sync with your fork, but your fork is not in sync with this repo
github for mac doesn't support that sync (yet)
the only way is to merge via terminal..
git pull origin/develop
and see where possible merge errors are, fix the files, and resync to your fork

@Shephiroth

Kyle, 703 didnt lack abstraction, use a diferent way of implementing it. Mine use an interface, yours use an abstract class. Both do their work.

The main difference is probably the /help command, you coded a way to list commands. Since this is a dev tool i never though that to be nedded.

@kylecbrodie

I apologize. I made a second pull request so that the changes can be pulled as I cannot figure out how to sync my branch feature/console with origin/develop

@kylecbrodie kylecbrodie closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 5, 2012
  1. @kylecbrodie

    Console!

    kylecbrodie authored
    A functioning console with a few predefined commands
This page is out of date. Refresh to see the latest.
View
314 src/com/mojang/mojam/Console.java
@@ -0,0 +1,314 @@
+package com.mojang.mojam;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.util.ArrayList;
+
+import com.mojang.mojam.gui.Font;
+import com.mojang.mojam.screen.Screen;
+
+public class Console implements KeyListener {
+
+ /***
+ * Maximum amount of verbose data kept in the console
+ * also the number of lines of data displayed
+ */
+ public static final int MAX_LINES = 20;
+
+ /***
+ * Maximum number of characters allowed to input into the console
+ */
+ public static final int MAX_INPUT_LENGTH = 60;
+
+ private ArrayList<String> verboseData = new ArrayList<String>(MAX_LINES);
+
+ private String typing = "";
+ private String input = null;
+ private boolean completedInput;
+
+ private boolean open;
+
+ /***
+ * Left padding size when drawing console text
+ */
+ public static final int xOffset = 5;
+
+ /***
+ * Top padding size when drawing console text. affects console height
+ */
+ public static final int yOffset = 5;
+
+ /***
+ * Logs the verbose info into the console
+ *
+ * @param s information to display in console
+ */
+ public void log(String s) {
+ if(s == null) return;
+
+ if(verboseData.size() + 1 > MAX_LINES)
+ verboseData.remove(verboseData.size() - 1);
+
+ verboseData.add(0,s);
+ }
+
+ /***
+ * Closes the console and cancels current input
+ */
+ public void close() {
+ typing = "";
+ input = null;
+ completedInput = false;
+ open = false;
+ }
+
+ /***
+ * Opens the console
+ */
+ public void open() {
+ open = true;
+ }
+
+ /***
+ * Toggles between open and close.
+ * equivalent to:
+ * if(isOpen())
+ * close();
+ * else
+ * open();
+ */
+ public void toggle() {
+ if(open)
+ close();
+ else
+ open();
+ }
+
+ /***
+ * Tells if the console is open or not
+ * @return the answer
+ */
+ public boolean isOpen() {
+ return open;
+ }
+
+ /***
+ * renders the console on the screen if it is open
+ * screen space it takes up is (MAX_LINES+1) * Font.FONT_WHITE_SMALL + yOffset
+ *
+ * @param s screen to render to
+ */
+ public void render(Screen s) {
+ if(open) {
+ int fontHeight = Font.FONT_WHITE_SMALL.getFontHeight();
+ int consoleHeight = (MAX_LINES + 1) * fontHeight + yOffset; //+1 for the input line
+
+ s.alphaFill(0, 0, s.w, consoleHeight, 0xff000000, 0x50); //50% black I believe. took it from PauseMenu and changed 0x30 to 0x50
+
+ Font.FONT_WHITE_SMALL.draw(s, typing + "|", xOffset,(consoleHeight -= fontHeight)); //draws bottom up starting with typing
+
+ for(int i = 0; i < verboseData.size(); i++) {
+ Font.FONT_WHITE_SMALL.draw(s, verboseData.get(i), xOffset, (consoleHeight -= fontHeight) ); // and then the verbose data in order of newest first
+ }
+ }
+ }
+
+ /***
+ * checks if the user has inputed anything
+ * unnecessary if the console is closed
+ */
+ public void tick() {
+ if(completedInput) {
+ processInput(input);
+ }
+ }
+
+ private void processInput(String input) {
+ log(">" + input);
+ String command = getCommand(input);
+
+ if(command.startsWith("/")) {
+ doCommand(command, input);
+ } else {
+ chat.doCommand(new String[]{input});
+ }
+
+ completedInput = false;
+ }
+
+ private String getCommand(String input) {
+ if(!input.contains(" ")) {
+ return input;
+ } else {
+ return input.substring(0, input.indexOf(' '));
+ }
+ }
+
+ /***
+ * Execute a console command
+ * if no command has that name nothing will be done
+ *
+ * @param command command name
+ * @param input arguments for the command separated by spaces
+ */
+ public void doCommand(String command, String input) {
+ if(command.charAt(0) == '/')
+ command = command.substring(1); //remove forward slash
+
+ for(Command c : Command.commands) {
+
+ if(c != null && c.name.equals(command)) {
+
+ String[] args = getArgs(input,c.numberOfArgs);
+ c.doCommand(args);
+ return;
+ }
+ }
+ }
+
+ private String[] getArgs(String input, int numberOfArgs) {
+ if(numberOfArgs == -1) { //see Command NumberOfArgs for reason
+ if(!input.contains(" ")) {
+ return new String[]{""};
+ } else {
+ return new String[]{removeCommand(input)};
+ }
+ }
+
+ if(numberOfArgs <= 0) return null;
+
+ String[] args = new String[numberOfArgs];
+ input = removeCommand(input);
+ if(numberOfArgs == 1) return new String[]{input};
+
+ for(int i = 0; i < numberOfArgs; i++) {
+ int index = input.indexOf(' ');
+
+ if(index > 0) {
+ args[i] = input.substring(0, index);
+ input = input.substring(index+1);
+ }
+ }
+ return args;
+ }
+
+ private String removeCommand(String input) {
+ if(input.charAt(0) != '/') {
+ return input;
+ }
+ if(!input.contains(" ")) {
+ return input;
+ }
+ return input.substring(input.indexOf(' ') + 1);
+ }
+
+ public void keyTyped(KeyEvent e) {
+ if(open) {
+ switch(e.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ case KeyEvent.VK_ENTER:
+ break;
+ case KeyEvent.VK_BACK_SPACE:
+ if(typing.length() > 0)
+ typing = typing.substring(0, typing.length()-1);
+ break;
+ default:
+ if(typing.length() < MAX_INPUT_LENGTH)
+ typing += e.getKeyChar();
+ break;
+ }
+ }
+ }
+
+ public void keyPressed(KeyEvent e) {
+ if(open) {
+ if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
+ if(typing.length() > 0)
+ typing = typing.substring(0, typing.length()-1);
+ }
+ }
+ }
+
+ public void keyReleased(KeyEvent e) {
+ if(open) {
+ switch(e.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ typing = "";
+ input = null;
+ break;
+ case KeyEvent.VK_ENTER:
+ typing = typing.trim();
+ if(!typing.equals("")) {
+ input = typing;
+ completedInput = true;
+ }
+ typing = "";
+ break;
+ case KeyEvent.VK_BACK_SPACE:
+ if(typing.length() > 0)
+ typing = typing.substring(0, typing.length()-1);
+ break;
+ }
+ }
+ }
+
+ /***
+ * List of possible commands
+ */
+ private Command help = new Command("help", 0, "Displays all possible commands") {
+ public void doCommand(String[] args) {
+ log("All Commands");
+ log("--------------");
+ for(int i = 0; i < Command.commands.size(); i++) {
+ Command c = Command.commands.get(i);
+ if(c != null)
+ log(c.name + " : " + c.helpMessage);
+ }
+ }
+ };
+
+ private Command pause = new Command("pause", 0, "Pauses the game") {
+ public void doCommand(String[] args) {
+ close();
+ MojamComponent.instance.synchronizer.addCommand(new com.mojang.mojam.network.PauseCommand(true));
+ }
+ };
+
+ private Command exit = new Command("exit", 1, "exits the game. 0 force exit, 1 regular game exit") {
+ public void doCommand(String[] args) {
+ if(args.length > 0 && args[0].equals("0"))
+ System.exit(0);
+ else
+ MojamComponent.instance.stop();
+ }
+ };
+
+ private Command chat = new Command("chat", -1, "Does the same as pressing T and typing in the after /chat and pressing enter") {
+ public void doCommand(String[] args) {
+ String msg = "";
+ for(int i = 0; i < args.length-1; i++) {
+ msg += args[i] + " ";
+ }
+ msg += args[args.length-1];
+ MojamComponent.instance.synchronizer.addCommand(new com.mojang.mojam.network.packet.ChatCommand(msg));
+ }
+ };
+
+ public abstract static class Command {
+
+ public String name;
+ public String helpMessage;
+ public int numberOfArgs; //-1 args means return raw input data minus the command
+ public static ArrayList<Command> commands = new ArrayList<Command>();
+
+ public Command(String name, int numberOfArgs, String helpMessage) {
+ this.name = name;
+ this.numberOfArgs = numberOfArgs;
+ this.helpMessage = helpMessage;
+ commands.add(this);
+ }
+
+ public abstract void doCommand(String[] args);
+ }
+
+}
View
3  src/com/mojang/mojam/InputHandler.java
@@ -33,6 +33,9 @@ public InputHandler(Keys keys) {
initKey(keys.screenShot, KeyEvent.VK_F2);
initKey(keys.fullscreen, KeyEvent.VK_F11);
initKey(keys.chat, KeyEvent.VK_T);
+
+ //console
+ initKey(keys.console, KeyEvent.VK_TAB);
}
private void initKey(Key key, int defaultKeyCode) {
View
1  src/com/mojang/mojam/Keys.java
@@ -52,6 +52,7 @@ public void release() {
public Key sprint = new Key("sprint");
public Key screenShot = new Key("screenShot");
public Key chat = new Key("chat");
+ public Key console = new Key("console");
public void tick() {
for (Key key : all)
View
24 src/com/mojang/mojam/MojamComponent.java
@@ -111,6 +111,7 @@
public static Screen screen = new Screen(GAME_WIDTH, GAME_HEIGHT);
private Level level;
private Chat chat = new Chat();
+ public Console console = new Console();
// Latency counter
private static final int CACHE_EMPTY=0, CACHE_PRIMING=1, CACHE_PRIMED=2;
@@ -168,6 +169,7 @@ public MojamComponent() {
addMenu(menu);
addKeyListener(this);
addKeyListener(chat);
+ addKeyListener(console);
instance = this;
LevelList.createLevelList();
@@ -453,6 +455,10 @@ private synchronized void render(Graphics g) {
if (isMultiplayer && menuStack.isEmpty()) {
chat.render(screen);
}
+
+ if(console.isOpen() && menuStack.isEmpty()) {
+ console.render(screen);
+ }
g.setColor(Color.BLACK);
@@ -531,6 +537,21 @@ private void renderMouse(Screen screen, MouseButtons mouseButtons) {
}
private void tick() {
+ //console open/close
+ if (this.isFocusOwner() && level != null) {
+ keys.console.tick();
+ if (keys.console.wasPressed()) {
+ console.toggle();
+ paused = !paused;
+ }
+ if(console.isOpen()) {
+ if(menuStack.isEmpty()) {
+ keys.release();
+ mouseButtons.releaseAll();
+ }
+ console.tick();
+ }
+ }
//Not-In-Focus-Pause
if (level != null && !isMultiplayer && !paused && !this.isFocusOwner()) {
keys.release();
@@ -585,8 +606,7 @@ private void tick() {
if (level == null) {
mouseButtons.tick();
- } else
- if (level != null) {
+ } else {
if (synchronizer.preTurn()) {
synchronizer.postTurn();
View
3  src/com/mojang/mojam/gui/KeyBindingsMenu.java
@@ -110,6 +110,8 @@ private void addButtons() {
* BUTTON_SPACING));
addButton(new KeyBindingButton(TitleMenu.KEY_FIRE_ID, keys.fire, tab1, yOffset + 5
* BUTTON_SPACING));
+ addButton(new KeyBindingButton(TitleMenu.KEY_CONSOLE_ID, keys.console, tab1, yOffset + 6
+ * BUTTON_SPACING));
addButton(new KeyBindingButton(TitleMenu.KEY_FIRE_UP_ID, keys.fireUp, tab2, yOffset + 0
* BUTTON_SPACING));
addButton(new KeyBindingButton(TitleMenu.KEY_FIRE_DOWN_ID, keys.fireDown, tab2, yOffset + 1
@@ -151,6 +153,7 @@ public void render(Screen screen) {
write(screen, txts.getStatic("keys.right"), 0, 3);
write(screen, txts.getStatic("keys.sprint"), 0, 4);
write(screen, txts.getStatic("keys.fire"), 0, 5);
+ write(screen, "CONSOLE", 0, 6); //add translations
write(screen, txts.getStatic("keys.fireUp"), 1, 0);
write(screen, txts.getStatic("keys.fireDown"), 1, 1);
View
1  src/com/mojang/mojam/gui/TitleMenu.java
@@ -57,6 +57,7 @@
public static final int KEY_FIRE_DOWN_ID = 3012;
public static final int KEY_FIRE_LEFT_ID = 3013;
public static final int KEY_FIRE_RIGHT_ID = 3014;
+ public static final int KEY_CONSOLE_ID = 3015;
public static final int CREDITS_ID = 4000;
public static final int CREDITS_TITLE_ID = 4001;
Something went wrong with that request. Please try again.