use of com.sx4.bot.entities.economy.item.ItemStack in project Sx4 by sx4-discord-bot.
the class FactoryCommand method buy.
@Command(value = "buy", description = "Buy a factory with some materials")
@CommandId(396)
@Examples({ "factory buy 5 Shoe Factory", "factory buy Shoe Factory", "factory buy all" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void buy(Sx4CommandEvent event, @Argument(value = "factories", endless = true) @AlternativeOptions("all") Alternative<ItemStack<Factory>> option) {
ItemStack<Factory> stack = option.getValue();
event.getMongo().withTransaction(session -> {
Bson userFilter = Filters.eq("userId", event.getAuthor().getIdLong()), filter;
List<Factory> factories;
if (stack == null) {
filter = Filters.and(userFilter, Filters.eq("item.type", ItemType.MATERIAL.getId()));
factories = event.getBot().getEconomyManager().getItems(Factory.class);
} else {
Factory factory = stack.getItem();
filter = Filters.and(userFilter, Filters.eq("item.id", factory.getCost().getItem().getId()));
factories = List.of(factory);
}
List<Document> materials = event.getMongo().getItems().find(session, filter).projection(Projections.include("amount", "item.id")).into(new ArrayList<>());
List<ItemStack<Factory>> boughtFactories = new ArrayList<>();
Factories: for (Factory factory : factories) {
ItemStack<Material> cost = factory.getCost();
Material costMaterial = cost.getItem();
for (Document material : materials) {
int id = material.getEmbedded(List.of("item", "id"), Integer.class);
if (costMaterial.getId() == id) {
long buyableAmount = (long) Math.floor((double) material.getLong("amount") / cost.getAmount());
long amount = stack == null ? buyableAmount : stack.getAmount();
if (amount == 0 || amount > buyableAmount) {
continue Factories;
}
event.getMongo().getItems().updateOne(session, Filters.and(userFilter, Filters.eq("item.id", id)), Updates.inc("amount", -amount * cost.getAmount()));
List<Bson> update = List.of(Operators.set("item", factory.toData()), Operators.set("amount", Operators.add(Operators.ifNull("$amount", 0L), amount)));
event.getMongo().getItems().updateOne(session, Filters.and(userFilter, Filters.eq("item.id", factory.getId())), update, new UpdateOptions().upsert(true));
boughtFactories.add(new ItemStack<>(factory, amount));
}
}
}
return boughtFactories;
}).whenComplete((factories, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (factories.isEmpty()) {
event.replyFailure("You cannot afford " + (stack == null ? "any factories" : "`" + stack.getAmount() + " " + stack.getItem().getName() + "`")).queue();
return;
}
String factoriesBought = factories.stream().sorted(Collections.reverseOrder(Comparator.comparingLong(ItemStack::getAmount))).map(ItemStack::toString).collect(Collectors.joining("\n• "));
EmbedBuilder embed = new EmbedBuilder().setColor(event.getMember().getColor()).setAuthor(event.getAuthor().getName(), null, event.getAuthor().getEffectiveAvatarUrl()).setDescription("With all your materials you have bought the following factories\n\n• " + factoriesBought);
event.reply(embed.build()).queue();
});
}
use of com.sx4.bot.entities.economy.item.ItemStack in project Sx4 by sx4-discord-bot.
the class FactoryCommand method shop.
@Command(value = "shop", description = "View all the factories you can buy")
@CommandId(395)
@Examples({ "factory shop" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void shop(Sx4CommandEvent event) {
List<Factory> factories = event.getBot().getEconomyManager().getItems(Factory.class);
PagedResult<Factory> paged = new PagedResult<>(event.getBot(), factories).setPerPage(15).setCustomFunction(page -> {
EmbedBuilder embed = new EmbedBuilder().setAuthor("Factory Shop", null, event.getSelfUser().getEffectiveAvatarUrl()).setTitle("Page " + page.getPage() + "/" + page.getMaxPage()).setDescription("Factories are a good way to make money from materials you have gained through mining").setFooter(PagedResult.DEFAULT_FOOTER_TEXT);
page.forEach((factory, index) -> {
ItemStack<Material> cost = factory.getCost();
embed.addField(factory.getName(), "Price: " + cost.getAmount() + " " + cost.getItem().getName(), true);
});
return new MessageBuilder().setEmbeds(embed.build());
});
paged.execute(event);
}
use of com.sx4.bot.entities.economy.item.ItemStack in project Sx4 by sx4-discord-bot.
the class FishingRodCommand method shop.
@Command(value = "shop", description = "View all the fishing rods you are able to buy or craft")
@CommandId(379)
@Examples({ "fishing rod shop" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void shop(Sx4CommandEvent event) {
List<Rod> rods = event.getBot().getEconomyManager().getItems(Rod.class);
PagedResult<Rod> paged = new PagedResult<>(event.getBot(), rods).setPerPage(12).setCustomFunction(page -> {
EmbedBuilder embed = new EmbedBuilder().setAuthor("Fishing Rod Shop", null, event.getSelfUser().getEffectiveAvatarUrl()).setTitle("Page " + page.getPage() + "/" + page.getMaxPage()).setDescription("Fishing rods are a good way to gain some money").setFooter(PagedResult.DEFAULT_FOOTER_TEXT);
page.forEach((rod, index) -> {
List<ItemStack<CraftItem>> items = rod.getCraft();
String craft = items.isEmpty() ? "None" : items.stream().map(ItemStack::toString).collect(Collectors.joining("\n"));
embed.addField(rod.getName(), String.format("Price: $%,d\nCraft: %s\nDurability: %,d", rod.getPrice(), craft, rod.getMaxDurability()), true);
});
return new MessageBuilder().setEmbeds(embed.build());
});
paged.execute(event);
}
use of com.sx4.bot.entities.economy.item.ItemStack in project Sx4 by sx4-discord-bot.
the class ItemsCommand method onCommand.
public void onCommand(Sx4CommandEvent event, @Argument(value = "user", endless = true, nullDefault = true) Member member) {
Member effectiveMember = member == null ? event.getMember() : member;
List<Bson> usersPipeline = List.of(Aggregates.match(Filters.eq("_id", effectiveMember.getIdLong())), Aggregates.project(Projections.computed("balance", Operators.ifNull("$economy.balance", 0L))), Aggregates.addFields(new Field<>("name", "balance")));
List<Bson> pipeline = List.of(Aggregates.match(Filters.and(Filters.eq("userId", effectiveMember.getIdLong()), Filters.ne("amount", 0))), Aggregates.project(Projections.fields(Projections.computed("name", "$item.name"), Projections.computed("type", "$item.type"), Projections.include("item", "amount"))), Aggregates.sort(Sorts.descending("amount")), Aggregates.unionWith("users", usersPipeline));
event.getMongo().aggregateItems(pipeline).whenComplete((items, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
EmbedBuilder embed = new EmbedBuilder().setAuthor("Items", null, effectiveMember.getUser().getEffectiveAvatarUrl()).setColor(effectiveMember.getColorRaw());
if (items.isEmpty()) {
event.replyFailure("That user does not have any items").queue();
return;
}
String footerText = "If a category isn't shown it means you have no items in that category | Balance: $";
StringBuilder footer = new StringBuilder(footerText);
Map<ItemType, StringJoiner> types = new HashMap<>();
for (Document item : items) {
String name = item.getString("name");
if (name.equals("balance")) {
if (items.size() == 1) {
event.replyFailure("That user does not have any items").queue();
return;
}
footer.append(String.format("%,d", item.get("balance", 0L)));
continue;
}
ItemType type = ItemType.fromId(item.getInteger("type"));
ItemStack<?> stack = new ItemStack<>(event.getBot().getEconomyManager(), item);
types.compute(type, (key, value) -> (value == null ? new StringJoiner("\n") : value).add(stack.toString()));
}
if (footer.length() == footerText.length()) {
footer.append("0");
}
types.forEach((type, joiner) -> embed.addField(type.getName(), joiner.toString(), true));
embed.setFooter(footer.toString());
event.reply(embed.build()).queue();
});
}
use of com.sx4.bot.entities.economy.item.ItemStack in project Sx4 by sx4-discord-bot.
the class AxeCommand method shop.
@Command(value = "shop", description = "View all the axes you are able to buy or craft")
@CommandId(388)
@Examples({ "axe shop" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void shop(Sx4CommandEvent event) {
List<Axe> axes = event.getBot().getEconomyManager().getItems(Axe.class);
PagedResult<Axe> paged = new PagedResult<>(event.getBot(), axes).setPerPage(12).setCustomFunction(page -> {
EmbedBuilder embed = new EmbedBuilder().setAuthor("Axe Shop", null, event.getSelfUser().getEffectiveAvatarUrl()).setTitle("Page " + page.getPage() + "/" + page.getMaxPage()).setDescription("Axes are a good way to get some wood for crafting").setFooter(PagedResult.DEFAULT_FOOTER_TEXT);
page.forEach((axe, index) -> {
List<ItemStack<CraftItem>> items = axe.getCraft();
String craft = items.isEmpty() ? "None" : items.stream().map(ItemStack::toString).collect(Collectors.joining("\n"));
embed.addField(axe.getName(), String.format("Price: $%,d\nCraft: %s\nDurability: %,d", axe.getPrice(), craft, axe.getMaxDurability()), true);
});
return new MessageBuilder().setEmbeds(embed.build());
});
paged.execute(event);
}
Aggregations