BKCommonLib/Recipes

From BergerHealer Wiki
Revision as of 19:14, 9 June 2021 by RyanDo (talk | contribs) (Created page with "« Go back ==Introduction== [https://github.com/bergerkiller/BKCommonLib/blob/master/src/main/java/com/bergerkiller/bukkit/common/utils/RecipeUtil.java Recipe...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

« Go back

Introduction

RecipeUtil provides various utility methods to work with item crafting and furnace recipes. It allows you to obtain the information required to do various recipe-related operations.

Craft Recipes

To perform crafting operations, BKCommonLib has it's own implementation, or wrapper class, to store and use a crafting (table) recipe: CraftRecipe. In this class, all required input items and resulting output item(s) are stored. You can then, by calling just one method, perform the crafting in an inventory, with an optional maximum amount of times.

Obtaining a Craft Recipe

There are methods in RecipeUtil to obtain the Craft Recipes that are able to produce a given item, this method is called getCraftingRequirements. As a plugin, you can then go through all recipes and try to craft something, or design your own filter to get the desired results.

Example of Craft Recipes being used

The following example will try to craft 64 wood 'pieces' (one item with amount 64) using the items from a player inventory. Note that craft does not take in account how many items you get per crafting operation, therefore craftItems was added that takes care of that. It only uses the first recipe that can be found in this example, so other ways to craft wood are ignored.

Inventory inventory = player.getInventory();
CraftRecipe[] recipes = RecipeUtil.getCraftingRequirements(Material.WOOD, null);
if (recipes.length > 0) {
    recipes[0].craftItems(inventory, 64);
}

If you want to be certain that crafting is possible, make a copy of the inventory (e.g. InventoryBaseImpl) and pass that into the crafting method first.

Furnace 'recipes'

The furnace does not really maintain a list of recipes, instead it more or less assigns a property to certain items that make them 'furnace craftable'. For that reason, and because the output amount is always 1, no special FurnaceCraftRecipe is added. Instead, you can obtain the furnace results, check the fuel time of items, check whether an item is fuel and whether an item can be heated, and so on. All of these methods can be found in RecipeUtil.