diff options
Diffstat (limited to 'src/main/java/net/uomc/mineshaft/crafting')
| -rw-r--r-- | src/main/java/net/uomc/mineshaft/crafting/Crafting.java | 137 | ||||
| -rw-r--r-- | src/main/java/net/uomc/mineshaft/crafting/CraftingRecipe.java | 66 |
2 files changed, 203 insertions, 0 deletions
diff --git a/src/main/java/net/uomc/mineshaft/crafting/Crafting.java b/src/main/java/net/uomc/mineshaft/crafting/Crafting.java new file mode 100644 index 0000000..5bbef1c --- /dev/null +++ b/src/main/java/net/uomc/mineshaft/crafting/Crafting.java @@ -0,0 +1,137 @@ +package net.uomc.mineshaft.crafting; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collector; +import java.util.stream.Collectors; + +import com.mouldycheerio.dbot.CustomBot; +import com.mouldycheerio.dbot.util.PeelingUtils; +import com.vdurmont.emoji.EmojiParser; +import com.mouldycheerio.dbot.commands.CommandFail; +import com.mouldycheerio.dbot.commands.DetailedCommand; +import com.mouldycheerio.dbot.commands.CommandDetails; + +import net.uomc.mineshaft.resources.Resource; +import net.uomc.mineshaft.Mineshaft; +import net.uomc.mineshaft.MineshaftItem; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.MessageEmbed; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; + +public class Crafting extends DetailedCommand { + private static final String COMMAND_IMAGE = "https://minecraft.wiki/images/Crafting_Table_JE4_BE3.png"; + private static final String COMMAND_TITLE = "Crafting Table"; + List<CraftingRecipe> recipes; + private Mineshaft bot; + + public Crafting(Mineshaft bot) { + setDetails(CommandDetails.from("craft", "Craft an item", "craft [item]")); + this.bot = bot; + + recipes = new ArrayList<>(); + + addRecipe( + CraftingRecipe.make(MineshaftItem.ENCHANTING_TABLE) + .addIngredient(MineshaftItem.BOOK, 1) + .addIngredient(MineshaftItem.OBSIDIAN, 4) + .addIngredient(MineshaftItem.DIAMOND, 2) + .setBuyMessage("Use `" + bot.getCommandController().getPrefix() + "enchant` to use your enchanting table!") + ); + + addRecipe( + CraftingRecipe.make(MineshaftItem.BOOKSHELF) + .addIngredient(MineshaftItem.BOOK, 3) + .setBuyMessage("**+1 enchanting table level**") + ); + + addRecipe( + CraftingRecipe.make(MineshaftItem.FURNACE) + .addIngredient(MineshaftItem.COAL, 800l) + .setBuyMessage("Use `" + bot.getCommandController().getPrefix() + "furnace` to get free ores") + ); + } + + + public List<CraftingRecipe> getRecipes() { + return recipes; + } + + public Crafting addRecipe(CraftingRecipe craft) { + recipes.add(craft); + return this; + } + + @Override + public void execute(MessageReceivedEvent e, CustomBot b, String[] args) throws CommandFail { + if (args.length > 0) { + craft(e, args); + return; + } + + String description = "This is your crafting table. Craft an item with `" + b.getPrefixManager().getPrefix(e.getGuild()) + "craft [item]`\n\n"; + + description += getCraftingRecipesList(); + + EmbedBuilder em = new EmbedBuilder(); + em.setTitle(COMMAND_TITLE); + em.setDescription(description); + em.setThumbnail(COMMAND_IMAGE); + em.setColor(PeelingUtils.hex2Rgb("#93764c")); + + em.setFooter(EmojiParser.parseToUnicode(":bulb:") + "View your inventory with " +b.getPrefixManager().getPrefix(e.getGuild()) + "inv"); + + e.getMessage().replyEmbeds(em.build()).queue(); + } + + public String getCraftingRecipesList() { + return getRecipes().stream().map(recipe -> { + Resource r = bot.getItem(recipe.getItem()); + + return "**" + r.getPrettyName() + "**\n" + + bot.createItemList(recipe.getIngredients(), "%s", ", ") + + " -> " + + r.prettyValue(recipe.getQuantity()); + }).collect(Collectors.joining("\n\n")); + } + + + private void craft(MessageReceivedEvent e, String[] args) { + long amount = 1; + String toCraft; + if (PeelingUtils.isLong(args[args.length-1])) { + amount = Long.parseLong(args[args.length-1]); + toCraft = String.join("_", Arrays.copyOfRange(args, 0, args.length -1)).toLowerCase(); + } else { + toCraft = String.join("_", args).toLowerCase(); + } + + Optional<CraftingRecipe> selected = getRecipes().stream().filter(r -> r.getItem().toString().equalsIgnoreCase(toCraft)).findFirst(); + + if (selected.isEmpty()) { + bot.sendErrorMessage(e, ":x: That item does not have a crafting recipe"); + return; + } + + CraftingRecipe recipe = selected.get(); + Map<MineshaftItem, Long> missing = recipe.getMissingIngredients(bot.getInventory(e.getMember()), amount); + + if (!missing.isEmpty()) { + bot.sendErrorMessage(e, ":x: Unable to craft!\nIngredients missing: **" + bot.createItemList(missing, "%s", ", ") + "**"); + return; + } + + bot.removeItems(e.getMember(), bot.multiply(recipe.getIngredients(), amount)); + bot.addItem(e.getMember(), recipe.getItem(), recipe.getQuantity() * amount); + bot.sendSuccessMessage(e, + "You crafted " + bot.getItem(recipe.getItem()).prettyValue(recipe.getQuantity() * amount) + + "!\n" + recipe.getBuyMessage() + ); + + } + + +} diff --git a/src/main/java/net/uomc/mineshaft/crafting/CraftingRecipe.java b/src/main/java/net/uomc/mineshaft/crafting/CraftingRecipe.java new file mode 100644 index 0000000..cb4892e --- /dev/null +++ b/src/main/java/net/uomc/mineshaft/crafting/CraftingRecipe.java @@ -0,0 +1,66 @@ +package net.uomc.mineshaft.crafting; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import net.uomc.mineshaft.MineshaftItem; + +public class CraftingRecipe { + MineshaftItem item; + long quantity; + Map<MineshaftItem, Long> ingredients; + + String buyMessage; + + private CraftingRecipe(MineshaftItem item, long quantity, Map<MineshaftItem, Long> ingredients) { + this.item = item; + this.quantity = quantity; + this.ingredients = ingredients; + } + + public static CraftingRecipe make(MineshaftItem item) { + Map<MineshaftItem, Long> ingredients = new HashMap<>(); + return new CraftingRecipe(item, 1, ingredients); + } + + public CraftingRecipe addIngredient(MineshaftItem item, long amount) { + ingredients.put(item, amount); + return this; + } + + public MineshaftItem getItem() { + return item; + } + + public long getQuantity() { + return quantity; + } + + public Map<MineshaftItem, Long> getIngredients() { + return ingredients; + } + + public String getBuyMessage() { + return buyMessage; + } + + public CraftingRecipe setBuyMessage(String message) { + buyMessage = message; + return this; + } + public Map<MineshaftItem, Long> getMissingIngredients(Map<MineshaftItem, Long> items) { + return getMissingIngredients(items, 1); + } + + public Map<MineshaftItem, Long> getMissingIngredients(Map<MineshaftItem, Long> items, long amount) { + Map<MineshaftItem, Long> lacking = new HashMap<MineshaftItem, Long>(); + getIngredients().forEach((item, q) -> { + if (items.get(item) < q* amount) { + lacking.put(item, (q * amount) - items.get(item)); + } + }); + + return Collections.unmodifiableMap(lacking); + } + +} |
