2024-04-02 00:39:21 +02:00
|
|
|
package com.minelittlepony.unicopia.diet;
|
|
|
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
2024-04-02 12:27:38 +02:00
|
|
|
import com.minelittlepony.unicopia.Debug;
|
|
|
|
import com.minelittlepony.unicopia.Unicopia;
|
2024-04-02 00:39:21 +02:00
|
|
|
import com.mojang.serialization.Codec;
|
|
|
|
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2024-04-02 12:27:38 +02:00
|
|
|
import net.minecraft.registry.Registries;
|
2024-04-02 00:39:21 +02:00
|
|
|
import net.minecraft.registry.RegistryKeys;
|
|
|
|
import net.minecraft.registry.tag.TagKey;
|
|
|
|
import net.minecraft.util.Identifier;
|
|
|
|
import net.minecraft.util.Util;
|
|
|
|
|
|
|
|
public interface FoodGroupKey {
|
|
|
|
Function<Identifier, FoodGroupKey> LOOKUP = Util.memoize(id -> {
|
|
|
|
return new FoodGroupKey() {
|
|
|
|
@Override
|
|
|
|
public Identifier id() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean contains(ItemStack stack) {
|
|
|
|
var group = PonyDiets.getEffect(id);
|
|
|
|
return group != null && group.test(stack);
|
|
|
|
}
|
2024-04-04 21:22:10 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
return o == this && (o instanceof FoodGroupKey k && k.id().equals(id()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return id().hashCode();
|
|
|
|
}
|
2024-04-02 00:39:21 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
Function<TagKey<Item>, FoodGroupKey> TAG_LOOKUP = Util.memoize(tag -> {
|
|
|
|
return new FoodGroupKey() {
|
2024-04-02 12:27:38 +02:00
|
|
|
private boolean check;
|
2024-04-02 00:39:21 +02:00
|
|
|
@Override
|
|
|
|
public Identifier id() {
|
|
|
|
return tag.id();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean contains(ItemStack stack) {
|
2024-04-02 12:27:38 +02:00
|
|
|
if (Debug.CHECK_GAME_VALUES && !check) {
|
|
|
|
check = true;
|
|
|
|
if (Registries.ITEM.getEntryList(tag).isEmpty()) {
|
|
|
|
Unicopia.LOGGER.info("Tag is empty: " + tag.id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-02 00:39:21 +02:00
|
|
|
return stack.isIn(tag);
|
|
|
|
}
|
2024-04-04 21:22:10 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
return o == this && (o instanceof FoodGroupKey k && k.id().equals(id()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return id().hashCode();
|
|
|
|
}
|
2024-04-02 00:39:21 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
Function<Identifier, FoodGroupKey> TAG_ID_LOOKUP = id -> TAG_LOOKUP.apply(TagKey.of(RegistryKeys.ITEM, id));
|
|
|
|
Codec<FoodGroupKey> CODEC = Identifier.CODEC.xmap(LOOKUP, FoodGroupKey::id);
|
|
|
|
Codec<FoodGroupKey> TAG_CODEC = TagKey.unprefixedCodec(RegistryKeys.ITEM).xmap(TAG_LOOKUP, k -> TagKey.of(RegistryKeys.ITEM, k.id()));
|
|
|
|
|
|
|
|
Identifier id();
|
|
|
|
|
|
|
|
boolean contains(ItemStack stack);
|
|
|
|
}
|