diff --git a/Common/build.gradle.kts b/Common/build.gradle.kts index 8eff9f8..4f348fc 100644 --- a/Common/build.gradle.kts +++ b/Common/build.gradle.kts @@ -44,6 +44,9 @@ dependencies { modCompileOnly("mezz.jei:jei-$minecraftVersion-common:$jeiVersion") // required for common jei plugin and mixin modCompileOnly("dev.latvian.mods:kubejs-fabric:$kubejsVersion") // required for common kubejs plugin | common has remapping issues + compileOnly("com.google.auto.service:auto-service:1.0.1") + annotationProcessor("com.google.auto.service:auto-service:1.0.1") + // JUnit Tests testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion") diff --git a/Common/src/main/java/com/almostreliable/unified/AlmostUnified.java b/Common/src/main/java/com/almostreliable/unified/AlmostUnified.java index acca11d..36387bc 100644 --- a/Common/src/main/java/com/almostreliable/unified/AlmostUnified.java +++ b/Common/src/main/java/com/almostreliable/unified/AlmostUnified.java @@ -23,6 +23,10 @@ public final class AlmostUnified { return STARTUP_CONFIG; } + public static boolean isRuntimeLoaded() { + return RUNTIME != null; + } + public static AlmostUnifiedRuntime getRuntime() { if (RUNTIME == null) { throw new IllegalStateException("AlmostUnifiedRuntime not initialized"); diff --git a/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedLookupImpl.java b/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedLookupImpl.java new file mode 100644 index 0000000..9f788e9 --- /dev/null +++ b/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedLookupImpl.java @@ -0,0 +1,76 @@ +package com.almostreliable.unified; + +import com.almostreliable.unified.api.AlmostUnifiedLookup; +import com.almostreliable.unified.utils.UnifyTag; +import com.google.auto.service.AutoService; +import net.minecraft.core.Registry; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.tags.TagKey; +import net.minecraft.world.item.Item; +import net.minecraft.world.level.ItemLike; +import org.jetbrains.annotations.Nullable; + +import java.util.Set; +import java.util.stream.Collectors; + +@AutoService(AlmostUnifiedLookup.class) +public class AlmostUnifiedLookupImpl implements AlmostUnifiedLookup { + + @Override + public boolean isLoaded() { + return AlmostUnified.isRuntimeLoaded(); + } + + @Nullable + @Override + public Item getReplacementForItem(ItemLike itemLike) { + ResourceLocation id = Registry.ITEM.getKey(itemLike.asItem()); + ResourceLocation result = AlmostUnified.getRuntime().getReplacementMap().getReplacementForItem(id); + return Registry.ITEM.getOptional(result).orElse(null); + } + + @Nullable + @Override + public Item getPreferredItemForTag(TagKey tag) { + UnifyTag asUnifyTag = UnifyTag.item(tag.location()); + ResourceLocation result = AlmostUnified + .getRuntime() + .getReplacementMap() + .getPreferredItemForTag(asUnifyTag, $ -> true); + return Registry.ITEM.getOptional(result).orElse(null); + } + + @Nullable + @Override + public TagKey getPreferredTagForItem(ItemLike itemLike) { + ResourceLocation id = Registry.ITEM.getKey(itemLike.asItem()); + UnifyTag unifyTag = AlmostUnified.getRuntime().getReplacementMap().getPreferredTagForItem(id); + if (unifyTag == null) { + return null; + } + return TagKey.create(Registry.ITEM_REGISTRY, unifyTag.location()); + } + + @Override + public Set getPotentialItems(TagKey tag) { + UnifyTag asUnifyTag = UnifyTag.item(tag.location()); + return AlmostUnified + .getRuntime() + .getFilteredTagMap() + .getItems(asUnifyTag) + .stream() + .flatMap(rl -> Registry.ITEM.getOptional(rl).stream()) + .collect(Collectors.toSet()); + } + + @Override + public Set> getConfiguredTags() { + return AlmostUnified + .getRuntime() + .getFilteredTagMap() + .getTags() + .stream() + .map(ut -> TagKey.create(Registry.ITEM_REGISTRY, ut.location())) + .collect(Collectors.toSet()); + } +} diff --git a/Common/src/main/java/com/almostreliable/unified/api/AlmostUnifiedLookup.java b/Common/src/main/java/com/almostreliable/unified/api/AlmostUnifiedLookup.java new file mode 100644 index 0000000..971be7c --- /dev/null +++ b/Common/src/main/java/com/almostreliable/unified/api/AlmostUnifiedLookup.java @@ -0,0 +1,97 @@ +package com.almostreliable.unified.api; + +import net.minecraft.tags.TagKey; +import net.minecraft.world.item.Item; +import net.minecraft.world.level.ItemLike; + +import javax.annotation.Nullable; +import java.util.ServiceLoader; +import java.util.Set; + +public interface AlmostUnifiedLookup { + + AlmostUnifiedLookup INSTANCE = ServiceLoader.load(AlmostUnifiedLookup.class).findFirst().orElseGet(Empty::new); + + boolean isLoaded(); + + /** + * Returns replacement item for given {@link ItemLike}. If no configured tag exists which includes the item it will return null.

+ * If the item is part of some stone strata, it will only check items within the same stone strata.
+ * => e.g. "modid:deepslate_foo_ore" would not return "prio_modid:foo_ore". + * + * @param itemLike The item like to find the replacement for + * @return replacement item or null if there is no replacement + */ + @Nullable + Item getReplacementForItem(ItemLike itemLike); + + /** + * Returns the preferred item for given {@link TagKey}. If no configured tag exists which includes the item it will return null.

+ * The preferred item is mainly chose by mod priorities, but it's possible to provide a fixed override through the config. + * + * @param tag The tag to find the preferred item for + * @return preferred item or null if there is no preferred item + */ + @Nullable + Item getPreferredItemForTag(TagKey tag); + + /** + * Returns the preferred tag for given {@link ItemLike}. If no configured tag exists which includes the item it will return null.

+ * + * @param itemLike The item like to find the preferred tag for + * @return preferred tag or null if there is no preferred tag + */ + @Nullable + TagKey getPreferredTagForItem(ItemLike itemLike); + + /** + * Returns all potential items which are part of the given tag. Tags are only considered if they are part of the config, otherwise they will always return an empty set. + * + * @param tag The tag to find the potential items for + * @return potential items or empty set if there are no potential items + */ + Set getPotentialItems(TagKey tag); + + /** + * Returns all configured tags. + * + * @return configured tags + */ + Set> getConfiguredTags(); + + class Empty implements AlmostUnifiedLookup { + + @Override + public boolean isLoaded() { + return false; + } + + @Nullable + @Override + public Item getReplacementForItem(ItemLike itemLike) { + return null; + } + + @Nullable + @Override + public Item getPreferredItemForTag(TagKey tag) { + return null; + } + + @Nullable + @Override + public TagKey getPreferredTagForItem(ItemLike itemLike) { + return null; + } + + @Override + public Set getPotentialItems(TagKey tag) { + return Set.of(); + } + + @Override + public Set> getConfiguredTags() { + return Set.of(); + } + } +} diff --git a/Fabric/build.gradle.kts b/Fabric/build.gradle.kts index a83001b..24d009f 100644 --- a/Fabric/build.gradle.kts +++ b/Fabric/build.gradle.kts @@ -90,6 +90,9 @@ dependencies { println("Extra mod $mod with version $version detected") modLocalRuntime("$extraModsDirectory:$mod:$version") } + + compileOnly("com.google.auto.service:auto-service:1.0.1") + annotationProcessor("com.google.auto.service:auto-service:1.0.1") } tasks { diff --git a/Forge/build.gradle.kts b/Forge/build.gradle.kts index 73c4024..a973fe3 100644 --- a/Forge/build.gradle.kts +++ b/Forge/build.gradle.kts @@ -97,6 +97,9 @@ dependencies { modLocalRuntime("$extraModsDirectory:$mod:$version") } + compileOnly("com.google.auto.service:auto-service:1.0.1") + annotationProcessor("com.google.auto.service:auto-service:1.0.1") + // JUnit Tests testImplementation(project(":Common")) testImplementation(commonTests)