diff options
| author | shedaniel <daniel@shedaniel.me> | 2021-10-21 23:36:28 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2021-10-22 01:41:20 +0800 |
| commit | 62045c9cc0416557e6b57b6f4101b98a0da02dde (patch) | |
| tree | 7361edee07b0fae370b0006de5c5e2b219a67408 /runtime | |
| parent | 843a29c5fcf8d20f7073438d9fbed7039dead719 (diff) | |
| download | RoughlyEnoughItems-62045c9cc0416557e6b57b6f4101b98a0da02dde.tar.gz RoughlyEnoughItems-62045c9cc0416557e6b57b6f4101b98a0da02dde.tar.bz2 RoughlyEnoughItems-62045c9cc0416557e6b57b6f4101b98a0da02dde.zip | |
Fix most of the issues in #643
Diffstat (limited to 'runtime')
11 files changed, 72 insertions, 25 deletions
diff --git a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCoreClient.java b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCoreClient.java index a411fcb77..9f0b4552f 100644 --- a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCoreClient.java +++ b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCoreClient.java @@ -118,6 +118,9 @@ public class RoughlyEnoughItemsCoreClient { private static final ExecutorService RELOAD_PLUGINS = Executors.newSingleThreadScheduledExecutor(task -> { Thread thread = new Thread(task, "REI-ReloadPlugins"); thread.setDaemon(true); + thread.setUncaughtExceptionHandler(($, exception) -> { + RoughlyEnoughItemsCore.LOGGER.throwException(exception); + }); return thread; }); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/ErrorDisplayer.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/ErrorDisplayer.java index 12bf9da50..7fb13845a 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/ErrorDisplayer.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/ErrorDisplayer.java @@ -29,7 +29,7 @@ import me.shedaniel.rei.impl.client.gui.screen.WarningAndErrorScreen; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screens.Screen; -import java.util.function.Consumer; +import java.util.function.UnaryOperator; public class ErrorDisplayer { public void onInitializeClient() { @@ -46,18 +46,15 @@ public class ErrorDisplayer { } }); warningAndErrorScreen.setParent(screen); - try { - if (minecraft.screen != null) minecraft.screen.removed(); - } catch (Throwable ignored) { - } - minecraft.screen = null; - minecraft.setScreen(warningAndErrorScreen); + return warningAndErrorScreen; } + + return null; }); } @ExpectPlatform - public static void registerGuiInit(Consumer<Screen> consumer) { + public static void registerGuiInit(UnaryOperator<Screen> consumer) { throw new AssertionError(); } } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/DefaultDisplayViewingScreen.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/DefaultDisplayViewingScreen.java index 55be0672d..5e341d3b9 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/DefaultDisplayViewingScreen.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/DefaultDisplayViewingScreen.java @@ -157,7 +157,7 @@ public class DefaultDisplayViewingScreen extends AbstractDisplayViewingScreen { int largestHeight = Math.max(height - 34 - 30, 100); int maxWidthDisplay = CollectionUtils.mapAndMax(getCurrentDisplayed(), getCurrentCategory()::getDisplayWidth, Comparator.naturalOrder()).orElse(150); int maxHeight = Math.min(largestHeight, CollectionUtils.<DisplayCategory<?>, Integer>mapAndMax(categories, - category -> (category.getDisplayHeight() + 4) * Math.max(1, getRecipesPerPage(largestHeight, category) + 1) + 36, Comparator.naturalOrder()).orElse(66)); + category -> (category.getDisplayHeight() + 4) * Math.max(1, Math.min(getRecipesPerPage(largestHeight, category) + 1, Math.max(categoryMap.get(category).size(), ConfigObject.getInstance().getMaxRecipePerPage()))) + 36, Comparator.naturalOrder()).orElse(66)); int totalDisplayHeight = (getCurrentCategory().getDisplayHeight() + 4) * Math.max(1, getRecipesPerPage(maxHeight, getCurrentCategory()) + 1) + 36; int guiWidth = Math.max(maxWidthDisplay + 10, 190); this.bounds = new Rectangle(width / 2 - guiWidth / 2, height / 2 - maxHeight / 2, guiWidth, maxHeight); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/screen/ExclusionZonesImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/screen/ExclusionZonesImpl.java index d95d6f8bc..606523bb7 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/screen/ExclusionZonesImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/screen/ExclusionZonesImpl.java @@ -27,10 +27,12 @@ import com.google.common.collect.HashMultimap; import com.google.common.collect.Lists; import com.google.common.collect.Multimap; import me.shedaniel.math.Rectangle; +import me.shedaniel.rei.RoughlyEnoughItemsCore; import me.shedaniel.rei.api.client.gui.config.DisplayPanelLocation; import me.shedaniel.rei.api.client.registry.screen.ExclusionZones; import me.shedaniel.rei.api.client.registry.screen.ExclusionZonesProvider; import me.shedaniel.rei.api.client.registry.screen.ScreenRegistry; +import me.shedaniel.rei.api.common.plugins.PluginManager; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.Minecraft; @@ -50,7 +52,7 @@ public class ExclusionZonesImpl implements ExclusionZones { private static final Comparator<? super Rectangle> RECTANGLE_COMPARER = Comparator.comparingLong(Rectangle::hashCode); private long lastArea = -1; - private Multimap<Class<?>, Supplier<Collection<Rectangle>>> list = HashMultimap.create(); + private final Multimap<Class<?>, Supplier<Collection<Rectangle>>> list = HashMultimap.create(); @Override public <R extends Screen> boolean isHandingScreen(Class<R> screen) { @@ -66,12 +68,14 @@ public class ExclusionZonesImpl implements ExclusionZones { public InteractionResult isInZone(double mouseX, double mouseY) { Class<? extends Screen> screenClass = Minecraft.getInstance().screen.getClass(); - for (Map.Entry<Class<?>, Collection<Supplier<Collection<Rectangle>>>> collectionEntry : list.asMap().entrySet()) { - if (collectionEntry.getKey().isAssignableFrom(screenClass)) { - for (Supplier<Collection<Rectangle>> listSupplier : collectionEntry.getValue()) { - for (Rectangle zone : listSupplier.get()) { - if (zone.contains(mouseX, mouseY)) { - return InteractionResult.FAIL; + synchronized (list) { + for (Map.Entry<Class<?>, Collection<Supplier<Collection<Rectangle>>>> collectionEntry : list.asMap().entrySet()) { + if (collectionEntry.getKey().isAssignableFrom(screenClass)) { + for (Supplier<Collection<Rectangle>> listSupplier : collectionEntry.getValue()) { + for (Rectangle zone : listSupplier.get()) { + if (zone.contains(mouseX, mouseY)) { + return InteractionResult.FAIL; + } } } } @@ -98,10 +102,12 @@ public class ExclusionZonesImpl implements ExclusionZones { @Override public List<Rectangle> getExclusionZones(Class<?> currentScreenClass, boolean sort) { List<Rectangle> rectangles = Lists.newArrayList(); - for (Map.Entry<Class<?>, Collection<Supplier<Collection<Rectangle>>>> collectionEntry : list.asMap().entrySet()) { - if (collectionEntry.getKey().isAssignableFrom(currentScreenClass)) { - for (Supplier<Collection<Rectangle>> listSupplier : collectionEntry.getValue()) { - rectangles.addAll(listSupplier.get()); + synchronized (list) { + for (Map.Entry<Class<?>, Collection<Supplier<Collection<Rectangle>>>> collectionEntry : list.asMap().entrySet()) { + if (collectionEntry.getKey().isAssignableFrom(currentScreenClass)) { + for (Supplier<Collection<Rectangle>> listSupplier : collectionEntry.getValue()) { + rectangles.addAll(listSupplier.get()); + } } } } @@ -118,7 +124,13 @@ public class ExclusionZonesImpl implements ExclusionZones { @Override public <T> void register(Class<? extends T> screenClass, ExclusionZonesProvider<? extends T> provider) { - list.put(screenClass, () -> ((ExclusionZonesProvider<T>) provider).provide((T) Minecraft.getInstance().screen)); + synchronized (list) { + list.put(screenClass, () -> ((ExclusionZonesProvider<T>) provider).provide((T) Minecraft.getInstance().screen)); + } + + if (!PluginManager.areAnyReloading()) { + RoughlyEnoughItemsCore.LOGGER.warn("Detected ExclusionZonesImpl modification at runtime, this may cause issues, a single ExclusionZonesProvider can dynamically provide boundaries instead!", new RuntimeException()); + } } private long areasHashCode(Rectangle rectangle, List<Rectangle> exclusionZones) { diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java index 46ba3eff0..0710d0a17 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java @@ -38,6 +38,7 @@ import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.display.Display; import me.shedaniel.rei.api.common.entry.EntryIngredient; import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.plugins.PluginManager; import me.shedaniel.rei.api.common.transfer.info.MenuInfo; import me.shedaniel.rei.api.common.transfer.info.MenuInfoContext; import me.shedaniel.rei.api.common.transfer.info.MenuInfoRegistry; @@ -58,6 +59,11 @@ import java.util.stream.Collectors; @ApiStatus.Internal public class ViewsImpl implements Views { public static Map<DisplayCategory<?>, List<Display>> buildMapFor(ViewSearchBuilder builder) { + if (PluginManager.areAnyReloading()) { + RoughlyEnoughItemsCore.LOGGER.info("Cancelled Views buildMap since plugins have not finished reloading."); + return Maps.newLinkedHashMap(); + } + Stopwatch stopwatch = Stopwatch.createStarted(); Set<CategoryIdentifier<?>> categories = builder.getCategories(); List<EntryStack<?>> recipesFor = builder.getRecipesFor(); @@ -131,11 +137,11 @@ public class ViewsImpl implements Views { if (CategoryRegistry.getInstance().isCategoryInvisible(category)) continue; Set<Display> set = new LinkedHashSet<>(); generatorsCount += entry.getValue().size(); - + for (DynamicDisplayGenerator<Display> generator : (List<DynamicDisplayGenerator<Display>>) (List<? extends DynamicDisplayGenerator<?>>) entry.getValue()) { generateLiveDisplays(displayRegistry, generator, builder, set::add); } - + if (!set.isEmpty()) { CollectionUtils.getOrPutEmptyList(result, category).addAll(set); } @@ -194,6 +200,10 @@ public class ViewsImpl implements Views { @Override public Collection<EntryStack<?>> findCraftableEntriesByMaterials() { + if (PluginManager.areAnyReloading()) { + return Collections.emptySet(); + } + AbstractContainerMenu menu = Minecraft.getInstance().player.containerMenu; Set<EntryStack<?>> craftables = new HashSet<>(); for (Map.Entry<CategoryIdentifier<?>, List<Display>> entry : DisplayRegistry.getInstance().getAll().entrySet()) { diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/type/EntryRegistryImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/type/EntryRegistryImpl.java index 024067b13..e91784f9f 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/type/EntryRegistryImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/type/EntryRegistryImpl.java @@ -31,6 +31,7 @@ import me.shedaniel.rei.api.client.config.ConfigObject; import me.shedaniel.rei.api.client.plugins.REIClientPlugin; import me.shedaniel.rei.api.client.registry.entry.EntryRegistry; import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.plugins.PluginManager; import me.shedaniel.rei.api.common.registry.ReloadStage; import me.shedaniel.rei.api.common.util.CollectionUtils; import me.shedaniel.rei.api.common.util.EntryStacks; @@ -106,6 +107,10 @@ public class EntryRegistryImpl implements EntryRegistry { @Override public List<EntryStack<?>> getPreFilteredList() { + if (PluginManager.areAnyReloading()) { + return Collections.emptyList(); + } + return Collections.unmodifiableList(preFilteredList); } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/logging/FileLogger.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/logging/FileLogger.java index 13428d377..ed0793f05 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/logging/FileLogger.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/logging/FileLogger.java @@ -29,7 +29,6 @@ import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.StandardOpenOption; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @@ -48,6 +47,11 @@ public class FileLogger implements Logger { } @Override + public void throwException(Throwable throwable) { + throwable.printStackTrace(new PrintWriter(writer, true)); + } + + @Override public void log(Level level, String message) { message = String.format("[%s] [%s/%s] %s", DATE_TIME_FORMATTER.format(LocalDateTime.now()), Thread.currentThread().getName(), level, message); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/logging/Log4JLogger.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/logging/Log4JLogger.java index 6faef4dc4..4ce490db1 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/logging/Log4JLogger.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/logging/Log4JLogger.java @@ -33,6 +33,11 @@ public class Log4JLogger implements Logger { } @Override + public void throwException(Throwable throwable) { + logger.throwing(throwable); + } + + @Override public void log(Level level, String message) { logger.log(level, message); } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/logging/Logger.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/logging/Logger.java index dcc0ef098..f5745ba02 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/logging/Logger.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/logging/Logger.java @@ -26,6 +26,8 @@ package me.shedaniel.rei.impl.common.logging; import org.apache.logging.log4j.Level; public interface Logger { + void throwException(Throwable throwable); + void log(Level level, String message); void log(Level level, String message, Throwable throwable); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/logging/MultiLogger.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/logging/MultiLogger.java index d333d4ba2..ac890dd36 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/logging/MultiLogger.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/logging/MultiLogger.java @@ -33,6 +33,13 @@ public class MultiLogger implements Logger { } @Override + public void throwException(Throwable throwable) { + for (Logger logger : loggers) { + logger.throwException(throwable); + } + } + + @Override public void log(Level level, String message) { for (Logger logger : loggers) { logger.log(level, message); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/util/IssuesDetector.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/util/IssuesDetector.java index bac6839f5..e2dc05461 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/util/IssuesDetector.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/util/IssuesDetector.java @@ -52,8 +52,10 @@ public final class IssuesDetector { if (ignoreFile.exists()) { return; } + RoughlyEnoughItemsState.warn(issue.getRight()); + } else { + RoughlyEnoughItemsState.error(issue.getRight()); } - RoughlyEnoughItemsState.warn(issue.getRight()); RoughlyEnoughItemsState.onContinue(() -> { try { if (issue.getMiddle() != null) { |
