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/types/ZorkContainer.java

72 lines
2.0 KiB
Java
Raw Normal View History

2022-11-19 09:46:28 +00:00
package com.github.dtschust.zork.types;
import com.github.dtschust.zork.Zork;
2022-11-21 21:29:36 +00:00
import com.github.dtschust.zork.ZorkTrigger;
import java.util.*;
import static com.github.dtschust.zork.Zork.Type.ITEM;
/* Container*/
public class ZorkContainer extends ZorkObject implements HasSetOfCollectable {
2022-11-22 10:27:56 +00:00
private final Set<String> items;
2022-11-21 21:29:36 +00:00
private final List<String> accepts;
private boolean open;
2022-11-21 21:29:36 +00:00
public ZorkContainer(final String name,
final String description,
final String status,
final Collection<String> items,
final Collection<String> accepts,
final Collection<ZorkTrigger> triggers) {
super(name, description, status, triggers);
// If a container has an accepts attribute, then it is always open
this.open = !accepts.isEmpty();
2022-11-22 10:27:56 +00:00
this.items = new HashSet<>(items);
2022-11-21 21:29:36 +00:00
this.accepts = new ArrayList<>(accepts);
}
2022-11-22 10:27:56 +00:00
public String getContents() {
if (this.items.isEmpty()) {
return getName() + " is empty";
} else {
return getName() + " contains " + String.join(", ", items) + ".";
}
}
2022-11-21 21:29:36 +00:00
public void addItem(final String item) {
2022-11-22 10:27:56 +00:00
this.items.add(item);
2022-11-21 21:29:36 +00:00
}
public void removeItem(final String item) {
2022-11-22 10:27:56 +00:00
this.items.remove(item);
2022-11-21 21:29:36 +00:00
}
public boolean containsItem(final String item) {
2022-11-22 10:27:56 +00:00
return this.items.contains(item);
2022-11-21 21:29:36 +00:00
}
2022-11-22 10:27:56 +00:00
public Iterable<String> items() {
return Collections.unmodifiableSet(items);
2022-11-21 21:29:36 +00:00
}
2022-11-21 16:10:08 +00:00
public boolean isOpen() {
return open;
}
2022-11-21 16:10:08 +00:00
public void open() {
open = true;
}
@Override
public Set<String> getSetFromType(Zork.Type type) {
if (type.equals(ITEM))
2022-11-22 10:27:56 +00:00
return items;
throw new IllegalStateException("Unexpected value: " + type);
}
2022-11-22 10:27:56 +00:00
public List<String> getAccepts() {
return Collections.unmodifiableList(accepts);
}
}