Add AlmostUnifiedLookup for mod devs

This commit is contained in:
LLytho 2023-02-01 16:21:07 +01:00
parent fa146c464a
commit 4211838133
6 changed files with 186 additions and 0 deletions

View file

@ -44,6 +44,9 @@ dependencies {
modCompileOnly("mezz.jei:jei-$minecraftVersion-common:$jeiVersion") // required for common jei plugin and mixin 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 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 // JUnit Tests
testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion") testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")

View file

@ -23,6 +23,10 @@ public final class AlmostUnified {
return STARTUP_CONFIG; return STARTUP_CONFIG;
} }
public static boolean isRuntimeLoaded() {
return RUNTIME != null;
}
public static AlmostUnifiedRuntime getRuntime() { public static AlmostUnifiedRuntime getRuntime() {
if (RUNTIME == null) { if (RUNTIME == null) {
throw new IllegalStateException("AlmostUnifiedRuntime not initialized"); throw new IllegalStateException("AlmostUnifiedRuntime not initialized");

View file

@ -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<Item> tag) {
UnifyTag<Item> asUnifyTag = UnifyTag.item(tag.location());
ResourceLocation result = AlmostUnified
.getRuntime()
.getReplacementMap()
.getPreferredItemForTag(asUnifyTag, $ -> true);
return Registry.ITEM.getOptional(result).orElse(null);
}
@Nullable
@Override
public TagKey<Item> getPreferredTagForItem(ItemLike itemLike) {
ResourceLocation id = Registry.ITEM.getKey(itemLike.asItem());
UnifyTag<Item> unifyTag = AlmostUnified.getRuntime().getReplacementMap().getPreferredTagForItem(id);
if (unifyTag == null) {
return null;
}
return TagKey.create(Registry.ITEM_REGISTRY, unifyTag.location());
}
@Override
public Set<Item> getPotentialItems(TagKey<Item> tag) {
UnifyTag<Item> 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<TagKey<Item>> getConfiguredTags() {
return AlmostUnified
.getRuntime()
.getFilteredTagMap()
.getTags()
.stream()
.map(ut -> TagKey.create(Registry.ITEM_REGISTRY, ut.location()))
.collect(Collectors.toSet());
}
}

View file

@ -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. <p>
* If the item is part of some stone strata, it will only check items within the same stone strata. <br>
* => 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. <p>
* 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<Item> tag);
/**
* Returns the preferred tag for given {@link ItemLike}. If no configured tag exists which includes the item it will return null. <p>
*
* @param itemLike The item like to find the preferred tag for
* @return preferred tag or null if there is no preferred tag
*/
@Nullable
TagKey<Item> 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<Item> getPotentialItems(TagKey<Item> tag);
/**
* Returns all configured tags.
*
* @return configured tags
*/
Set<TagKey<Item>> 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<Item> tag) {
return null;
}
@Nullable
@Override
public TagKey<Item> getPreferredTagForItem(ItemLike itemLike) {
return null;
}
@Override
public Set<Item> getPotentialItems(TagKey<Item> tag) {
return Set.of();
}
@Override
public Set<TagKey<Item>> getConfiguredTags() {
return Set.of();
}
}
}

View file

@ -90,6 +90,9 @@ dependencies {
println("Extra mod $mod with version $version detected") println("Extra mod $mod with version $version detected")
modLocalRuntime("$extraModsDirectory:$mod:$version") 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 { tasks {

View file

@ -97,6 +97,9 @@ dependencies {
modLocalRuntime("$extraModsDirectory:$mod:$version") 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 // JUnit Tests
testImplementation(project(":Common")) testImplementation(project(":Common"))
testImplementation(commonTests) testImplementation(commonTests)