Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ sortClassFields {
add 'main', 'org.spongepowered.api.world.gen.WorldGeneratorModifiers'
add 'main', 'org.spongepowered.api.world.weather.Weathers'
add 'main', 'org.spongepowered.api.util.TypeTokens'
add 'main', 'org.spongepowered.api.item.recipe.crafting.CraftingRecipes'
}

// Shaded API build (with all dependencies included)
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/spongepowered/api/CatalogTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.InventoryArchetype;
import org.spongepowered.api.item.inventory.equipment.EquipmentType;
import org.spongepowered.api.item.recipe.crafting.CraftingRecipe;
import org.spongepowered.api.scoreboard.CollisionRule;
import org.spongepowered.api.scoreboard.Visibility;
import org.spongepowered.api.scoreboard.critieria.Criterion;
Expand Down Expand Up @@ -136,6 +137,8 @@ public final class CatalogTypes {

public static final Class<CookedFish> COOKED_FISH = CookedFish.class;

public static final Class<CraftingRecipe> CRAFTING_RECIPES = CraftingRecipe.class;

public static final Class<Criterion> CRITERION = Criterion.class;

public static final Class<DamageModifierType> DAMAGE_MODIFIER_TYPE = DamageModifierType.class;
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/org/spongepowered/api/GameRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.merchant.TradeOfferGenerator;
import org.spongepowered.api.item.merchant.VillagerRegistry;
import org.spongepowered.api.item.recipe.RecipeRegistry;
import org.spongepowered.api.item.recipe.crafting.CraftingRecipeRegistry;
import org.spongepowered.api.item.recipe.smelting.SmeltingRecipeRegistry;
import org.spongepowered.api.network.status.Favicon;
import org.spongepowered.api.plugin.PluginContainer;
import org.spongepowered.api.plugin.PluginManager;
Expand Down Expand Up @@ -324,11 +325,18 @@ <T extends CatalogType> GameRegistry registerModule(Class<T> catalogClass, Catal
Favicon loadFavicon(BufferedImage image) throws IOException;

/**
* Retrieves the RecipeRegistry for this GameRegistry.
* Retrieves the crafting RecipeRegistry for this GameRegistry.
*
* @return The recipe registry
* @return The crafting recipe registry
*/
RecipeRegistry getRecipeRegistry();
CraftingRecipeRegistry getCraftingRecipeRegistry();

/**
* Retrieves the smelting RecipeRegistry for this GameRegistry.
*
* @return The smelting recipe registry
*/
SmeltingRecipeRegistry getSmeltingRecipeRegistry();

/**
* Gets a {@link ResourcePack} that's already been created by its ID.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.spongepowered.api.item.inventory.crafting;

import org.spongepowered.api.item.inventory.type.GridInventory;
import org.spongepowered.api.item.recipe.crafting.CraftingRecipe;
import org.spongepowered.api.world.World;

import java.util.Optional;

/**
* A CraftingGridInventory represents the inventory of something that can craft
* items. This is excluding the Result slot.
*/
public interface CraftingGridInventory extends GridInventory {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is result excluded?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the equivalent to InventoryCrafting.

CraftingInventory is a view on InventoryCrafting and the ResultSlot

/**
* Retrieves the recipe formed by this CraftingGridInventory, if any.
*
* @param world The world where the item would be crafted in
* @return The recipe or {@link Optional#empty()} if no recipe is formed
*/
Optional<CraftingRecipe> getRecipe(World world);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does World matter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vanilla matches Recipes with an InventoryCrafting and a World.
In vanilla it is only used to get MapData.

Maybe we could provide some kind of CraftingContext instead?

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
package org.spongepowered.api.item.inventory.crafting;

import org.spongepowered.api.item.inventory.type.GridInventory;
import org.spongepowered.api.item.recipe.Recipe;
import org.spongepowered.api.item.recipe.crafting.CraftingRecipe;
import org.spongepowered.api.world.World;

import java.util.Optional;

Expand All @@ -40,7 +41,7 @@ public interface CraftingInventory extends GridInventory {
*
* @return The crafting matrix
*/
GridInventory getCraftingGrid();
CraftingGridInventory getCraftingGrid();

/**
* Gets the result slot of this CraftingInventory.
Expand All @@ -52,8 +53,11 @@ public interface CraftingInventory extends GridInventory {
/**
* Retrieves the recipe formed by this CraftingInventory, if any.
*
* @param world The world where the item would be crafted in
* @return The recipe or {@link Optional#empty()} if no recipe is formed
*/
Optional<Recipe> getRecipe();
default Optional<CraftingRecipe> getRecipe(World world) {
return getCraftingGrid().getRecipe(world);
}

}
45 changes: 6 additions & 39 deletions src/main/java/org/spongepowered/api/item/recipe/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,52 +24,19 @@
*/
package org.spongepowered.api.item.recipe;

import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.type.GridInventory;

import java.util.List;
import java.util.Optional;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;

/**
* <p>A Recipe represents some craftable recipe in the game.</p>
*
* <p>It is essentially a Predicate that checks for if a recipe is valid as well
* as a function from a crafting matrix to a list of {@link ItemStack}
* (the crafting result), therefore making it an immutable interface.</p>
*
* <p>The passed in ItemGrid is usually a crafting inventory, e.g.
* a 2x2 or 3x3 crafting matrix.</p>
*
* <p>The requirements of a Recipe can be general, they just have to
* eventually return a boolean given an itemgrid.</p>
* A general interface for recipes
*/
public interface Recipe {

/**
* Returns the list of item types that result when successful crafting of
* this Recipe is completed.
*
* @return The resultant list of item types
*/
List<ItemType> getResultTypes();

/**
* Checks if the given {@link GridInventory} fits the required constraints
* to craft this Recipe.
*
* @param grid The ItemGrid to check for validity
* @return True if the given input matches this recipe's requirements
*/
boolean isValid(GridInventory grid);

/**
* Returns the results for running this Recipe over an {@link GridInventory}
* A general result of this recipe. This result may be customized depending
* on the context.
*
* @param grid An ItemGrid as input
* @return A list of ItemStacks or {@link Optional#empty()} if the given
* ItemGrid does not match this recipe's requirements.
* @return The exemplary result of this recipe
*/
Optional<List<ItemStack>> getResults(GridInventory grid);
ItemStackSnapshot getExemplaryResult();

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,25 @@
*/
package org.spongepowered.api.item.recipe;

import java.util.Set;
import java.util.Collection;

/**
* A RecipeRegistry holds all registered recipes for a given game.
*/
public interface RecipeRegistry {
public interface RecipeRegistry<T extends Recipe> {

/**
* Registers the given Recipe to make it available to craft.
* Registers the given {@link Recipe} to make it available to craft.
*
* @param recipe The Recipe to register
* @param recipe The {@link Recipe} to register
*/
void register(Recipe recipe);

/**
* Removes the given Recipe from registration in this registry.
*
* @param recipe The Recipe to unregister
*/
void remove(Recipe recipe);
void register(T recipe);

/**
* Retrieves all recipes registered in this registry.
*
* @return All registered recipes
* @return An unmodifiable collection of registered recipes
*/
Set<Recipe> getRecipes();
Collection<T> getRecipes();

}
143 changes: 0 additions & 143 deletions src/main/java/org/spongepowered/api/item/recipe/ShapedRecipe.java
Original file line number Diff line number Diff line change
@@ -1,143 +0,0 @@
/*
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.api.item.recipe;

import com.flowpowered.math.vector.Vector2i;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.util.ResettableBuilder;

import java.util.Optional;

import javax.annotation.Nullable;

/**
* A ShapedRecipe is a Recipe that has shape and fits into a grid.
*/
public interface ShapedRecipe extends Recipe {

/**
* Gets the width of the grid this ShapedRecipe fits into.
*
* @return The width of the grid
*/
int getWidth();

/**
* Gets the height of the grid this ShapedRecipe fits into.
*
* @return The height of the grid
*/
int getHeight();

/**
* Gets the ingredient required in the given coordinates.
*
* @param x The x coordinate
* @param y The y coordinate
* @return The ingredient
*/
Optional<ItemStack> getIngredient(int x, int y);

/**
* Gets the ingredient required in the given position.
*
* @param pos The position
* @return The ingredient
*/
Optional<ItemStack> getIngredient(Vector2i pos);

interface Builder extends ResettableBuilder<ShapedRecipe, Builder> {
/**
* Sets the width of the grid for the ShapedRecipe.
*
* @param width The width of the grid
* @return fluent interface
*/
Builder width(int width);

/**
* Sets the height of the grid for the ShapedRecipe.
*
* @param height The height of the grid
* @return fluent interface
*/
Builder height(int height);

/**
* Sets the dimensions of the grid for the ShapedRecipe in one method
* call.
*
* @param dimensions The dimensions of the grid
* @return fluent interface
*/
Builder dimensions(Vector2i dimensions);

/**
* Sets the ingredient required by the recipe in the given coordinates.
*
* @param x The x coordinate
* @param y The y coordinate
* @param ingredient The ingredient to set, or remove if null
* @return fluent interface
*/
Builder ingredient(int x, int y, @Nullable ItemStack ingredient);

/**
* Sets the ingredient required by the recipe in the given position.
*
* @param pos The position
* @param ingredient The ingredient to set, or remove if null
* @return fluent interface
*/
Builder ingredient(Vector2i pos, @Nullable ItemStack ingredient);

/**
* Sets the ingredients required by the recipe in the given row.
*
* @param row The number of the row
* @param ingredients A list of ItemStacks to set as ingredients. If one
* of them is null the ingredient in that position is
* not added.
* @return fluent interface
*/
Builder row(int row, ItemStack... ingredients);

/**
* Adds a resultant ItemStack for when this ShapedRecipe is correctly
* crafted.
*
* @param result The result
* @return fluent interface
*/
Builder addResult(ItemStack result);

/**
* Builds a ShapedRecipe from this builder.
*
* @return A new ShapedRecipe
*/
ShapedRecipe build();
}
}
Loading