use of com.demod.fbsr.BlueprintStringData in project Factorio-FBSR by demodude4u.
the class BlueprintBotDiscordService method handleBlueprintMessageCommand.
private void handleBlueprintMessageCommand(MessageCommandEvent event) throws IOException {
String content = event.getMessage().getContentDisplay();
for (Attachment attachment : event.getMessage().getAttachments()) {
content += " " + attachment.getUrl();
}
JSONObject options = new JSONObject();
findDebugOptions(event.getReporting(), content, options);
List<BlueprintStringData> blueprintStringDatas = BlueprintFinder.search(content, event.getReporting());
List<EmbedBuilder> embedBuilders = processBlueprints(blueprintStringDatas, event.getReporting(), options);
embedBuilders.get(0).setAuthor(event.getMessage().getAuthor().getName(), event.getMessage().getJumpUrl(), event.getMessage().getAuthor().getEffectiveAvatarUrl());
if (blueprintStringDatas.stream().anyMatch(d -> d.getBlueprints().stream().anyMatch(b -> b.isModsDetected()))) {
embedBuilders.get(embedBuilders.size() - 1).setFooter("(Modded features are shown as question marks)");
}
List<MessageEmbed> embeds = embedBuilders.stream().map(EmbedBuilder::build).collect(Collectors.toList());
for (MessageEmbed embed : embeds) {
event.replyEmbed(embed);
}
}
use of com.demod.fbsr.BlueprintStringData in project Factorio-FBSR by demodude4u.
the class BlueprintBotDiscordService method handleBlueprintSlashCommand.
private void handleBlueprintSlashCommand(SlashCommandEvent event) throws IOException {
String content = event.getCommandString();
Optional<Attachment> attachment = event.optParamAttachment("file");
if (attachment.isPresent()) {
content += " " + attachment.get().getUrl();
}
JSONObject options = new JSONObject();
findDebugOptions(event.getReporting(), content, options);
event.optParamBoolean("simple").ifPresent(b -> options.put("show-info-panels", !b));
event.optParamLong("max-width").ifPresent(l -> options.put("max-width", l.intValue()));
event.optParamLong("max-height").ifPresent(l -> options.put("max-height", l.intValue()));
List<BlueprintStringData> blueprintStringDatas = BlueprintFinder.search(content, event.getReporting());
List<EmbedBuilder> embedBuilders = processBlueprints(blueprintStringDatas, event.getReporting(), options);
if (blueprintStringDatas.stream().anyMatch(d -> d.getBlueprints().stream().anyMatch(b -> b.isModsDetected()))) {
embedBuilders.get(embedBuilders.size() - 1).setFooter("(Modded features are shown as question marks)");
}
List<MessageEmbed> embeds = embedBuilders.stream().map(EmbedBuilder::build).collect(Collectors.toList());
for (MessageEmbed embed : embeds) {
event.replyEmbed(embed);
}
}
use of com.demod.fbsr.BlueprintStringData in project Factorio-FBSR by demodude4u.
the class BlueprintBotDiscordService method handleBlueprintItemsRawCommand.
private void handleBlueprintItemsRawCommand(SlashCommandEvent event) throws IOException {
DataTable table;
try {
table = FactorioData.getTable();
} catch (JSONException | IOException e1) {
throw new InternalError(e1);
}
String content = event.getCommandString();
Optional<Attachment> attachment = event.optParamAttachment("file");
if (attachment.isPresent()) {
content += " " + attachment.get().getUrl();
}
List<BlueprintStringData> blueprintStringDatas = BlueprintFinder.search(content, event.getReporting());
Map<String, Double> totalItems = new LinkedHashMap<>();
for (BlueprintStringData blueprintStringData : blueprintStringDatas) {
for (Blueprint blueprint : blueprintStringData.getBlueprints()) {
Map<String, Double> items = FBSR.generateTotalItems(table, blueprint);
items.forEach((k, v) -> {
totalItems.compute(k, ($, old) -> old == null ? v : old + v);
});
}
}
Map<String, Double> rawItems = FBSR.generateTotalRawItems(table, table.getRecipes(), totalItems);
if (!rawItems.isEmpty()) {
String responseContent = rawItems.entrySet().stream().sorted((e1, e2) -> e1.getKey().compareTo(e2.getKey())).map(e -> table.getWikiItemName(e.getKey()) + ": " + RenderUtils.fmtDouble2(e.getValue())).collect(Collectors.joining("\n"));
String response = "```ldif\n" + responseContent + "```";
if (response.length() < 2000) {
event.reply(response);
} else {
event.replyFile(responseContent.getBytes(), "raw-items.txt");
}
} else if (blueprintStringDatas.stream().anyMatch(d -> !d.getBlueprints().isEmpty())) {
event.replyIfNoException("I couldn't find any items!");
} else {
event.replyIfNoException("No blueprint found!");
}
}
use of com.demod.fbsr.BlueprintStringData in project Factorio-FBSR by demodude4u.
the class BlueprintBotDiscordService method handleBlueprintItemsCommand.
private void handleBlueprintItemsCommand(SlashCommandEvent event) throws IOException {
DataTable table;
try {
table = FactorioData.getTable();
} catch (JSONException | IOException e1) {
throw new InternalError(e1);
}
String content = event.getCommandString();
Optional<Attachment> attachment = event.optParamAttachment("file");
if (attachment.isPresent()) {
content += " " + attachment.get().getUrl();
}
List<BlueprintStringData> blueprintStringDatas = BlueprintFinder.search(content, event.getReporting());
Map<String, Double> totalItems = new LinkedHashMap<>();
for (BlueprintStringData blueprintStringData : blueprintStringDatas) {
for (Blueprint blueprint : blueprintStringData.getBlueprints()) {
Map<String, Double> items = FBSR.generateTotalItems(table, blueprint);
items.forEach((k, v) -> {
totalItems.compute(k, ($, old) -> old == null ? v : old + v);
});
}
}
if (!totalItems.isEmpty()) {
String responseContent = totalItems.entrySet().stream().sorted((e1, e2) -> e1.getKey().compareTo(e2.getKey())).map(e -> table.getWikiItemName(e.getKey()) + ": " + RenderUtils.fmtDouble2(e.getValue())).collect(Collectors.joining("\n"));
String response = "```ldif\n" + responseContent + "```";
if (response.length() < 2000) {
event.reply(response);
} else {
event.replyFile(responseContent.getBytes(), "items.txt");
}
} else if (blueprintStringDatas.stream().anyMatch(d -> !d.getBlueprints().isEmpty())) {
event.replyIfNoException("I couldn't find any items!");
} else {
event.replyIfNoException("No blueprint found!");
}
}
use of com.demod.fbsr.BlueprintStringData 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