use of com.sx4.bot.entities.economy.item.CraftItem in project Sx4 by sx4-discord-bot.
the class PickaxeCommand method repair.
@Command(value = "repair", description = "Repair your current pickaxe with the material it is made from")
@CommandId(363)
@Examples({ "pickaxe repair 10", "pickaxe repair all" })
public void repair(Sx4CommandEvent event, @Argument(value = "durability") @AlternativeOptions("all") Alternative<Integer> option) {
Bson filter = Filters.and(Filters.eq("userId", event.getAuthor().getIdLong()), Filters.eq("item.type", ItemType.PICKAXE.getId()));
Document data = event.getMongo().getItem(filter, Projections.include("item"));
if (data == null) {
event.replyFailure("You do not have a pickaxe").queue();
return;
}
Pickaxe pickaxe = Pickaxe.fromData(event.getBot().getEconomyManager(), data.get("item", Document.class));
CraftItem item = pickaxe.getRepairItem();
if (item == null) {
event.replyFailure("That pickaxe is not repairable").queue();
return;
}
int maxDurability = pickaxe.getMaxDurability() - pickaxe.getDurability();
if (maxDurability <= 0) {
event.replyFailure("Your pickaxe is already at full durability").queue();
return;
}
int durability;
if (option.isAlternative()) {
durability = maxDurability;
} else {
int amount = option.getValue();
if (amount > maxDurability) {
event.reply("You can only repair your pickaxe by **" + maxDurability + "** durability :no_entry:").queue();
return;
}
durability = amount;
}
int itemCount = (int) Math.ceil((((double) pickaxe.getPrice() / item.getPrice()) / pickaxe.getMaxDurability()) * durability);
List<Button> buttons = List.of(Button.success("yes", "Yes"), Button.danger("no", "No"));
event.reply("It will cost you `" + itemCount + " " + item.getName() + "` to repair your pickaxe by **" + durability + "** durability, are you sure you want to repair it?").setActionRow(buttons).submit().thenCompose(message -> {
return new Waiter<>(event.getBot(), ButtonClickEvent.class).setPredicate(e -> ButtonUtility.handleButtonConfirmation(e, message, event.getAuthor())).setCancelPredicate(e -> ButtonUtility.handleButtonCancellation(e, message, event.getAuthor())).onFailure(e -> ButtonUtility.handleButtonFailure(e, message)).setTimeout(60).start();
}).whenComplete((e, exception) -> {
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof CancelException) {
GenericEvent cancelEvent = ((CancelException) cause).getEvent();
if (cancelEvent != null) {
((ButtonClickEvent) cancelEvent).reply("Cancelled " + event.getConfig().getSuccessEmote()).queue();
}
return;
} else if (cause instanceof TimeoutException) {
event.reply("Timed out :stopwatch:").queue();
return;
} else if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
List<Bson> update = List.of(Operators.set("amount", Operators.let(new Document("amount", Operators.ifNull("$amount", 0L)), Operators.cond(Operators.lte(itemCount, "$$amount"), Operators.subtract("$$amount", itemCount), "$$amount"))));
event.getMongo().updateItem(Filters.and(Filters.eq("item.id", item.getId()), Filters.eq("userId", event.getAuthor().getIdLong())), update, new UpdateOptions()).thenCompose(result -> {
if (result.getMatchedCount() == 0 || result.getModifiedCount() == 0) {
e.reply("You do not have `" + itemCount + " " + item.getName() + "` " + event.getConfig().getFailureEmote()).queue();
return CompletableFuture.completedFuture(null);
}
List<Bson> itemUpdate = List.of(Operators.set("item.durability", Operators.cond(Operators.eq("$item.durability", pickaxe.getDurability()), Operators.add("$item.durability", durability), "$item.durability")));
return event.getMongo().updateItem(Filters.and(Filters.eq("item.id", pickaxe.getId()), Filters.eq("userId", event.getAuthor().getIdLong())), itemUpdate, new UpdateOptions());
}).whenComplete((result, databaseException) -> {
if (ExceptionUtility.sendExceptionally(event, databaseException) || result == null) {
return;
}
if (result.getMatchedCount() == 0) {
e.reply("You no longer have that pickaxe " + event.getConfig().getFailureEmote()).queue();
return;
}
if (result.getMatchedCount() == 0) {
e.reply("The durability of your pickaxe has changed " + event.getConfig().getFailureEmote()).queue();
return;
}
e.reply("You just repaired your pickaxe by **" + durability + "** durability " + event.getConfig().getSuccessEmote()).queue();
});
});
}
use of com.sx4.bot.entities.economy.item.CraftItem 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.CraftItem in project Sx4 by sx4-discord-bot.
the class FishingRodCommand method repair.
@Command(value = "repair", description = "Repair your current fishing rod with the material it is made from")
@CommandId(383)
@Examples({ "fishing rod repair 10", "fishing rod repair all" })
public void repair(Sx4CommandEvent event, @Argument(value = "durability") @AlternativeOptions("all") Alternative<Integer> option) {
Bson filter = Filters.and(Filters.eq("userId", event.getAuthor().getIdLong()), Filters.eq("item.type", ItemType.ROD.getId()));
Document data = event.getMongo().getItem(filter, Projections.include("item"));
if (data == null) {
event.replyFailure("You do not have a fishing rod").queue();
return;
}
Rod rod = Rod.fromData(event.getBot().getEconomyManager(), data.get("item", Document.class));
CraftItem item = rod.getRepairItem();
if (item == null) {
event.replyFailure("That fishing rod is not repairable").queue();
return;
}
int maxDurability = rod.getMaxDurability() - rod.getDurability();
if (maxDurability <= 0) {
event.replyFailure("Your fishing rod is already at full durability").queue();
return;
}
int durability;
if (option.isAlternative()) {
durability = maxDurability;
} else {
int amount = option.getValue();
if (amount > maxDurability) {
event.reply("You can only repair your fishing rod by **" + maxDurability + "** durability :no_entry:").queue();
return;
}
durability = amount;
}
int itemCount = (int) Math.ceil((((double) rod.getPrice() / item.getPrice()) / rod.getMaxDurability()) * durability);
List<Button> buttons = List.of(Button.success("yes", "Yes"), Button.danger("no", "No"));
event.reply("It will cost you `" + itemCount + " " + item.getName() + "` to repair your fishing rod by **" + durability + "** durability, are you sure you want to repair it?").setActionRow(buttons).submit().thenCompose(message -> {
return new Waiter<>(event.getBot(), ButtonClickEvent.class).setPredicate(e -> ButtonUtility.handleButtonConfirmation(e, message, event.getAuthor())).setCancelPredicate(e -> ButtonUtility.handleButtonCancellation(e, message, event.getAuthor())).onFailure(e -> ButtonUtility.handleButtonFailure(e, message)).setTimeout(60).start();
}).whenComplete((e, exception) -> {
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof CancelException) {
GenericEvent cancelEvent = ((CancelException) cause).getEvent();
if (cancelEvent != null) {
((ButtonClickEvent) cancelEvent).reply("Cancelled " + event.getConfig().getSuccessEmote()).queue();
}
return;
} else if (cause instanceof TimeoutException) {
event.reply("Timed out :stopwatch:").queue();
return;
} else if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
List<Bson> update = List.of(Operators.set("amount", Operators.let(new Document("amount", Operators.ifNull("$amount", 0L)), Operators.cond(Operators.lte(itemCount, "$$amount"), Operators.subtract("$$amount", itemCount), "$$amount"))));
event.getMongo().updateItem(Filters.and(Filters.eq("item.id", item.getId()), Filters.eq("userId", event.getAuthor().getIdLong())), update, new UpdateOptions()).thenCompose(result -> {
if (result.getMatchedCount() == 0 || result.getModifiedCount() == 0) {
e.reply("You do not have `" + itemCount + " " + item.getName() + "` " + event.getConfig().getFailureEmote()).queue();
return CompletableFuture.completedFuture(null);
}
List<Bson> itemUpdate = List.of(Operators.set("item.durability", Operators.cond(Operators.eq("$item.durability", rod.getDurability()), Operators.add("$item.durability", durability), "$item.durability")));
return event.getMongo().updateItem(Filters.and(Filters.eq("item.id", rod.getId()), Filters.eq("userId", event.getAuthor().getIdLong())), itemUpdate, new UpdateOptions());
}).whenComplete((result, databaseException) -> {
if (ExceptionUtility.sendExceptionally(event, databaseException) || result == null) {
return;
}
if (result.getMatchedCount() == 0) {
e.reply("You no longer have that fishing rod " + event.getConfig().getFailureEmote()).queue();
return;
}
if (result.getMatchedCount() == 0) {
e.reply("The durability of your fishing rod has changed " + event.getConfig().getFailureEmote()).queue();
return;
}
e.reply("You just repaired your fishing rod by **" + durability + "** durability " + event.getConfig().getSuccessEmote()).queue();
});
});
}
use of com.sx4.bot.entities.economy.item.CraftItem 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);
}
use of com.sx4.bot.entities.economy.item.CraftItem in project Sx4 by sx4-discord-bot.
the class AxeCommand method repair.
@Command(value = "repair", description = "Repair your current axe with the material it is made from")
@CommandId(392)
@Examples({ "axe repair 10", "axe repair all" })
public void repair(Sx4CommandEvent event, @Argument(value = "durability") @AlternativeOptions("all") Alternative<Integer> option) {
Bson filter = Filters.and(Filters.eq("userId", event.getAuthor().getIdLong()), Filters.eq("item.type", ItemType.AXE.getId()));
Document data = event.getMongo().getItem(filter, Projections.include("item"));
if (data == null) {
event.replyFailure("You do not have a axe").queue();
return;
}
Axe axe = Axe.fromData(event.getBot().getEconomyManager(), data.get("item", Document.class));
CraftItem item = axe.getRepairItem();
if (item == null) {
event.replyFailure("That axe is not repairable").queue();
return;
}
int maxDurability = axe.getMaxDurability() - axe.getDurability();
if (maxDurability <= 0) {
event.replyFailure("Your axe is already at full durability").queue();
return;
}
int durability;
if (option.isAlternative()) {
durability = maxDurability;
} else {
int amount = option.getValue();
if (amount > maxDurability) {
event.reply("You can only repair your axe by **" + maxDurability + "** durability :no_entry:").queue();
return;
}
durability = amount;
}
int itemCount = (int) Math.ceil((((double) axe.getPrice() / item.getPrice()) / axe.getMaxDurability()) * durability);
List<Button> buttons = List.of(Button.success("yes", "Yes"), Button.danger("no", "No"));
event.reply("It will cost you `" + itemCount + " " + item.getName() + "` to repair your axe by **" + durability + "** durability, are you sure you want to repair it?").setActionRow(buttons).submit().thenCompose(message -> {
return new Waiter<>(event.getBot(), ButtonClickEvent.class).setPredicate(e -> ButtonUtility.handleButtonConfirmation(e, message, event.getAuthor())).setCancelPredicate(e -> ButtonUtility.handleButtonCancellation(e, message, event.getAuthor())).onFailure(e -> ButtonUtility.handleButtonFailure(e, message)).setTimeout(60).start();
}).whenComplete((e, exception) -> {
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof CancelException) {
GenericEvent cancelEvent = ((CancelException) cause).getEvent();
if (cancelEvent != null) {
((ButtonClickEvent) cancelEvent).reply("Cancelled " + event.getConfig().getSuccessEmote()).queue();
}
return;
} else if (cause instanceof TimeoutException) {
event.reply("Timed out :stopwatch:").queue();
return;
} else if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
List<Bson> update = List.of(Operators.set("amount", Operators.let(new Document("amount", Operators.ifNull("$amount", 0L)), Operators.cond(Operators.lte(itemCount, "$$amount"), Operators.subtract("$$amount", itemCount), "$$amount"))));
event.getMongo().updateItem(Filters.and(Filters.eq("item.id", item.getId()), Filters.eq("userId", event.getAuthor().getIdLong())), update, new UpdateOptions()).thenCompose(result -> {
if (result.getMatchedCount() == 0 || result.getModifiedCount() == 0) {
e.reply("You do not have `" + itemCount + " " + item.getName() + "` " + event.getConfig().getFailureEmote()).queue();
return CompletableFuture.completedFuture(null);
}
List<Bson> itemUpdate = List.of(Operators.set("item.durability", Operators.cond(Operators.eq("$item.durability", axe.getDurability()), Operators.add("$item.durability", durability), "$item.durability")));
return event.getMongo().updateItem(Filters.and(Filters.eq("item.id", axe.getId()), Filters.eq("userId", event.getAuthor().getIdLong())), itemUpdate, new UpdateOptions());
}).whenComplete((result, databaseException) -> {
if (ExceptionUtility.sendExceptionally(event, databaseException) || result == null) {
return;
}
if (result.getMatchedCount() == 0) {
e.reply("You no longer have that axe " + event.getConfig().getFailureEmote()).queue();
return;
}
if (result.getMatchedCount() == 0) {
e.reply("The durability of your axe has changed " + event.getConfig().getFailureEmote()).queue();
return;
}
e.reply("You just repaired your axe by **" + durability + "** durability " + event.getConfig().getSuccessEmote()).queue();
});
});
}
Aggregations