diff --git a/minecraft/1.7/src/main/java/nova/core/wrapper/mc/forge/v17/recipes/MinecraftRecipeRegistry.java b/minecraft/1.7/src/main/java/nova/core/wrapper/mc/forge/v17/recipes/MinecraftRecipeRegistry.java index e076aa23d..00a28f05c 100644 --- a/minecraft/1.7/src/main/java/nova/core/wrapper/mc/forge/v17/recipes/MinecraftRecipeRegistry.java +++ b/minecraft/1.7/src/main/java/nova/core/wrapper/mc/forge/v17/recipes/MinecraftRecipeRegistry.java @@ -20,22 +20,30 @@ package nova.core.wrapper.mc.forge.v17.recipes; +import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.CraftingManager; +import net.minecraft.item.crafting.FurnaceRecipes; import net.minecraft.item.crafting.IRecipe; import nova.core.event.RecipeEvent; +import nova.core.item.Item; +import nova.core.recipes.crafting.ItemIngredient; import nova.core.recipes.RecipeManager; import nova.core.recipes.crafting.CraftingRecipe; +import nova.core.recipes.smelting.SmeltingRecipe; import nova.core.wrapper.mc.forge.v17.util.ReflectionUtil; +import nova.core.wrapper.mc.forge.v17.wrapper.item.ItemConverter; import nova.internal.core.Game; import java.util.AbstractList; import java.util.Collection; +import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Map; +import java.util.Optional; import java.util.Spliterator; import java.util.function.Consumer; import java.util.stream.Stream; @@ -73,6 +81,9 @@ public void registerRecipes() { recipeManager.whenRecipeAdded(CraftingRecipe.class, this::onNOVARecipeAdded); recipeManager.whenRecipeRemoved(CraftingRecipe.class, this::onNOVARecipeRemoved); + + recipeManager.whenRecipeAdded(SmeltingRecipe.class, this::onNOVASmeltingAdded); + recipeManager.whenRecipeRemoved(SmeltingRecipe.class, this::onNOVASmeltingRemoved); } private CraftingRecipe convert(IRecipe recipe) { @@ -128,6 +139,26 @@ private void onMinecraftRecipeRemoved(IRecipe recipe) { Game.recipes().removeRecipe(novaRecipe); } + private void onNOVASmeltingAdded(RecipeEvent.Add evt) { + SmeltingRecipe recipe = evt.recipe; + + Collection inputs = recipe.getInput().flatMap(ItemIngredient::getExampleItems).orElse(Collections.emptyList()); + + final Optional output = recipe.getNominalOutput().map(ItemConverter.instance()::toNative); + if (!output.isPresent()) + return; + + inputs.stream().map(ItemConverter.instance()::toNative).forEach(input -> FurnaceRecipes.smelting().func_151394_a(input, output.get(), 0)); + } + + private void onNOVASmeltingRemoved(RecipeEvent.Remove evt) { + SmeltingRecipe recipe = evt.recipe; + + Collection inputs = recipe.getInput().flatMap(ItemIngredient::getExampleItems).orElse(Collections.emptyList()); + Map smeltingList = FurnaceRecipes.smelting().getSmeltingList(); + inputs.stream().map(ItemConverter.instance()::toNative).forEach(input -> smeltingList.remove(input)); + } + private class RecipeListWrapper extends AbstractList { private final List original; diff --git a/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v18/recipes/MinecraftRecipeRegistry.java b/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v18/recipes/MinecraftRecipeRegistry.java index 6bf591619..31014b8e0 100644 --- a/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v18/recipes/MinecraftRecipeRegistry.java +++ b/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v18/recipes/MinecraftRecipeRegistry.java @@ -20,22 +20,30 @@ package nova.core.wrapper.mc.forge.v18.recipes; +import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.CraftingManager; +import net.minecraft.item.crafting.FurnaceRecipes; import net.minecraft.item.crafting.IRecipe; import nova.core.event.RecipeEvent; +import nova.core.item.Item; +import nova.core.recipes.crafting.ItemIngredient; import nova.core.recipes.RecipeManager; import nova.core.recipes.crafting.CraftingRecipe; +import nova.core.recipes.smelting.SmeltingRecipe; import nova.core.wrapper.mc.forge.v18.util.ReflectionUtil; +import nova.core.wrapper.mc.forge.v18.wrapper.item.ItemConverter; import nova.internal.core.Game; import java.util.AbstractList; import java.util.Collection; +import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Map; +import java.util.Optional; import java.util.Spliterator; import java.util.function.Consumer; import java.util.stream.Stream; @@ -73,6 +81,9 @@ public void registerRecipes() { recipeManager.whenRecipeAdded(CraftingRecipe.class, this::onNOVARecipeAdded); recipeManager.whenRecipeRemoved(CraftingRecipe.class, this::onNOVARecipeRemoved); + + recipeManager.whenRecipeAdded(SmeltingRecipe.class, this::onNOVASmeltingAdded); + recipeManager.whenRecipeRemoved(SmeltingRecipe.class, this::onNOVASmeltingRemoved); } private CraftingRecipe convert(IRecipe recipe) { @@ -128,6 +139,26 @@ private void onMinecraftRecipeRemoved(IRecipe recipe) { Game.recipes().removeRecipe(novaRecipe); } + private void onNOVASmeltingAdded(RecipeEvent.Add evt) { + SmeltingRecipe recipe = evt.recipe; + + Collection inputs = recipe.getInput().flatMap(ItemIngredient::getExampleItems).orElse(Collections.emptyList()); + + final Optional output = recipe.getNominalOutput().map(ItemConverter.instance()::toNative); + if (!output.isPresent()) + return; + + inputs.stream().map(ItemConverter.instance()::toNative).forEach(input -> FurnaceRecipes.instance().addSmeltingRecipe(input, output.get(), 0)); + } + + private void onNOVASmeltingRemoved(RecipeEvent.Remove evt) { + SmeltingRecipe recipe = evt.recipe; + + Collection inputs = recipe.getInput().flatMap(ItemIngredient::getExampleItems).orElse(Collections.emptyList()); + Map smeltingList = FurnaceRecipes.instance().getSmeltingList(); + inputs.stream().map(ItemConverter.instance()::toNative).forEach(input -> smeltingList.remove(input)); + } + private class RecipeListWrapper extends AbstractList { private final List original; diff --git a/src/main/java/nova/core/recipes/smelting/BasicSmeltingRecipe.java b/src/main/java/nova/core/recipes/smelting/BasicSmeltingRecipe.java new file mode 100644 index 000000000..8f05058c9 --- /dev/null +++ b/src/main/java/nova/core/recipes/smelting/BasicSmeltingRecipe.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2017 NOVA, All rights reserved. + * This library is free software, licensed under GNU Lesser General Public License version 3 + * + * This file is part of NOVA. + * + * NOVA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NOVA 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NOVA. If not, see . + */ +package nova.core.recipes.smelting; + +import nova.core.item.Item; +import nova.core.item.ItemFactory; +import nova.core.recipes.crafting.ItemIngredient; + +import java.util.Optional; + +/** + * @author ExE Boss + */ +public class BasicSmeltingRecipe implements SmeltingRecipe { + + private final ItemFactory nominalOutput; + private final ItemIngredient ingredient; + + /** + * Defines a basic structured crafting recipe, using a format string. + * @param output Output {@link Item} of the recipe + * @param ingredient {@link ItemIngredient} + */ + public BasicSmeltingRecipe(ItemFactory output, ItemIngredient ingredient) { + this.nominalOutput = output; + this.ingredient = ingredient; + } + + @Override + public boolean matches(Optional input) { + return input.isPresent() && this.ingredient.matches(input.get()); + } + + @Override + public Optional getCraftingResult(Optional input) { + return matches(input) ? getNominalOutput() : Optional.empty(); + } + + @Override + public Optional getNominalInput() { + return this.ingredient.getExampleItems().flatMap(c -> c.stream().findFirst()); + } + + @Override + public Optional getNominalOutput() { + return Optional.of(this.nominalOutput.build()); + } + + @Override + public Optional getInput() { + return Optional.of(this.ingredient); + } +} diff --git a/src/main/java/nova/core/recipes/smelting/SmeltingRecipe.java b/src/main/java/nova/core/recipes/smelting/SmeltingRecipe.java new file mode 100644 index 000000000..b2fed09b1 --- /dev/null +++ b/src/main/java/nova/core/recipes/smelting/SmeltingRecipe.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2017 NOVA, All rights reserved. + * This library is free software, licensed under GNU Lesser General Public License version 3 + * + * This file is part of NOVA. + * + * NOVA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NOVA 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NOVA. If not, see . + */ + +package nova.core.recipes.smelting; + +import nova.core.item.Item; +import nova.core.recipes.crafting.ItemIngredient; +import nova.core.recipes.Recipe; + +import java.util.Optional; + +/** + * @author ExE Boss + */ +public interface SmeltingRecipe extends Recipe { + /** + * Checks if this crafting recipe matches the content of the given smelting input. + * + * @param input smelting input to read from + * @return true if a smelting operation would return a valid result + */ + boolean matches(Optional input); + + /** + * Calculates the crafting result for the given crafting grid. Does not + * modify the contents of the crafting grid. + * + * @param input crafting grid + * @return crafting result, empty if the recipe doesn't match + */ + Optional getCraftingResult(Optional input); + + /** + * Gets a nominal (example) input for this recipe. Used in recipe display. + * + * @return example input + */ + Optional getNominalInput(); + + /** + * Gets a nominal (example) output for this recipe. Used in recipe display. + * + * @return example output + */ + Optional getNominalOutput(); + + /** + * Gets the input for this recipe. Used during registration. + * + * @return the input + */ + Optional getInput(); +} diff --git a/src/main/java/nova/core/recipes/smelting/SmeltingRecipeManager.java b/src/main/java/nova/core/recipes/smelting/SmeltingRecipeManager.java new file mode 100644 index 000000000..7a9ec995a --- /dev/null +++ b/src/main/java/nova/core/recipes/smelting/SmeltingRecipeManager.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2017 NOVA, All rights reserved. + * This library is free software, licensed under GNU Lesser General Public License version 3 + * + * This file is part of NOVA. + * + * NOVA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NOVA 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NOVA. If not, see . + */ +package nova.core.recipes.smelting; + +import nova.core.event.RecipeEvent; +import nova.core.recipes.RecipeManager; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author ExE Boss + */ +public class SmeltingRecipeManager { + private final RecipeManager recipeManager; + private final List recipes; + + public SmeltingRecipeManager(RecipeManager recipeManager) { + this.recipeManager = recipeManager; + this.recipes = new ArrayList<>(); + + this.recipeManager.whenRecipeAdded(SmeltingRecipe.class, this::onSmeltingRecipeAdded); + this.recipeManager.whenRecipeRemoved(SmeltingRecipe.class, this::onSmeltingRecipeRemoved); + } + + /** + * Adds a recipe. Adds it to the global recipe list as SmeltingRecipe. + * + * @param recipe {@link SmeltingRecipe} + */ + public void addRecipe(SmeltingRecipe recipe) { + recipeManager.addRecipe(recipe); + } + + /** + * Removes a recipe. Removes it from the global recipe list. + * + * @param recipe {@link SmeltingRecipe} + */ + public void removeRecipe(SmeltingRecipe recipe) { + recipeManager.removeRecipe(recipe); + } + + // ####################### + // ### Private Methods ### + // ####################### + + private void onSmeltingRecipeAdded(RecipeEvent.Add e) { + + } + + private void onSmeltingRecipeRemoved(RecipeEvent.Remove e) { + + } +}