Crafting manager fixes

This commit is contained in:
modmuss50 2019-02-22 09:47:07 +00:00
parent a003656419
commit 6f9baaeb30
8 changed files with 108 additions and 82 deletions

View file

@ -25,9 +25,12 @@
package techreborn.events;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.world.World;
import net.minecraftforge.common.crafting.RecipeType;
import net.minecraftforge.common.crafting.VanillaRecipeTypes;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.event.entity.player.EntityItemPickupEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
@ -38,6 +41,9 @@ import techreborn.init.TRContent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class TRRecipeHandler {
@ -53,9 +59,10 @@ public class TRRecipeHandler {
if(entityItemPickupEvent.getEntityPlayer() instanceof FakePlayer){
return;
}
World world = entityItemPickupEvent.getItem().world;
if (entityItemPickupEvent.getEntityPlayer() instanceof EntityPlayerMP) {
if (ItemUtils.isInputEqual("logWood", entityItemPickupEvent.getItem().getItem(), false, false, true)) {
for (IRecipe recipe : CraftingManager.REGISTRY) {
if (ItemUtils.isInputEqual("logWood", entityItemPickupEvent.getItem().getItem(), false, true)) {
for (IRecipe recipe : world.getRecipeManager().getRecipes()) {
if (recipe.getRecipeOutput().getItem() == TRContent.TREE_TAP) {
entityItemPickupEvent.getEntityPlayer().unlockRecipes(Collections.singletonList(recipe));
}
@ -66,7 +73,7 @@ public class TRRecipeHandler {
public static void unlockTRRecipes(EntityPlayerMP playerMP) {
List<IRecipe> recipeList = new ArrayList<>();
for (IRecipe recipe : CraftingManager.REGISTRY) {
for (IRecipe recipe : getRecipes(playerMP.world, VanillaRecipeTypes.CRAFTING)) {
if (isRecipeValid(recipe)) {
recipeList.add(recipe);
}
@ -74,11 +81,28 @@ public class TRRecipeHandler {
playerMP.unlockRecipes(recipeList);
}
public static <T extends IRecipe> List<IRecipe> getRecipes(World world, RecipeType<T> type){
return world.getRecipeManager().getRecipes().stream().filter(iRecipe -> iRecipe.getType() == type).collect(Collectors.toList());
}
/**
*
* Used to get the matching output of a recipe type that only has 1 input
*
*/
public static <T extends IRecipe> ItemStack getMatchingRecipes(World world, RecipeType<T> type, ItemStack input){
return getRecipes(world, type).stream()
.filter(iRecipe -> iRecipe.getIngredients().size() == 1 && iRecipe.getIngredients().get(0).test(input))
.map(IRecipe::getRecipeOutput)
.findFirst()
.orElse(ItemStack.EMPTY);
}
private static boolean isRecipeValid(IRecipe recipe) {
if (recipe.getRegistryName() == null) {
if (recipe.getId() == null) {
return false;
}
if (!recipe.getRegistryName().getNamespace().equals(TechReborn.MOD_ID)) {
if (!recipe.getId().getNamespace().equals(TechReborn.MOD_ID)) {
return false;
}
if (!recipe.getRecipeOutput().getItem().getRegistryName().getNamespace().equals(TechReborn.MOD_ID)) {
@ -89,7 +113,7 @@ public class TRRecipeHandler {
// }
//Hide uu recipes
for (Ingredient ingredient : recipe.getIngredients()) {
if (ingredient.apply(TRContent.Parts.UU_MATTER.getStack())) {
if (ingredient.test(TRContent.Parts.UU_MATTER.getStack())) {
return false;
}
}