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/DropItemAction.java

40 lines
1.1 KiB
Java
Raw Normal View History

package com.github.dtschust.zork.repl.actions;
import com.github.dtschust.zork.ZorkGame;
2022-11-22 17:31:06 +00:00
import com.github.dtschust.zork.objects.ZorkItem;
import com.github.dtschust.zork.objects.ZorkRoom;
2022-11-22 17:31:06 +00:00
import com.github.dtschust.zork.repl.Action;
import java.util.List;
2022-11-22 17:31:06 +00:00
import java.util.Optional;
public class DropItemAction implements Action {
@Override
public boolean matchesInput(List<String> arguments) {
return arguments.get(0).equals("drop");
}
@Override
public int getMinimumArgCount() {
return 2;
}
@Override
2022-11-22 17:03:09 +00:00
public boolean run(ZorkGame game, List<String> arguments) {
2022-11-22 17:31:06 +00:00
final String whatName = arguments.get(1);
final Optional<ZorkItem> what = game.getItem(whatName);
if (game.inventory.contains(whatName) && what.isPresent()) {
final ZorkRoom tempRoom = game.getCurrentRoom();
game.inventory.remove(whatName);
tempRoom.addObject(what.get());
game.stream.println(whatName + " dropped.");
return true;
} else {
2022-11-22 15:46:23 +00:00
return false;
}
}
}