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

42 lines
1.3 KiB
Java

package com.github.dtschust.zork.repl.actions;
import com.github.dtschust.zork.parser.ZorkGame;
import com.github.dtschust.zork.Zork.Type;
import com.github.dtschust.zork.repl.Action;
import com.github.dtschust.zork.types.HasSetOfCollectable;
import com.github.dtschust.zork.types.ZorkObject;
import java.util.List;
/**
* Add: figure out what type the destination is, then what type the object is. Then add object to destination if it makes sense
*/
public class AddAction implements Action {
@Override
public boolean matchesInput(List<String> arguments) {
return arguments.get(0).equals("Add");
}
@Override
public int getMinimumArgCount() {
return 4;
}
@Override
public void run(ZorkGame game, List<String> arguments) {
final String object = arguments.get(1);
final String destination = arguments.get(3);
try {
Type destType = game.getTypeFromLookup(destination);
Type objType = game.getTypeFromLookup(object);
ZorkObject tempObject = game.get(destType, destination);
((HasSetOfCollectable) tempObject).getSetFromType(objType).add(object);
game.put(destType, tempObject);
} catch (Exception e) {
System.out.println("Error");
}
}
}