use of com.demod.fbsr.MapVersion in project Factorio-FBSR by demodude4u.
the class BlueprintBotDiscordService method handleBlueprintBookAssembleCommand.
private void handleBlueprintBookAssembleCommand(SlashCommandEvent event) {
String content = event.getCommandString();
Optional<Attachment> attachment = event.optParamAttachment("file");
if (attachment.isPresent()) {
content += " " + attachment.get().getUrl();
}
List<BlueprintStringData> blueprintStrings = BlueprintFinder.search(content, event.getReporting());
if (!blueprintStrings.isEmpty()) {
List<Blueprint> blueprints = blueprintStrings.stream().flatMap(bs -> bs.getBlueprints().stream()).collect(Collectors.toList());
JSONObject json = new JSONObject();
Utils.terribleHackToHaveOrderedJSONObject(json);
JSONObject bookJson = new JSONObject();
Utils.terribleHackToHaveOrderedJSONObject(bookJson);
json.put("blueprint_book", bookJson);
JSONArray blueprintsJson = new JSONArray();
bookJson.put("blueprints", blueprintsJson);
bookJson.put("item", "blueprint-book");
bookJson.put("active_index", 0);
MapVersion latestVersion = new MapVersion();
int index = 0;
for (Blueprint blueprint : blueprints) {
blueprint.json().put("index", index);
latestVersion = MapVersion.max(latestVersion, blueprint.getVersion());
blueprintsJson.put(blueprint.json());
index++;
}
String bookLabel = blueprintStrings.stream().filter(BlueprintStringData::isBook).map(BlueprintStringData::getLabel).filter(Optional::isPresent).map(Optional::get).map(String::trim).distinct().collect(Collectors.joining(" & "));
if (!bookLabel.isEmpty()) {
bookJson.put("label", bookLabel);
}
if (!latestVersion.isEmpty()) {
bookJson.put("version", latestVersion.getSerialized());
}
try {
event.replyFile(BlueprintStringData.encode(json).getBytes(), "blueprintBook.txt");
} catch (Exception e) {
event.getReporting().addException(e);
}
} else {
event.replyIfNoException("No blueprint found!");
}
}
Aggregations