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 22, 2017
1 parent f422465 commit 4944356
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 0 deletions.
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.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;
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 @@ -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;
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
70 changes: 70 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,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.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<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.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<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();
}
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<SmeltingRecipe> 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 <T extends SmeltingRecipe> void onSmeltingRecipeAdded(RecipeEvent.Add<T> e) {

}

private <T extends SmeltingRecipe> void onSmeltingRecipeRemoved(RecipeEvent.Remove<T> e) {

}
}

0 comments on commit 4944356

Please sign in to comment.