Show Call.java syntax highlighted
/*
* Copyright (C) 2001-2005 Pleasant nightmare studio
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.pleasantnightmare.riotinecity.console;
import org.pleasantnightmare.riotinecity.entities.Action;
import org.pleasantnightmare.riotinecity.entities.Entity;
import org.pleasantnightmare.riotinecity.spatials.InteractibleNode;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author deus
* @version 1.0
* @since Apr 10, 2007 2:41:40 PM
*/
public class Call extends InputProcessor {
public static final String COMMAND = "call";
private static final Pattern INPUT = Pattern.compile(" *call (.+) *");
public void process(String input) throws ProcessingError {
Matcher m = INPUT.matcher(input);
if (!m.matches())
throw new ProcessingError("Bad usage for 'call'. See help.");
InteractibleNode selectedInteractible = engine.getSelected();
if (selectedInteractible == null)
throw new ProcessingError("No selected Interactibles.");
Entity entity = selectedInteractible.getEntity();
String actionName = m.group(1);
if (hasAction(entity, actionName))
entity.action(actionName);
else
throw new ProcessingError("Selected Interactible cannot do: " + actionName);
}
private boolean hasAction(Entity entity, String actionName) {
List<Action> actions = entity.getAvailableActions();
for (Action action : actions)
if (action.getName().equals(actionName))
return true;
return false;
}
public boolean isProcessable(String input) {
return input.trim().startsWith(COMMAND);
}
public String getCommand() {
return COMMAND;
}
}
See more files for this project here