Skip to content

Commit

Permalink
All items involving apples (vanilla or modded) should now work with e…
Browse files Browse the repository at this point in the history
…very apple variant
  • Loading branch information
Sollace committed Feb 1, 2019
1 parent 41dfb01 commit 3fb6ef7
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 12 deletions.
7 changes: 7 additions & 0 deletions src/main/java/com/minelittlepony/unicopia/UItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.minelittlepony.unicopia.edibles.UItemFoodDelegate;
import com.minelittlepony.unicopia.forgebullshit.BuildInTexturesBakery;
import com.minelittlepony.unicopia.forgebullshit.ItemModels;
import com.minelittlepony.unicopia.forgebullshit.OreReplacer;
import com.minelittlepony.unicopia.forgebullshit.RegistryLockSpinner;

public class UItems {
Expand Down Expand Up @@ -243,6 +244,12 @@ static void registerFuels() {
FurnaceRecipes.instance().addSmeltingRecipe(new ItemStack(juice), new ItemStack(burned_juice), 0);
}

static void fixRecipes() {
new OreReplacer()
.registerAll(stack -> stack.getItem().getRegistryName().equals(apple.getRegistryName()))
.done();
}

@SideOnly(Side.CLIENT)
static void registerColors(ItemColors registry) {
registry.registerItemColorHandler((stack, tint) -> {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/minelittlepony/unicopia/Unicopia.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ protected void registerRecipeTypes(Map<String, Function<JsonObject, IRecipe>> ty

Biome.REGISTRY.forEach(UEntities::registerSpawnEntries);
UClient.instance().posInit(event);

UItems.fixRecipes();
}

public static CraftingManager getCraftingManager() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package com.minelittlepony.unicopia.forgebullshit;

import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.google.common.collect.Lists;
import com.google.common.collect.Streams;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraft.util.NonNullList;
import net.minecraftforge.fml.common.registry.ForgeRegistries;

/**
* An Ore Replacer.
* Similar to what the OreDictionary does
* except this is configurable and works for vanilla and modded recipes.
*/
public class OreReplacer {

/**
* The vanilla remapper. Supports shaped recipes and shapeless recipes, and that's about it.
*/
public static final IIngredientRemapper VANILLA = new IIngredientRemapper() {

@Override
public boolean canRemap(IRecipe recipe) {
return recipe.getClass() == ShapedRecipes.class
|| recipe.getClass() == ShapelessRecipes.class;
}

@Override
public int replaceIngredients(OreReplacer sender, IRecipe recipe) {
NonNullList<Ingredient> ingredients = recipe.getIngredients();

int replacements = 0;

for (int i = 0; i < ingredients.size(); i++) {
Ingredient ingredient = ingredients.get(i);

NonNullList<ItemStack> newStacks = NonNullList.create();

boolean altered = false;

ItemStack[] stacks = ingredient.getMatchingStacks();

for (int k = 0; k < stacks.length; k++) {
ItemStack stack = stacks[k];

boolean found = !stack.isEmpty() && sender.replaceOre(stack, newStacks);

if (!found) {
newStacks.add(stack);
}

altered |= found;
}

if (altered) {
replacements++;
ingredients.set(i, Ingredient.fromStacks(newStacks.stream().distinct().toArray(ItemStack[]::new)));
}
}

return replacements;
}

};

private static final Logger log = LogManager.getLogger();

private int replacements = 0;

private List<IOre> ores = new ArrayList<>();

private final List<IIngredientRemapper> remappers = Lists.newArrayList(VANILLA);

/**
* Adds additional recipe handlers.
* By default only the vanilla crafting recipes are supported.
*/
public OreReplacer registerRecipeTypeHandler(IIngredientRemapper... remappers) {
this.remappers.addAll(Lists.newArrayList(remappers));

return this;
}

/**
* Adds all the specified ore conversions to be processed by this replacer.
*/
public OreReplacer registerAll(IOre... ores) {
this.ores.addAll(Lists.newArrayList(ores));

return this;
}

public void done() {
log.info("Searching for ore replacements...");
Streams.stream(ForgeRegistries.RECIPES).forEach(recipe -> {
remappers.stream()
.filter(remapper -> remapper.canRemap(recipe))
.findFirst()
.ifPresent(remapper -> remapper.replaceIngredients(this, recipe));
});
log.info("Replaced {} ingredients.", replacements);
}

public boolean replaceOre(ItemStack stack, NonNullList<ItemStack> newStacks) {
return ores.stream().filter(ore -> ore.matches(stack)).map(ore -> {
ore.getSubItems(stack, newStacks);

return ore;
}).findFirst().isPresent();
}

public interface IIngredientRemapper {

boolean canRemap(IRecipe recipe);

int replaceIngredients(OreReplacer sender, IRecipe recipe);
}

@FunctionalInterface
public interface IOre {
boolean matches(ItemStack stack);

default void getSubItems(ItemStack stack, NonNullList<ItemStack> newStacks) {
NonNullList<ItemStack> newList = NonNullList.create();

stack.getItem().getSubItems(CreativeTabs.SEARCH, newList);

if (stack.hasTagCompound()) {
newList.forEach(i -> i.setTagCompound(stack.getTagCompound().copy()));
}

newStacks.addAll(newList);
}
}
}
5 changes: 1 addition & 4 deletions src/main/resources/assets/unicopia/recipes/cider.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
{ "item": "minecraft:iron_nugget" }
],
"A": [
{ "item": "minecraft:apple", "data": 0 },
{ "item": "minecraft:apple", "data": 1 },
{ "item": "minecraft:apple", "data": 2 },
{ "item": "minecraft:apple", "data": 3 }
{ "item": "minecraft:apple", "data": 0 }
]
},
"result": { "item": "unicopia:apple_cider", "data": 0, "count": 1 }
Expand Down
5 changes: 1 addition & 4 deletions src/main/resources/assets/unicopia/recipes/cider_two.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
{ "item": "unicopia:burned_juice", "data": 0 }
],
"A": [
{ "item": "minecraft:apple", "data": 0 },
{ "item": "minecraft:apple", "data": 1 },
{ "item": "minecraft:apple", "data": 2 },
{ "item": "minecraft:apple", "data": 3 }
{ "item": "minecraft:apple", "data": 0 }
]
},
"result": { "item": "unicopia:apple_cider", "data": 0, "count": 1 }
Expand Down
5 changes: 1 addition & 4 deletions src/main/resources/assets/unicopia/recipes/juice.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
{ "item": "minecraft:glass_bottle" }
],
"A": [
{ "item": "minecraft:apple", "data": 0 },
{ "item": "minecraft:apple", "data": 1 },
{ "item": "minecraft:apple", "data": 2 },
{ "item": "minecraft:apple", "data": 3 }
{ "item": "minecraft:apple", "data": 0 }
]
},
"result": { "item": "unicopia:juice", "data": 0, "count": 1 }
Expand Down

0 comments on commit 3fb6ef7

Please sign in to comment.