use of com.demod.dcba.SlashCommandEvent in project Factorio-FBSR by demodude4u.
the class BlueprintBotDiscordService method handleBlueprintUpgradeBeltsCommand.
private void handleBlueprintUpgradeBeltsCommand(SlashCommandEvent event) {
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());
int upgradedCount = 0;
for (BlueprintStringData blueprintStringData : blueprintStringDatas) {
for (Blueprint blueprint : blueprintStringData.getBlueprints()) {
for (BlueprintEntity blueprintEntity : blueprint.getEntities()) {
String upgradeName = upgradeBeltsEntityMapping.get(blueprintEntity.getName());
if (upgradeName != null) {
blueprintEntity.json().put("name", upgradeName);
upgradedCount++;
}
}
}
}
if (upgradedCount > 0) {
for (BlueprintStringData blueprintStringData : blueprintStringDatas) {
try {
event.replyFile(BlueprintStringData.encode(blueprintStringData.json()).getBytes(), "blueprint.txt");
} catch (IOException e) {
event.getReporting().addException(e);
}
}
event.reply("Upgraded " + upgradedCount + " entities.");
} else if (blueprintStringDatas.stream().anyMatch(d -> !d.getBlueprints().isEmpty())) {
event.replyIfNoException("I couldn't find anything to upgrade!");
} else {
event.replyIfNoException("No blueprint found!");
}
}
use of com.demod.dcba.SlashCommandEvent in project Factorio-FBSR by demodude4u.
the class BlueprintBotDiscordService method handleBlueprintTotalsCommand.
private void handleBlueprintTotalsCommand(SlashCommandEvent event) {
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.generateSummedTotalItems(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 -> 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(), "totals.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.dcba.SlashCommandEvent in project Factorio-FBSR by demodude4u.
the class BlueprintBotDiscordService method handleBlueprintBookExtractCommand.
private void handleBlueprintBookExtractCommand(SlashCommandEvent event) {
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());
List<Blueprint> blueprints = blueprintStringDatas.stream().flatMap(bs -> bs.getBlueprints().stream()).collect(Collectors.toList());
List<Entry<String, String>> links = new ArrayList<>();
for (Blueprint blueprint : blueprints) {
try {
blueprint.json().remove("index");
String url = WebUtils.uploadToHostingService("blueprint.txt", BlueprintStringData.encode(blueprint.json()).getBytes());
links.add(new SimpleEntry<>(url, blueprint.getLabel().orElse(null)));
} catch (Exception e) {
event.getReporting().addException(e);
}
}
List<EmbedBuilder> embedBuilders = new ArrayList<>();
if (!links.isEmpty()) {
ArrayDeque<String> lines = links.stream().map(p -> (p.getValue() != null && !p.getValue().isEmpty()) ? ("[" + p.getValue() + "](" + p.getKey() + ")") : p.getKey()).collect(Collectors.toCollection(ArrayDeque::new));
while (!lines.isEmpty()) {
EmbedBuilder builder = new EmbedBuilder();
StringBuilder description = new StringBuilder();
while (!lines.isEmpty()) {
if (description.length() + lines.peek().length() + 1 < MessageEmbed.DESCRIPTION_MAX_LENGTH) {
description.append(lines.pop()).append('\n');
} else {
break;
}
}
builder.setDescription(description);
embedBuilders.add(builder);
}
} else {
embedBuilders.add(new EmbedBuilder().setDescription("Blueprint not found!"));
}
List<MessageEmbed> embeds = embedBuilders.stream().map(EmbedBuilder::build).collect(Collectors.toList());
for (MessageEmbed embed : embeds) {
event.replyEmbed(embed);
}
}
use of com.demod.dcba.SlashCommandEvent 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.dcba.SlashCommandEvent 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!");
}
}
Aggregations