Skip to content

Commit

Permalink
Removed KJS' TagIngredient and rewrote how tags are applied. This sho…
Browse files Browse the repository at this point in the history
…uld now work much better
  • Loading branch information
LatvianModder committed Jul 24, 2024
1 parent 18c3772 commit 90b9a02
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 195 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.latvian.mods.kubejs.bindings;

import dev.latvian.mods.kubejs.ingredient.TagIngredient;
import dev.latvian.mods.kubejs.ingredient.WildcardIngredient;
import dev.latvian.mods.kubejs.typings.Info;
import net.minecraft.tags.TagKey;
Expand Down Expand Up @@ -43,10 +42,22 @@ static ItemStack first(Ingredient ingredient) {
static TagKey<Item> tagKeyOf(Ingredient in) {
if (!in.isCustom() && in.getValues().length == 1 && in.getValues()[0] instanceof Ingredient.TagValue value) {
return value.tag();
} else if (in.getCustomIngredient() instanceof TagIngredient tin) {
return tin.tagKey;
} else {
return null;
}
}

static boolean containsAnyTag(Ingredient in) {
if (in.isCustom()) {
return false;
}

for (var value : in.getValues()) {
if (value instanceof Ingredient.TagValue) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ public void generateAssets(KubeAssetGenerator generator) {
generator.itemModel(itemBuilder.id, this::generateItemModelJson);
}
}


}

protected abstract void generateMultipartBlockStateJson(MultipartBlockStateGenerator bs);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/core/IngredientKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
import dev.latvian.mods.kubejs.util.WithCodec;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.neoforged.neoforge.common.crafting.CompoundIngredient;
import net.neoforged.neoforge.common.crafting.DifferenceIngredient;
import net.neoforged.neoforge.common.crafting.IntersectionIngredient;
import net.neoforged.neoforge.common.crafting.SizedIngredient;
import org.jetbrains.annotations.Nullable;

@RemapPrefixForJS("kjs$")
public interface IngredientKJS extends ItemPredicate, Replaceable, WithCodec, ItemMatch {
Expand Down Expand Up @@ -123,4 +126,13 @@ default boolean matches(Context cx, Ingredient in, boolean exact) {

return false;
}

@Nullable
default TagKey<Item> kjs$getTagKey() {
return IngredientWrapper.tagKeyOf(kjs$self());
}

default boolean kjs$containsAnyTag() {
return IngredientWrapper.containsAnyTag(kjs$self());
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public interface KubeJSIngredients {
DeferredRegister<IngredientType<?>> REGISTRY = DeferredRegister.create(NeoForgeRegistries.Keys.INGREDIENT_TYPES, KubeJS.MOD_ID);

Supplier<IngredientType<WildcardIngredient>> WILDCARD = REGISTRY.register("wildcard", () -> new IngredientType<>(WildcardIngredient.CODEC, WildcardIngredient.STREAM_CODEC));
Supplier<IngredientType<TagIngredient>> TAG = REGISTRY.register("tag", () -> new IngredientType<>(TagIngredient.CODEC, TagIngredient.STREAM_CODEC));
Supplier<IngredientType<NamespaceIngredient>> NAMESPACE = REGISTRY.register("namespace", () -> new IngredientType<>(NamespaceIngredient.CODEC, NamespaceIngredient.STREAM_CODEC));
Supplier<IngredientType<RegExIngredient>> REGEX = REGISTRY.register("regex", () -> new IngredientType<>(RegExIngredient.CODEC, RegExIngredient.STREAM_CODEC));
Supplier<IngredientType<CreativeTabIngredient>> CREATIVE_TAB = REGISTRY.register("creative_tab", () -> new IngredientType<>(CreativeTabIngredient.CODEC, CreativeTabIngredient.STREAM_CODEC));
Expand Down
108 changes: 0 additions & 108 deletions src/main/java/dev/latvian/mods/kubejs/ingredient/TagIngredient.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/item/ItemStackJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.mojang.serialization.JsonOps;
import dev.latvian.mods.kubejs.bindings.DataComponentWrapper;
import dev.latvian.mods.kubejs.ingredient.RegExIngredient;
import dev.latvian.mods.kubejs.ingredient.TagIngredient;
import dev.latvian.mods.kubejs.util.ID;
import dev.latvian.mods.kubejs.util.Lazy;
import dev.latvian.mods.kubejs.util.MapJS;
Expand Down Expand Up @@ -157,7 +156,8 @@ static ItemStack wrap(RegistryAccessContainer registries, @Nullable Object o) {

return stack;
} else if (map.containsKey("tag")) {
var stack = new TagIngredient(registries.cachedItemTags, ItemTags.create(ID.mc(map.get("tag")))).toVanilla().kjs$getFirst();
// var stack = new TagIngredient(registries.cachedItemTags, ItemTags.create(ID.mc(map.get("tag")))).toVanilla().kjs$getFirst();
var stack = Ingredient.of(ItemTags.create(ID.mc(map.get("tag")))).kjs$getFirst();

if (map.containsKey("count")) {
stack.setCount(UtilsJS.parseInt(map.get("count"), 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import dev.latvian.mods.kubejs.ingredient.CreativeTabIngredient;
import dev.latvian.mods.kubejs.ingredient.NamespaceIngredient;
import dev.latvian.mods.kubejs.ingredient.RegExIngredient;
import dev.latvian.mods.kubejs.ingredient.TagIngredient;
import dev.latvian.mods.kubejs.item.ItemStackJS;
import dev.latvian.mods.kubejs.util.ListJS;
import dev.latvian.mods.kubejs.util.MapJS;
Expand Down Expand Up @@ -145,7 +144,8 @@ static Ingredient read(RegistryAccessContainer registries, StringReader reader)
}
case '#' -> {
reader.skip();
yield new TagIngredient(registries.cachedItemTags, ItemTags.create(ResourceLocation.read(reader))).toVanilla();
// yield new TagIngredient(registries.cachedItemTags, ItemTags.create(ResourceLocation.read(reader))).toVanilla();
yield Ingredient.of(ItemTags.create(ResourceLocation.read(reader)));
}
case '@' -> {
reader.skip();
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/recipe/CachedTagLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.datafixers.util.Either;
import dev.latvian.mods.kubejs.KubeJS;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
Expand Down Expand Up @@ -122,4 +123,21 @@ public Set<TagKey<T>> keys(T value) {

return valueToKey.getOrDefault(value, Set.of());
}

public Map<TagKey<T>, List<Holder<T>>> bindingMap() {
var k2v = keyToValue();
var map = new IdentityHashMap<TagKey<T>, List<Holder<T>>>(k2v.size());

for (var entry : k2v.entrySet()) {
var list = new ArrayList<Holder<T>>(entry.getValue().size());

for (var value : entry.getValue()) {
list.add(registry.wrapAsHolder(value));
}

map.put(entry.getKey(), list);
}

return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.item.crafting.RecipeSerializer;
import org.apache.commons.lang3.mutable.MutableObject;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
Expand All @@ -69,8 +68,6 @@
import java.util.stream.Stream;

public class RecipesKubeEvent implements KubeEvent {
public static final MutableObject<CachedItemTagLookup> TEMP_ITEM_TAG_LOOKUP = new MutableObject<>(null);

public static final Pattern POST_SKIP_ERROR = Pattern.compile("dev\\.latvian\\.mods\\.kubejs\\.recipe\\.RecipesKubeEvent\\.post");
public static final Pattern CREATE_RECIPE_SKIP_ERROR = Pattern.compile("dev\\.latvian\\.mods\\.kubejs\\.recipe\\.RecipesKubeEvent\\.createRecipe");
private static final Predicate<KubeRecipe> RECIPE_NOT_REMOVED = r -> r != null && !r.removed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ public boolean recipes(RecipeManagerKJS recipeManager, ResourceManager resourceM
}

boolean result = false;
RecipesKubeEvent.TEMP_ITEM_TAG_LOOKUP.setValue(getRegistries().cachedItemTags);

for (var entry : getRegistries().cachedRegistryTags.values()) {
entry.getLeft().bindTags((Map) entry.getRight().bindingMap());
}

recipeSchemaStorage.fireEvents(getRegistries(), resourceManager);

SpecialRecipeSerializerManager.INSTANCE.reset();
Expand All @@ -234,7 +238,6 @@ public boolean recipes(RecipeManagerKJS recipeManager, ResourceManager resourceM
}

serverData = new SyncServerDataPayload(KubeServerData.collect());
RecipesKubeEvent.TEMP_ITEM_TAG_LOOKUP.setValue(null);
return result;
}

Expand Down
Loading

0 comments on commit 90b9a02

Please sign in to comment.