Skip to content

Commit

Permalink
Implement Smelting
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Jan 17, 2017
1 parent f422465 commit 7f4fee7
Show file tree
Hide file tree
Showing 14 changed files with 294 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
package nova.core.wrapper.mc.forge.v17.recipes;

import nova.core.item.Item;
import nova.core.recipes.crafting.SpecificItemIngredient;
import nova.core.recipes.SpecificItemIngredient;
import nova.internal.core.Game;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -128,6 +139,26 @@ private void onMinecraftRecipeRemoved(IRecipe recipe) {
Game.recipes().removeRecipe(novaRecipe);
}

private void onNOVASmeltingAdded(RecipeEvent.Add<SmeltingRecipe> evt) {
SmeltingRecipe recipe = evt.recipe;

Collection<Item> inputs = recipe.getInput().flatMap(ItemIngredient::getExampleItems).orElse(Collections.emptyList());

final Optional<ItemStack> 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<SmeltingRecipe> evt) {
SmeltingRecipe recipe = evt.recipe;

Collection<Item> inputs = recipe.getInput().flatMap(ItemIngredient::getExampleItems).orElse(Collections.emptyList());
Map<ItemStack, ItemStack> smeltingList = FurnaceRecipes.smelting().getSmeltingList();
inputs.stream().map(ItemConverter.instance()::toNative).forEach(input -> smeltingList.remove(input));
}

private class RecipeListWrapper extends AbstractList<IRecipe> {
private final List<IRecipe> original;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
import net.minecraftforge.oredict.ShapelessOreRecipe;
import nova.core.item.Item;
import nova.core.recipes.crafting.CraftingRecipe;
import nova.core.recipes.crafting.ItemIngredient;
import nova.core.recipes.crafting.OreItemIngredient;
import nova.core.recipes.ItemIngredient;
import nova.core.recipes.OreItemIngredient;
import nova.core.recipes.crafting.ShapedCraftingRecipe;
import nova.core.recipes.crafting.ShapelessCraftingRecipe;
import nova.core.recipes.crafting.SpecificItemIngredient;
import nova.core.recipes.SpecificItemIngredient;
import nova.core.wrapper.mc.forge.v17.util.ReflectionUtil;
import nova.internal.core.Game;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
package nova.core.wrapper.mc.forge.v18.recipes;

import nova.core.item.Item;
import nova.core.recipes.crafting.SpecificItemIngredient;
import nova.core.recipes.SpecificItemIngredient;
import nova.internal.core.Game;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -128,6 +139,26 @@ private void onMinecraftRecipeRemoved(IRecipe recipe) {
Game.recipes().removeRecipe(novaRecipe);
}

private void onNOVASmeltingAdded(RecipeEvent.Add<SmeltingRecipe> evt) {
SmeltingRecipe recipe = evt.recipe;

Collection<Item> inputs = recipe.getInput().flatMap(ItemIngredient::getExampleItems).orElse(Collections.emptyList());

final Optional<ItemStack> 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<SmeltingRecipe> evt) {
SmeltingRecipe recipe = evt.recipe;

Collection<Item> inputs = recipe.getInput().flatMap(ItemIngredient::getExampleItems).orElse(Collections.emptyList());
Map<ItemStack, ItemStack> smeltingList = FurnaceRecipes.instance().getSmeltingList();
inputs.stream().map(ItemConverter.instance()::toNative).forEach(input -> smeltingList.remove(input));
}

private class RecipeListWrapper extends AbstractList<IRecipe> {
private final List<IRecipe> original;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
import net.minecraftforge.oredict.ShapelessOreRecipe;
import nova.core.item.Item;
import nova.core.recipes.crafting.CraftingRecipe;
import nova.core.recipes.crafting.ItemIngredient;
import nova.core.recipes.crafting.OreItemIngredient;
import nova.core.recipes.ItemIngredient;
import nova.core.recipes.OreItemIngredient;
import nova.core.recipes.crafting.ShapedCraftingRecipe;
import nova.core.recipes.crafting.ShapelessCraftingRecipe;
import nova.core.recipes.crafting.SpecificItemIngredient;
import nova.core.recipes.SpecificItemIngredient;
import nova.core.wrapper.mc.forge.v18.util.ReflectionUtil;
import nova.internal.core.Game;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* along with NOVA. If not, see <http://www.gnu.org/licenses/>.
*/

package nova.core.recipes.crafting;
package nova.core.recipes;

import nova.core.block.BlockFactory;
import nova.core.item.Item;
Expand All @@ -27,6 +27,8 @@
import java.util.Collection;
import java.util.Optional;

import nova.core.recipes.crafting.CraftingGrid;

/**
* Contains a single item ingredient. An ingredient can be a specific item,
* an ore dictionary entry, or anything else that can match against items.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* along with NOVA. If not, see <http://www.gnu.org/licenses/>.
*/

package nova.core.recipes.crafting;
package nova.core.recipes;

import nova.core.item.Item;
import nova.internal.core.Game;
Expand All @@ -29,6 +29,8 @@
import java.util.Optional;
import java.util.stream.Collectors;

import nova.core.recipes.crafting.CraftingGrid;

/**
* @author Stan
* @since 3/02/2015.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* You should have received a copy of the GNU General Public License
* along with NOVA. If not, see <http://www.gnu.org/licenses/>.
*/package nova.core.recipes.crafting;
*/package nova.core.recipes;

import nova.core.item.Item;
import nova.core.item.ItemFactory;
Expand All @@ -27,6 +27,8 @@
import java.util.Collections;
import java.util.Optional;

import nova.core.recipes.crafting.CraftingGrid;

/**
* Specifies an ingredient identifying a single kind of item.
* @author Stan Hebben
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package nova.core.recipes.crafting;

import nova.core.recipes.ItemIngredient;
import nova.core.item.Item;
import org.apache.commons.math3.geometry.euclidean.twod.Vector2D;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package nova.core.recipes.crafting;

import nova.core.recipes.ItemIngredient;
import nova.core.item.Item;

import java.util.HashMap;
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/nova/core/recipes/smelting/BasicSmeltingRecipe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package nova.core.recipes.smelting;

import nova.core.item.Item;
import nova.core.item.ItemFactory;
import nova.core.recipes.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 format Format
* @param ingredients {@link ItemIngredient ItemIngredients}
*/
public BasicSmeltingRecipe(ItemFactory output, ItemIngredient ingredient) {
this.nominalOutput = output;
this.ingredient = ingredient;
}

@Override
public boolean matches(Optional<Item> input) {
return input.isPresent() && this.ingredient.matches(input.get());
}

@Override
public Optional<Item> getCraftingResult(Optional<Item> input) {
return matches(input) ? getNominalOutput() : Optional.empty();
}

@Override
public Optional<Item> getNominalInput() {
return this.ingredient.getExampleItems().flatMap(c -> c.stream().findFirst());
}

@Override
public Optional<Item> getNominalOutput() {
return Optional.of(this.nominalOutput.build());
}

@Override
public Optional<ItemIngredient> getInput() {
return Optional.of(this.ingredient);
}
}
70 changes: 70 additions & 0 deletions src/main/java/nova/core/recipes/smelting/SmeltingRecipe.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

package nova.core.recipes.smelting;

import nova.core.item.Item;
import nova.core.recipes.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<Item> 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<Item> getCraftingResult(Optional<Item> input);

/**
* Gets a nominal (example) input for this recipe. Used in recipe display.
*
* @return example input
*/
Optional<Item> getNominalInput();

/**
* Gets a nominal (example) output for this recipe. Used in recipe display.
*
* @return example output
*/
Optional<Item> getNominalOutput();

/**
* Gets the input for this recipe. Used during registration.
*
* @return the input
*/
Optional<ItemIngredient> getInput();
}
Loading

0 comments on commit 7f4fee7

Please sign in to comment.