ActionProcessor.java 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package eu.oschl.gui;
  2. import eu.oschl.gui.actions.Action;
  3. import eu.oschl.gui.actions.Explore;
  4. import eu.oschl.gui.exceptions.InvalidActionId;
  5. import eu.oschl.textadventure.Game;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. public class ActionProcessor extends Observable {
  9. private final ArrayList<Action> actions;
  10. public ActionProcessor(ArrayList<Action> actions) {
  11. this.actions = actions;
  12. }
  13. public ArrayList<Action> getActions() {
  14. return actions;
  15. }
  16. /**
  17. * Factory method to create a ActionProcessor with a predefined set of Actions.
  18. *
  19. * @param game the game instance to be used by the actions
  20. * @return a new ActionProcessor instance with initialized actions
  21. */
  22. public static ActionProcessor create(Game game) {
  23. ArrayList<Action> actions = new ArrayList<>(List.of(
  24. new Explore(game)
  25. ));
  26. return new ActionProcessor(actions);
  27. }
  28. /**
  29. * Executes the action of the provided ID and arguments.
  30. *
  31. * @param id the action ID
  32. * @throws InvalidActionId if the action ID is not found or is invalid
  33. */
  34. public void executeAction(String id, String[] args) throws InvalidActionId {
  35. Output.clear();
  36. for (Action action : actions) {
  37. if (action.getId().equals(id)) {
  38. action.execute(args);
  39. sendEvent();
  40. return;
  41. }
  42. }
  43. throw new InvalidActionId("Invalid action ID");
  44. }
  45. }