This repository has been archived on 2022-12-21. You can view files and clone it, but cannot push or open issues or pull requests.
sdm03/src/main/java/com/github/dtschust/zork/repl/actions/TakeAction.java

50 lines
1.8 KiB
Java

package com.github.dtschust.zork.repl.actions;
import com.github.dtschust.zork.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;
import static com.github.dtschust.zork.types.ZorkGameStatusType.CONTAINER;
import static com.github.dtschust.zork.types.ZorkGameStatusType.ROOM;
public class TakeAction implements Action {
@Override
public boolean matchesInput(List<String> arguments) {
return arguments.get(0).equals("take");
}
@Override
public int getMinimumArgCount() {
return 2;
}
@Override
public void run(ZorkGame game, List<String> arguments) {
final String tempString = arguments.get(1);
if ((game.getCurrentRoom()).getItem().contains(tempString)) {
game.inventory.add(tempString);
ZorkRoom tempRoom = (game.getCurrentRoom());
tempRoom.getItem().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().getContainer()) {
ZorkContainer tempContainer = (ZorkContainer) game.get(CONTAINER, key);
if (tempContainer != null && tempContainer.isOpen() && tempContainer.containsItem(tempString)) {
game.inventory.add(tempString);
tempContainer.removeItem(tempString);
game.put(CONTAINER, tempContainer);
System.out.println("Item " + tempString + " added to inventory.");
return;
}
}
System.out.println("Error");
}
}
}