Search in sources :

Example 1 with ShapedCraftingRecipe

use of org.spongepowered.api.item.recipe.crafting.ShapedCraftingRecipe in project SpongeCommon by SpongePowered.

the class SpongeShapedCraftingRecipeBuilder method build.

@Override
public ShapedCraftingRecipe build(String id, Object plugin) {
    checkState(!this.aisle.isEmpty(), "aisle has not been set");
    checkState(!this.ingredientMap.isEmpty(), "no ingredients set");
    checkState(!this.result.isEmpty(), "no result set");
    checkNotNull(id, "id");
    checkNotNull(id, "plugin");
    PluginContainer container = SpongeImpl.getPluginContainer(plugin);
    if (!id.startsWith(container.getId() + ":")) {
        id = container.getId() + ":" + id;
    }
    Iterator<String> aisleIterator = this.aisle.iterator();
    String aisleRow = aisleIterator.next();
    int width = aisleRow.length();
    int height = 1;
    checkState(width > 0, "The aisle cannot be empty.");
    while (aisleIterator.hasNext()) {
        height++;
        aisleRow = aisleIterator.next();
        checkState(aisleRow.length() == width, "The aisle has an inconsistent width.");
    }
    String[] keys = this.aisle.toArray(new String[this.aisle.size()]);
    Map<String, net.minecraft.item.crafting.Ingredient> ingredientsMap = this.ingredientMap.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().toString(), e -> IngredientUtil.toNative(e.getValue())));
    // Default space to Empty Ingredient
    ingredientsMap.putIfAbsent(" ", net.minecraft.item.crafting.Ingredient.EMPTY);
    // Throws JsonException when pattern is not complete or defines unused Ingredients
    NonNullList<net.minecraft.item.crafting.Ingredient> ingredients = ShapedRecipes.deserializeIngredients(keys, ingredientsMap, width, height);
    return ((ShapedCraftingRecipe) new SpongeShapedRecipe(id, this.groupName, width, height, ingredients, ItemStackUtil.toNative(this.result)));
}
Also used : SpongeImpl(org.spongepowered.common.SpongeImpl) Iterator(java.util.Iterator) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Char2ObjectArrayMap(it.unimi.dsi.fastutil.chars.Char2ObjectArrayMap) ItemStackUtil(org.spongepowered.common.item.inventory.util.ItemStackUtil) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Strings(com.google.common.base.Strings) ShapedCraftingRecipe(org.spongepowered.api.item.recipe.crafting.ShapedCraftingRecipe) ItemStack(org.spongepowered.api.item.inventory.ItemStack) List(java.util.List) Lists(com.google.common.collect.Lists) Ingredient(org.spongepowered.api.item.recipe.crafting.Ingredient) Map(java.util.Map) Preconditions(com.google.common.base.Preconditions) NonNullList(net.minecraft.util.NonNullList) ShapedRecipes(net.minecraft.item.crafting.ShapedRecipes) PluginContainer(org.spongepowered.api.plugin.PluginContainer) Collections(java.util.Collections) Nullable(javax.annotation.Nullable) PluginContainer(org.spongepowered.api.plugin.PluginContainer) Ingredient(org.spongepowered.api.item.recipe.crafting.Ingredient)

Example 2 with ShapedCraftingRecipe

use of org.spongepowered.api.item.recipe.crafting.ShapedCraftingRecipe in project SpongeCommon by SpongePowered.

the class RecipeTest method onInit.

@Listener
public void onInit(GamePreInitializationEvent event) {
    final Ingredient s = Ingredient.of(STONE);
    final Ingredient b = Ingredient.of(BED, WOOL);
    final ItemStack item = ItemStack.of(BEDROCK, 1);
    final DataTransactionResult trans = item.offer(Keys.ITEM_ENCHANTMENTS, Collections.singletonList(Enchantment.of(EnchantmentTypes.UNBREAKING, 1)));
    if (trans.getType() != DataTransactionResult.Type.SUCCESS) {
        this.plugin.getLogger().error("Could not build recipe output!");
    }
    final ShapedCraftingRecipe recipe = CraftingRecipe.shapedBuilder().rows().row(s, s, s).row(s, b, s).row(s, s, s).result(item).build("bedrock", this.plugin);
    Sponge.getRegistry().getCraftingRecipeRegistry().register(recipe);
}
Also used : Ingredient(org.spongepowered.api.item.recipe.crafting.Ingredient) ShapedCraftingRecipe(org.spongepowered.api.item.recipe.crafting.ShapedCraftingRecipe) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Listener(org.spongepowered.api.event.Listener)

Aggregations

ItemStack (org.spongepowered.api.item.inventory.ItemStack)2 Ingredient (org.spongepowered.api.item.recipe.crafting.Ingredient)2 ShapedCraftingRecipe (org.spongepowered.api.item.recipe.crafting.ShapedCraftingRecipe)2 Preconditions (com.google.common.base.Preconditions)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 Char2ObjectArrayMap (it.unimi.dsi.fastutil.chars.Char2ObjectArrayMap)1 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 ShapedRecipes (net.minecraft.item.crafting.ShapedRecipes)1 NonNullList (net.minecraft.util.NonNullList)1 DataTransactionResult (org.spongepowered.api.data.DataTransactionResult)1 Listener (org.spongepowered.api.event.Listener)1 PluginContainer (org.spongepowered.api.plugin.PluginContainer)1