From aa7464ad4a4c2134c6c1a7b96f467182cea79dbc Mon Sep 17 00:00:00 2001 From: drcrazy Date: Tue, 30 Jul 2019 01:46:12 +0300 Subject: [PATCH] Moved recipe utility methods to RecipeUtils --- .../iron/IronAlloyFurnaceBlockEntity.java | 1 - .../machine/iron/IronFurnaceBlockEntity.java | 8 ++--- .../tier1/AutoCraftingTableBlockEntity.java | 6 ++-- .../tier1/ElectricFurnaceBlockEntity.java | 8 ++--- .../techreborn/events/TRRecipeHandler.java | 31 ++----------------- .../java/techreborn/utils/RecipeUtils.java | 24 ++++++++++++++ 6 files changed, 38 insertions(+), 40 deletions(-) diff --git a/src/main/java/techreborn/blockentity/machine/iron/IronAlloyFurnaceBlockEntity.java b/src/main/java/techreborn/blockentity/machine/iron/IronAlloyFurnaceBlockEntity.java index e88f0068a..6197fbfa0 100644 --- a/src/main/java/techreborn/blockentity/machine/iron/IronAlloyFurnaceBlockEntity.java +++ b/src/main/java/techreborn/blockentity/machine/iron/IronAlloyFurnaceBlockEntity.java @@ -45,7 +45,6 @@ import techreborn.init.TRContent; import techreborn.init.TRBlockEntities; import java.util.concurrent.atomic.AtomicInteger; -import java.util.function.Consumer; @RebornRegister(TechReborn.MOD_ID) public class IronAlloyFurnaceBlockEntity extends MachineBaseBlockEntity diff --git a/src/main/java/techreborn/blockentity/machine/iron/IronFurnaceBlockEntity.java b/src/main/java/techreborn/blockentity/machine/iron/IronFurnaceBlockEntity.java index 899a72f4c..f6211ea05 100644 --- a/src/main/java/techreborn/blockentity/machine/iron/IronFurnaceBlockEntity.java +++ b/src/main/java/techreborn/blockentity/machine/iron/IronFurnaceBlockEntity.java @@ -38,8 +38,8 @@ import reborncore.common.blockentity.MachineBaseBlockEntity; import reborncore.common.util.IInventoryAccess; import reborncore.common.util.RebornInventory; import reborncore.common.util.ItemUtils; -import techreborn.events.TRRecipeHandler; import techreborn.init.TRBlockEntities; +import techreborn.utils.RecipeUtils; public class IronFurnaceBlockEntity extends MachineBaseBlockEntity implements InventoryProvider, IContainerProvider { @@ -120,7 +120,7 @@ public class IronFurnaceBlockEntity extends MachineBaseBlockEntity public void cookItems() { if (this.canSmelt()) { - final ItemStack itemstack = TRRecipeHandler.getMatchingRecipes(world, RecipeType.SMELTING, inventory.getInvStack(this.input1)); + final ItemStack itemstack = RecipeUtils.getMatchingRecipes(world, RecipeType.SMELTING, inventory.getInvStack(this.input1)); if (inventory.getInvStack(this.output).isEmpty()) { inventory.setInvStack(this.output, itemstack.copy()); @@ -138,7 +138,7 @@ public class IronFurnaceBlockEntity extends MachineBaseBlockEntity public boolean canSmelt() { if (inventory.getInvStack(this.input1).isEmpty()) return false; - final ItemStack itemstack = TRRecipeHandler.getMatchingRecipes(world, RecipeType.SMELTING, inventory.getInvStack(this.input1)); + final ItemStack itemstack = RecipeUtils.getMatchingRecipes(world, RecipeType.SMELTING, inventory.getInvStack(this.input1)); if (itemstack.isEmpty()) return false; if (inventory.getInvStack(this.output).isEmpty()) @@ -154,7 +154,7 @@ public class IronFurnaceBlockEntity extends MachineBaseBlockEntity } public ItemStack getResultFor(final ItemStack stack) { - final ItemStack result = TRRecipeHandler.getMatchingRecipes(world, RecipeType.SMELTING, stack); + final ItemStack result = RecipeUtils.getMatchingRecipes(world, RecipeType.SMELTING, stack); if (!result.isEmpty()) { return result.copy(); } diff --git a/src/main/java/techreborn/blockentity/machine/tier1/AutoCraftingTableBlockEntity.java b/src/main/java/techreborn/blockentity/machine/tier1/AutoCraftingTableBlockEntity.java index cd6090a39..81385d0e0 100644 --- a/src/main/java/techreborn/blockentity/machine/tier1/AutoCraftingTableBlockEntity.java +++ b/src/main/java/techreborn/blockentity/machine/tier1/AutoCraftingTableBlockEntity.java @@ -48,9 +48,9 @@ import reborncore.common.util.IInventoryAccess; import reborncore.common.util.RebornInventory; import reborncore.common.util.ItemUtils; import techreborn.TechReborn; -import techreborn.events.TRRecipeHandler; import techreborn.init.ModSounds; import techreborn.init.TRContent; +import techreborn.utils.RecipeUtils; import techreborn.init.TRBlockEntities; import javax.annotation.Nullable; @@ -93,7 +93,7 @@ public class AutoCraftingTableBlockEntity extends PowerAcceptorBlockEntity return lastRecipe; } } - for (Recipe testRecipe : TRRecipeHandler.getRecipes(world, RecipeType.CRAFTING)) { + for (Recipe testRecipe : RecipeUtils.getRecipes(world, RecipeType.CRAFTING)) { if (testRecipe.matches(crafting, world)) { lastRecipe = testRecipe; return testRecipe; @@ -118,7 +118,7 @@ public class AutoCraftingTableBlockEntity extends PowerAcceptorBlockEntity return inventoryCrafting; } - public boolean canMake(Recipe recipe) { + public boolean canMake(Recipe recipe) { if (recipe != null && recipe.fits(3, 3)) { boolean missingOutput = false; int[] stacksInSlots = new int[9]; diff --git a/src/main/java/techreborn/blockentity/machine/tier1/ElectricFurnaceBlockEntity.java b/src/main/java/techreborn/blockentity/machine/tier1/ElectricFurnaceBlockEntity.java index 545722aea..a6a9572b0 100644 --- a/src/main/java/techreborn/blockentity/machine/tier1/ElectricFurnaceBlockEntity.java +++ b/src/main/java/techreborn/blockentity/machine/tier1/ElectricFurnaceBlockEntity.java @@ -40,8 +40,8 @@ import reborncore.common.registration.RebornRegister; import reborncore.common.registration.config.ConfigRegistry; import reborncore.common.util.RebornInventory; import techreborn.TechReborn; -import techreborn.events.TRRecipeHandler; import techreborn.init.TRContent; +import techreborn.utils.RecipeUtils; import techreborn.init.TRBlockEntities; @RebornRegister(TechReborn.MOD_ID) @@ -71,7 +71,7 @@ public class ElectricFurnaceBlockEntity extends PowerAcceptorBlockEntity public void cookItems() { if (canSmelt()) { - final ItemStack itemstack = TRRecipeHandler.getMatchingRecipes(world, RecipeType.SMELTING, inventory.getInvStack(input1)); + final ItemStack itemstack = RecipeUtils.getMatchingRecipes(world, RecipeType.SMELTING, inventory.getInvStack(input1)); if (inventory.getInvStack(output).isEmpty()) { inventory.setInvStack(output, itemstack.copy()); @@ -90,7 +90,7 @@ public class ElectricFurnaceBlockEntity extends PowerAcceptorBlockEntity if (inventory.getInvStack(input1).isEmpty()) { return false; } - final ItemStack itemstack = TRRecipeHandler.getMatchingRecipes(world, RecipeType.SMELTING, inventory.getInvStack(input1)); + final ItemStack itemstack = RecipeUtils.getMatchingRecipes(world, RecipeType.SMELTING, inventory.getInvStack(input1)); if (itemstack.isEmpty()) { return false; } @@ -109,7 +109,7 @@ public class ElectricFurnaceBlockEntity extends PowerAcceptorBlockEntity } public ItemStack getResultFor(ItemStack stack) { - final ItemStack result = TRRecipeHandler.getMatchingRecipes(world, RecipeType.SMELTING, stack); + final ItemStack result = RecipeUtils.getMatchingRecipes(world, RecipeType.SMELTING, stack); if (!result.isEmpty()) { return result.copy(); } diff --git a/src/main/java/techreborn/events/TRRecipeHandler.java b/src/main/java/techreborn/events/TRRecipeHandler.java index 57856de79..9b4fa6cc8 100644 --- a/src/main/java/techreborn/events/TRRecipeHandler.java +++ b/src/main/java/techreborn/events/TRRecipeHandler.java @@ -24,26 +24,24 @@ package techreborn.events; -import net.minecraft.item.ItemStack; import net.minecraft.recipe.Ingredient; import net.minecraft.recipe.Recipe; import net.minecraft.recipe.RecipeType; import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.world.World; import techreborn.TechReborn; import techreborn.init.TRContent; +import techreborn.utils.RecipeUtils; import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; -import java.util.stream.Collectors; public class TRRecipeHandler { public static void unlockTRRecipes(ServerPlayerEntity playerMP) { List> recipeList = new ArrayList<>(); - for (Recipe recipe : getRecipes(playerMP.world, RecipeType.CRAFTING)) { + for (Recipe recipe : RecipeUtils.getRecipes(playerMP.world, RecipeType.CRAFTING)) { if (isRecipeValid(recipe)) { recipeList.add(recipe); } @@ -51,36 +49,13 @@ public class TRRecipeHandler { playerMP.unlockRecipes(recipeList); } - public static > List getRecipes(World world, RecipeType type){ - return world.getRecipeManager().values().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 > ItemStack getMatchingRecipes(World world, RecipeType type, ItemStack input){ - return getRecipes(world, type).stream() - .filter(recipe -> recipe.getPreviewInputs().size() == 1 && ((Ingredient)recipe.getPreviewInputs().get(0)).test(input)) - .map(Recipe::getOutput) - .findFirst() - .orElse(ItemStack.EMPTY); - } - - private static boolean isRecipeValid(Recipe recipe) { + private static boolean isRecipeValid(Recipe recipe) { if (recipe.getId() == null) { return false; } if (!recipe.getId().getNamespace().equals(TechReborn.MOD_ID)) { return false; } -// if (!recipe.getOutput().getItem().getRegistryName().getNamespace().equals(TechReborn.MOD_ID)) { -// return false; -// } -// if (hiddenEntrys.contains(recipe.getRecipeOutput().getItem())) { -// return false; -// } return !recipe.getPreviewInputs().stream().anyMatch((Predicate) ingredient -> ingredient.method_8093(TRContent.Parts.UU_MATTER.getStack())); } diff --git a/src/main/java/techreborn/utils/RecipeUtils.java b/src/main/java/techreborn/utils/RecipeUtils.java index 3e63310e3..0b3bf7b90 100644 --- a/src/main/java/techreborn/utils/RecipeUtils.java +++ b/src/main/java/techreborn/utils/RecipeUtils.java @@ -25,8 +25,15 @@ package techreborn.utils; import net.minecraft.item.ItemStack; +import net.minecraft.recipe.Ingredient; +import net.minecraft.recipe.Recipe; +import net.minecraft.recipe.RecipeType; +import net.minecraft.world.World; import techreborn.items.ItemDynamicCell; +import java.util.List; +import java.util.stream.Collectors; + import javax.annotation.Nonnull; public class RecipeUtils { @@ -34,4 +41,21 @@ public class RecipeUtils { public static ItemStack getEmptyCell(int stackSize) { return ItemDynamicCell.getEmptyCell(stackSize); } + + /** + * + * Used to get the matching output of a recipe type that only has 1 input + * + */ + public static > ItemStack getMatchingRecipes(World world, RecipeType type, ItemStack input){ + return getRecipes(world, type).stream() + .filter(recipe -> recipe.getPreviewInputs().size() == 1 && ((Ingredient)recipe.getPreviewInputs().get(0)).test(input)) + .map(Recipe::getOutput) + .findFirst() + .orElse(ItemStack.EMPTY); + } + + public static > List> getRecipes(World world, RecipeType type){ + return world.getRecipeManager().values().stream().filter(iRecipe -> iRecipe.getType() == type).collect(Collectors.toList()); + } }