Skip to content

Instantly share code, notes, and snippets.

@riking
Created February 21, 2013 02:54
Show Gist options
  • Save riking/5001624 to your computer and use it in GitHub Desktop.
Save riking/5001624 to your computer and use it in GitHub Desktop.
Lots of detail has been stripped. Command is the main class, holding the lists. ModuleChoose is a sample usage.
public abstract class Command implements Comparable<Command> {
private static final Map<Command, Object> cmdSources = Collections.synchronizedMap(new HashMap<Command, Object>());
private static final Map<String, Command> cmds = Collections.synchronizedSortedMap(new TreeMap<String, Command>());
private static final List<String> aliases = Collections.synchronizedList(new ArrayList<String>());
public static enum EType {
Channel, Private, Notice, Console;
}
public static void addCommand(Object source, String name, Command command) {
cmdSources.put(command, source);
cmds.put(name,command);
aliases.add(name);
}
public static void addCommands(Object source, Command... commands) {
...
}
public static void addCommands(Object source, Map<String,Command> commands) {
...
}
public static void removeCommands(String... commands) {
for (int i = 0; i < commands.length; i++) {
Command cmd = cmds.remove(commands[i]);
if (cmd != null) {
cmdSources.remove(cmd);
aliases.remove(commands[i]);
}
}
}
public static void removeCommands(Command... commands) {
...
}
public static Command getCommand(PircBotX bot, User sender, String channel, Command.EType type, CommandCallback callback, String message) {
String[] args = message.split(" ");
if (args.length==0 || args[0].length()==0)
return null;
String cmdName = args[0];
Map<String,Command> matchMap = getCommands(cmdName, channel);
if (matchMap.size()==1)
return matchMap.values().iterator().next();
else if (matchMap.size()>1)
{
String[] keys = matchMap.keySet().toArray(new String[0]);
String s = String.format("Did you mean: %s or %s",StringTools.implode(keys, 0, keys.length-2, ", "), keys[keys.length-1]);
callback.type = EType.Notice;
callback.append(s);
}
return null;
}
public static boolean matches(Command command, PircBotX bot, EType type, String channel, String cmd) {
if (type == EType.Channel) {
if (cmd.length()<=1)
return false;
if (!Data.forChannel(channel).getString("main-cmdchar").contains(cmd.substring(0, 1)))
return false;
cmd=cmd.substring(1);
}
return getCommands(cmd, channel).keySet().size()==1;
}
public final int compareTo(Command command) {
return command().compareTo(command.command());
}
public abstract String command();
public abstract String help(PircBotX bot, EType type, Channel channel, User sender);
public void visible(PircBotX bot, EType type, Channel channel, User sender) {}
public abstract void doCommand(PircBotX bot, EType type, CommandCallback callback, Channel channel, User sender, String message);
public String toString() {
return command();
}
}
public class ModuleChoose extends Module {
public void onEnable() {
Command.addCommands(this, cmd = new CmdChoose());
}
public class CmdChoose extends Command {
public String command() {return "choose";}
public String help(PircBotX bot, EType type, Channel channel, User sender) {
return "choose {1} {2} ... {n} - makes a decision";
}
public void doCommand(PircBotX bot, EType type, CommandCallback callback, Channel channel, User sender, String message) {
if (Data.isBlacklisted(event.getUser())) return;
String[] args = message.split(" ");
if (args.length == 1) {
callback.type = EType.Notice;
callback.append(help(bot,type,channel,sender));
return;
}
......
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment