PageRenderTime 34ms CodeModel.GetById 9ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 1ms

/common/buildcraft/lib/client/guide/parts/recipe/GuideCraftingRecipes.java

https://github.com/2xsaiko/BuildCraft
Java | 102 lines | 82 code | 14 blank | 6 comment | 24 complexity | 87c6a5052b8d577554744c4cff231671 MD5 | raw file
  1/*
  2 * Copyright (c) 2017 SpaceToad and the BuildCraft team
  3 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
  4 * distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/
  5 */
  6
  7package buildcraft.lib.client.guide.parts.recipe;
  8
  9import java.util.ArrayList;
 10import java.util.List;
 11
 12import javax.annotation.Nonnull;
 13import javax.annotation.Nullable;
 14
 15import net.minecraft.item.ItemStack;
 16import net.minecraft.item.crafting.IRecipe;
 17import net.minecraft.item.crafting.Ingredient;
 18import net.minecraft.util.NonNullList;
 19
 20import net.minecraftforge.fml.common.registry.ForgeRegistries;
 21import net.minecraftforge.oredict.OreDictionary;
 22
 23import buildcraft.lib.client.guide.parts.GuidePartFactory;
 24import buildcraft.lib.misc.StackUtil;
 25import buildcraft.lib.recipe.ChangingItemStack;
 26import buildcraft.lib.recipe.IRecipeViewable;
 27
 28public enum GuideCraftingRecipes implements IStackRecipes {
 29    INSTANCE;
 30
 31    @Override
 32    public List<GuidePartFactory> getUsages(@Nonnull ItemStack target) {
 33        List<GuidePartFactory> list = new ArrayList<>();
 34
 35        for (IRecipe recipe : ForgeRegistries.RECIPES) {
 36            if (checkRecipeUses(recipe, target)) {
 37                GuidePartFactory factory = GuideCraftingFactory.getFactory(recipe);
 38                if (factory != null) {
 39                    list.add(factory);
 40                }
 41            }
 42        }
 43        return list;
 44    }
 45
 46    private static boolean checkRecipeUses(IRecipe recipe, @Nonnull ItemStack target) {
 47        NonNullList<Ingredient> ingrediants = recipe.getIngredients();
 48        if (ingrediants.isEmpty()) {
 49            if (recipe instanceof IRecipeViewable) {
 50                // TODO!
 51            }
 52        }
 53        for (Ingredient ing : ingrediants) {
 54            if (ing.test(target)) {
 55                return true;
 56            }
 57        }
 58        return false;
 59    }
 60
 61    private static boolean matches(@Nonnull ItemStack target, @Nullable Object in) {
 62        if (in instanceof ItemStack) {
 63            return StackUtil.doesEitherStackMatch((ItemStack) in, target);
 64        } else if (in instanceof List) {
 65            for (Object obj : (List<?>) in) {
 66                if (obj instanceof ItemStack) {
 67                    if (StackUtil.doesEitherStackMatch((ItemStack) obj, target)) {
 68                        return true;
 69                    }
 70                }
 71            }
 72        }
 73        return false;
 74    }
 75
 76    @Override
 77    public List<GuidePartFactory> getRecipes(@Nonnull ItemStack target) {
 78        List<GuidePartFactory> list = new ArrayList<>();
 79
 80        for (IRecipe recipe : ForgeRegistries.RECIPES) {
 81            if (recipe instanceof IRecipeViewable) {
 82                ChangingItemStack changing = ((IRecipeViewable) recipe).getRecipeOutputs();
 83                if (changing.matches(target)) {
 84                    GuidePartFactory factory = GuideCraftingFactory.getFactory(recipe);
 85                    if (factory != null) {
 86                        list.add(factory);
 87                    }
 88                }
 89            } else {
 90                ItemStack out = StackUtil.asNonNull(recipe.getRecipeOutput());
 91                if (OreDictionary.itemMatches(target, out, false) || OreDictionary.itemMatches(out, target, false)) {
 92                    GuidePartFactory factory = GuideCraftingFactory.getFactory(recipe);
 93                    if (factory != null) {
 94                        list.add(factory);
 95                    }
 96                }
 97            }
 98        }
 99
100        return list;
101    }
102}