Move search tree replacement to common module

This commit is contained in:
embeddedt 2023-05-15 21:05:01 -04:00
parent 4a0b633970
commit 6f8815200e
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
10 changed files with 66 additions and 38 deletions

View File

@ -23,6 +23,8 @@ dependencies {
modCompileOnly("me.shedaniel:RoughlyEnoughItems-fabric:${rei_version}") {
transitive = false
}
// compile against the JEI API but do not include it at runtime
modCompileOnly("mezz.jei:jei-${minecraft_version}-common:${jei_version}")
// Remove the next line if you don't want to depend on the API
// modApi "me.shedaniel:architectury:${rootProject.architectury_version}"
}

View File

@ -14,6 +14,9 @@ import net.minecraft.world.entity.Entity;
import org.embeddedt.modernfix.core.ModernFixMixinPlugin;
import org.embeddedt.modernfix.packet.EntityIDSyncPacket;
import org.embeddedt.modernfix.platform.ModernFixPlatformHooks;
import org.embeddedt.modernfix.searchtree.JEIBackedSearchTree;
import org.embeddedt.modernfix.searchtree.REIBackedSearchTree;
import org.embeddedt.modernfix.searchtree.SearchTreeProviderRegistry;
import org.embeddedt.modernfix.world.IntegratedWatchdog;
import java.lang.management.ManagementFactory;
@ -36,6 +39,8 @@ public class ModernFixClient {
if(ModernFixMixinPlugin.instance.isOptionEnabled("feature.branding.F3Screen")) {
brandingString = "ModernFix " + ModernFixPlatformHooks.getVersionString();
}
SearchTreeProviderRegistry.register(JEIBackedSearchTree.PROVIDER);
SearchTreeProviderRegistry.register(REIBackedSearchTree.PROVIDER);
}
public void resetWorldLoadStateMachine() {

View File

@ -21,6 +21,8 @@ public class MinecraftMixin {
@Inject(method = "createSearchTrees", at = @At("HEAD"), cancellable = true)
private void replaceSearchTrees(CallbackInfo ci) {
SearchTreeProviderRegistry.Provider provider = SearchTreeProviderRegistry.getSearchTreeProvider();
if(provider == null)
return;
ModernFix.LOGGER.info("Replacing search trees with '{}' provider", provider.getName());
this.searchRegistry.register(SearchRegistry.CREATIVE_NAMES, provider.getSearchTree(false));
this.searchRegistry.register(SearchRegistry.CREATIVE_TAGS, provider.getSearchTree(true));

View File

@ -130,13 +130,8 @@ public class ModernFixEarlyConfig {
}
}
private static final boolean shouldReplaceSearchTrees;
private static final boolean isDevEnv = ModernFixPlatformHooks.isDevEnv();
static {
shouldReplaceSearchTrees = modPresent("jei");
}
private static final ImmutableMap<String, Boolean> DEFAULT_SETTING_OVERRIDES = ImmutableMap.<String, Boolean>builder()
.put("mixin.perf.dynamic_resources", false)
.put("mixin.feature.direct_stack_trace", false)
@ -148,7 +143,6 @@ public class ModernFixEarlyConfig {
.put("mixin.perf.dynamic_entity_renderers", false)
.put("mixin.feature.integrated_server_watchdog", true)
.put("mixin.perf.faster_item_rendering", false)
.put("mixin.perf.blast_search_trees", shouldReplaceSearchTrees)
.put("mixin.devenv", isDevEnv)
.put("mixin.perf.remove_spawn_chunks", isDevEnv)
.build();

View File

@ -1,5 +1,6 @@
package org.embeddedt.modernfix.forge.searchtree;
package org.embeddedt.modernfix.searchtree;
import com.google.common.collect.ImmutableList;
import mezz.jei.api.ingredients.ITypedIngredient;
import mezz.jei.common.Internal;
import mezz.jei.common.ingredients.IngredientFilter;
@ -8,12 +9,12 @@ import mezz.jei.common.runtime.JeiRuntime;
import net.minecraft.client.searchtree.ReloadableIdSearchTree;
import net.minecraft.world.item.ItemStack;
import org.embeddedt.modernfix.ModernFix;
import org.embeddedt.modernfix.forge.mixin.perf.blast_search_trees.IngredientFilterInvoker;
import org.embeddedt.modernfix.platform.ModernFixPlatformHooks;
import org.embeddedt.modernfix.searchtree.DummySearchTree;
import org.embeddedt.modernfix.searchtree.SearchTreeProviderRegistry;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -27,7 +28,25 @@ public class JEIBackedSearchTree extends DummySearchTree<ItemStack> {
private String lastSearchText = "";
private final List<ItemStack> listCache = new ArrayList<>();
private static Field filterField = null;
private static final Field filterField;
private static final MethodHandle getIngredientListUncached;
static {
MethodHandle m;
Field f;
try {
Method jeiMethod = IngredientFilter.class.getDeclaredMethod("getIngredientListUncached", String.class);
jeiMethod.setAccessible(true);
m = MethodHandles.lookup().unreflect(jeiMethod);
f = IngredientFilterApi.class.getDeclaredField("ingredientFilter");
f.setAccessible(true);
} catch(ReflectiveOperationException | RuntimeException | NoClassDefFoundError e) {
m = null;
f = null;
}
getIngredientListUncached = m;
filterField = f;
}
public JEIBackedSearchTree(boolean filteringByTag) {
this.filteringByTag = filteringByTag;
@ -39,10 +58,6 @@ public class JEIBackedSearchTree extends DummySearchTree<ItemStack> {
IngredientFilterApi iFilterApi = (IngredientFilterApi)runtime.get().getIngredientFilter();
IngredientFilter filter;
try {
if(filterField == null) {
filterField = IngredientFilterApi.class.getDeclaredField("ingredientFilter");
filterField.setAccessible(true);
}
filter = (IngredientFilter)filterField.get(iFilterApi);
} catch(ReflectiveOperationException e) {
ModernFix.LOGGER.error(e);
@ -58,7 +73,14 @@ public class JEIBackedSearchTree extends DummySearchTree<ItemStack> {
private List<ItemStack> searchJEI(IngredientFilter filter, String pSearchText) {
if(!pSearchText.equals(lastSearchText)) {
listCache.clear();
List<ITypedIngredient<?>> ingredients = ((IngredientFilterInvoker)filter).invokeGetIngredientListUncached(filteringByTag ? ("$" + pSearchText) : pSearchText);
List<ITypedIngredient<?>> ingredients;
String finalSearchTerm = filteringByTag ? ("$" + pSearchText) : pSearchText;
try {
ingredients = (List<ITypedIngredient<?>>)getIngredientListUncached.invokeExact(filter, finalSearchTerm);
} catch(Throwable e) {
ModernFix.LOGGER.error("Error searching", e);
ingredients = ImmutableList.of();
}
for(ITypedIngredient<?> ingredient : ingredients) {
if(ingredient.getIngredient() instanceof ItemStack) {
listCache.add((ItemStack)ingredient.getIngredient());
@ -77,7 +99,7 @@ public class JEIBackedSearchTree extends DummySearchTree<ItemStack> {
@Override
public boolean canUse() {
return ModernFixPlatformHooks.modPresent("jei");
return ModernFixPlatformHooks.modPresent("jei") && getIngredientListUncached != null && filterField != null;
}
@Override

View File

@ -4,8 +4,10 @@ import me.shedaniel.rei.api.client.registry.entry.EntryRegistry;
import me.shedaniel.rei.api.common.entry.EntryStack;
import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes;
import me.shedaniel.rei.impl.client.search.AsyncSearchManager;
import net.minecraft.client.searchtree.ReloadableIdSearchTree;
import net.minecraft.world.item.ItemStack;
import org.embeddedt.modernfix.ModernFix;
import org.embeddedt.modernfix.platform.ModernFixPlatformHooks;
import java.util.ArrayList;
import java.util.Collections;
@ -53,4 +55,21 @@ public class REIBackedSearchTree extends DummySearchTree<ItemStack> {
}
return listCache;
}
public static final SearchTreeProviderRegistry.Provider PROVIDER = new SearchTreeProviderRegistry.Provider() {
@Override
public ReloadableIdSearchTree<ItemStack> getSearchTree(boolean tag) {
return new REIBackedSearchTree(tag);
}
@Override
public boolean canUse() {
return ModernFixPlatformHooks.modPresent("roughlyenoughitems");
}
@Override
public String getName() {
return "REI";
}
};
}

View File

@ -2,6 +2,7 @@ package org.embeddedt.modernfix.searchtree;
import net.minecraft.client.searchtree.ReloadableIdSearchTree;
import net.minecraft.world.item.ItemStack;
import org.embeddedt.modernfix.core.ModernFixMixinPlugin;
import java.util.ArrayList;
import java.util.List;
@ -14,7 +15,10 @@ public class SearchTreeProviderRegistry {
if(p.canUse())
return p;
}
return DummySearchTree.PROVIDER;
if(ModernFixMixinPlugin.instance.config.getEffectiveOptionForMixin("mixin.perf.blast_search_trees.Registry").isOverridden())
return DummySearchTree.PROVIDER;
else
return null;
}
public static synchronized void register(Provider p) {

View File

@ -38,9 +38,6 @@ dependencies {
modCompileOnly("curse.maven:refinedstorage-243076:${refined_storage_version}")
// compile against the JEI API but do not include it at runtime
modCompileOnly("mezz.jei:jei-${minecraft_version}-forge:${jei_version}")
modCompileOnly("curse.maven:jeresources-240630:3831559")
modCompileOnly "me.shedaniel:RoughlyEnoughItems-forge:${rei_version}"
modCompileOnly("dev.latvian.mods:kubejs-forge:${kubejs_version}")

View File

@ -22,9 +22,7 @@ import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.util.ObfuscationReflectionHelper;
import org.embeddedt.modernfix.ModernFixClient;
import org.embeddedt.modernfix.forge.searchtree.JEIBackedSearchTree;
import org.embeddedt.modernfix.screen.ModernFixConfigScreen;
import org.embeddedt.modernfix.searchtree.SearchTreeProviderRegistry;
public class ModernFixClientForge {
private static ModernFixClient commonMod;
@ -36,7 +34,6 @@ public class ModernFixClientForge {
ConfigGuiHandler.ConfigGuiFactory.class,
() -> new ConfigGuiHandler.ConfigGuiFactory((mc, screen) -> new ModernFixConfigScreen(screen))
);
SearchTreeProviderRegistry.register(JEIBackedSearchTree.PROVIDER);
}
private KeyMapping configKey;

View File

@ -1,14 +0,0 @@
package org.embeddedt.modernfix.forge.mixin.perf.blast_search_trees;
import mezz.jei.api.ingredients.ITypedIngredient;
import mezz.jei.common.ingredients.IngredientFilter;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;
import java.util.List;
@Mixin(IngredientFilter.class)
public interface IngredientFilterInvoker {
@Invoker(remap = false)
List<ITypedIngredient<?>> invokeGetIngredientListUncached(String filterText);
}