mirror of
https://github.com/GTNewHorizons/NotEnoughItems.git
synced 2026-11-15 17:54:50 +00:00
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
52ae4b2bd4 | ||
|
|
acacd13bd9 | ||
|
|
030795e3c7 | ||
|
|
897a22ad17 | ||
|
|
5faed21218 | ||
|
|
aa9da2e0a0 |
No files matched your search
@@ -3,9 +3,9 @@ name: Build and test
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches: [ master, main ]
|
||||
branches: [ master, main, release/** ]
|
||||
push:
|
||||
branches: [ master, main ]
|
||||
branches: [ master, main, release/** ]
|
||||
|
||||
jobs:
|
||||
build-and-test:
|
||||
|
||||
@@ -9,7 +9,6 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.GuiScreen;
|
||||
@@ -37,7 +36,6 @@ import codechicken.lib.config.ConfigTag;
|
||||
import codechicken.lib.config.ConfigTagParent;
|
||||
import codechicken.nei.api.API;
|
||||
import codechicken.nei.api.GuiInfo;
|
||||
import codechicken.nei.api.IConfigureNEI;
|
||||
import codechicken.nei.api.ItemInfo;
|
||||
import codechicken.nei.api.NEIInfo;
|
||||
import codechicken.nei.config.ConfigSet;
|
||||
@@ -1026,20 +1024,11 @@ public class NEIClientConfig {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
final Stream<Class<?>> stream = NEIClientConfig.getBooleanSetting("loadPluginsInParallel")
|
||||
? NEIClientConfig.pluginsList.parallelStream()
|
||||
: NEIClientConfig.pluginsList.stream();
|
||||
|
||||
stream.forEach(clazz -> {
|
||||
try {
|
||||
IConfigureNEI config = (IConfigureNEI) clazz.getConstructor().newInstance();
|
||||
config.loadConfig();
|
||||
NEIModContainer.plugins.add(config);
|
||||
logger.debug("Loaded {}", clazz.getName());
|
||||
} catch (Throwable e) {
|
||||
logger.error("Failed to Load {}", clazz.getName(), e);
|
||||
}
|
||||
});
|
||||
PluginLoader.load(
|
||||
NEIClientConfig.pluginsList,
|
||||
NEIClientConfig.getBooleanSetting("loadPluginsInParallel"),
|
||||
NEIModContainer.plugins,
|
||||
logger);
|
||||
|
||||
KeyManager.requestOptionsReload();
|
||||
RecipeCatalysts.loadCatalystInfo();
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package codechicken.nei;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.ForkJoinPool;
|
||||
import java.util.concurrent.ForkJoinWorkerThread;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import codechicken.nei.api.IConfigureNEI;
|
||||
|
||||
final class PluginLoader {
|
||||
|
||||
private PluginLoader() {}
|
||||
|
||||
static void load(Collection<Class<?>> plugins, boolean parallel, Collection<IConfigureNEI> loadedPlugins,
|
||||
Logger logger) {
|
||||
if (parallel && canSetContextClassLoader()) {
|
||||
// Common pool workers use the system loader, which cannot see FML-only classes (old Log4j then throws
|
||||
// while logging a plugin's stack frames), and under a SecurityManager they forbid changing it.
|
||||
final ClassLoader classLoader = IConfigureNEI.class.getClassLoader();
|
||||
final ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors(), owner -> {
|
||||
final ForkJoinWorkerThread worker = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(owner);
|
||||
worker.setContextClassLoader(classLoader);
|
||||
return worker;
|
||||
}, null, false);
|
||||
|
||||
try {
|
||||
pool.submit(() -> plugins.parallelStream().forEach(clazz -> load(clazz, loadedPlugins, logger))).join();
|
||||
} finally {
|
||||
pool.shutdown();
|
||||
}
|
||||
} else {
|
||||
if (parallel) {
|
||||
logger.warn("Context class loader cannot be changed, loading plugins sequentially");
|
||||
}
|
||||
|
||||
plugins.forEach(clazz -> load(clazz, loadedPlugins, logger));
|
||||
}
|
||||
}
|
||||
|
||||
private static void load(Class<?> clazz, Collection<IConfigureNEI> loadedPlugins, Logger logger) {
|
||||
try {
|
||||
IConfigureNEI config = (IConfigureNEI) clazz.getConstructor().newInstance();
|
||||
config.loadConfig();
|
||||
loadedPlugins.add(config);
|
||||
logger.debug("Loaded {}", clazz.getName());
|
||||
} catch (Throwable e) {
|
||||
logger.error("Failed to Load {}", clazz.getName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean canSetContextClassLoader() {
|
||||
final SecurityManager securityManager = System.getSecurityManager();
|
||||
|
||||
if (securityManager == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
securityManager.checkPermission(new RuntimePermission("setContextClassLoader"));
|
||||
return true;
|
||||
} catch (SecurityException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -343,7 +343,6 @@ public abstract class ShortcutInputHandler {
|
||||
if (handlerRef != null) {
|
||||
final GuiFavoriteButton button = new GuiFavoriteButton(handlerRef, 0, 0);
|
||||
button.saveRecipeInBookmark();
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -501,13 +501,20 @@ public class GuiContainerManager {
|
||||
public boolean lastKeyTyped(int keyID, char keyChar) {
|
||||
if (keyID == 1) return false;
|
||||
|
||||
// hotkeys go before lastKeyTyped, otherwise a third-party handler that consumes every key blocks them
|
||||
if (handleHotkeys()) return true;
|
||||
|
||||
for (IContainerInputHandler inputhander : inputHandlers)
|
||||
if (inputhander.lastKeyTyped(window, keyChar, keyID)) return true;
|
||||
|
||||
return onHotkeyPressed();
|
||||
return handleScreenHotkeys();
|
||||
}
|
||||
|
||||
public boolean onHotkeyPressed() {
|
||||
return handleHotkeys() || handleScreenHotkeys();
|
||||
}
|
||||
|
||||
private boolean handleHotkeys() {
|
||||
|
||||
if (window instanceof IGuiHotkeyListener && ((IGuiHotkeyListener) window).onHotkeyPressed()) {
|
||||
return true;
|
||||
@@ -519,6 +526,10 @@ public class GuiContainerManager {
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean handleScreenHotkeys() {
|
||||
return window instanceof IGuiHotkeyListener && ((IGuiHotkeyListener) window).onScreenHotkeyPressed();
|
||||
}
|
||||
|
||||
|
||||
@@ -123,6 +123,11 @@ public class AutoCraftingManager {
|
||||
AutoCraftingManager.math = math;
|
||||
|
||||
if (AutoCraftingManager.math != null) {
|
||||
|
||||
if (NEIClientUtils.getGuiContainer() instanceof GuiRecipe<?>gui && gui.firstGui != null) {
|
||||
gui.mc.displayGuiScreen(gui.firstGui);
|
||||
}
|
||||
|
||||
ItemPanels.bookmarkPanel.clearTooltips();
|
||||
math.createMasterRoot();
|
||||
task.restart();
|
||||
|
||||
@@ -149,6 +149,7 @@ public class RecipeHandlerRef {
|
||||
if (overlayHandler != null && overlayHandler.canCraft(firstGui, this.handler, this.recipeIndex)) {
|
||||
|
||||
if (!firstGui.equals(firstGui.mc.currentScreen)) {
|
||||
if (!firstGui.mc.func_152345_ab()) return false;
|
||||
firstGui.mc.displayGuiScreen(firstGui);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,11 +3,9 @@ package codechicken.nei.recipe.chain;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import net.minecraft.client.gui.inventory.GuiContainer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
@@ -23,7 +21,6 @@ import codechicken.nei.ItemSorter;
|
||||
import codechicken.nei.ItemStackAmount;
|
||||
import codechicken.nei.ItemsTooltipLineHandler;
|
||||
import codechicken.nei.ItemsTooltipLineHandler.AmountRenderer;
|
||||
import codechicken.nei.ItemsTooltipLineHandler.StacksAmountRenderer;
|
||||
import codechicken.nei.ItemsTooltipLineHandler.TotalAmountRenderer;
|
||||
import codechicken.nei.NEIClientConfig;
|
||||
import codechicken.nei.NEIClientUtils;
|
||||
@@ -39,25 +36,6 @@ import codechicken.nei.recipe.StackInfo;
|
||||
|
||||
public class RecipeChainTooltipLineHandler extends CompositeTooltipLineHandler {
|
||||
|
||||
protected static class MissingStacksAmountRenderer extends StacksAmountRenderer {
|
||||
|
||||
protected static final int MISSING_COLOR = 0xFF5555;
|
||||
|
||||
protected final Set<NBTTagCompound> missing;
|
||||
private final Map<ItemStack, Boolean> cache = new IdentityHashMap<>();
|
||||
|
||||
public MissingStacksAmountRenderer(Set<NBTTagCompound> missing) {
|
||||
this.missing = missing;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getColor(ItemStack stack) {
|
||||
return this.cache.computeIfAbsent(stack, key -> this.missing.contains(StackInfo.itemStackToNBT(key, false)))
|
||||
? MISSING_COLOR
|
||||
: super.getColor(stack);
|
||||
}
|
||||
}
|
||||
|
||||
protected static class HandlerIconRenderer extends TotalAmountRenderer {
|
||||
|
||||
protected static final int ICON_SIZE = 16;
|
||||
@@ -285,35 +263,15 @@ public class RecipeChainTooltipLineHandler extends CompositeTooltipLineHandler {
|
||||
}
|
||||
}
|
||||
|
||||
final Set<NBTTagCompound> missing = new HashSet<>();
|
||||
|
||||
if (this.lastShiftKey) {
|
||||
for (ItemStack stack : inputs) {
|
||||
missing.add(StackInfo.itemStackToNBT(stack, false));
|
||||
}
|
||||
}
|
||||
|
||||
// items with a shortage are already in the inputs list, holding the missing amount instead of the required one
|
||||
final List<ItemStack> covered = new ArrayList<>();
|
||||
|
||||
for (ItemStack stack : available) {
|
||||
if (!missing.contains(StackInfo.itemStackToNBT(stack, false))) {
|
||||
covered.add(stack);
|
||||
}
|
||||
}
|
||||
|
||||
final Comparator<ItemStack> comparator = Comparator
|
||||
.comparing((ItemStack stack) -> StackInfo.getFluid(stack) != null)
|
||||
.thenComparingInt(stack -> -1 * stack.stackSize).thenComparing(ItemSorter.instance);
|
||||
|
||||
inputs.sort(comparator);
|
||||
covered.sort(comparator);
|
||||
available.sort(comparator);
|
||||
outputs.sort(comparator);
|
||||
remainder.sort(comparator);
|
||||
|
||||
// missing items stay on top of the list
|
||||
inputs.addAll(covered);
|
||||
|
||||
if (this.lastShiftKey) {
|
||||
outputs.addAll(remainder);
|
||||
}
|
||||
@@ -332,12 +290,21 @@ public class RecipeChainTooltipLineHandler extends CompositeTooltipLineHandler {
|
||||
EnumChatFormatting.BLUE,
|
||||
new HandlerIconRenderer(craftingHandlers));
|
||||
|
||||
if (this.lastShiftKey) {
|
||||
addLine(
|
||||
NEIClientUtils.translate("bookmark.crafting_chain.missing"),
|
||||
inputs,
|
||||
true,
|
||||
EnumChatFormatting.RED,
|
||||
new TotalAmountRenderer());
|
||||
}
|
||||
|
||||
addLine(
|
||||
NEIClientUtils.translate("bookmark.crafting_chain.input"),
|
||||
inputs,
|
||||
this.lastShiftKey ? available : inputs,
|
||||
this.lastShiftKey,
|
||||
EnumChatFormatting.YELLOW,
|
||||
this.lastShiftKey ? new MissingStacksAmountRenderer(missing) : new TotalAmountRenderer());
|
||||
new TotalAmountRenderer());
|
||||
}
|
||||
|
||||
protected static void putHandlerInfo(Map<NBTTagCompound, HandlerInfo> handlers, ItemStack stack,
|
||||
|
||||
@@ -48,6 +48,7 @@ nei.bookmark.recipe.toggle_collapse=Toggle Collapse/Expand
|
||||
nei.bookmark.recipe.chat_link=Send Recipe to Chat
|
||||
nei.bookmark.crafting_chain=Crafting Chain
|
||||
nei.bookmark.crafting_chain.input=Ingredients
|
||||
nei.bookmark.crafting_chain.missing=Missing Items
|
||||
nei.bookmark.crafting_chain.needed=Required Crafts
|
||||
nei.bookmark.tree.title=Crafting Tree
|
||||
nei.bookmark.tree.empty=No crafting chain to display
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
package codechicken.nei;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.ForkJoinPool;
|
||||
import java.util.concurrent.ForkJoinWorkerThread;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import codechicken.nei.api.IConfigureNEI;
|
||||
|
||||
class PluginLoaderTest {
|
||||
|
||||
@Test
|
||||
void loadsPluginsInParallelAndLogsFailuresWithTheGameClassLoader() throws Exception {
|
||||
ClassLoader workerClassLoader = new ClassLoader(null) {};
|
||||
Set<Thread> workers = ConcurrentHashMap.newKeySet();
|
||||
ForkJoinPool pool = new ForkJoinPool(2, owner -> {
|
||||
ForkJoinWorkerThread worker = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(owner);
|
||||
worker.setContextClassLoader(workerClassLoader);
|
||||
workers.add(worker);
|
||||
return worker;
|
||||
}, null, false);
|
||||
Logger logger = mock(Logger.class);
|
||||
doAnswer(invocation -> {
|
||||
assertSame(IConfigureNEI.class.getClassLoader(), Thread.currentThread().getContextClassLoader());
|
||||
return null;
|
||||
}).when(logger).error(eq("Failed to Load {}"), anyString(), any(Throwable.class));
|
||||
|
||||
List<Class<?>> plugins = new ArrayList<>();
|
||||
for (int i = 0; i < 16; i++) {
|
||||
plugins.add(HealthyPlugin.class);
|
||||
plugins.add(ConstructorFailurePlugin.class);
|
||||
plugins.add(ConfigurationFailurePlugin.class);
|
||||
}
|
||||
List<IConfigureNEI> loaded = new CopyOnWriteArrayList<>();
|
||||
|
||||
try {
|
||||
pool.submit(() -> PluginLoader.load(plugins, true, loaded, logger)).get(10, TimeUnit.SECONDS);
|
||||
|
||||
assertEquals(16, loaded.size());
|
||||
verify(logger, times(32)).error(eq("Failed to Load {}"), anyString(), any(Throwable.class));
|
||||
workers.forEach(worker -> assertSame(workerClassLoader, worker.getContextClassLoader()));
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
public static class HealthyPlugin implements IConfigureNEI {
|
||||
|
||||
public HealthyPlugin() {
|
||||
checkClassLoader();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadConfig() {
|
||||
checkClassLoader();
|
||||
}
|
||||
|
||||
private static void checkClassLoader() {
|
||||
if (Thread.currentThread().getContextClassLoader() != IConfigureNEI.class.getClassLoader()) {
|
||||
throw new IllegalStateException("Plugin cannot see the game classloader");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Test plugin";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getVersion() {
|
||||
return "1";
|
||||
}
|
||||
}
|
||||
|
||||
public static class ConstructorFailurePlugin extends HealthyPlugin {
|
||||
|
||||
public ConstructorFailurePlugin() {
|
||||
throw new IllegalStateException("Constructor failed");
|
||||
}
|
||||
}
|
||||
|
||||
public static class ConfigurationFailurePlugin extends HealthyPlugin {
|
||||
|
||||
@Override
|
||||
public void loadConfig() {
|
||||
super.loadConfig();
|
||||
throw new IllegalStateException("Configuration failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user