Java tutorial
/* * This file is part of ProjectX. * Copyright (c) 2015 - 2018, KitsuneAlex, All rights reserved. * * ProjectX is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * ProjectX is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with ProjectX. If not, see <http://www.gnu.org/licenses/lgpl>. */ package de.kitsunealex.projectx.api.recipe; import com.google.common.collect.*; import net.minecraft.block.Block; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.Ingredient; import net.minecraft.util.NonNullList; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.oredict.OreDictionary; import net.minecraftforge.registries.IForgeRegistryEntry; import java.util.Collection; import java.util.List; //@formatter:off /** * Recipe class for the Engineering Table. * The slot-order of ingredients is as following: * ____ ____ * / \/ \ * * 1 ** 2 * * __\____/\____/__ * / \/ \/ \ * * 3 ** 4 ** 5 * * \____/\____/\____/ * / \/ \ * * 6 ** 7 * * \____/\____/ */ //formatter:on public class EngineeringRecipe extends IForgeRegistryEntry.Impl<EngineeringRecipe> { private ItemStack output; private boolean shaped; private List<Ingredient> ingredients = NonNullList.create(); private boolean hasMissingIngredients = false; public EngineeringRecipe(ItemStack output, boolean shaped, Object... params) { this.output = output; this.shaped = shaped; processIngredients(params); } private void processIngredients(Object... params) { List<String> rows = Lists.newArrayList(); Multimap<Character, ItemStack> ingredientMap = Multimaps.newListMultimap(Maps.newHashMap(), Lists::newArrayList); boolean isPatternDefined = false; for (int i = 0; i < params.length; i++) { Object param = params[i]; if (param instanceof String) { if (!isPatternDefined) { rows.add((String) param); } } else if (param instanceof Character) { if (!isPatternDefined) { isPatternDefined = true; } Object nextParam = params[i + 1]; if (nextParam instanceof Item) { ingredientMap.put((Character) param, new ItemStack((Item) nextParam, 1, OreDictionary.WILDCARD_VALUE)); } else if (nextParam instanceof Block) { ingredientMap.put((Character) param, new ItemStack((Block) nextParam, 1, OreDictionary.WILDCARD_VALUE)); } else if (nextParam instanceof ItemStack) { ingredientMap.put((Character) param, (ItemStack) nextParam); } else if (nextParam instanceof String) { NonNullList<ItemStack> items = OreDictionary.getOres((String) nextParam); items.forEach(item -> ingredientMap.put((Character) param, item)); } else { String message = String.format("Unknown ingredient type '%s'!", nextParam.getClass().getName()); throw new RuntimeException(message); } } } for (String row : rows) { for (int i = 0; i < row.length(); i++) { if (row.charAt(i) != ' ') { Collection<ItemStack> stacks = ingredientMap.get(row.charAt(i)); if (stacks != null && !stacks.isEmpty()) { ingredients.add(Ingredient.fromStacks(stacks.toArray(new ItemStack[stacks.size()]))); } else { hasMissingIngredients = true; } } else { ingredients.add(Ingredient.EMPTY); } } } } /** * @return the output ItemStack of this recipe */ public ItemStack getOutput() { return output; } /** * @return an ImmutableList containing all Ingredients from this recipe */ public ImmutableList<Ingredient> getIngredients() { return ImmutableList.copyOf(ingredients); } /** * Returns true if the passed in IItemHandler contains all valid ingredients for this recipe. * * @param handler the IItemHandler * @return true if the item stacks in the passed in handler match with the ingredients of this recipe */ public boolean matches(IItemHandler handler) { if (!hasMissingIngredients) { if (shaped) { return false; } else { return false; } } else { return false; } } }