1
0
mirror of https://github.com/moparisthebest/Yaaic synced 2024-11-29 12:22:21 -05:00

Commands can throw CommandExceptions

This commit is contained in:
Sebastian Kaspari 2010-03-09 20:13:29 +01:00
parent 4c3fb69a02
commit 9b572792ea
4 changed files with 38 additions and 27 deletions

View File

@ -38,8 +38,9 @@ public abstract class BaseCommand
* @param server The server object * @param server The server object
* @param channel The channel object or null if no channel is selected * @param channel The channel object or null if no channel is selected
* @param service The service with all server connections * @param service The service with all server connections
* @throws CommandException if command couldn't be executed
*/ */
public abstract void execute(String[] params, Server server, Channel channel, IRCService service); public abstract void execute(String[] params, Server server, Channel channel, IRCService service) throws CommandException;
/** /**
* Get the usage description for this command * Get the usage description for this command
@ -47,13 +48,4 @@ public abstract class BaseCommand
* @return The usage description * @return The usage description
*/ */
public abstract String getUsage(); public abstract String getUsage();
/**
* How much params does this command need?
*
* Default: 0
*
* @return The number of params needed
*/
public abstract int needsParams();
} }

View File

@ -0,0 +1,20 @@
package org.yaaic.command;
/**
* The CommandException is thrown on command execution if the
* command couldn't be executed due to invalid params
*
* @author Sebastian Kaspari
*/
public class CommandException extends Throwable
{
private static final long serialVersionUID = -8317993941455253288L;
/**
* Create a new CommandException object
*/
public CommandException(String message)
{
super(message);
}
}

View File

@ -28,6 +28,7 @@ import org.yaaic.model.Channel;
import org.yaaic.model.Message; import org.yaaic.model.Message;
import org.yaaic.model.Server; import org.yaaic.model.Server;
import android.R.color;
import android.content.Intent; import android.content.Intent;
/** /**
@ -90,14 +91,18 @@ public class CommandParser
if (isCommand(type)) { if (isCommand(type)) {
BaseCommand command = commands.get(type); BaseCommand command = commands.get(type);
if (command.needsParams() > 0 && params.length - 1 == command.needsParams()) { try {
command.execute(params, server, channel, service); command.execute(params, server, channel, service);
} else { } catch(CommandException e) {
// Wrong number of params // Wrong number of params
if (channel != null) { if (channel != null) {
Message message = new Message("Usage of " + type + ": " + command.getUsage()); Message errorMessage = new Message(type + ": " + e.getMessage());
message.setColor(Message.COLOR_RED); errorMessage.setColor(Message.COLOR_RED);
channel.addMessage(message); channel.addMessage(errorMessage);
Message usageMessage = new Message("Usage of " + type + ": " + command.getUsage());
//usageMessage.setColor(Message.COLOR_RED);
channel.addMessage(usageMessage);
Intent intent = new Intent(Broadcast.CHANNEL_MESSAGE); Intent intent = new Intent(Broadcast.CHANNEL_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId()); intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());

View File

@ -35,9 +35,13 @@ public class NickCommand extends BaseCommand
* Execute /nick * Execute /nick
*/ */
@Override @Override
public void execute(String[] params, Server server, Channel channel, IRCService service) public void execute(String[] params, Server server, Channel channel, IRCService service) throws CommandException
{ {
service.getConnection(server.getId()).changeNick(params[1]); if (params.length == 2) {
service.getConnection(server.getId()).changeNick(params[1]);
} else {
throw new CommandException("Invalid number of params");
}
} }
/** /**
@ -48,14 +52,4 @@ public class NickCommand extends BaseCommand
{ {
return "/nick nickname"; return "/nick nickname";
} }
/**
* Number of params needed (1)
*/
@Override
public int needsParams()
{
return 1;
}
} }