Search in sources :

Example 6 with Ingredient

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

the class LanternShapedCraftingRecipe method matchAt.

/**
 * Matches this recipe at the given start x
 * and y coordinates within the {@link CraftingMatrix}.
 *
 * @param craftingMatrix The crafting matrix
 * @param startX The initial x coordinate
 * @param startY The initial y coordinate
 * @return Whether the recipe matches
 */
@Nullable
private Result matchAt(CraftingMatrix craftingMatrix, int startX, int startY, int flags, int[][] itemQuantities) {
    // Clear the quantities
    fill(itemQuantities, 0);
    final int cw = craftingMatrix.width();
    final int ch = craftingMatrix.height();
    final int rw = getWidth();
    final int rh = getHeight();
    final int ew = startX + rw;
    final int eh = startY + rh;
    // The recipe no longer fits within the grid when starting from the coordinates
    if (ew > cw || eh > ch) {
        return null;
    }
    final boolean resultItem = (flags & Flags.RESULT_ITEM) != 0;
    // 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;
    int times = -1;
    for (int y = 0; y < ch; y++) {
        for (int x = 0; x < cw; x++) {
            final ItemStack itemStack = craftingMatrix.get(x, y);
            final int i = x - startX;
            final int j = y - startY;
            final IIngredient ingredient = i < 0 || i >= rw || j < 0 || j >= rh ? null : this.ingredients[i][j];
            if (ingredient == null) {
                if (itemStack.isEmpty()) {
                    continue;
                }
                return null;
            }
            final int quantity = ingredient.getQuantity(itemStack);
            if (!ingredient.test(itemStack) || itemStack.getQuantity() < quantity) {
                return null;
            }
            itemQuantities[x][y] = quantity;
            final int times1 = itemStack.getQuantity() / quantity;
            if (times == -1 || times1 < times) {
                times = times1;
            }
            if (ingredientItems != null) {
                ingredientItems.put(ingredient, itemStack);
            }
        }
    }
    // 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.");
    }
    // Generate a list with all the remaining items, doing this for every
    // slot, even empty ones, empty ones are added as a empty remaining
    // item.
    List<ItemStackSnapshot> remainingItemsList = null;
    if ((flags & Flags.REMAINING_ITEMS) != 0) {
        final List<ItemStackSnapshot> builder = new ArrayList<>();
        for (int i = 0; i < ch * cw; i++) {
            builder.add(ItemStackSnapshot.NONE);
        }
        for (int j = 0; j < rh; j++) {
            for (int i = 0; i < rw; i++) {
                final IIngredient ingredient = this.ingredients[i][j];
                if (ingredient != null) {
                    final Optional<ItemStack> remainingItem = ingredient.getRemainingItem(craftingMatrix.get(i, j));
                    if (remainingItem.isPresent()) {
                        builder.set((j + startY) * cw + (i + startX), LanternItemStack.toSnapshot(remainingItem.get()));
                    }
                }
            }
        }
        remainingItemsList = ImmutableList.copyOf(builder);
    }
    return new Result(resultItemStack, remainingItemsList, itemQuantities, times);
}
Also used : IIngredient(org.lanternpowered.server.item.recipe.IIngredient) 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) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) Nullable(javax.annotation.Nullable)

Example 7 with Ingredient

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

the class SpongeShapedCraftingRecipeBuilder method from.

@Override
public ShapedCraftingRecipe.Builder from(ShapedCraftingRecipe value) {
    this.aisle.clear();
    this.ingredientMap.clear();
    this.groupName = "";
    if (value instanceof ShapedRecipes) {
        this.groupName = ((ShapedRecipes) value).group;
    }
    if (value != null) {
        for (int y = 0; y < value.getHeight(); y++) {
            String row = "";
            for (int x = 0; x < value.getWidth(); x++) {
                char symbol = (char) ('a' + x + y * value.getWidth());
                row += symbol;
                Ingredient ingredient = value.getIngredient(x, y);
                this.ingredientMap.put(symbol, ingredient);
            }
            this.aisle.add(row);
        }
        this.result = value.getExemplaryResult().createStack();
    } else {
        this.result = null;
    }
    return this;
}
Also used : ShapedRecipes(net.minecraft.item.crafting.ShapedRecipes) Ingredient(org.spongepowered.api.item.recipe.crafting.Ingredient)

Example 8 with Ingredient

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

the class SpongeShapedCraftingRecipeBuilder 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.");
    }
    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++;
        char character = (char) key;
        row.append(character);
        this.ingredientMap.put(character, ingredient);
    }
    this.aisle.add(row.toString());
    return this;
}
Also used : Ingredient(org.spongepowered.api.item.recipe.crafting.Ingredient)

Example 9 with Ingredient

use of org.spongepowered.api.item.recipe.crafting.Ingredient in project modules-extra by CubeEngine.

the class ItemDuctManager method setup.

public void setup(PluginContainer plugin, ItemductConfig config) {
    if (!this.init) {
        DataRegistration.<DuctData, ImmutableDuctData>builder().dataClass(DuctData.class).immutableClass(ImmutableDuctData.class).builder(new DuctDataBuilder()).manipulatorId("duct").dataName("CubeEngine ItemDuct Data").buildAndRegister(plugin);
        DuctData.FILTERS.getQuery();
        Ingredient hopper = Ingredient.of(ItemTypes.HOPPER);
        activatorItem = ItemStack.of(ItemTypes.HOPPER, 1);
        activatorItem.offer(Keys.ITEM_ENCHANTMENTS, singletonList(Enchantment.builder().type(EnchantmentTypes.LOOTING).level(1).build()));
        activatorItem.offer(Keys.DISPLAY_NAME, Text.of(TextColors.GOLD, "ItemDuct Activator"));
        activatorItem.offer(Keys.HIDE_ENCHANTMENTS, true);
        activatorItem.offer(new DuctData(config.activatorUses));
        activatorItem.offer(Keys.ITEM_LORE, Collections.singletonList(Text.of("Uses: ", config.activatorUses)));
        this.recipe = CraftingRecipe.shapedBuilder().rows().row(hopper, hopper, hopper).row(hopper, Ingredient.of(ItemTypes.DIAMOND), hopper).row(hopper, hopper, hopper).result(activatorItem.copy()).build("ItemDuctActivator", plugin);
        Sponge.getRegistry().getCraftingRecipeRegistry().register(this.recipe);
        activatorItem.offer(new DuctData(config.superActivatorUses));
        activatorItem.offer(Keys.ITEM_LORE, Collections.singletonList(Text.of("Uses: Infinite")));
        activatorItem.offer(Keys.DISPLAY_NAME, Text.of(TextColors.GOLD, "ItemDuct Super Activator"));
        this.superRecipe = CraftingRecipe.shapedBuilder().rows().row(hopper, hopper, hopper).row(hopper, Ingredient.of(ItemTypes.NETHER_STAR), hopper).row(hopper, hopper, hopper).result(activatorItem.copy()).build("ItemDuctSuperActivator", plugin);
        Sponge.getRegistry().getCraftingRecipeRegistry().register(this.superRecipe);
    }
    this.reload(config);
    this.init = true;
}
Also used : Ingredient(org.spongepowered.api.item.recipe.crafting.Ingredient) ImmutableDuctData(org.cubeengine.module.itemduct.data.ImmutableDuctData) IDuctData(org.cubeengine.module.itemduct.data.IDuctData) DuctData(org.cubeengine.module.itemduct.data.DuctData) ImmutableDuctData(org.cubeengine.module.itemduct.data.ImmutableDuctData) DuctDataBuilder(org.cubeengine.module.itemduct.data.DuctDataBuilder)

Example 10 with Ingredient

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

the class LanternSmeltingRecipeBuilder method build.

@Override
public ISmeltingRecipe build() {
    check();
    // Attempt to generate a id for the smelting recipe
    String ingredient = this.exemplaryIngredient.getType().getId();
    ingredient = ingredient.substring(ingredient.indexOf(':') + 1);
    final ItemStackSnapshot exemplaryResult = getResultProvider().get(this.exemplaryIngredient).getResult();
    String result = exemplaryResult.getType().getId();
    result = result.substring(result.indexOf(':') + 1);
    final String id = ingredient + "_to_" + result;
    final int count = idCounters.computeIfAbsent(id, s -> 0) + 1;
    idCounters.put(id, count);
    return build0(id + "_" + count, Lantern.getGame().getImplementationPlugin());
}
Also used : SmeltingResult(org.spongepowered.api.item.recipe.smelting.SmeltingResult) Predicate(java.util.function.Predicate) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) IIngredient(org.lanternpowered.server.item.recipe.IIngredient) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) HashMap(java.util.HashMap) SmeltingRecipe(org.spongepowered.api.item.recipe.smelting.SmeltingRecipe) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Lantern(org.lanternpowered.server.game.Lantern) Ingredient(org.spongepowered.api.item.recipe.crafting.Ingredient) Map(java.util.Map) Conditions.checkPlugin(org.lanternpowered.server.util.Conditions.checkPlugin) ItemType(org.spongepowered.api.item.ItemType) PluginContainer(org.spongepowered.api.plugin.PluginContainer) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot)

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