From a31f7254d76add6c34f134713c131463602f8cef Mon Sep 17 00:00:00 2001 From: shedaniel Date: Wed, 4 Sep 2024 16:20:39 +0900 Subject: New plugin reload setup 1. Plugin reloads will now interrupt existing reloading tasks if a new plugin reload has been requested 2. Plugin reloads will be automatically interrupted when the player leaves the level / the level is removed 3. More logging in DisplayRegistryImpl showing the stats of displays 4. Failure in filling recipes will now not stop the caching of display lookup 5. Slightly improve performance of checking display visibility on display lookup --- .../rei/api/common/plugins/PluginView.java | 21 +++++--- .../impl/common/plugins/PluginReloadContext.java | 48 +++++++++++++++++ .../common/plugins/ReloadInterruptionContext.java | 63 ++++++++++++++++++++++ 3 files changed, 125 insertions(+), 7 deletions(-) create mode 100644 api/src/main/java/me/shedaniel/rei/impl/common/plugins/PluginReloadContext.java create mode 100644 api/src/main/java/me/shedaniel/rei/impl/common/plugins/ReloadInterruptionContext.java (limited to 'api/src/main') diff --git a/api/src/main/java/me/shedaniel/rei/api/common/plugins/PluginView.java b/api/src/main/java/me/shedaniel/rei/api/common/plugins/PluginView.java index b1072cb21..0e9918c66 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/plugins/PluginView.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/plugins/PluginView.java @@ -24,9 +24,9 @@ package me.shedaniel.rei.api.common.plugins; import me.shedaniel.rei.api.client.plugins.REIClientPlugin; -import me.shedaniel.rei.api.common.registry.ReloadStage; import me.shedaniel.rei.impl.ClientInternals; import me.shedaniel.rei.impl.Internals; +import me.shedaniel.rei.impl.common.plugins.PluginReloadContext; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import org.jetbrains.annotations.ApiStatus; @@ -62,18 +62,25 @@ public interface PluginView

> { } @Override - public void pre(ReloadStage stage) { - PluginView.this.pre(stage); + public void pre(PluginReloadContext context) throws InterruptedException { + PluginView.this.pre(context); } @Override - public void post(ReloadStage stage) { - PluginView.this.post(stage); + public void reload(PluginReloadContext context) throws InterruptedException { + PluginView.this.reload(context); + } + + @Override + public void post(PluginReloadContext context) throws InterruptedException { + PluginView.this.post(context); } }; } - void pre(ReloadStage stage); + void pre(PluginReloadContext context) throws InterruptedException; + + void reload(PluginReloadContext context) throws InterruptedException; - void post(ReloadStage stage); + void post(PluginReloadContext context) throws InterruptedException; } diff --git a/api/src/main/java/me/shedaniel/rei/impl/common/plugins/PluginReloadContext.java b/api/src/main/java/me/shedaniel/rei/impl/common/plugins/PluginReloadContext.java new file mode 100644 index 000000000..0481b38d0 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/impl/common/plugins/PluginReloadContext.java @@ -0,0 +1,48 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022, 2023 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.common.plugins; + +import me.shedaniel.rei.api.common.registry.ReloadStage; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +public interface PluginReloadContext { + ReloadStage stage(); + + ReloadInterruptionContext interruptionContext(); + + static PluginReloadContext of(ReloadStage stage, ReloadInterruptionContext interruptionContext) { + return new PluginReloadContext() { + @Override + public ReloadStage stage() { + return stage; + } + + @Override + public ReloadInterruptionContext interruptionContext() { + return interruptionContext; + } + }; + } +} diff --git a/api/src/main/java/me/shedaniel/rei/impl/common/plugins/ReloadInterruptionContext.java b/api/src/main/java/me/shedaniel/rei/impl/common/plugins/ReloadInterruptionContext.java new file mode 100644 index 000000000..19201b20b --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/impl/common/plugins/ReloadInterruptionContext.java @@ -0,0 +1,63 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022, 2023 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.common.plugins; + +import me.shedaniel.rei.impl.common.InternalLogger; +import org.jetbrains.annotations.ApiStatus; + +@FunctionalInterface +@ApiStatus.Internal +public interface ReloadInterruptionContext { + boolean isInterrupted(); + + default void checkInterrupted() throws InterruptedException { + if (isInterrupted()) { + InternalLogger.getInstance().debug("Plugin reload interrupted!"); + throw new InterruptedException(); + } + } + + default ReloadInterruptionContext withJob(Runnable ifInterrupted) { + return new ReloadInterruptionContext() { + @Override + public boolean isInterrupted() { + return ReloadInterruptionContext.this.isInterrupted(); + } + + @Override + public void checkInterrupted() throws InterruptedException { + try { + ReloadInterruptionContext.this.checkInterrupted(); + } catch (InterruptedException e) { + ifInterrupted.run(); + throw e; + } + } + }; + } + + static ReloadInterruptionContext ofNever() { + return () -> false; + } +} -- cgit From b8d0975527d86b79a1f9c160415baf8a6e6cf5cb Mon Sep 17 00:00:00 2001 From: shedaniel Date: Tue, 17 Sep 2024 13:46:46 +0800 Subject: New Slot.withEntriesListener --- api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/Slot.java | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'api/src/main') diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/Slot.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/Slot.java index 02527dafd..c968e685f 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/Slot.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/Slot.java @@ -25,10 +25,12 @@ package me.shedaniel.rei.api.client.gui.widgets; import me.shedaniel.math.Rectangle; import me.shedaniel.rei.api.common.entry.EntryStack; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; import java.util.Collection; import java.util.List; +import java.util.function.Consumer; public abstract class Slot extends WidgetWithBounds { public static final byte UN_MARKED = 0; @@ -133,6 +135,10 @@ public abstract class Slot extends WidgetWithBounds { public abstract Slot entries(Collection> stacks); + @ApiStatus.Experimental + @ApiStatus.Internal + public abstract Slot withEntriesListener(Consumer listener); + public abstract EntryStack getCurrentEntry(); public abstract List> getEntries(); -- cgit From 644c3f591d42d91c8445b199202b37ba05b0bf93 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Tue, 17 Sep 2024 21:36:09 +0800 Subject: Use InstanceHelper to get registryAccess --- .../me/shedaniel/rei/api/common/display/basic/BasicDisplay.java | 9 ++------- api/src/main/java/me/shedaniel/rei/impl/Internals.java | 6 ++++++ 2 files changed, 8 insertions(+), 7 deletions(-) (limited to 'api/src/main') diff --git a/api/src/main/java/me/shedaniel/rei/api/common/display/basic/BasicDisplay.java b/api/src/main/java/me/shedaniel/rei/api/common/display/basic/BasicDisplay.java index 09f913832..1d4ecb392 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/display/basic/BasicDisplay.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/display/basic/BasicDisplay.java @@ -23,12 +23,11 @@ package me.shedaniel.rei.api.common.display.basic; -import dev.architectury.utils.EnvExecutor; -import dev.architectury.utils.GameInstance; import me.shedaniel.rei.api.common.display.Display; import me.shedaniel.rei.api.common.display.SimpleDisplaySerializer; import me.shedaniel.rei.api.common.entry.EntryIngredient; import me.shedaniel.rei.api.common.util.EntryIngredients; +import me.shedaniel.rei.impl.Internals; import net.minecraft.core.RegistryAccess; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.Tag; @@ -38,16 +37,12 @@ import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.Optional; -import java.util.function.Supplier; /** * A basic implementation of a display, consisting of a list of inputs, a list of outputs * and a possible display location. */ public abstract class BasicDisplay implements Display { - protected static final Supplier REGISTRY_ACCESS = - EnvExecutor.getEnvSpecific(() -> () -> () -> GameInstance.getClient().player.level.registryAccess(), - () -> () -> () -> GameInstance.getServer().registryAccess()); protected List inputs; protected List outputs; protected Optional location; @@ -64,7 +59,7 @@ public abstract class BasicDisplay implements Display { @ApiStatus.Experimental public static RegistryAccess registryAccess() { - return REGISTRY_ACCESS.get(); + return Internals.getRegistryAccess(); } /** diff --git a/api/src/main/java/me/shedaniel/rei/impl/Internals.java b/api/src/main/java/me/shedaniel/rei/impl/Internals.java index 112b030ce..bcbfcc7fb 100644 --- a/api/src/main/java/me/shedaniel/rei/impl/Internals.java +++ b/api/src/main/java/me/shedaniel/rei/impl/Internals.java @@ -35,6 +35,7 @@ import me.shedaniel.rei.api.common.plugins.REIPlugin; import me.shedaniel.rei.api.common.plugins.REIServerPlugin; import me.shedaniel.rei.api.common.transfer.info.MenuInfoRegistry; import me.shedaniel.rei.impl.common.InternalLogger; +import net.minecraft.core.RegistryAccess; import net.minecraft.nbt.Tag; import net.minecraft.resources.ResourceLocation; import net.minecraft.util.Unit; @@ -55,6 +56,7 @@ public final class Internals { private static Function> categoryIdentifier = (object) -> throwNotSetup(); private static Supplier stubMenuInfoRegistry = Internals::throwNotSetup; private static Supplier logger = Internals::throwNotSetup; + private static Supplier registryAccess = Internals::throwNotSetup; private static T throwNotSetup() { throw new AssertionError("REI Internals have not been initialized!"); @@ -117,6 +119,10 @@ public final class Internals { return logger.get(); } + public static RegistryAccess getRegistryAccess() { + return registryAccess.get(); + } + public interface EntryStackProvider { EntryStack empty(); -- cgit