49 lines
1.7 KiB
Java
49 lines
1.7 KiB
Java
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;
|
|
|
|
import static com.github.dtschust.zork.Zork.Type.*;
|
|
|
|
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()).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");
|
|
}
|
|
}
|
|
}
|