Moved recipe utility methods to RecipeUtils

This commit is contained in:
drcrazy 2019-07-30 01:46:12 +03:00
parent 878734e4ad
commit aa7464ad4a
6 changed files with 38 additions and 40 deletions

View file

@ -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

View file

@ -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();
}

View file

@ -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<CraftingInventory> recipe) {
if (recipe != null && recipe.fits(3, 3)) {
boolean missingOutput = false;
int[] stacksInSlots = new int[9];

View file

@ -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();
}

View file

@ -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<Recipe<?>> 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 <T extends Recipe<?>> List<Recipe> getRecipes(World world, RecipeType<T> 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 <T extends Recipe<?>> ItemStack getMatchingRecipes(World world, RecipeType<T> 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 -> ingredient.method_8093(TRContent.Parts.UU_MATTER.getStack()));
}

View file

@ -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 <T extends Recipe<?>> ItemStack getMatchingRecipes(World world, RecipeType<T> 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 <T extends Recipe<?>> List<Recipe<?>> getRecipes(World world, RecipeType<T> type){
return world.getRecipeManager().values().stream().filter(iRecipe -> iRecipe.getType() == type).collect(Collectors.toList());
}
}