Skip to content

Commit

Permalink
Add ability to configure specific JEI plugins to run on main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
embeddedt committed Apr 14, 2023
1 parent 2e07a44 commit d4ce812
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Common/src/main/java/mezz/jei/common/config/ClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import mezz.jei.common.config.file.IConfigSchemaBuilder;
import mezz.jei.common.config.file.serializers.EnumSerializer;
import mezz.jei.common.config.file.serializers.ListSerializer;
import mezz.jei.common.config.file.serializers.StringSerializer;
import org.jetbrains.annotations.Nullable;

import java.util.List;
Expand All @@ -18,6 +19,7 @@ public final class ClientConfig implements IClientConfig {
private final Supplier<Boolean> lowMemorySlowSearchEnabled;
private final Supplier<Boolean> cheatToHotbarUsingHotkeysEnabled;
private final Supplier<Boolean> asyncLoadingEnabled;
private final Supplier<List<String>> mainThreadPluginUids;
private final Supplier<Boolean> addBookmarksToFront;
private final Supplier<GiveMode> giveMode;
private final Supplier<Integer> maxRecipeGuiHeight;
Expand Down Expand Up @@ -64,6 +66,11 @@ public ClientConfig(IConfigSchemaBuilder schema) {
false,
"Whether JEI should load asynchronously"
);
mainThreadPluginUids = advanced.addList("AsyncPluginCompat",
List.of("namespace:mod"),
new ListSerializer<>(new StringSerializer()),
"List of plugin UIDs that should be loaded on the main thread"
);

IConfigCategoryBuilder sorting = schema.addCategory("sorting");
ingredientSorterStages = sorting.addList(
Expand Down Expand Up @@ -103,6 +110,11 @@ public boolean isAsyncLoadingEnabled() {
return asyncLoadingEnabled.get();
}

@Override
public List<String> getAsyncCompatPluginUids() {
return mainThreadPluginUids.get();
}

@Override
public boolean isAddingBookmarksToFront() {
return addBookmarksToFront.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface IClientConfig {
boolean isCheatToHotbarUsingHotkeysEnabled();

boolean isAsyncLoadingEnabled();
List<String> getAsyncCompatPluginUids();

boolean isAddingBookmarksToFront();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package mezz.jei.common.config.file.serializers;

import mezz.jei.api.runtime.config.IJeiConfigValueSerializer;

import java.util.Collection;
import java.util.Optional;

public class StringSerializer implements IJeiConfigValueSerializer<String> {
@Override
public String serialize(String value) {
return value;
}

@Override
public IDeserializeResult<String> deserialize(String string) {
string = string.trim();
if (string.startsWith("\"") && string.endsWith("\"")) {
string = string.substring(1, string.length() - 1);
};
return new DeserializeResult<>(string);
}

@Override
public boolean isValid(String value) {
return true;
}

@Override
public Optional<Collection<String>> getAllValidValues() {
return Optional.empty();
}

@Override
public String getValidValuesDescription() {
return "";
}
}
14 changes: 14 additions & 0 deletions CommonApi/src/main/java/mezz/jei/api/IModPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import mezz.jei.api.registration.IVanillaCategoryExtensionRegistration;
import mezz.jei.api.runtime.IJeiRuntime;

import java.util.EnumSet;

/**
* The main class to implement to create a JEI plugin. Everything communicated between a mod and JEI is through this class.
* IModPlugins must have the {@link JeiPlugin} annotation to get loaded by JEI.
Expand Down Expand Up @@ -137,4 +139,16 @@ default void onRuntimeUnavailable() {
default void onConfigManagerAvailable(IJeiConfigManager configManager) {

}

/**
* Called to find out whether this plugin wants to load on the main thread (legacy behavior), instead of the async
* loading thread.
* <p></p>
* Most plugins should use Minecraft.getInstance().executeBlocking() for their purposes, as plugins loading on the
* main thread will cause lag spikes.
* @since TODO
*/
default boolean needsLoadingOnClientThread() {
return false;
}
}
18 changes: 17 additions & 1 deletion Library/src/main/java/mezz/jei/library/load/PluginCaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

import com.google.common.base.Stopwatch;
import mezz.jei.api.IModPlugin;
import mezz.jei.common.Internal;
import mezz.jei.common.async.JeiStartTask;
import mezz.jei.library.startup.JeiStarter;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

public class PluginCaller {
Expand All @@ -26,7 +32,10 @@ public static void callOnPlugins(String title, List<IModPlugin> plugins, Consume
try {
ResourceLocation pluginUid = plugin.getPluginUid();
timer.begin(title, pluginUid);
func.accept(plugin);
if(plugin.needsLoadingOnClientThread() || isLegacyPlugin(plugin.getPluginUid())) {
Minecraft.getInstance().executeBlocking(() -> func.accept(plugin));
} else
func.accept(plugin);
timer.end();
} catch (RuntimeException | LinkageError e) {
LOGGER.error("Caught an error from mod plugin: {} {}", plugin.getClass(), plugin.getPluginUid(), e);
Expand All @@ -38,4 +47,11 @@ public static void callOnPlugins(String title, List<IModPlugin> plugins, Consume

LOGGER.info("{} took {}", title, stopwatch);
}

private static boolean isLegacyPlugin(ResourceLocation uid) {
String uidString = uid.toString();
// scales poorly if there are many plugins, but there shouldn't be as modders should support this mode
// in the worst case, this can be cached
return Internal.getJeiClientConfigs().getClientConfig().getAsyncCompatPluginUids().contains(uidString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@
import mezz.jei.library.runtime.JeiHelpers;
import mezz.jei.library.runtime.JeiRuntime;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.AbstractContainerMenu;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;

import java.nio.file.Path;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
Expand Down

0 comments on commit d4ce812

Please sign in to comment.