mirror of
https://github.com/2006-Scape/2006Scape.git
synced 2026-07-03 00:31:51 +00:00
Implemented Command Processor (#375)
* Added CommandConsole and CommandProcessor Interface * Added two commands, ListPlayers and Stop * Registered commands with CommandConsole * Added request for console from main. Made help prompt not print after requesting help... * Moved some files around You didn't see anything * Bug fix * swap `break;` for `continue;` * *cough* bug fix
This commit is contained in:
@@ -13,6 +13,7 @@ import org.apache.mina.common.IoAcceptor;
|
|||||||
import org.apache.mina.transport.socket.nio.SocketAcceptor;
|
import org.apache.mina.transport.socket.nio.SocketAcceptor;
|
||||||
import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
|
import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
|
||||||
|
|
||||||
|
import com.rebotted.console.CommandConsole;
|
||||||
import com.rebotted.event.CycleEventHandler;
|
import com.rebotted.event.CycleEventHandler;
|
||||||
import com.rebotted.game.content.minigames.FightCaves;
|
import com.rebotted.game.content.minigames.FightCaves;
|
||||||
import com.rebotted.game.content.minigames.FightPits;
|
import com.rebotted.game.content.minigames.FightPits;
|
||||||
@@ -213,6 +214,8 @@ public class GameEngine {
|
|||||||
}
|
}
|
||||||
}, 0, GameConstants.CYCLE_TIME, TimeUnit.MILLISECONDS);
|
}, 0, GameConstants.CYCLE_TIME, TimeUnit.MILLISECONDS);
|
||||||
|
|
||||||
|
CommandConsole.getInstance();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
while (!scheduler.awaitTermination(60, TimeUnit.SECONDS)) {
|
while (!scheduler.awaitTermination(60, TimeUnit.SECONDS)) {
|
||||||
// TODO
|
// TODO
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package com.rebotted.console;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import com.rebotted.GameConstants;
|
||||||
|
import com.rebotted.console.commands.ListPlayers;
|
||||||
|
import com.rebotted.console.commands.Stop;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author RS-Emulators
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class CommandConsole implements Runnable {
|
||||||
|
|
||||||
|
private static CommandConsole cc;
|
||||||
|
private Thread self;
|
||||||
|
private Scanner scanner;
|
||||||
|
// Lazy, not intending to add runtime class loading.
|
||||||
|
private ArrayList<CommandProcessor> cmds = new ArrayList<CommandProcessor>();
|
||||||
|
|
||||||
|
private CommandConsole() {
|
||||||
|
cmds.add(new ListPlayers());
|
||||||
|
cmds.add(new Stop());
|
||||||
|
|
||||||
|
scanner = new Scanner(System.in);
|
||||||
|
cc = this;
|
||||||
|
self = new Thread(cc);
|
||||||
|
self.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
System.out.println("Welcome to " + GameConstants.SERVER_NAME + ".");
|
||||||
|
while (true) {
|
||||||
|
System.out.print("> ");
|
||||||
|
String input = null;
|
||||||
|
try {
|
||||||
|
input = scanner.nextLine();
|
||||||
|
String[] splited = input.split("\\s+");
|
||||||
|
if (splited[0].isEmpty()) {
|
||||||
|
System.out.println("Command not recognized. Try 'help'.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (splited[0].equalsIgnoreCase("help")) {
|
||||||
|
for (CommandProcessor cmd : cmds) {
|
||||||
|
System.out.println(cmd.help());
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (CommandProcessor cmd : cmds) {
|
||||||
|
if (cmd.command(splited)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (NoSuchElementException | NullPointerException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CommandConsole getInstance() {
|
||||||
|
if (cc == null) {
|
||||||
|
cc = new CommandConsole();
|
||||||
|
}
|
||||||
|
return cc;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.rebotted.console;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author RS-Emulators
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public interface CommandProcessor {
|
||||||
|
|
||||||
|
public boolean command(String[] cmd);
|
||||||
|
|
||||||
|
public String help();
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package com.rebotted.console.commands;
|
||||||
|
|
||||||
|
import com.rebotted.console.CommandProcessor;
|
||||||
|
import com.rebotted.game.players.Player;
|
||||||
|
import com.rebotted.game.players.PlayerHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author RS-Emulators
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ListPlayers implements CommandProcessor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean command(String[] cmd) {
|
||||||
|
if (!cmd[0].equalsIgnoreCase("list")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
System.out.println("Currently online: " + PlayerHandler.getPlayerCount());
|
||||||
|
for (Player p : PlayerHandler.players) {
|
||||||
|
System.out.print(p.playerName + ",");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String help() {
|
||||||
|
return "list - lists online players.";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package com.rebotted.console.commands;
|
||||||
|
|
||||||
|
import com.rebotted.GameEngine;
|
||||||
|
import com.rebotted.console.CommandProcessor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author RS-Emulators
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class Stop implements CommandProcessor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean command(String[] cmd) {
|
||||||
|
if (!cmd[0].equalsIgnoreCase("stop")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
System.out.println("Setting shutdown flag to true...");
|
||||||
|
GameEngine.shutdownServer = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String help() {
|
||||||
|
return "stop - sets GameEngine.shutdownServer to true.";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user