public AbstractRecipe(Object[] input, Object... output) { this.input = new RecipeInput(input); this.output = new RecipeOutput(output); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AbstractRecipe that = (AbstractRecipe) o; if (input != null ? !input.equals(that.input) : that.input != null) return false; return output != null ? output.equals(that.output) : that.output == null; }
@Nullable public <T, R extends Recipe<T>> R getIngredientRecipeNullable(Class<R> recipeClass, T input) { if (!(input instanceof RecipeInput) && !((((RecipeInput) input).value instanceof ItemStack[]) || ((RecipeInput) input).value instanceof Item[])) return null; return getRecipes(recipeClass).stream() .filter(recipe -> recipe.getInput() instanceof RecipeInput) .filter(recipe -> ((RecipeInput) recipe.getInput()).getValue() instanceof Ingredient[]) .filter(recipe -> ((RecipeInput) recipe.getInput()).getValue().length == ((RecipeInput) input).getValue().length) .filter(recipe -> { Ingredient[] ingredients = (Ingredient[]) ((RecipeInput) recipe.getInput()).getValue(); Object[] inputs = ((RecipeInput) input).getValue(); for (int i = 0; i < inputs.length; i++) { ItemStack stack; if (inputs[i] instanceof Item) stack = new ItemStack((Item) inputs[i]); else stack = (ItemStack) inputs[i]; if (!ingredients[i].apply(stack)) return false; } return true; }) .findAny().orElse(null); } }
@Test public void testRecipeIngredients() { // Add null -> null recipe RecipeManager.getInstance().addRecipe(new DrinkRecipe(null, null)); // Should be null recipe DrinkRecipe recipe = RecipeManager.getInstance().getRecipeNullable(DrinkRecipe.class, new RecipeInput((Item)null)); // Should be presented Assert.assertNotNull(recipe); // Null input Assert.assertNull(recipe.getInput().first()); // Null output Assert.assertNull(recipe.getOutput().first()); } }
@Override public void getIngredients(@Nonnull IIngredients ingredients) { ingredients.setInput(ItemStack.class, new ItemStack(getRecipe().getInput().<Item>first())); ingredients.setOutput(FluidStack.class, getRecipe().getOutput().first()); ingredients.setOutput(ItemStack.class, getRecipe().getOutput().second()); } }
@Override public int hashCode() { int result = input != null ? input.hashCode() : 0; result = 31 * result + (output != null ? output.hashCode() : 0); return result; } }
@Override public boolean canStart() { DrinkRecipe recipe = RecipeManager.getInstance().getRecipeNullable(DrinkRecipe.class, new RecipeInput(itemStackHandler.getStackInSlot(0).getItem())); return super.canStart() && recipe != null && tank.getFluidAmount() < TileEntityDrinkMachine.FLUID_CAPACITY; }
@Override public boolean canStart() { DrinkRecipe recipe = RecipeManager.getInstance().getRecipeNullable(DrinkRecipe.class, new RecipeInput(itemStackHandler.getStackInSlot(0).getItem())); return super.canStart() && recipe != null && tank.getFluidAmount() < TileEntityDrinkMachine.FLUID_CAPACITY; }
@Override public void startProgress() { DrinkRecipe recipe = RecipeManager.getInstance().getRecipeNullable(DrinkRecipe.class, new RecipeInput(itemStackHandler.getStackInSlot(0).getItem())); if (recipe != null) { FoodCraftReloaded.getLogger().debug("Smelting drink machine: Found recipe " + recipe.getOutput().<FluidStack>first().getFluid().getName()); fluidOutput = recipe.getOutput().first(); output = recipe.getOutput().second(); itemStackHandler.getStackInSlot(0).setCount(itemStackHandler.getStackInSlot(0).getCount() - 1); } }
@Override public void startProgress() { DrinkRecipe recipe = RecipeManager.getInstance().getRecipeNullable(DrinkRecipe.class, new RecipeInput(itemStackHandler.getStackInSlot(0).getItem())); if (recipe != null) { FoodCraftReloaded.getLogger().debug("Smelting drink machine: Found recipe " + recipe.getOutput().<FluidStack>first().getFluid().getName()); fluidOutput = recipe.getOutput().first(); output = recipe.getOutput().second(); itemStackHandler.getStackInSlot(0).setCount(itemStackHandler.getStackInSlot(0).getCount() - 1); } }