Skip to content

Commit

Permalink
Fixed tag conditions (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Jul 6, 2024
1 parent 03ea297 commit b87d7a0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dev.latvian.mods.kubejs.recipe;

import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.tags.TagLoader;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;

import java.util.List;
import java.util.Map;

public class CachedItemTagLookup extends CachedTagLookup<Item> {
public CachedItemTagLookup(Registry<Item> registry, Map<ResourceLocation, List<TagLoader.EntryWithSource>> originalMap) {
super(registry, originalMap);
}

@Override
public boolean isEmpty(TagKey<Item> key) {
var set = values(key);
return set.size() - ((set.contains(Items.AIR) ? 1 : 0) + (set.contains(Items.BARRIER) ? 1 : 0)) <= 0;
}
}
15 changes: 4 additions & 11 deletions src/main/java/dev/latvian/mods/kubejs/recipe/CachedTagLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.mojang.datafixers.util.Either;
import dev.latvian.mods.kubejs.KubeJS;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagEntry;
import net.minecraft.tags.TagKey;
Expand Down Expand Up @@ -103,17 +105,8 @@ public Set<T> values(TagKey<T> key) {

public boolean isEmpty(TagKey<T> key) {
var set = values(key);

if (set.size() == 1) {
var item = set.iterator().next();

// kinda cringe fix
if (item == Items.BARRIER) {
return true;
}
}

return set.isEmpty();
// noinspection RedundantCast
return set.size() - ((ResourceKey) registry.key() == Registries.ITEM ? ((set.contains(Items.AIR) ? 1 : 0) + (set.contains(Items.BARRIER) ? 1 : 0)) : 0) <= 0;
}

public Set<TagKey<T>> keys(T value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import dev.latvian.mods.rhino.util.HideFromJS;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeHolder;
Expand Down Expand Up @@ -67,7 +66,7 @@
import java.util.stream.Stream;

public class RecipesKubeEvent implements KubeEvent {
public static final MutableObject<CachedTagLookup<Item>> TEMP_ITEM_TAG_LOOKUP = new MutableObject<>(null);
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.mojang.serialization.JsonOps;
import com.mojang.serialization.MapCodec;
import dev.latvian.mods.kubejs.bindings.RegistryWrapper;
import dev.latvian.mods.kubejs.recipe.CachedItemTagLookup;
import dev.latvian.mods.kubejs.recipe.CachedTagLookup;
import dev.latvian.mods.rhino.Context;
import net.minecraft.core.Registry;
Expand Down Expand Up @@ -40,7 +41,7 @@ public final class RegistryAccessContainer {
private final RegistryOps<Object> java;
private DamageSources damageSources;
private final Map<String, ItemStack> itemStackParseCache;
public CachedTagLookup<Item> cachedItemTags;
public CachedItemTagLookup cachedItemTags;
public CachedTagLookup<Item> cachedBlockTags;
public CachedTagLookup<Item> cachedFluidTags;
private Map<ResourceLocation, RegistryWrapper> cachedRegistryWrappers;
Expand Down Expand Up @@ -87,7 +88,7 @@ public <T> void cacheTags(Registry<T> registry, Map<ResourceLocation, List<TagLo
var key1 = (ResourceKey) registry.key();

if (key1 == Registries.ITEM) {
cachedItemTags = Cast.to(new CachedTagLookup<>(registry, map));
cachedItemTags = Cast.to(new CachedItemTagLookup((Registry) registry, map));
} else if (key1 == Registries.BLOCK) {
cachedBlockTags = Cast.to(new CachedTagLookup<>(registry, map));
} else if (key1 == Registries.FLUID) {
Expand Down

0 comments on commit b87d7a0

Please sign in to comment.