use of org.terasology.engine.logic.console.commandSystem.annotations.CommandParam in project Terasology by MovingBlocks.
the class CoreCommands method spawnPrefab.
/**
* Spawns an instance of a prefab in the world
*
* @param sender Sender of command
* @param prefabName String containing prefab name
* @return String containing final message
*/
@Command(shortDescription = "Spawns an instance of a prefab in the world", runOnServer = true, requiredPermission = PermissionManager.CHEAT_PERMISSION)
public String spawnPrefab(@Sender EntityRef sender, @CommandParam("prefabId") String prefabName) {
ClientComponent clientComponent = sender.getComponent(ClientComponent.class);
LocationComponent characterLocation = clientComponent.character.getComponent(LocationComponent.class);
Vector3f spawnPos = characterLocation.getWorldPosition(new Vector3f());
Vector3f offset = characterLocation.getWorldDirection(new Vector3f());
offset.mul(2);
spawnPos.add(offset);
Vector3f dir = characterLocation.getWorldDirection(new Vector3f());
dir.y = 0;
if (dir.lengthSquared() > 0.001f) {
dir.normalize();
} else {
dir.set(Direction.FORWARD.asVector3f());
}
return Assets.getPrefab(prefabName).map(prefab -> {
LocationComponent loc = prefab.getComponent(LocationComponent.class);
if (loc != null) {
entityManager.create(prefab, spawnPos);
return "Done";
} else {
return "Prefab cannot be spawned (no location component)";
}
}).orElse("Unknown prefab");
}
use of org.terasology.engine.logic.console.commandSystem.annotations.CommandParam in project Terasology by MovingBlocks.
the class MethodCommand method getParameterTypeFor.
private static Parameter getParameterTypeFor(Class<?> type, Annotation[] annotations, Context context) {
for (Annotation annotation : annotations) {
if (annotation instanceof CommandParam) {
CommandParam parameterAnnotation = (CommandParam) annotation;
String name = parameterAnnotation.value();
Class<? extends CommandParameterSuggester> suggesterClass = parameterAnnotation.suggester();
boolean required = parameterAnnotation.required();
CommandParameterSuggester suggester = InjectionHelper.createWithConstructorInjection(suggesterClass, context);
if (type.isArray()) {
Class<?> childType = type.getComponentType();
return CommandParameter.array(name, childType, required, suggester, context);
} else {
return CommandParameter.single(name, type, required, suggester, context);
}
} else if (annotation instanceof Sender) {
return MarkerParameters.SENDER;
}
}
return MarkerParameters.INVALID;
}
use of org.terasology.engine.logic.console.commandSystem.annotations.CommandParam in project Terasology by MovingBlocks.
the class CoreCommands method dumpEntities.
/**
* Writes out information on entities having specific components to a text file for debugging
* If no component names provided - writes out information on all entities
*
* @param componentNames string contains one or several component names, if more then one name
* provided - they must be braced with double quotes and all names separated
* by space
* @return String containing information about number of entities saved
* @throws IOException thrown when error with saving file occures
*/
@Command(shortDescription = "Writes out information on all entities to a JSON file for debugging", helpText = "Writes entity information out into a file named \"<timestamp>-entityDump.json\"." + " Supports list of component names, which will be used to only save entities that contains" + " one or more of those components. Names should be separated by spaces.")
public String dumpEntities(@CommandParam(value = "componentNames", required = false) String... componentNames) throws IOException {
int savedEntityCount;
EngineEntityManager engineEntityManager = (EngineEntityManager) entityManager;
PrefabSerializer prefabSerializer = new PrefabSerializer(engineEntityManager.getComponentLibrary(), engineEntityManager.getTypeSerializerLibrary());
WorldDumper worldDumper = new WorldDumper(engineEntityManager, prefabSerializer);
Path outFile = PathManager.getInstance().getHomePath().resolve(Instant.now() + "-entityDump.json");
if (componentNames.length == 0) {
savedEntityCount = worldDumper.save(outFile);
} else {
List<Class<? extends Component>> filterComponents = Arrays.stream(componentNames).map(// Trim off whitespace
String::trim).filter(// Remove empty strings
o -> !o.isEmpty()).map(// All component class names end with "component"
o -> o.toLowerCase().endsWith("component") ? o : o + "component").map(o -> Streams.stream(moduleManager.getEnvironment().getSubtypesOf(Component.class)).filter(e -> e.getSimpleName().equalsIgnoreCase(o)).findFirst()).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
if (!filterComponents.isEmpty()) {
savedEntityCount = worldDumper.save(outFile, filterComponents);
} else {
return "Could not find components matching given names";
}
}
return "Number of entities saved: " + savedEntityCount;
}
Aggregations