Search in sources :

Example 1 with Ingredient

use of org.spongepowered.api.item.recipe.crafting.Ingredient 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 Ingredient

use of org.spongepowered.api.item.recipe.crafting.Ingredient 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)

Example 3 with Ingredient

use of org.spongepowered.api.item.recipe.crafting.Ingredient in project LanternServer by LanternPowered.

the class LanternShapedCraftingRecipeBuilder method from.

@Override
public IShapedCraftingRecipe.Builder from(ShapedCraftingRecipe value) {
    this.aisle.clear();
    this.ingredientMap.clear();
    this.groupName = value.getGroup().orElse(null);
    for (int y = 0; y < value.getHeight(); y++) {
        final StringBuilder row = new StringBuilder();
        for (int x = 0; x < value.getWidth(); x++) {
            final char symbol = (char) ('a' + x + y * value.getWidth());
            row.append(symbol);
            final Ingredient ingredient = value.getIngredient(x, y);
            this.ingredientMap.put(symbol, ingredient);
        }
        this.aisle.add(row.toString());
    }
    this.resultProvider = ((LanternShapedCraftingRecipe) value).resultProvider;
    return this;
}
Also used : IIngredient(org.lanternpowered.server.item.recipe.IIngredient) Ingredient(org.spongepowered.api.item.recipe.crafting.Ingredient)

Example 4 with Ingredient

use of org.spongepowered.api.item.recipe.crafting.Ingredient in project LanternServer by LanternPowered.

the class LanternShapedCraftingRecipeBuilder method row.

@Override
public RowsStep.ResultStep row(int skip, Ingredient... ingredients) {
    int columns = ingredients.length + skip;
    if (!this.aisle.isEmpty()) {
        checkState(this.aisle.get(0).length() == columns, "The rows have an inconsistent width.");
    }
    final StringBuilder row = new StringBuilder();
    for (int i = 0; i < skip; i++) {
        row.append(" ");
    }
    int key = 'a' + columns * this.aisle.size();
    for (Ingredient ingredient : ingredients) {
        key++;
        final char character = (char) key;
        row.append(character);
        this.ingredientMap.put(character, ingredient);
    }
    this.aisle.add(row.toString());
    return this;
}
Also used : IIngredient(org.lanternpowered.server.item.recipe.IIngredient) Ingredient(org.spongepowered.api.item.recipe.crafting.Ingredient)

Example 5 with Ingredient

use of org.spongepowered.api.item.recipe.crafting.Ingredient in project LanternServer by LanternPowered.

the class LanternShapelessCraftingRecipe method match.

@Override
public Optional<Result> match(CraftingMatrix craftingMatrix, @Nullable World world, int flags) {
    final int w = craftingMatrix.width();
    final int h = craftingMatrix.height();
    final int s = w * h;
    // Check if all the ingredients can fit in the crafting grid
    if (this.ingredients.size() > s) {
        return Optional.empty();
    }
    final boolean resultItem = (flags & Flags.RESULT_ITEM) != 0;
    final boolean remainingItems = (flags & Flags.REMAINING_ITEMS) != 0;
    final List<Ingredient> ingredients = new ArrayList<>(this.ingredients);
    // Generate a ingredient map that can be useful to generate a result item
    final Multimap<Ingredient, ItemStack> ingredientItems = resultItem && !(this.resultProvider instanceof ConstantCraftingResultProvider) ? HashMultimap.create() : null;
    final ImmutableList.Builder<ItemStackSnapshot> remainingItemsBuilder = remainingItems ? ImmutableList.builder() : null;
    int times = -1;
    int[][] itemQuantities = remainingItems ? new int[w][h] : null;
    for (int j = 0; j < h; j++) {
        for (int i = 0; i < w; i++) {
            final ItemStack itemStack = craftingMatrix.get(i, j);
            // Don't check empty item stacks
            if (itemStack.isEmpty()) {
                remainingItemsBuilder.add(ItemStackSnapshot.NONE);
                continue;
            }
            final Iterator<Ingredient> it = ingredients.iterator();
            Optional<ItemStack> remainingItem = Optional.empty();
            boolean success = false;
            while (it.hasNext()) {
                final IIngredient ingredient = (IIngredient) it.next();
                if (!ingredient.test(itemStack)) {
                    continue;
                }
                final int quantity = ingredient.getQuantity(itemStack);
                if (quantity < itemStack.getQuantity()) {
                    continue;
                }
                itemQuantities[i][j] = quantity;
                final int times1 = itemStack.getQuantity() / quantity;
                if (times == -1 || times1 < times) {
                    times = times1;
                }
                if (ingredientItems != null) {
                    ingredientItems.put(ingredient, itemStack);
                }
                if (remainingItemsBuilder != null) {
                    remainingItem = ingredient.getRemainingItem(itemStack);
                }
                it.remove();
                success = true;
                break;
            }
            // A faulty input ingredient was found
            if (!success) {
                return Optional.empty();
            }
            if (remainingItemsBuilder != null) {
                remainingItemsBuilder.add(remainingItem.map(ItemStack::createSnapshot).orElse(ItemStackSnapshot.NONE));
            }
        }
    }
    // Not all the ingredients were found
    if (!ingredients.isEmpty()) {
        return Optional.empty();
    }
    // Generate the result item
    ItemStackSnapshot resultItemStack = null;
    if (resultItem) {
        resultItemStack = this.resultProvider.getSnapshot(craftingMatrix, ingredientItems == null ? null : new SimpleIngredientList(ingredientItems));
        checkNotNull(resultItemStack, "Something funky happened.");
    }
    return Optional.of(new Result(resultItemStack, remainingItemsBuilder == null ? null : remainingItemsBuilder.build(), itemQuantities, times));
}
Also used : IIngredient(org.lanternpowered.server.item.recipe.IIngredient) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) IIngredient(org.lanternpowered.server.item.recipe.IIngredient) Ingredient(org.spongepowered.api.item.recipe.crafting.Ingredient) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Aggregations

Ingredient (org.spongepowered.api.item.recipe.crafting.Ingredient)11 IIngredient (org.lanternpowered.server.item.recipe.IIngredient)6 ItemStack (org.spongepowered.api.item.inventory.ItemStack)5 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)4 PluginContainer (org.spongepowered.api.plugin.PluginContainer)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 Preconditions.checkState (com.google.common.base.Preconditions.checkState)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Nullable (javax.annotation.Nullable)2 ShapedRecipes (net.minecraft.item.crafting.ShapedRecipes)2 ShapedCraftingRecipe (org.spongepowered.api.item.recipe.crafting.ShapedCraftingRecipe)2 Preconditions (com.google.common.base.Preconditions)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 Lists (com.google.common.collect.Lists)1 Char2ObjectArrayMap (it.unimi.dsi.fastutil.chars.Char2ObjectArrayMap)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1