Skip to content

Commit

Permalink
Implement Smelting (#254)
Browse files Browse the repository at this point in the history
* Implement Smelting

* Fix Stan Hebben's weirdness
  • Loading branch information
ExE-Boss authored and RX14 committed Feb 24, 2017
1 parent 8f3e97a commit ea52a9a
Show file tree
Hide file tree
Showing 16 changed files with 349 additions and 65 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.ingredient.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.ingredient.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,27 @@ 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().map(ItemIngredient::getExampleItems).orElse(Collections.emptyList());

final Optional<ItemStack> output = recipe.getExampleOutput().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().map(ItemIngredient::getExampleItems).orElse(Collections.emptyList());
@SuppressWarnings("unchecked")
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.ingredient.ItemIngredient;
import nova.core.recipes.ingredient.OreItemIngredient;
import nova.core.recipes.crafting.ShapedCraftingRecipe;
import nova.core.recipes.crafting.ShapelessCraftingRecipe;
import nova.core.recipes.crafting.SpecificItemIngredient;
import nova.core.recipes.ingredient.SpecificItemIngredient;
import nova.core.wrapper.mc.forge.v17.util.ReflectionUtil;
import nova.internal.core.Game;

Expand Down Expand Up @@ -110,7 +110,7 @@ private static String findOreDictEntryFor(List ingredient) {
}

private static ItemStack wrapSpecific(SpecificItemIngredient ingredient) {
for (Item item : ingredient.getExampleItems().get()) {
for (Item item : ingredient.getExampleItems()) {
return Game.natives().toNative(item.getFactory().build());
}

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.ingredient.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.ingredient.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,27 @@ 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().map(ItemIngredient::getExampleItems).orElse(Collections.emptyList());

final Optional<ItemStack> output = recipe.getExampleOutput().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().map(ItemIngredient::getExampleItems).orElse(Collections.emptyList());
@SuppressWarnings("unchecked")
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,12 @@
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.ingredient.ItemIngredient;
import nova.core.recipes.ingredient.OreItemIngredient;
import nova.core.recipes.crafting.ShapedCraftingRecipe;
import nova.core.recipes.crafting.ShapelessCraftingRecipe;
import nova.core.recipes.crafting.SpecificItemIngredient;
import nova.core.recipes.ingredient.SpecificItemIngredient;
import nova.core.util.Identifiable;
import nova.core.wrapper.mc.forge.v18.util.ReflectionUtil;
import nova.internal.core.Game;

Expand Down Expand Up @@ -84,11 +85,11 @@ private static ItemIngredient getIngredient(Object ingredient) {
if (ingredient == null) {
return null;
} else if (ingredient instanceof ItemStack) {
return new SpecificItemIngredient(((Item) Game.natives().toNova(ingredient)).getID());
return new SpecificItemIngredient(((Identifiable) Game.natives().toNova(ingredient)).getID());
} else if (ingredient instanceof String) {
return new OreItemIngredient((String) ingredient);
} else if (ingredient instanceof List) {
String oreDictEntry = findOreDictEntryFor((List) ingredient);
String oreDictEntry = findOreDictEntryFor((List<?>) ingredient);
if (oreDictEntry == null) {
return null;
}
Expand All @@ -99,7 +100,7 @@ private static ItemIngredient getIngredient(Object ingredient) {
}
}

private static String findOreDictEntryFor(List ingredient) {
private static String findOreDictEntryFor(List<?> ingredient) {
for (String key : net.minecraftforge.oredict.OreDictionary.getOreNames()) {
if (net.minecraftforge.oredict.OreDictionary.getOres(key).equals(ingredient)) {
return key;
Expand All @@ -110,7 +111,7 @@ private static String findOreDictEntryFor(List ingredient) {
}

private static ItemStack wrapSpecific(SpecificItemIngredient ingredient) {
for (Item item : ingredient.getExampleItems().get()) {
for (Item item : ingredient.getExampleItems()) {
return Game.natives().toNative(item.getFactory().build());
}

Expand Down Expand Up @@ -139,20 +140,21 @@ public static IRecipe convert(ShapelessCraftingRecipe recipe) {
ItemIngredient[] ingredients = recipe.getIngredients();
int type = getRecipeType(ingredients);

if (type == TYPE_BASIC) {
ItemStack[] items = new ItemStack[ingredients.length];
for (int i = 0; i < ingredients.length; i++) {
items[i] = wrapSpecific((SpecificItemIngredient) ingredients[i]);
}
return new ShapelessRecipeBasic(items, recipe);
} else if (type == TYPE_ORE) {
Object[] items = new Object[ingredients.length];
for (int i = 0; i < ingredients.length; i++) {
items[i] = getInternal(ingredients[i]);
}
return new ShapelessRecipeOre(items, recipe);
} else {
return new NovaCraftingRecipe(recipe);
switch (type) {
case TYPE_BASIC: {
ItemStack[] items = new ItemStack[ingredients.length];
for (int i = 0; i < ingredients.length; i++) {
items[i] = wrapSpecific((SpecificItemIngredient) ingredients[i]);
}
return new ShapelessRecipeBasic(items, recipe);
} case TYPE_ORE: {
Object[] items = new Object[ingredients.length];
for (int i = 0; i < ingredients.length; i++) {
items[i] = getInternal(ingredients[i]);
}
return new ShapelessRecipeOre(items, recipe);
} default:
return new NovaCraftingRecipe(recipe);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/nova/core/recipes/crafting/CraftingRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import nova.core.recipes.Recipe;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;

/**
Expand Down Expand Up @@ -73,7 +74,7 @@ public interface CraftingRecipe extends Recipe {
*
* @return The items
*/
default Optional<Collection<String>> getPossibleItemsInFirstSlot() {
return Optional.empty();
default Collection<String> getPossibleItemsInFirstSlot() {
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ public Optional<CraftingRecipe> getRecipe(CraftingGrid grid) {
// #######################

private <T extends CraftingRecipe> void onCraftingRecipeAdded(RecipeEvent.Add<T> evt) {
Optional<Collection<String>> possibleFirstItemIds = evt.recipe.getPossibleItemsInFirstSlot();
if (possibleFirstItemIds.isPresent()) {
for (String itemId : possibleFirstItemIds.get()) {
Collection<String> possibleFirstItemIds = evt.recipe.getPossibleItemsInFirstSlot();
if (!possibleFirstItemIds.isEmpty()) {
for (String itemId : possibleFirstItemIds) {
staticRecipes.put(itemId, evt.recipe);
}
} else {
Expand All @@ -117,9 +117,9 @@ private <T extends CraftingRecipe> void onCraftingRecipeAdded(RecipeEvent.Add<T>
}

private <T extends CraftingRecipe> void onCraftingRecipeRemoved(RecipeEvent.Remove<T> evt) {
Optional<Collection<String>> possibleFirstItemIds = evt.recipe.getPossibleItemsInFirstSlot();
if (possibleFirstItemIds.isPresent()) {
for (String itemId : possibleFirstItemIds.get()) {
Collection<String> possibleFirstItemIds = evt.recipe.getPossibleItemsInFirstSlot();
if (!possibleFirstItemIds.isEmpty()) {
for (String itemId : possibleFirstItemIds) {
staticRecipes.remove(itemId, evt.recipe);
}
} else {
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/nova/core/recipes/crafting/ShapedCraftingRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@

package nova.core.recipes.crafting;

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

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand Down Expand Up @@ -272,22 +274,22 @@ public void consumeItems(CraftingGrid craftingGrid) {
}

@Override
public Optional<Collection<String>> getPossibleItemsInFirstSlot() {
public Collection<String> getPossibleItemsInFirstSlot() {
if (isMirrored()) {
Optional<Collection<String>> optionsForFirstItem = ingredients[0].getPossibleItemIds();
if (!optionsForFirstItem.isPresent()) {
return Optional.empty();
Collection<String> optionsForFirstItem = ingredients[0].getPossibleItemIds();
if (optionsForFirstItem.isEmpty()) {
return Collections.emptyList();
}

Optional<Collection<String>> optionsForSecondItem = ingredients[lastIngredientIndexOnFirstLine].getPossibleItemIds();
if (!optionsForSecondItem.isPresent()) {
return Optional.empty();
Collection<String> optionsForSecondItem = ingredients[lastIngredientIndexOnFirstLine].getPossibleItemIds();
if (optionsForFirstItem.isEmpty()) {
return Collections.emptyList();
}

Set<String> result = new HashSet<>();
result.addAll(optionsForFirstItem.get());
result.addAll(optionsForSecondItem.get());
return Optional.of(result);
result.addAll(optionsForFirstItem);
result.addAll(optionsForSecondItem);
return result;
} else {
return ingredients[0].getPossibleItemIds();
}
Expand Down Expand Up @@ -403,12 +405,14 @@ private NonMirroredMapping(Vector2D firstItemOffset) {
firstItemOffset.getY() - posy[0]));
}

@Override
public Optional<Item> getStack(CraftingGrid craftingGrid, int ingredient) {
return craftingGrid.getStack(
offsetX + posx[ingredient],
offsetY + posy[ingredient]);
}

@Override
public void setStack(CraftingGrid craftingGrid, int ingredient, Optional<Item> value) {
craftingGrid.setStack(
offsetX + posx[ingredient],
Expand All @@ -426,12 +430,14 @@ private MirroredMapping(Vector2D firstItemOffset, int craftingGridWidth) {
firstItemOffset.getY() - posy[0]));
}

@Override
public Optional<Item> getStack(CraftingGrid craftingGrid, int ingredient) {
return craftingGrid.getStack(
craftingGrid.getWidth() - (offsetX + posx[ingredient]) - 1,
offsetY + posy[ingredient]);
}

@Override
public void setStack(CraftingGrid craftingGrid, int ingredient, Optional<Item> value) {
craftingGrid.setStack(
craftingGrid.getWidth() - (offsetX + posx[ingredient]) - 1,
Expand Down
Loading

0 comments on commit ea52a9a

Please sign in to comment.