Search in sources :

Example 1 with NonNullList

use of net.minecraft.util.NonNullList in project MinecraftForge by MinecraftForge.

the class ShapelessOreRecipe method matches.

@SuppressWarnings("unchecked")
@Override
public boolean matches(InventoryCrafting var1, World world) {
    NonNullList<Object> required = NonNullList.create();
    required.addAll(input);
    for (int x = 0; x < var1.getSizeInventory(); x++) {
        ItemStack slot = var1.getStackInSlot(x);
        if (!slot.isEmpty()) {
            boolean inRecipe = false;
            Iterator<Object> req = required.iterator();
            while (req.hasNext()) {
                boolean match = false;
                Object next = req.next();
                if (next instanceof ItemStack) {
                    match = OreDictionary.itemMatches((ItemStack) next, slot, false);
                } else if (next instanceof List) {
                    Iterator<ItemStack> itr = ((List<ItemStack>) next).iterator();
                    while (itr.hasNext() && !match) {
                        match = OreDictionary.itemMatches(itr.next(), slot, false);
                    }
                }
                if (match) {
                    inRecipe = true;
                    required.remove(next);
                    break;
                }
            }
            if (!inRecipe) {
                return false;
            }
        }
    }
    return required.isEmpty();
}
Also used : Iterator(java.util.Iterator) List(java.util.List) NonNullList(net.minecraft.util.NonNullList) ItemStack(net.minecraft.item.ItemStack)

Example 2 with NonNullList

use of net.minecraft.util.NonNullList 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 3 with NonNullList

use of net.minecraft.util.NonNullList in project Charset by CharsetMC.

the class RecipeReplacement method replaceIngredient.

@Nullable
private Ingredient replaceIngredient(Ingredient ing) {
    if (ing.getClass() == Ingredient.class || ing.getClass() == IngredientNBT.class) {
        boolean checkNBT = ing.getClass() == IngredientNBT.class;
        ItemStack[] matchingStacks = ing.getMatchingStacks();
        ItemStack[] matchingStacksNew = null;
        int replacementOreMatches = 0;
        String replacementOre = null;
        boolean dirty = false;
        for (int j = 0; j < matchingStacks.length; j++) {
            ItemStack stack = matchingStacks[j];
            if (stack.isEmpty())
                continue;
            ItemStack newStack = null;
            Object replacement = null;
            for (int i : OreDictionary.getOreIDs(stack)) {
                if (replaceableOres.containsKey(i)) {
                    replacement = replaceableOres.get(i);
                    break;
                }
            }
            if (replacement == null) {
                if (replacements.containsKey(stack)) {
                    replacement = replacements.get(stack);
                } else if (!checkNBT && replaceableItems.containsKey(stack.getItem())) {
                    replacement = replaceableItems.get(stack.getItem());
                }
            }
            if (replacement instanceof Item) {
                newStack = new ItemStack((Item) replacement, stack.getCount(), stack.getItemDamage());
                newStack.setTagCompound(stack.getTagCompound());
            } else if (replacement instanceof ItemStack) {
                newStack = ((ItemStack) replacements.get(stack)).copy();
                newStack.setCount(stack.getCount());
            } else if (replacement instanceof String) {
                replacementOreMatches++;
                replacementOre = (String) replacement;
            }
            if (newStack != null) {
                if (!dirty) {
                    matchingStacksNew = new ItemStack[matchingStacks.length];
                    System.arraycopy(matchingStacks, 0, matchingStacksNew, 0, matchingStacks.length);
                    dirty = true;
                }
                matchingStacksNew[j] = newStack;
            }
        }
        if (replacementOre != null && replacementOreMatches == matchingStacks.length) {
            return new OreIngredient(replacementOre);
        } else {
            if (matchingStacksNew != null) {
                return Ingredient.fromStacks(matchingStacksNew);
            }
        }
    } else if (ing.getClass() == OreIngredient.class) {
        try {
            NonNullList<ItemStack> list = (NonNullList<ItemStack>) ORES_GETTER.invokeExact((OreIngredient) ing);
            if (list.isEmpty()) {
                return null;
            }
            TIntIterator it = replaceableOres.keySet().iterator();
            while (it.hasNext()) {
                int oreId = it.next();
                NonNullList<ItemStack> oreList = OreDictionary.getOres(OreDictionary.getOreName(oreId));
                if (!oreList.isEmpty() && list == oreList) {
                    Object replacement = replaceableOres.get(oreId);
                    if (replacement instanceof Item) {
                        return Ingredient.fromItem((Item) replacement);
                    } else if (replacement instanceof ItemStack) {
                        return Ingredient.fromStacks((ItemStack) replacement);
                    } else if (replacement instanceof String) {
                        return new OreIngredient((String) replacement);
                    }
                }
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
    return null;
}
Also used : TIntIterator(gnu.trove.iterator.TIntIterator) IngredientNBT(net.minecraftforge.common.crafting.IngredientNBT) Item(net.minecraft.item.Item) Ingredient(net.minecraft.item.crafting.Ingredient) OreIngredient(net.minecraftforge.oredict.OreIngredient) NonNullList(net.minecraft.util.NonNullList) OreIngredient(net.minecraftforge.oredict.OreIngredient) ItemStack(net.minecraft.item.ItemStack) Nullable(javax.annotation.Nullable)

Example 4 with NonNullList

use of net.minecraft.util.NonNullList in project BaseMetals by MinecraftModDevelopmentMods.

the class ShieldUpgradeRecipe method getCraftingResult.

@Override
public ItemStack getCraftingResult(final InventoryCrafting inv) {
    final Map<String, NonNullList<ItemStack>> plates = getPlates();
    Pair<Map<Enchantment, Integer>, ItemStack> matchedBits = Pair.of(Collections.emptyMap(), ItemStack.EMPTY);
    for (int i = 0; i < inv.getSizeInventory(); i++) {
        final ItemStack curItem = inv.getStackInSlot(i);
        if (curItem != null) {
            matchedBits = matchAndFind(curItem, plates);
        }
    }
    BaseMetals.logger.debug("Adding %d enchantments to output item", matchedBits.getLeft().size());
    if (!(matchedBits.getRight().isEmpty())) {
        EnchantmentHelper.setEnchantments(matchedBits.getLeft(), matchedBits.getRight());
    }
    return matchedBits.getRight();
}
Also used : NonNullList(net.minecraft.util.NonNullList) ItemStack(net.minecraft.item.ItemStack) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 5 with NonNullList

use of net.minecraft.util.NonNullList in project BaseMetals by MinecraftModDevelopmentMods.

the class ShieldUpgradeRecipe method getPlates.

private Map<String, NonNullList<ItemStack>> getPlates() {
    final Collection<MMDMaterial> allmats = Materials.getAllMaterials();
    final int hardness = ((Float) Materials.getMaterialByName(materialName).getStat(MaterialStats.HARDNESS)).intValue();
    final Map<String, NonNullList<ItemStack>> plates = new TreeMap<>();
    for (final MMDMaterial mat : allmats) {
        if (mat.getStat(MaterialStats.HARDNESS) >= hardness && (!mat.getName().equals(materialName))) {
            final NonNullList<ItemStack> mats = OreDictionary.getOres(Oredicts.PLATE + mat.getCapitalizedName());
            plates.put(mat.getName(), mats);
        }
    }
    return plates;
}
Also used : NonNullList(net.minecraft.util.NonNullList) MMDMaterial(com.mcmoddev.lib.material.MMDMaterial) TreeMap(java.util.TreeMap) ItemStack(net.minecraft.item.ItemStack)

Aggregations

NonNullList (net.minecraft.util.NonNullList)42 ItemStack (net.minecraft.item.ItemStack)37 List (java.util.List)13 EntityPlayer (net.minecraft.entity.player.EntityPlayer)8 Item (net.minecraft.item.Item)8 TileEntity (net.minecraft.tileentity.TileEntity)8 BlockPos (net.minecraft.util.math.BlockPos)7 ArrayList (java.util.ArrayList)6 Collectors (java.util.stream.Collectors)6 ResourceLocation (net.minecraft.util.ResourceLocation)6 Iterator (java.util.Iterator)5 Map (java.util.Map)5 Nonnull (javax.annotation.Nonnull)5 Nullable (javax.annotation.Nullable)5 CreativeTabs (net.minecraft.creativetab.CreativeTabs)5 World (net.minecraft.world.World)5 Block (net.minecraft.block.Block)4 IBlockState (net.minecraft.block.state.IBlockState)4 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)4 RayTraceResult (net.minecraft.util.math.RayTraceResult)4