Skip to content

Commit

Permalink
Added safety check for when recipes return null in output item instea…
Browse files Browse the repository at this point in the history
…d of ItemStack.EMPTY. (why)
  • Loading branch information
LatvianModder authored and MaxNeedsSnacks committed Feb 21, 2024
1 parent de7a88c commit ef66307
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import dev.latvian.mods.kubejs.registry.KubeJSRegistries;
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Recipe;

@RemapPrefixForJS("kjs$")
Expand Down Expand Up @@ -54,7 +55,13 @@ default boolean replaceInput(ReplacementMatch match, InputReplacement with) {
}

default boolean hasOutput(ReplacementMatch match) {
return match instanceof ItemMatch m && m.contains(((Recipe<?>) this).getResultItem());
if (match instanceof ItemMatch m) {
var result = ((Recipe<?>) this).getResultItem();
//noinspection ConstantValue
return result != null && result != ItemStack.EMPTY && !result.isEmpty() && m.contains(result);
}

return false;
}

default boolean replaceOutput(ReplacementMatch match, OutputReplacement with) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,11 @@ public boolean replaceInput(ReplacementMatch match, InputReplacement with) {
}

@Override
@SuppressWarnings("ConstantValue")
public boolean hasOutput(ReplacementMatch match) {
if (CommonProperties.get().matchJsonRecipes && match instanceof ItemMatch m && getOriginalRecipe() != null) {
var r = getOriginalRecipe().getResultItem();
//noinspection ConstantValue
if (r == null) {
throw new NullPointerException("ItemStack should never be null, but recipe " + this + " returned null as the output!");
}
return r != ItemStack.EMPTY && m.contains(r);
return r != null && r != ItemStack.EMPTY && m.contains(r);
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,9 @@ public ItemStack getOriginalRecipeResult() {
return ItemStack.EMPTY;
}

return getOriginalRecipe().getResultItem();
var result = getOriginalRecipe().getResultItem();
//noinspection ConstantValue
return result == null ? ItemStack.EMPTY : result;
}

public List<Ingredient> getOriginalRecipeIngredients() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.Bootstrap;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeManager;
import net.minecraft.world.item.crafting.RecipeSerializer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ public interface KubeJSCraftingRecipe extends CraftingRecipe {
}

var modifyResult = kjs$getModifyResult();
var result = getResultItem();
//noinspection ConstantValue
result = result == null ? ItemStack.EMPTY : result.copy();
if (modifyResult != null) {
return modifyResult.modify(new ModifyRecipeCraftingGrid(container), getResultItem().copy());
return modifyResult.modify(new ModifyRecipeCraftingGrid(container), result);
}

return getResultItem().copy();
return result.copy();
}

@Nullable
Expand Down

0 comments on commit ef66307

Please sign in to comment.