Skip to content

Commit

Permalink
Fixed ItemStacks with components not being converted to ingredients p…
Browse files Browse the repository at this point in the history
…roperly when using Item.of() syntax
  • Loading branch information
LatvianModder committed Sep 16, 2024
1 parent 0b52eed commit 7105ad6
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import dev.latvian.mods.kubejs.level.BlockContainerJS;
import dev.latvian.mods.kubejs.recipe.match.ItemMatch;
import dev.latvian.mods.kubejs.recipe.match.Replaceable;
import dev.latvian.mods.kubejs.util.Cast;
import dev.latvian.mods.kubejs.util.ID;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import dev.latvian.mods.kubejs.util.WithCodec;
Expand All @@ -18,7 +19,10 @@
import dev.latvian.mods.rhino.util.SpecialEquality;
import dev.latvian.mods.rhino.util.ToStringJS;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet;
import net.minecraft.core.Registry;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponentPredicate;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
Expand All @@ -37,6 +41,7 @@
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.neoforged.neoforge.common.crafting.DataComponentIngredient;
import org.jetbrains.annotations.Nullable;

import java.util.List;
Expand Down Expand Up @@ -229,7 +234,21 @@ default String toStringJS(Context cx) {

@Override
default Ingredient kjs$asIngredient() {
return kjs$self().getItem().kjs$asIngredient();
var p = kjs$self().getComponentsPatch();

if (p.isEmpty()) {
return kjs$self().getItem().kjs$asIngredient();
}

var map = DataComponentMap.builder();

for (var entry : p.entrySet()) {
if (entry.getValue().isPresent()) {
map.set(entry.getKey(), Cast.to(entry.getValue().get()));
}
}

return new DataComponentIngredient(HolderSet.direct(kjs$asHolder()), DataComponentPredicate.allOf(map.build()), false).toVanilla();
}

@Override
Expand Down

0 comments on commit 7105ad6

Please sign in to comment.