[Fix] BCLRecipManager could generate Recipes with empty material List or Result (quiqueck/BetterEnd#41)
This commit is contained in:
parent
de84eb0f87
commit
abf24a41da
2 changed files with 18 additions and 1 deletions
|
@ -5,6 +5,7 @@ import org.betterx.bclib.util.CollectionsUtil;
|
||||||
import net.minecraft.core.Registry;
|
import net.minecraft.core.Registry;
|
||||||
import net.minecraft.resources.ResourceLocation;
|
import net.minecraft.resources.ResourceLocation;
|
||||||
import net.minecraft.world.Container;
|
import net.minecraft.world.Container;
|
||||||
|
import net.minecraft.world.item.Items;
|
||||||
import net.minecraft.world.item.crafting.Recipe;
|
import net.minecraft.world.item.crafting.Recipe;
|
||||||
import net.minecraft.world.item.crafting.RecipeSerializer;
|
import net.minecraft.world.item.crafting.RecipeSerializer;
|
||||||
import net.minecraft.world.item.crafting.RecipeType;
|
import net.minecraft.world.item.crafting.RecipeType;
|
||||||
|
@ -118,7 +119,7 @@ public class BCLRecipeManager {
|
||||||
if (item instanceof Block) {
|
if (item instanceof Block) {
|
||||||
return Registry.BLOCK.getKey((Block) item) != Registry.BLOCK.getDefaultKey();
|
return Registry.BLOCK.getKey((Block) item) != Registry.BLOCK.getDefaultKey();
|
||||||
} else {
|
} else {
|
||||||
return Registry.ITEM.getKey(item.asItem()) != Registry.ITEM.getDefaultKey();
|
return item != Items.AIR && Registry.ITEM.getKey(item.asItem()) != Registry.ITEM.getDefaultKey();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package org.betterx.bclib.recipes;
|
package org.betterx.bclib.recipes;
|
||||||
|
|
||||||
|
import org.betterx.bclib.BCLib;
|
||||||
import org.betterx.bclib.config.PathConfig;
|
import org.betterx.bclib.config.PathConfig;
|
||||||
|
|
||||||
import net.minecraft.core.NonNullList;
|
import net.minecraft.core.NonNullList;
|
||||||
|
@ -7,6 +8,7 @@ import net.minecraft.resources.ResourceLocation;
|
||||||
import net.minecraft.tags.TagKey;
|
import net.minecraft.tags.TagKey;
|
||||||
import net.minecraft.world.item.Item;
|
import net.minecraft.world.item.Item;
|
||||||
import net.minecraft.world.item.ItemStack;
|
import net.minecraft.world.item.ItemStack;
|
||||||
|
import net.minecraft.world.item.Items;
|
||||||
import net.minecraft.world.item.crafting.*;
|
import net.minecraft.world.item.crafting.*;
|
||||||
import net.minecraft.world.level.ItemLike;
|
import net.minecraft.world.level.ItemLike;
|
||||||
|
|
||||||
|
@ -101,25 +103,38 @@ public class GridRecipe {
|
||||||
private NonNullList<Ingredient> getMaterials(int width, int height) {
|
private NonNullList<Ingredient> getMaterials(int width, int height) {
|
||||||
NonNullList<Ingredient> materials = NonNullList.withSize(width * height, Ingredient.EMPTY);
|
NonNullList<Ingredient> materials = NonNullList.withSize(width * height, Ingredient.EMPTY);
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
|
boolean hasNonEmpty = false;
|
||||||
for (String line : shape) {
|
for (String line : shape) {
|
||||||
for (int i = 0; i < width; i++) {
|
for (int i = 0; i < width; i++) {
|
||||||
char c = line.charAt(i);
|
char c = line.charAt(i);
|
||||||
Ingredient material = materialKeys.get(c);
|
Ingredient material = materialKeys.get(c);
|
||||||
|
if (material != null && !material.isEmpty()) hasNonEmpty = true;
|
||||||
materials.set(pos++, material == null ? Ingredient.EMPTY : material);
|
materials.set(pos++, material == null ? Ingredient.EMPTY : material);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!hasNonEmpty) return null;
|
||||||
return materials;
|
return materials;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void build() {
|
public void build() {
|
||||||
if (!exist) {
|
if (!exist) {
|
||||||
|
BCLib.LOGGER.warning("Unable to build Recipe " + id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int height = shape.length;
|
int height = shape.length;
|
||||||
int width = shape[0].length();
|
int width = shape[0].length();
|
||||||
ItemStack result = new ItemStack(output, count);
|
ItemStack result = new ItemStack(output, count);
|
||||||
|
if (result.is(Items.AIR)) {
|
||||||
|
BCLib.LOGGER.warning("Unable to build Recipe " + id + ": Result is AIR");
|
||||||
|
return;
|
||||||
|
}
|
||||||
NonNullList<Ingredient> materials = this.getMaterials(width, height);
|
NonNullList<Ingredient> materials = this.getMaterials(width, height);
|
||||||
|
if (materials == null || materials.isEmpty()) {
|
||||||
|
BCLib.LOGGER.warning("Unable to build Recipe " + id + ": Empty Material List");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
CraftingRecipe recipe = shaped ? new ShapedRecipe(
|
CraftingRecipe recipe = shaped ? new ShapedRecipe(
|
||||||
id,
|
id,
|
||||||
|
@ -129,6 +144,7 @@ public class GridRecipe {
|
||||||
materials,
|
materials,
|
||||||
result
|
result
|
||||||
) : new ShapelessRecipe(id, group, result, materials);
|
) : new ShapelessRecipe(id, group, result, materials);
|
||||||
|
|
||||||
BCLRecipeManager.addRecipe(type, recipe);
|
BCLRecipeManager.addRecipe(type, recipe);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue