-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTakeCommand.java
41 lines (36 loc) · 1.21 KB
/
TakeCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
*
*/
/** TakeCommand deals with any Commands relating to taking an item from a Dungeon and putting it in the player's
* inventory
* @author Team Red
*
*/
class TakeCommand extends Command {
private String itemName;
TakeCommand(String itemName) {
this.itemName = itemName;
}
public String execute() {
if (itemName == null || itemName.trim().length() == 0) {
return "Take what?\n";
}
try {
Room currentRoom =
GameState.instance().getAdventurersCurrentRoom();
Item theItem = currentRoom.getItemNamed(itemName);
GameState.instance().addToInventory(theItem);
currentRoom.remove(theItem);
return itemName + " taken.\n";
} catch (Item.NoItemException e) {
// Check and see if we have this already. If no exception is
// thrown from the line below, then we do.
try {
GameState.instance().getItemFromInventoryNamed(itemName);
return "You already have the " + itemName + ".\n";
} catch (Item.NoItemException e2) {
return "There's no " + itemName + " here.\n";
}
}
}
}