Implement JEI/REI hiding

This commit is contained in:
LLytho 2022-08-18 23:21:42 +02:00
parent 787a9eca2d
commit 619bb6b9da
9 changed files with 129 additions and 5 deletions

View file

@ -13,7 +13,8 @@ val modId: String by project
val fabricLoaderVersion: String by project
val mappingsChannel: String by project
val mappingsVersion: String by project
val almostlibVersion: String by project
val reiVersion: String by project
val jeiVersion: String by project
val baseArchiveName = "${modName}-common-${minecraftVersion}"
@ -41,6 +42,8 @@ dependencies {
include("com.electronwill.night-config:core:3.6.4");
include("com.electronwill.night-config:toml:3.6.4");
modCompileOnlyApi("mezz.jei:jei-${minecraftVersion}-common-api:${jeiVersion}")
/**
* DON'T USE THIS! NEEDED TO COMPILE THIS PROJECT
*/

View file

@ -0,0 +1,34 @@
package com.almostreliable.unified.compat;
import com.almostreliable.unified.AlmostUnifiedPlatform;
import com.almostreliable.unified.BuildConfig;
import mezz.jei.api.IModPlugin;
import mezz.jei.api.JeiPlugin;
import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.runtime.IJeiRuntime;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import java.util.Collection;
@JeiPlugin
public class AlmostJEI implements IModPlugin {
@Override
public ResourceLocation getPluginUid() {
return new ResourceLocation(BuildConfig.MOD_ID, "jei");
}
@Override
public void onRuntimeAvailable(IJeiRuntime jei) {
if (AlmostUnifiedPlatform.INSTANCE.isModLoaded("rei")) {
return;
}
Collection<ItemStack> items = HideHelper.createHidingList();
if (!items.isEmpty()) {
jei.getIngredientManager().removeIngredientsAtRuntime(VanillaTypes.ITEM_STACK, items);
}
}
}

View file

@ -0,0 +1,48 @@
package com.almostreliable.unified.compat;
import com.almostreliable.unified.AlmostUnified;
import com.almostreliable.unified.config.Config;
import com.almostreliable.unified.config.UnifyConfig;
import com.almostreliable.unified.utils.ReplacementMap;
import com.almostreliable.unified.utils.TagMap;
import com.almostreliable.unified.utils.UnifyTag;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
public class HideHelper {
public static Collection<ItemStack> createHidingList() {
UnifyConfig config = Config.load(UnifyConfig.NAME, new UnifyConfig.Serializer());
List<UnifyTag<Item>> unifyTags = config.bakeTags();
TagMap tagMap = TagMap.create(unifyTags);
ReplacementMap repMap = new ReplacementMap(tagMap, config);
return tagMap.getTags().stream().map(unifyTag -> {
Collection<ResourceLocation> itemsByTag = tagMap.getItems(unifyTag);
Set<ResourceLocation> replacements = itemsByTag
.stream()
.map(repMap::getReplacementForItem)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
List<ResourceLocation> toHide = itemsByTag.stream().filter(rl -> !replacements.contains(rl)).toList();
if (!toHide.isEmpty()) {
AlmostUnified.LOG.info("Hiding {}/{} items for tag {} -> {}",
toHide.size(),
itemsByTag.size(),
unifyTag.location(),
toHide);
}
return toHide.stream().flatMap(rl -> Registry.ITEM.getOptional(rl).stream()).map(ItemStack::new).toList();
}).flatMap(Collection::stream).toList();
}
}

View file

@ -5,6 +5,7 @@ import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.Tag;
import net.minecraft.tags.TagKey;
import net.minecraft.tags.TagManager;
import net.minecraft.world.item.Item;
@ -17,6 +18,20 @@ public class TagMap {
protected TagMap() {}
public static TagMap create(Collection<UnifyTag<Item>> unifyTags) {
TagMap tagMap = new TagMap();
unifyTags.forEach(ut -> {
TagKey<Item> asTagKey = TagKey.create(Registry.ITEM_REGISTRY, ut.location());
Registry.ITEM.getTagOrEmpty(asTagKey).forEach(holder -> {
ResourceLocation key = Registry.ITEM.getKey(holder.value());
tagMap.put(ut, key);
});
});
return tagMap;
}
public static TagMap create(TagManager tagManager, Predicate<UnifyTag<Item>> filter) {
Objects.requireNonNull(tagManager, "Requires a non-null tag manager");

View file

@ -12,7 +12,7 @@ val modId: String by project
val mappingsChannel: String by project
val mappingsVersion: String by project
val reiVersion: String by project
val almostlibVersion: String by project
val jeiVersion: String by project
val baseArchiveName = "${modName}-fabric-${minecraftVersion}"

View file

@ -0,0 +1,17 @@
package com.almostreliable.unified.compat;
import com.almostreliable.unified.AlmostUnifiedPlatform;
import me.shedaniel.rei.api.client.plugins.REIClientPlugin;
import me.shedaniel.rei.api.client.registry.entry.EntryRegistry;
import me.shedaniel.rei.api.common.util.EntryStacks;
public class AlmostREI implements REIClientPlugin {
@Override
public void registerEntries(EntryRegistry registry) {
if (AlmostUnifiedPlatform.INSTANCE.isModLoaded("jei")) {
return;
}
HideHelper.createHidingList().stream().map(EntryStacks::of).forEach(registry::removeEntry);
}
}

View file

@ -17,6 +17,12 @@
"entrypoints": {
"main": [
"com.almostreliable.unified.AlmostUnifiedFabric"
],
"jei_mod_plugin": [
"com.almostreliable.unified.compat.AlmostJEI"
],
"rei_client": [
"com.almostreliable.unified.compat.AlmostREI"
]
},
"mixins": [

View file

@ -68,8 +68,9 @@ dependencies {
minecraft("net.minecraftforge:forge:${minecraftVersion}-${forgeVersion}")
compileOnly(project(":Common"))
compileOnly(fg.deobf("mezz.jei:jei-${minecraftVersion}:${jeiVersion}:api"))
runtimeOnly(fg.deobf("mezz.jei:jei-${minecraftVersion}:${jeiVersion}"))
compileOnly(fg.deobf("mezz.jei:jei-${minecraftVersion}-common-api:${jeiVersion}"))
compileOnly(fg.deobf("mezz.jei:jei-${minecraftVersion}-forge-api:${jeiVersion}"))
runtimeOnly(fg.deobf("mezz.jei:jei-${minecraftVersion}-forge:${jeiVersion}"))
fileTree("extra-mods-$minecraftVersion") { include("**/*.jar") }
.forEach { f ->

View file

@ -18,7 +18,7 @@ fabricLoaderVersion = 0.14.5
# Dependencies
extraModsDirectory = extra-mods
reiVersion = 8.1.457
jeiVersion = 9+
jeiVersion = 10+
# Mappings
mappingsChannel = parchment