package com.github.dtschust.zork.repl.actions; import com.github.dtschust.zork.parser.ZorkGame; import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.types.ZorkContainer; import com.github.dtschust.zork.types.ZorkRoom; import java.util.List; public class TakeAction extends Action { @Override public boolean matchesInput(List arguments) { return arguments.get(0).equals("take"); } @Override public int getMinimumArgCount() { return 2; } @Override public void run(ZorkGame game, List arguments) { final String tempString = arguments.get(1); if ((game.getCurrentRoom()).item.contains(tempString)) { game.inventory.add(tempString); ZorkRoom tempRoom = (game.getCurrentRoom()); tempRoom.item.remove(tempString); game.put("room", tempRoom); System.out.println("Item " + tempString + " added to inventory."); } else { /* Search all containers in the current room for the item! */ for (String key : game.getCurrentRoom().container) { ZorkContainer tempContainer = (ZorkContainer) game.get("container", key); if (tempContainer != null && tempContainer.isOpen() && tempContainer.item.contains(tempString)) { game.inventory.add(tempString); tempContainer.item.remove(tempString); game.put("container", tempContainer); System.out.println("Item " + tempString + " added to inventory."); return; } } System.out.println("Error"); } } }