From 7f2cea976276ef14030964eea6c91ebee9f39204 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sun, 12 Apr 2020 02:13:15 +0800 Subject: Adds warning screen Signed-off-by: shedaniel --- gradle.properties | 2 +- .../me/shedaniel/rei/RoughlyEnoughItemsCore.java | 34 +++++++++- .../shedaniel/rei/RoughlyEnoughItemsNetwork.java | 2 +- .../me/shedaniel/rei/RoughlyEnoughItemsState.java | 72 +++++++++++++++++++--- .../me/shedaniel/rei/gui/FailedToLoadScreen.java | 71 ++++++++++++++++++--- .../me/shedaniel/rei/impl/ClientHelperImpl.java | 2 +- .../java/me/shedaniel/rei/impl/ScreenHelper.java | 8 ++- 7 files changed, 164 insertions(+), 27 deletions(-) diff --git a/gradle.properties b/gradle.properties index d19765697..e65a30f05 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -mod_version=4.1.6-unstable +mod_version=4.1.7-unstable minecraft_version=20w14a yarn_version=20w14a+build.2 fabricloader_version=0.7.9+build.190 diff --git a/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java b/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java index 4f1b19cb4..be7b3b696 100644 --- a/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java +++ b/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java @@ -64,6 +64,9 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.ApiStatus; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Field; import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; @@ -174,6 +177,7 @@ public class RoughlyEnoughItemsCore implements ClientModInitializer { public void onInitializeClient() { configManager = new ConfigManagerImpl(); + detectFabricLoader(); registerClothEvents(); discoverPluginEntries(); for (ModContainer modContainer : FabricLoader.getInstance().getAllMods()) { @@ -183,7 +187,7 @@ public class RoughlyEnoughItemsCore implements ClientModInitializer { boolean networkingLoaded = FabricLoader.getInstance().isModLoaded("fabric-networking-v0"); if (!networkingLoaded) { - RoughlyEnoughItemsState.failedToLoad("Fabric API is not installed!", "https://www.curseforge.com/minecraft/mc-mods/fabric-api/files/all"); + RoughlyEnoughItemsState.error("Fabric API is not installed!", "https://www.curseforge.com/minecraft/mc-mods/fabric-api/files/all"); return; } Executor.run(() -> () -> { @@ -224,6 +228,34 @@ public class RoughlyEnoughItemsCore implements ClientModInitializer { }); } + private void detectFabricLoader() { + Executor.run(() -> () -> { + try { + FabricLoader instance = FabricLoader.getInstance(); + for (Field field : instance.getClass().getDeclaredFields()) { + if (Logger.class.isAssignableFrom(field.getType())) { + field.setAccessible(true); + Logger logger = (Logger) field.get(instance); + if (logger.getName().toLowerCase(Locale.ROOT).contains("subsystem")) { + if (!new File(instance.getConfigDirectory(), "roughlyenoughitems/.ignoresubsystem").exists()) { + RoughlyEnoughItemsState.warn("Subsystem is detected (probably though Aristois), please contact support from them if anything happens."); + RoughlyEnoughItemsState.onContinue(() -> { + try { + new File(instance.getConfigDirectory(), "roughlyenoughitems").mkdirs(); + new File(instance.getConfigDirectory(), "roughlyenoughitems/.ignoresubsystem").createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } + } + } + } + } catch (Throwable ignored) { + } + }); + } + private void discoverPluginEntries() { for (REIPluginEntry reiPlugin : FabricLoader.getInstance().getEntrypoints("rei_plugins", REIPluginEntry.class)) { try { diff --git a/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsNetwork.java b/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsNetwork.java index 2893b1e84..e84b24b93 100644 --- a/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsNetwork.java +++ b/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsNetwork.java @@ -57,7 +57,7 @@ public class RoughlyEnoughItemsNetwork implements ModInitializer { public void onInitialize() { boolean loaded = FabricLoader.getInstance().isModLoaded("fabric-networking-v0"); if (!loaded) { - RoughlyEnoughItemsState.failedToLoad("Fabric API is not installed!", "https://www.curseforge.com/minecraft/mc-mods/fabric-api/files/all"); + RoughlyEnoughItemsState.error("Fabric API is not installed!", "https://www.curseforge.com/minecraft/mc-mods/fabric-api/files/all"); return; } Executor.run(() -> () -> { diff --git a/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsState.java b/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsState.java index a6c71f6cb..0a67d6f75 100644 --- a/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsState.java +++ b/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsState.java @@ -26,6 +26,8 @@ package me.shedaniel.rei; import net.fabricmc.api.EnvType; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.util.Pair; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.ApiStatus; import java.util.ArrayList; @@ -37,24 +39,74 @@ import java.util.Set; public class RoughlyEnoughItemsState { private RoughlyEnoughItemsState() {} - private static List> failedToLoad = new ArrayList<>(); - private static Set failedToLoadSet = new LinkedHashSet<>(); + public static final Logger LOGGER = LogManager.getFormatterLogger("REI"); - public static void failedToLoad(String reason) { + private static List> errors = new ArrayList<>(); + private static List> warnings = new ArrayList<>(); + private static Set errorSet = new LinkedHashSet<>(); + private static Set warningSet = new LinkedHashSet<>(); + private static List continueCallbacks = new ArrayList<>(); + + public static void error(String reason) { if (FabricLoader.getInstance().getEnvironmentType() == EnvType.SERVER || FabricLoader.getInstance().isDevelopmentEnvironment()) throw new RuntimeException(reason); - if (RoughlyEnoughItemsState.failedToLoadSet.add(reason + " " + null)) - RoughlyEnoughItemsState.failedToLoad.add(new Pair<>(reason, null)); + if (RoughlyEnoughItemsState.errorSet.add(reason + " " + null)) { + RoughlyEnoughItemsState.errors.add(new Pair<>(reason, null)); + LOGGER.error(reason); + } } - public static void failedToLoad(String reason, String link) { + public static void error(String reason, String link) { if (FabricLoader.getInstance().getEnvironmentType() == EnvType.SERVER || FabricLoader.getInstance().isDevelopmentEnvironment()) throw new RuntimeException(reason + " " + link); - if (RoughlyEnoughItemsState.failedToLoadSet.add(reason + " " + link)) - RoughlyEnoughItemsState.failedToLoad.add(new Pair<>(reason, link)); + if (RoughlyEnoughItemsState.errorSet.add(reason + " " + link)) { + RoughlyEnoughItemsState.errors.add(new Pair<>(reason, link)); + LOGGER.error(reason + " " + link); + } + } + + public static void warn(String reason) { + if (RoughlyEnoughItemsState.warningSet.add(reason + " " + null)) { + RoughlyEnoughItemsState.warnings.add(new Pair<>(reason, null)); + LOGGER.warn(reason); + } + } + + public static void warn(String reason, String link) { + if (RoughlyEnoughItemsState.warningSet.add(reason + " " + link)) { + RoughlyEnoughItemsState.warnings.add(new Pair<>(reason, link)); + LOGGER.warn(reason + " " + link); + } + } + + @SuppressWarnings({"Convert2MethodRef", "FunctionalExpressionCanBeFolded"}) + public static void onContinue(Runnable runnable) { + continueCallbacks.add(runnable); + } + + public static List> getErrors() { + return errors; + } + + public static List> getWarnings() { + return warnings; + } + + public static void clear() { + errors.clear(); + errorSet.clear(); + warnings.clear(); + warningSet.clear(); } - public static List> getFailedToLoad() { - return failedToLoad; + public static void continues() { + for (Runnable callback : continueCallbacks) { + try { + callback.run(); + } catch (Throwable throwable) { + throwable.printStackTrace(); + } + } + continueCallbacks.clear(); } } diff --git a/src/main/java/me/shedaniel/rei/gui/FailedToLoadScreen.java b/src/main/java/me/shedaniel/rei/gui/FailedToLoadScreen.java index 4f1f48917..a1d4ca3a3 100644 --- a/src/main/java/me/shedaniel/rei/gui/FailedToLoadScreen.java +++ b/src/main/java/me/shedaniel/rei/gui/FailedToLoadScreen.java @@ -31,8 +31,8 @@ import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.widget.AbstractButtonWidget; import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.client.sound.PositionedSoundInstance; +import net.minecraft.client.util.NarratorManager; import net.minecraft.sound.SoundEvents; -import net.minecraft.text.LiteralText; import net.minecraft.text.Text; import net.minecraft.util.Lazy; import net.minecraft.util.Pair; @@ -47,9 +47,10 @@ public class FailedToLoadScreen extends Screen { public static final Lazy INSTANCE = new Lazy<>(FailedToLoadScreen::new); private AbstractButtonWidget buttonExit; private StringEntryListWidget listWidget; + private Screen parent; private FailedToLoadScreen() { - super(new LiteralText("REI has failed to init")); + super(NarratorManager.EMPTY); } @Override @@ -57,16 +58,47 @@ public class FailedToLoadScreen extends Screen { return false; } + public void setParent(Screen parent) { + this.parent = parent; + } + + private void addText(String string) { + for (String s : textRenderer.wrapStringToWidthAsList(string, width - 80)) { + listWidget.creditsAddEntry(new TextItem(s)); + } + } + + private void addLink(String string, String link) { + for (String s : textRenderer.wrapStringToWidthAsList(string, width - 80)) { + listWidget.creditsAddEntry(new LinkItem(s, link)); + } + } + @Override protected void init() { children.add(listWidget = new StringEntryListWidget(client, width, height, 32, height - 32)); listWidget.max = 80; listWidget.creditsClearEntries(); listWidget.creditsAddEntry(new EmptyItem()); - for (Pair pair : RoughlyEnoughItemsState.getFailedToLoad()) { - listWidget.creditsAddEntry(new TextItem(pair.getLeft())); + if (!RoughlyEnoughItemsState.getWarnings().isEmpty()) + listWidget.creditsAddEntry(new TextItem("§6Warnings:")); + for (Pair pair : RoughlyEnoughItemsState.getWarnings()) { + addText(pair.getLeft()); if (pair.getRight() != null) - listWidget.creditsAddEntry(this.new LinkItem(pair.getRight())); + addLink(pair.getRight(), pair.getRight()); + for (int i = 0; i < 2; i++) { + listWidget.creditsAddEntry(new EmptyItem()); + } + } + if (!RoughlyEnoughItemsState.getWarnings().isEmpty() && !RoughlyEnoughItemsState.getErrors().isEmpty()) { + listWidget.creditsAddEntry(new EmptyItem()); + } + if (!RoughlyEnoughItemsState.getErrors().isEmpty()) + listWidget.creditsAddEntry(new TextItem("§cErrors:")); + for (Pair pair : RoughlyEnoughItemsState.getErrors()) { + addText(pair.getLeft()); + if (pair.getRight() != null) + addLink(pair.getRight(), pair.getRight()); for (int i = 0; i < 2; i++) { listWidget.creditsAddEntry(new EmptyItem()); } @@ -74,9 +106,18 @@ public class FailedToLoadScreen extends Screen { for (StringItem child : listWidget.children()) { listWidget.max = Math.max(listWidget.max, child.getWidth()); } - children.add(buttonExit = new ButtonWidget(width / 2 - 100, height - 26, 200, 20, "Exit", button -> { - MinecraftClient.getInstance().scheduleStop(); - })); + children.add(buttonExit = new ButtonWidget(width / 2 - 100, height - 26, 200, 20, + RoughlyEnoughItemsState.getErrors().isEmpty() ? "Continue" : "Exit", + button -> { + if (RoughlyEnoughItemsState.getErrors().isEmpty()) { + RoughlyEnoughItemsState.clear(); + RoughlyEnoughItemsState.continues(); + MinecraftClient.getInstance().openScreen(parent); + setParent(null); + } else { + MinecraftClient.getInstance().scheduleStop(); + } + })); } @Override @@ -88,7 +129,11 @@ public class FailedToLoadScreen extends Screen { public void render(int int_1, int int_2, float float_1) { this.renderDirtBackground(0); this.listWidget.render(int_1, int_2, float_1); - this.drawCenteredString(this.textRenderer, this.title.asFormattedString(), this.width / 2, 16, 16777215); + if (RoughlyEnoughItemsState.getErrors().isEmpty()) { + this.drawCenteredString(this.textRenderer, "Warnings during Roughly Enough Items' initialization", this.width / 2, 16, 16777215); + } else { + this.drawCenteredString(this.textRenderer, "Errors during Roughly Enough Items' initialization", this.width / 2, 16, 16777215); + } super.render(int_1, int_2, float_1); this.buttonExit.render(int_1, int_2, float_1); } @@ -195,6 +240,7 @@ public class FailedToLoadScreen extends Screen { private class LinkItem extends StringItem { private String text; + private String link; private boolean contains; public LinkItem(Text textComponent) { @@ -202,7 +248,12 @@ public class FailedToLoadScreen extends Screen { } public LinkItem(String text) { + this(text, text); + } + + public LinkItem(String text, String link) { this.text = text; + this.link = link; } @Override @@ -236,7 +287,7 @@ public class FailedToLoadScreen extends Screen { if (contains && button == 0) { MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F)); try { - Util.getOperatingSystem().open(new URI(text)); + Util.getOperatingSystem().open(new URI(link)); return true; } catch (URISyntaxException e) { e.printStackTrace(); diff --git a/src/main/java/me/shedaniel/rei/impl/ClientHelperImpl.java b/src/main/java/me/shedaniel/rei/impl/ClientHelperImpl.java index 13af0c0a0..9b711e155 100644 --- a/src/main/java/me/shedaniel/rei/impl/ClientHelperImpl.java +++ b/src/main/java/me/shedaniel/rei/impl/ClientHelperImpl.java @@ -289,7 +289,7 @@ public class ClientHelperImpl implements ClientHelper, ClientModInitializer { public void registerFabricKeyBinds() { boolean keybindingsLoaded = FabricLoader.getInstance().isModLoaded("fabric-keybindings-v0"); if (!keybindingsLoaded) { - RoughlyEnoughItemsState.failedToLoad("Fabric API is not installed!", "https://www.curseforge.com/minecraft/mc-mods/fabric-api/files/all"); + RoughlyEnoughItemsState.error("Fabric API is not installed!", "https://www.curseforge.com/minecraft/mc-mods/fabric-api/files/all"); return; } Executor.run(() -> () -> { diff --git a/src/main/java/me/shedaniel/rei/impl/ScreenHelper.java b/src/main/java/me/shedaniel/rei/impl/ScreenHelper.java index c1bafeb1e..5cdb2a87f 100644 --- a/src/main/java/me/shedaniel/rei/impl/ScreenHelper.java +++ b/src/main/java/me/shedaniel/rei/impl/ScreenHelper.java @@ -194,15 +194,17 @@ public class ScreenHelper implements ClientModInitializer, REIHelper { @Override public void onInitializeClient() { ClothClientHooks.SCREEN_INIT_PRE.register((client, screen, screenHooks) -> { - if (!RoughlyEnoughItemsState.getFailedToLoad().isEmpty() && !(screen instanceof FailedToLoadScreen)) { - client.openScreen(FailedToLoadScreen.INSTANCE.get()); + if ((!RoughlyEnoughItemsState.getErrors().isEmpty() || !RoughlyEnoughItemsState.getWarnings().isEmpty()) && !(screen instanceof FailedToLoadScreen)) { + FailedToLoadScreen failedToLoadScreen = FailedToLoadScreen.INSTANCE.get(); + failedToLoadScreen.setParent(screen); + client.openScreen(failedToLoadScreen); } else if (lastHandledScreen != screen && screen instanceof HandledScreen) lastHandledScreen = (HandledScreen) screen; return ActionResult.PASS; }); boolean loaded = FabricLoader.getInstance().isModLoaded("fabric-events-lifecycle-v0"); if (!loaded) { - RoughlyEnoughItemsState.failedToLoad("Fabric API is not installed!", "https://www.curseforge.com/minecraft/mc-mods/fabric-api/files/all"); + RoughlyEnoughItemsState.error("Fabric API is not installed!", "https://www.curseforge.com/minecraft/mc-mods/fabric-api/files/all"); return; } Executor.run(() -> () -> { -- cgit