diff options
| author | shedaniel <daniel@shedaniel.me> | 2021-10-16 00:37:19 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2021-10-16 00:50:42 +0800 |
| commit | 6e7a641926d8b0ea2d0a1caddbf348fe24b669ce (patch) | |
| tree | 58820c892f893b7a53eeb33448f5936a70469433 /runtime | |
| parent | 3a84bfa28bb233b3a6f0a14f2e90956a98512453 (diff) | |
| download | RoughlyEnoughItems-6e7a641926d8b0ea2d0a1caddbf348fe24b669ce.tar.gz RoughlyEnoughItems-6e7a641926d8b0ea2d0a1caddbf348fe24b669ce.tar.bz2 RoughlyEnoughItems-6e7a641926d8b0ea2d0a1caddbf348fe24b669ce.zip | |
Fix JEI recipes duplicating and going to the wrong category, Fix JER's insane resource reload during plugin application, Add DisplayAdditionReason, Safeguard DisplayCategory#setupDisplay
Diffstat (limited to 'runtime')
5 files changed, 122 insertions, 62 deletions
diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/AbstractDisplayViewingScreen.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/AbstractDisplayViewingScreen.java index 0d69d3fce..48aea3c1a 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/AbstractDisplayViewingScreen.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/AbstractDisplayViewingScreen.java @@ -24,6 +24,7 @@ package me.shedaniel.rei.impl.client.gui.screen; import com.google.common.collect.Lists; +import me.shedaniel.architectury.fluid.FluidStack; import me.shedaniel.math.Rectangle; import me.shedaniel.rei.api.client.gui.screen.DisplayScreen; import me.shedaniel.rei.api.client.gui.widgets.Slot; @@ -33,12 +34,20 @@ import me.shedaniel.rei.api.client.registry.display.DisplayCategory; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.display.Display; import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.entry.type.EntryType; +import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; import me.shedaniel.rei.api.common.util.CollectionUtils; import me.shedaniel.rei.impl.client.ClientHelperImpl; import me.shedaniel.rei.impl.client.gui.widget.EntryWidget; +import net.minecraft.client.Minecraft; import net.minecraft.client.gui.chat.NarratorChatListener; import net.minecraft.client.gui.components.events.GuiEventListener; import net.minecraft.client.gui.screens.Screen; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.tags.Tag; +import net.minecraft.tags.TagCollection; +import net.minecraft.tags.TagContainer; +import net.minecraft.world.item.ItemStack; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; @@ -151,4 +160,35 @@ public abstract class AbstractDisplayViewingScreen extends Screen implements Dis } } } + + protected void setupTags(List<Widget> widgets) { + TagContainer tags = Minecraft.getInstance().getConnection().getTags(); + outer: + for (EntryWidget widget : Widgets.<EntryWidget>walk(widgets, EntryWidget.class::isInstance)) { + widget.removeTagMatch = false; + if (widget.getEntries().size() <= 1) continue; + EntryType<?> type = null; + for (EntryStack<?> entry : widget.getEntries()) { + if (type == null) { + type = entry.getType(); + } else if (type != entry.getType()) { + continue outer; + } + } + // TODO: Don't hardcode + TagCollection<?> collection; + List<Object> objects; + if (type == VanillaEntryTypes.ITEM) { + collection = tags.getItems(); + objects = CollectionUtils.map(widget.getEntries(), stack -> stack.<ItemStack>castValue().getItem()); + } else if (type == VanillaEntryTypes.FLUID) { + collection = tags.getFluids(); + objects = CollectionUtils.map(widget.getEntries(), stack -> stack.<FluidStack>castValue().getFluid()); + } else continue; + Map.Entry<ResourceLocation, ? extends Tag<?>> firstOrNull = CollectionUtils.findFirstOrNull(collection.getAllTags().entrySet(), entry -> entry.getValue().getValues().equals(objects)); + if (firstOrNull != null) { + widget.tagMatch = firstOrNull.getKey(); + } + } + } } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/CompositeDisplayViewingScreen.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/CompositeDisplayViewingScreen.java index b2b86fb47..5d1d66a98 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/CompositeDisplayViewingScreen.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/CompositeDisplayViewingScreen.java @@ -47,6 +47,7 @@ import me.shedaniel.rei.api.common.display.Display; import me.shedaniel.rei.api.common.entry.EntryIngredient; import me.shedaniel.rei.impl.client.ClientHelperImpl; import me.shedaniel.rei.impl.client.REIRuntimeImpl; +import me.shedaniel.rei.impl.client.gui.widget.EntryWidget; import me.shedaniel.rei.impl.client.gui.widget.InternalWidgets; import me.shedaniel.rei.impl.client.gui.widget.TabWidget; import net.minecraft.client.Minecraft; @@ -61,6 +62,7 @@ import net.minecraft.util.Mth; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Optional; @@ -152,9 +154,22 @@ public class CompositeDisplayViewingScreen extends AbstractDisplayViewingScreen this.widgets.add(Widgets.createSlotBase(scrollListBounds)); Rectangle recipeBounds = new Rectangle(bounds.x + 100 + (guiWidth - 100) / 2 - category.getDisplayWidth(display) / 2, bounds.y + bounds.height / 2 - category.getDisplayHeight() / 2, category.getDisplayWidth(display), category.getDisplayHeight()); - List<Widget> setupDisplay = category.setupDisplay(display, recipeBounds); + List<Widget> setupDisplay; + try { + setupDisplay = category.setupDisplay(display, recipeBounds); + } catch (Throwable throwable) { + throwable.printStackTrace(); + setupDisplay = new ArrayList<>(); + setupDisplay.add(Widgets.createRecipeBase(bounds).color(0xFFBB0000)); + setupDisplay.add(Widgets.createLabel(new Point(bounds.getCenterX(), bounds.getCenterY() - 8), new TextComponent("Failed to initiate setupDisplay"))); + setupDisplay.add(Widgets.createLabel(new Point(bounds.getCenterX(), bounds.getCenterY() + 1), new TextComponent("Check console for error"))); + } + setupTags(setupDisplay); transformIngredientNotice(setupDisplay, ingredientStackToNotice); transformResultNotice(setupDisplay, resultStackToNotice); + for (EntryWidget widget : Widgets.<EntryWidget>walk(widgets, EntryWidget.class::isInstance)) { + widget.removeTagMatch = true; + } this.widgets.addAll(setupDisplay); Optional<ButtonArea> supplier = CategoryRegistry.getInstance().get(category.getCategoryIdentifier()).getPlusButtonArea(); if (supplier.isPresent() && supplier.get().get(recipeBounds) != null) 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 c190033b2..52e5b3a67 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 @@ -29,7 +29,6 @@ import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.Tesselator; import com.mojang.math.Matrix4f; -import me.shedaniel.architectury.fluid.FluidStack; import me.shedaniel.clothconfig2.api.ModifierKeyCode; import me.shedaniel.math.Point; import me.shedaniel.math.Rectangle; @@ -47,9 +46,6 @@ import me.shedaniel.rei.api.client.view.ViewSearchBuilder; 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.entry.type.EntryType; -import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; import me.shedaniel.rei.api.common.util.CollectionUtils; import me.shedaniel.rei.api.common.util.ImmutableTextComponent; import me.shedaniel.rei.impl.client.ClientHelperImpl; @@ -71,18 +67,11 @@ import net.minecraft.network.chat.TextComponent; import net.minecraft.network.chat.TranslatableComponent; import net.minecraft.resources.ResourceLocation; import net.minecraft.sounds.SoundEvents; -import net.minecraft.tags.Tag; -import net.minecraft.tags.TagCollection; -import net.minecraft.tags.TagContainer; import net.minecraft.util.Mth; -import net.minecraft.world.item.ItemStack; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; -import java.util.Comparator; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.util.*; import java.util.function.Supplier; @ApiStatus.Internal @@ -253,7 +242,16 @@ public class DefaultDisplayViewingScreen extends AbstractDisplayViewingScreen { final Supplier<Display> displaySupplier = () -> display; int displayWidth = getCurrentCategory().getDisplayWidth(displaySupplier.get()); final Rectangle displayBounds = new Rectangle(getBounds().getCenterX() - displayWidth / 2, getBounds().getCenterY() + 16 - recipeHeight * (getRecipesPerPage() + 1) / 2 - 2 * (getRecipesPerPage() + 1) + recipeHeight * i + 4 * i, displayWidth, recipeHeight); - List<Widget> setupDisplay = getCurrentCategory().setupDisplay(display, displayBounds); + List<Widget> setupDisplay; + try { + setupDisplay = getCurrentCategory().setupDisplay(display, displayBounds); + } catch (Throwable throwable) { + throwable.printStackTrace(); + setupDisplay = new ArrayList<>(); + setupDisplay.add(Widgets.createRecipeBase(bounds).color(0xFFBB0000)); + setupDisplay.add(Widgets.createLabel(new Point(bounds.getCenterX(), bounds.getCenterY() - 8), new TextComponent("Failed to initiate setupDisplay"))); + setupDisplay.add(Widgets.createLabel(new Point(bounds.getCenterX(), bounds.getCenterY() + 1), new TextComponent("Check console for error"))); + } setupTags(setupDisplay); transformIngredientNotice(setupDisplay, ingredientStackToNotice); transformResultNotice(setupDisplay, resultStackToNotice); @@ -299,37 +297,6 @@ public class DefaultDisplayViewingScreen extends AbstractDisplayViewingScreen { _children().addAll(preWidgets); } - private void setupTags(List<Widget> widgets) { - TagContainer tags = Minecraft.getInstance().getConnection().getTags(); - outer: - for (EntryWidget widget : Widgets.<EntryWidget>walk(widgets, EntryWidget.class::isInstance)) { - widget.removeTagMatch = false; - if (widget.getEntries().size() <= 1) continue; - EntryType<?> type = null; - for (EntryStack<?> entry : widget.getEntries()) { - if (type == null) { - type = entry.getType(); - } else if (type != entry.getType()) { - continue outer; - } - } - // TODO: Don't hardcode - TagCollection<?> collection; - List<Object> objects; - if (type == VanillaEntryTypes.ITEM) { - collection = tags.getItems(); - objects = CollectionUtils.map(widget.getEntries(), stack -> stack.<ItemStack>castValue().getItem()); - } else if (type == VanillaEntryTypes.FLUID) { - collection = tags.getFluids(); - objects = CollectionUtils.map(widget.getEntries(), stack -> stack.<FluidStack>castValue().getFluid()); - } else continue; - Map.Entry<ResourceLocation, ? extends Tag<?>> firstOrNull = CollectionUtils.findFirstOrNull(collection.getAllTags().entrySet(), entry -> entry.getValue().getValues().equals(objects)); - if (firstOrNull != null) { - widget.tagMatch = firstOrNull.getKey(); - } - } - } - public List<Widget> getWidgets() { return widgets; } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java index e638f14fd..bfc975188 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java @@ -31,6 +31,8 @@ import me.shedaniel.rei.api.client.registry.category.CategoryRegistry; import me.shedaniel.rei.api.client.registry.display.DisplayCategory; import me.shedaniel.rei.api.client.registry.display.DisplayRegistry; import me.shedaniel.rei.api.client.registry.display.DynamicDisplayGenerator; +import me.shedaniel.rei.api.client.registry.display.reason.DisplayAdditionReason; +import me.shedaniel.rei.api.client.registry.display.reason.DisplayAdditionReasons; import me.shedaniel.rei.api.client.registry.display.visibility.DisplayVisibilityPredicate; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.display.Display; @@ -41,6 +43,7 @@ import org.jetbrains.annotations.Nullable; import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.BiPredicate; import java.util.function.Function; import java.util.function.Predicate; @@ -140,8 +143,13 @@ public class DisplayRegistryImpl extends RecipeManagerContextImpl<REIClientPlugi } @Override + public <T, D extends Display> void registerFiller(Class<T> typeClass, BiPredicate<? extends T, DisplayAdditionReasons> predicate, Function<? extends T, D> filler) { + fillers.add(new DisplayFiller<>((o, s) -> typeClass.isInstance(o) && ((BiPredicate<Object, DisplayAdditionReasons>) predicate).test(o, s), (Function<Object, D>) filler)); + } + + @Override public <D extends Display> void registerFiller(Predicate<?> predicate, Function<?, D> filler) { - fillers.add(new DisplayFiller<>((Predicate<Object>) predicate, (Function<Object, D>) filler)); + fillers.add(new DisplayFiller<>((o, s) -> ((Predicate<Object>) predicate).test(o), (Function<Object, D>) filler)); } @Override @@ -160,17 +168,18 @@ public class DisplayRegistryImpl extends RecipeManagerContextImpl<REIClientPlugi List<Recipe<?>> allSortedRecipes = getAllSortedRecipes(); for (int i = allSortedRecipes.size() - 1; i >= 0; i--) { Recipe<?> recipe = allSortedRecipes.get(i); - add(recipe); + addWithReason(recipe, DisplayAdditionReason.RECIPE_MANAGER); } } } @Override - public <T> Collection<Display> tryFillDisplay(T value) { + public <T> Collection<Display> tryFillDisplay(T value, DisplayAdditionReason... reason) { if (value instanceof Display) return Collections.singleton((Display) value); List<Display> displays = null; + DisplayAdditionReasons reasons = reason.length == 0 ? DisplayAdditionReasons.Impl.EMPTY : new DisplayAdditionReasons.Impl(reason); for (DisplayFiller<?> filler : fillers) { - Display display = tryFillDisplayGenerics(filler, value); + Display display = tryFillDisplayGenerics(filler, value, reasons); if (display != null) { if (displays == null) displays = Collections.singletonList(display); else { @@ -185,9 +194,9 @@ public class DisplayRegistryImpl extends RecipeManagerContextImpl<REIClientPlugi return Collections.emptyList(); } - private <D extends Display> D tryFillDisplayGenerics(DisplayFiller<D> filler, Object value) { + private <D extends Display> D tryFillDisplayGenerics(DisplayFiller<D> filler, Object value, DisplayAdditionReasons reasons) { try { - if (filler.predicate.test(value)) { + if (filler.predicate.test(value, reasons)) { return filler.mappingFunction.apply(value); } } catch (Throwable e) { @@ -204,7 +213,7 @@ public class DisplayRegistryImpl extends RecipeManagerContextImpl<REIClientPlugi } private static record DisplayFiller<D extends Display>( - Predicate<Object> predicate, + BiPredicate<Object, DisplayAdditionReasons> predicate, Function<Object, D> mappingFunction ) {} diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/plugins/PluginManagerImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/plugins/PluginManagerImpl.java index 82bc8f518..f3421e76e 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/plugins/PluginManagerImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/plugins/PluginManagerImpl.java @@ -23,10 +23,13 @@ package me.shedaniel.rei.impl.common.plugins; -import com.google.common.base.MoreObjects; import com.google.common.base.Stopwatch; import com.google.common.collect.FluentIterable; import com.google.common.collect.Iterables; +import me.shedaniel.architectury.platform.Platform; +import me.shedaniel.architectury.utils.Env; +import me.shedaniel.architectury.utils.EnvExecutor; +import me.shedaniel.architectury.utils.GameInstance; import me.shedaniel.rei.RoughlyEnoughItemsCore; import me.shedaniel.rei.api.common.plugins.PluginManager; import me.shedaniel.rei.api.common.plugins.PluginView; @@ -36,8 +39,11 @@ import me.shedaniel.rei.api.common.registry.ReloadStage; import me.shedaniel.rei.api.common.registry.Reloadable; import me.shedaniel.rei.api.common.util.CollectionUtils; import net.minecraft.Util; +import net.minecraft.client.Minecraft; +import net.minecraft.server.MinecraftServer; import org.apache.commons.lang3.tuple.MutablePair; import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; import java.io.Closeable; import java.util.*; @@ -103,7 +109,7 @@ public class PluginManagerImpl<P extends REIPlugin<?>> implements PluginManager< @Override public void registerPlugin(REIPluginProvider<? extends P> plugin) { plugins.add((REIPluginProvider<P>) plugin); - RoughlyEnoughItemsCore.LOGGER.info("Registered plugin provider %s for %s", plugin.getPluginProviderName(), pluginClass.getSimpleName()); + RoughlyEnoughItemsCore.LOGGER.info("Registered plugin provider %s for %s", plugin.getPluginProviderName(), name(pluginClass)); } @Override @@ -139,23 +145,40 @@ public class PluginManagerImpl<P extends REIPlugin<?>> implements PluginManager< return new SectionClosable(sectionData, section); } - private void pluginSection(MutablePair<Stopwatch, String> sectionData, String sectionName, List<P> list, Consumer<P> consumer) { + private void pluginSection(MutablePair<Stopwatch, String> sectionData, String sectionName, List<P> list, @Nullable Reloadable<?> reloadable, Consumer<P> consumer) { for (P plugin : list) { try (SectionClosable section = section(sectionData, sectionName + " for " + plugin.getPluginProviderName())) { - consumer.accept(plugin); + if (reloadable == null || !plugin.shouldBeForcefullyDoneOnMainThread(reloadable)) { + consumer.accept(plugin); + } else if (Platform.getEnvironment() == Env.CLIENT) { + EnvExecutor.runInEnv(Env.CLIENT, () -> () -> queueExecutionClient(() -> consumer.accept(plugin))); + } else { + queueExecution(() -> consumer.accept(plugin)); + } } catch (Throwable throwable) { RoughlyEnoughItemsCore.LOGGER.error(plugin.getPluginProviderName() + " plugin failed to " + sectionName + "!", throwable); } } } + private void queueExecution(Runnable runnable) { + MinecraftServer server = GameInstance.getServer(); + if (server != null) { + server.executeBlocking(runnable); + } + } + + private void queueExecutionClient(Runnable runnable) { + Minecraft.getInstance().executeBlocking(runnable); + } + @Override public void pre() { List<P> plugins = new ArrayList<>(getPlugins().toList()); plugins.sort(Comparator.comparingDouble(P::getPriority).reversed()); Collections.reverse(plugins); MutablePair<Stopwatch, String> sectionData = new MutablePair<>(Stopwatch.createUnstarted(), ""); - pluginSection(sectionData, "pre-register", plugins, REIPlugin::preRegister); + pluginSection(sectionData, "pre-register", plugins, null, REIPlugin::preRegister); } @Override @@ -164,7 +187,13 @@ public class PluginManagerImpl<P extends REIPlugin<?>> implements PluginManager< plugins.sort(Comparator.comparingDouble(P::getPriority).reversed()); Collections.reverse(plugins); MutablePair<Stopwatch, String> sectionData = new MutablePair<>(Stopwatch.createUnstarted(), ""); - pluginSection(sectionData, "post-register", plugins, REIPlugin::postRegister); + pluginSection(sectionData, "post-register", plugins, null, REIPlugin::postRegister); + } + + private static String name(Class<?> clazz) { + String simpleName = clazz.getSimpleName(); + if (simpleName.isEmpty()) return clazz.getName(); + return simpleName; } @Override @@ -176,7 +205,7 @@ public class PluginManagerImpl<P extends REIPlugin<?>> implements PluginManager< for (Reloadable<P> reloadable : reloadables) { Class<?> reloadableClass = reloadable.getClass(); - try (SectionClosable startReload = section(sectionData, "start-reload-" + MoreObjects.firstNonNull(reloadableClass.getSimpleName(), reloadableClass.getName()))) { + try (SectionClosable startReload = section(sectionData, "start-reload-" + name(reloadableClass))) { reloadable.startReload(stage); } catch (Throwable throwable) { throwable.printStackTrace(); @@ -185,17 +214,17 @@ public class PluginManagerImpl<P extends REIPlugin<?>> implements PluginManager< List<P> plugins = new ArrayList<>(getPlugins().toList()); plugins.sort(Comparator.comparingDouble(P::getPriority).reversed()); - RoughlyEnoughItemsCore.LOGGER.info("Reloading Plugin Manager [%s] stage [%s], registered %d plugins: %s", pluginClass.getSimpleName(), stage.toString(), plugins.size(), CollectionUtils.mapAndJoinToString(plugins, REIPlugin::getPluginProviderName, ", ")); + RoughlyEnoughItemsCore.LOGGER.info("Reloading Plugin Manager [%s] stage [%s], registered %d plugins: %s", name(pluginClass), stage.toString(), plugins.size(), CollectionUtils.mapAndJoinToString(plugins, REIPlugin::getPluginProviderName, ", ")); Collections.reverse(plugins); for (Reloadable<P> reloadable : getReloadables()) { Class<?> reloadableClass = reloadable.getClass(); - pluginSection(sectionData, "reloadable-plugin-" + MoreObjects.firstNonNull(reloadableClass.getSimpleName(), reloadableClass.getName()), plugins, plugin -> reloadable.acceptPlugin(plugin, stage)); + pluginSection(sectionData, "reloadable-plugin-" + name(reloadableClass), plugins, reloadable, plugin -> reloadable.acceptPlugin(plugin, stage)); } for (Reloadable<P> reloadable : reloadables) { Class<?> reloadableClass = reloadable.getClass(); - try (SectionClosable endReload = section(sectionData, "end-reload-" + MoreObjects.firstNonNull(reloadableClass.getSimpleName(), reloadableClass.getName()))) { + try (SectionClosable endReload = section(sectionData, "end-reload-" + name(reloadableClass))) { reloadable.endReload(stage); } catch (Throwable throwable) { throwable.printStackTrace(); |
