
CommandTable.java
- Kód: Vybrat vše
import java.util.*;
/**
*
* @author Gelidus
*/
public final class CommandTable {
/**
* Container for all table commands
*/
public Map<String, Command> CommandMap;
/**
* Constructor used to fill Command table
*/
public CommandTable(Command[] commands) {
this();
for(Command command : commands) {
this.RegisterCommand(command);
}
}
/**
* Default constructor for CommandTable
*/
public CommandTable() {
this.CommandMap = new HashMap<>();
}
/**
* Main function for Command Table, call every time you want to search for a
* command in table
*/
public boolean Call(String argument) {
String[] args = argument.split(" ");
if(this.CommandMap.containsKey(args[0])) {
return this.CommandMap.get(args[0]).Call(RemoveFirstString(args));
}
System.out.println("Command not recognized");
return false;
}
/**
* Function used to register new command for table
*/
public boolean RegisterCommand(Command command) {
if(!this.CommandMap.containsKey(command.Trigger)) {
this.CommandMap.put(command.Trigger, command);
return true;
}
return false;
}
/**
* Function used to unregister existing command for table
*/
public boolean UnregisterCommand(Command command) {
if(!this.CommandMap.containsKey(command.Trigger)) {
this.CommandMap.remove(command.Trigger);
return true;
}
return false;
}
public static String[] RemoveFirstString(String[] args) {
String[] ret = new String[args.length-1];
for(int i = 1; i < args.length; i++) {
ret[i-1] = args[i];
}
return ret;
}
}
Command.java
- Kód: Vybrat vše
/**
*
* @author Gelidus
*/
public class Command {
public String Trigger;
public CommandTable SubCommandTable;
/**
* Constructor for Command class
*/
public Command(String trigger) {
this.SubCommandTable = new CommandTable();
this.Trigger = trigger;
}
/**
* Method used by CommandTable when Command is triggered
*/
public final boolean Call(String[] args) {
if(this.SubCommandTable.CommandMap.isEmpty() || args.length == 0) {
OnCall(args);
return true;
}
if(this.SubCommandTable.CommandMap.containsKey(args[0])) {
return this.SubCommandTable.CommandMap.get(args[0]).Call(CommandTable.RemoveFirstString(args));
}
OnCall(args);
return true;
}
/**
* This should be override
*/
boolean OnCall(String[] args) {
return true;
}
}
Tak, ako bude teda vyzerať taký "example" ?
- Kód: Vybrat vše
/**
*
* @author Gelidus
*/
public class ExampleCommand extends Command {
public ExampleCommand(String Trigger) {
super(Trigger);
//Registrujeme sub-command
this.SubCommandTable.RegisterCommand(new ExampleSubCommand("druhy"));
}
@Override
boolean OnCall(String[] args) {
System.out.println("Normalny command");
return true;
}
//vytvorenie triedy pre ExampleCommand ktorej objekt zas vytvoríme v constructore ExampleCommand
public class ExampleSubCommand extends Command {
public ExampleSubCommand(String Trigger) {
super(Trigger);
}
@Override
boolean OnCall(String[] args) {
System.out.println("Subcommand");
return true;
}
}
}
Ako to teraz všetko využiť?
- Kód: Vybrat vše
public static void main(String[] args) {
CommandTable table = new CommandTable();
table.RegisterCommand(new ExampleCommand("prvy"));
InputStreamReader inputStream = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(inputStream);
System.out.print("Zadaj Command : ");
String c = "";
try {
c = reader.readLine();
}
catch(IOException ex) {
}
table.Call(c);
}
Vstupy a výstupy
Na základe tohoto kódu sme vytvorili CommandTable s jedným commandom. Tento náš Command má ešte svoj subcommand.
Ak teda vložíme do konzoli "prvy", dostaneme výstup "Normalny command"
Ak vložíme "prvy druhy", dotaneme výstup "Subcommand"
Ak vložíme "prvy bla", dostaneme ten istý výstup ako pri vložení "prvy", pretože subcommand nebol nájdený, argumenty spadajú pod hlavný command
Ak vložíme "prvy druhy treti", dostaneme výstup "Subcommand", pretože ku commandu "druhy" neexistuje žiadny ďalší subcommand s triggerom "treti", "treti" teda spadá ako argument pod tento command.
Pokiaľ by neboli niektoré časti kódu zrozumitelné, napíšte sem a ja to dopíšem

Edit : ešte k Jave a C#, často som mal pri písaní tohoto taký pocit, že v C# by som to napísal o dosť ľahšie, no možno to je aj tým časom.
Gelidus

P.S. Wolf nedá sa ti dať tie kódy bez scrollu? :/ radšej by som to dal bez scrollu do spoileru ako takto.
