001 /** 002 * Contains methods common to all types of chat commands. 003 * @author lightweight 004 * 005 */ 006 public abstract class BaseCommand { 007 public final String tooltip; 008 public final String errorMessage; 009 public final int minParam; 010 public final int maxParam; 011 012 public BaseCommand(String tooltip) { 013 this(tooltip, "Undefined", 0); 014 } 015 016 public BaseCommand(String tooltip, String errorMessage, int minParam) { 017 this(tooltip, errorMessage, minParam, 0); 018 } 019 020 public BaseCommand(String tooltip, String errorMessage, int minParam, int maxParam) { 021 this.tooltip = tooltip; 022 this.errorMessage = errorMessage; 023 this.minParam = minParam; 024 this.maxParam = maxParam; 025 } 026 027 public boolean parseCommand(MessageReceiver caller, String[] parameters) { 028 if (parameters.length < minParam || (parameters.length > maxParam && maxParam != 0)) { 029 onBadSyntax(caller, parameters); 030 return false; 031 } 032 execute(caller, parameters); 033 return true; 034 } 035 036 public void onBadSyntax(MessageReceiver caller, String[] parameters) { 037 if (errorMessage != "") 038 caller.notify(errorMessage); 039 } 040 041 /** 042 * Executes a command. 043 * Note: should not be called directly. Use parseCommand() instead! 044 * @param player 045 * @param parameters 046 */ 047 abstract void execute(MessageReceiver caller, String[] parameters); 048 }