1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

Added documentation

This commit is contained in:
TheBusyBiscuit 2021-02-06 11:09:13 +01:00
parent eeb89c3c3b
commit baa4f92ea1
4 changed files with 139 additions and 5 deletions

View File

@ -180,6 +180,15 @@ public abstract class AbstractAutoCrafter extends SlimefunItem implements Energy
*/
protected abstract void updateRecipe(@Nonnull Block b, @Nonnull Player p);
/**
* This method sets the selected {@link AbstractRecipe} for the given {@link Block}.
* The recipe will be stored using the {@link PersistentDataAPI}.
*
* @param b
* The {@link Block} to store the data on
* @param recipe
* The {@link AbstractRecipe} to select
*/
protected void setSelectedRecipe(@Nonnull Block b, @Nullable AbstractRecipe recipe) {
BlockState state = PaperLib.getBlockState(b, false).getState();
@ -194,8 +203,22 @@ public abstract class AbstractAutoCrafter extends SlimefunItem implements Energy
}
}
/**
* This shows the given {@link AbstractRecipe} to the {@link Player} in a preview window.
*
* @param p
* The {@link Player}
* @param b
* The {@link Block} of the {@link AbstractAutoCrafter}
* @param recipe
* The {@link AbstractRecipe} to show them
*/
@ParametersAreNonnullByDefault
protected void showRecipe(Player p, Block b, AbstractRecipe recipe) {
Validate.notNull(p, "The Player should not be null");
Validate.notNull(b, "The Block should not be null");
Validate.notNull(recipe, "The Recipe should not be null");
ChestMenu menu = new ChestMenu(getItemName());
menu.setPlayerInventoryClickable(false);
menu.setEmptySlotsClickable(false);
@ -259,6 +282,20 @@ public abstract class AbstractAutoCrafter extends SlimefunItem implements Energy
return false;
}
/**
* This method performs a crafting operation.
* It will attempt to fulfill the provided {@link AbstractRecipe} using
* the given {@link Inventory}.
* This will consume items and add the result to the {@link Inventory}.
* This method does not handle energy consumption.
*
* @param inv
* The {@link Inventory} to take resources from
* @param recipe
* The {@link AbstractRecipe} to craft
*
* @return Whether this crafting operation was successful or not
*/
public boolean craft(@Nonnull Inventory inv, @Nonnull AbstractRecipe recipe) {
Validate.notNull(inv, "The Inventory must not be null");
Validate.notNull(recipe, "The Recipe shall not be null");

View File

@ -10,40 +10,103 @@ import javax.annotation.ParametersAreNonnullByDefault;
import org.apache.commons.lang.Validate;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.RecipeChoice.MaterialChoice;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.ShapelessRecipe;
import io.github.thebusybiscuit.slimefun4.implementation.items.multiblocks.EnhancedCraftingTable;
import io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
/**
* This class abstracts away from concrete recipes.
* It supports {@link ShapedRecipe}, {@link ShapelessRecipe} and
* recipes made using the {@link EnhancedCraftingTable}.
*
* @author TheBusyBiscuit
*
* @see AbstractAutoCrafter
* @see VanillaRecipe
* @see EnhancedRecipe
*
*/
public abstract class AbstractRecipe {
private final Collection<Predicate<ItemStack>> inputs;
/**
* Our {@link Collection} of ingredients / predicates.
*/
private final Collection<Predicate<ItemStack>> ingredients;
/**
* The recipe result.
*/
private final ItemStack result;
/**
* Protected constructor. For implementation classes only.
*
* @param ingredients
* The ingredients for this recipe as predicates
* @param result
* The resulting {@link ItemStack}
*/
@ParametersAreNonnullByDefault
protected AbstractRecipe(Collection<Predicate<ItemStack>> inputs, ItemStack result) {
Validate.notEmpty(inputs, "The input predicates cannot be null or an empty array");
protected AbstractRecipe(Collection<Predicate<ItemStack>> ingredients, ItemStack result) {
Validate.notEmpty(ingredients, "The input predicates cannot be null or an empty array");
Validate.notNull(result, "The recipe result must not be null!");
this.inputs = inputs;
this.ingredients = ingredients;
this.result = result;
}
/**
* This returns the {@link Collection} of ingredients as {@link Predicate Predicates}.
*
* @return The ingredients for this {@link AbstractRecipe}
*/
@Nonnull
public Collection<Predicate<ItemStack>> getIngredients() {
return inputs;
return ingredients;
}
/**
* This returns the result of this {@link AbstractRecipe}.
* This will return the original {@link ItemStack}, so make sure to {@link ItemStack#clone()}
* it.
*
* @return The resulting {@link ItemStack}
*/
@Nonnull
public ItemStack getResult() {
return result;
}
/**
* This will visually represent this {@link AbstractRecipe} in the given {@link ChestMenu}.
* Any {@link MaterialChoice} will be cycled through using the {@link AsyncRecipeChoiceTask}.
*
* @param menu
* The {@link ChestMenu} to display the recipe in
* @param task
* The {@link AsyncRecipeChoiceTask} instance
*/
public abstract void show(@Nonnull ChestMenu menu, @Nonnull AsyncRecipeChoiceTask task);
/**
* This is our static accessor for the {@link AbstractRecipe} class.
* It will create a new {@link VanillaRecipe} for the given {@link Recipe}
* if it is a valid {@link Recipe}.
* <p>
* Currently supported recipe types are {@link ShapedRecipe} and {@link ShapelessRecipe}.
* If the {@link Recipe} is null or none of the aforementioned types, null will be returned.
*
* @param recipe
* The {@link Recipe} to wrap
*
* @return The wrapped {@link AbstractRecipe} or null
*/
@Nullable
public static AbstractRecipe of(@Nullable Recipe recipe) {
if (recipe instanceof ShapedRecipe) {
@ -55,6 +118,16 @@ public abstract class AbstractRecipe {
}
}
/**
* This static accessor is for {@link SlimefunItem} recipes.
* Note that the {@link SlimefunItem} must be crafted using an {@link EnhancedCraftingTable},
* otherwise null will be returned.
*
* @param item
* The {@link SlimefunItem} the recipe belongs to
*
* @return The wrapped {@link AbstractRecipe} or null
*/
@Nullable
public static AbstractRecipe of(@Nullable SlimefunItem item) {
if (item != null && item.getRecipeType().equals(RecipeType.ENHANCED_CRAFTING_TABLE)) {

View File

@ -16,6 +16,7 @@ import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.cscorelib2.data.PersistentDataAPI;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.items.multiblocks.EnhancedCraftingTable;
import io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import io.papermc.lib.PaperLib;
@ -25,6 +26,17 @@ import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
/**
* The {@link EnhancedAutoCrafter} is an implementation of the {@link AbstractAutoCrafter}.
* It can craft items that are crafted using the {@link EnhancedCraftingTable}.
*
* @author TheBusyBiscuit
*
* @see AbstractAutoCrafter
* @see VanillaAutoCrafter
* @see EnhancedRecipe
*
*/
public class EnhancedAutoCrafter extends AbstractAutoCrafter {
@ParametersAreNonnullByDefault

View File

@ -36,6 +36,18 @@ import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
/**
* The {@link VanillaAutoCrafter} is an implementation of the {@link AbstractAutoCrafter}.
* It can craft items that are crafted using a normal crafting table.
* Only {@link ShapedRecipe} and {@link ShapelessRecipe} are therefore supported.
*
* @author TheBusyBiscuit
*
* @see AbstractAutoCrafter
* @see EnhancedAutoCrafter
* @see VanillaRecipe
*
*/
public class VanillaAutoCrafter extends AbstractAutoCrafter {
@ParametersAreNonnullByDefault