瀏覽代碼

Detect a good optifabric instance, it should support Chocohead/OptiFabric

Signed-off-by: shedaniel <daniel@shedaniel.me>
shedaniel 4 年之前
父節點
當前提交
4c320c4872

+ 21 - 22
RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/gui/WarningAndErrorScreen.java

@@ -39,22 +39,30 @@ import net.minecraft.network.chat.Component;
 import net.minecraft.network.chat.TextComponent;
 import net.minecraft.sounds.SoundEvents;
 import net.minecraft.util.FormattedCharSequence;
-import net.minecraft.util.LazyLoadedValue;
 import net.minecraft.util.Tuple;
 import org.jetbrains.annotations.ApiStatus;
 
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.List;
+import java.util.function.Consumer;
 
 @ApiStatus.Internal
 public class WarningAndErrorScreen extends Screen {
-    public static final LazyLoadedValue<WarningAndErrorScreen> INSTANCE = new LazyLoadedValue<>(WarningAndErrorScreen::new);
     private AbstractWidget buttonExit;
     private StringEntryListWidget listWidget;
+    private String action;
     private Screen parent;
+    private List<Tuple<String, String>> warnings;
+    private List<Tuple<String, String>> errors;
+    private Consumer<Screen> onContinue;
     
-    private WarningAndErrorScreen() {
+    public WarningAndErrorScreen(String action, List<Tuple<String, String>> warnings, List<Tuple<String, String>> errors, Consumer<Screen> onContinue) {
         super(NarratorChatListener.NO_TITLE);
+        this.action = action;
+        this.warnings = warnings;
+        this.errors = errors;
+        this.onContinue = onContinue;
     }
     
     @Override
@@ -84,9 +92,9 @@ public class WarningAndErrorScreen extends Screen {
         listWidget.max = 80;
         listWidget.creditsClearEntries();
         listWidget.creditsAddEntry(new EmptyItem());
-        if (!RoughlyEnoughItemsState.getWarnings().isEmpty())
-            listWidget.creditsAddEntry(new TextItem(new TextComponent("Warnings:").withStyle(ChatFormatting.RED).getVisualOrderText()));
-        for (Tuple<String, String> pair : RoughlyEnoughItemsState.getWarnings()) {
+        if (!warnings.isEmpty())
+            listWidget.creditsAddEntry(new TextItem(new TextComponent("Warnings:").withStyle(ChatFormatting.GOLD).getVisualOrderText()));
+        for (Tuple<String, String> pair : warnings) {
             addText(new TextComponent(pair.getA()));
             if (pair.getB() != null)
                 addLink(new TextComponent(pair.getB()), pair.getB());
@@ -94,12 +102,12 @@ public class WarningAndErrorScreen extends Screen {
                 listWidget.creditsAddEntry(new EmptyItem());
             }
         }
-        if (!RoughlyEnoughItemsState.getWarnings().isEmpty() && !RoughlyEnoughItemsState.getErrors().isEmpty()) {
+        if (!warnings.isEmpty() && !errors.isEmpty()) {
             listWidget.creditsAddEntry(new EmptyItem());
         }
-        if (!RoughlyEnoughItemsState.getErrors().isEmpty())
+        if (!errors.isEmpty())
             listWidget.creditsAddEntry(new TextItem(new TextComponent("Errors:").withStyle(ChatFormatting.RED).getVisualOrderText()));
-        for (Tuple<String, String> pair : RoughlyEnoughItemsState.getErrors()) {
+        for (Tuple<String, String> pair : errors) {
             addText(new TextComponent(pair.getA()));
             if (pair.getB() != null)
                 addLink(new TextComponent(pair.getB()), pair.getB());
@@ -111,17 +119,8 @@ public class WarningAndErrorScreen extends Screen {
             listWidget.max = Math.max(listWidget.max, child.getWidth());
         }
         children.add(buttonExit = new Button(width / 2 - 100, height - 26, 200, 20,
-                new TextComponent(RoughlyEnoughItemsState.getErrors().isEmpty() ? "Continue" : "Exit"),
-                button -> {
-                    if (RoughlyEnoughItemsState.getErrors().isEmpty()) {
-                        RoughlyEnoughItemsState.clear();
-                        RoughlyEnoughItemsState.continues();
-                        Minecraft.getInstance().setScreen(parent);
-                        setParent(null);
-                    } else {
-                        Minecraft.getInstance().stop();
-                    }
-                }));
+                new TextComponent(errors.isEmpty() ? "Continue" : "Exit"),
+                button -> onContinue.accept(parent)));
     }
     
     @Override
@@ -134,9 +133,9 @@ public class WarningAndErrorScreen extends Screen {
         this.renderDirtBackground(0);
         this.listWidget.render(matrices, int_1, int_2, float_1);
         if (RoughlyEnoughItemsState.getErrors().isEmpty()) {
-            this.drawCenteredString(matrices, this.font, "Warnings during Roughly Enough Items' initialization", this.width / 2, 16, 16777215);
+            drawCenteredString(matrices, this.font, "Warnings during Roughly Enough Items' " + action, this.width / 2, 16, 16777215);
         } else {
-            this.drawCenteredString(matrices, this.font, "Errors during Roughly Enough Items' initialization", this.width / 2, 16, 16777215);
+            drawCenteredString(matrices, this.font, "Errors during Roughly Enough Items' " + action, this.width / 2, 16, 16777215);
         }
         super.render(matrices, int_1, int_2, float_1);
         this.buttonExit.render(matrices, int_1, int_2, float_1);

+ 28 - 5
RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/ConfigManagerImpl.java

@@ -29,6 +29,7 @@ import com.google.gson.GsonBuilder;
 import com.google.gson.JsonElement;
 import com.mojang.blaze3d.platform.InputConstants;
 import com.mojang.blaze3d.vertex.PoseStack;
+import com.mojang.math.Matrix4f;
 import me.sargunvohra.mcmods.autoconfig1u.AutoConfig;
 import me.sargunvohra.mcmods.autoconfig1u.annotation.ConfigEntry;
 import me.sargunvohra.mcmods.autoconfig1u.gui.ConfigScreenProvider;
@@ -44,11 +45,13 @@ import me.shedaniel.clothconfig2.api.Modifier;
 import me.shedaniel.clothconfig2.api.ModifierKeyCode;
 import me.shedaniel.clothconfig2.gui.entries.KeyCodeEntry;
 import me.shedaniel.rei.RoughlyEnoughItemsCore;
+import me.shedaniel.rei.RoughlyEnoughItemsState;
 import me.shedaniel.rei.api.ConfigManager;
 import me.shedaniel.rei.api.EntryRegistry;
 import me.shedaniel.rei.api.EntryStack;
 import me.shedaniel.rei.api.REIHelper;
 import me.shedaniel.rei.gui.ContainerScreenOverlay;
+import me.shedaniel.rei.gui.WarningAndErrorScreen;
 import me.shedaniel.rei.gui.config.RecipeScreenType;
 import me.shedaniel.rei.gui.config.entry.FilteringEntry;
 import me.shedaniel.rei.gui.config.entry.NoFilteringEntry;
@@ -61,8 +64,11 @@ import net.fabricmc.api.EnvType;
 import net.fabricmc.api.Environment;
 import net.fabricmc.loader.api.FabricLoader;
 import net.minecraft.client.Minecraft;
+import net.minecraft.client.gui.Font;
 import net.minecraft.client.gui.components.Button;
 import net.minecraft.client.gui.screens.Screen;
+import net.minecraft.client.renderer.MultiBufferSource;
+import net.minecraft.client.resources.language.I18n;
 import net.minecraft.nbt.CompoundTag;
 import net.minecraft.nbt.TagParser;
 import net.minecraft.network.chat.CommonComponents;
@@ -70,8 +76,10 @@ import net.minecraft.network.chat.Component;
 import net.minecraft.network.chat.TextComponent;
 import net.minecraft.network.chat.TranslatableComponent;
 import net.minecraft.util.Mth;
+import net.minecraft.util.Tuple;
 import org.jetbrains.annotations.ApiStatus;
 
+import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -177,8 +185,13 @@ public class ConfigManagerImpl implements ConfigManager {
     @Override
     public Screen getConfigScreen(Screen parent) {
         try {
-            if (FabricLoader.getInstance().isModLoaded("optifabric")) {
-                return new ConfigErrorScreen(parent, new TranslatableComponent("text.rei.config.optifine.title"), new TranslatableComponent("text.rei.config.optifine.description"));
+            if (!detectWorkingOptifabric()) {
+                List<Tuple<String, String>> warnings = Lists.newArrayList();
+                warnings.add(new Tuple<>(I18n.get("text.rei.config.optifine.title"), null));
+                warnings.add(new Tuple<>(I18n.get("text.rei.config.optifine.description"), null));
+                WarningAndErrorScreen screen = new WarningAndErrorScreen("config screen", warnings, Collections.emptyList(), Minecraft.getInstance()::setScreen);
+                screen.setParent(parent);
+                return screen;
             }
             ConfigScreenProvider<ConfigObjectImpl> provider = (ConfigScreenProvider<ConfigObjectImpl>) AutoConfig.getConfigScreen(ConfigObjectImpl.class, parent);
             provider.setI13nFunction(manager -> "config.roughlyenoughitems");
@@ -196,7 +209,7 @@ public class ConfigManagerImpl implements ConfigManager {
                     }));
                 }).setSavingRunnable(() -> {
                     saveConfig();
-                    ((EntryRegistryImpl) EntryRegistry.getInstance()).refilter();
+                    EntryRegistry.getInstance().refilter();
                     if (ScreenHelper.getSearchField() != null)
                         ContainerScreenOverlay.getEntryListWidget().updateSearch(ScreenHelper.getSearchField().getText(), true);
                 }).build();
@@ -208,6 +221,16 @@ public class ConfigManagerImpl implements ConfigManager {
         return null;
     }
     
+    private boolean detectWorkingOptifabric() {
+        try {
+            String renderText = FabricLoader.getInstance().getMappingResolver().mapMethodName("intermediary", "net.minecraft.class_327", "method_1724", "(Ljava/lang/String;FFIZLnet/minecraft/class_1159;Lnet/minecraft/class_4597;ZII)F");
+            Method method = Font.class.getDeclaredMethod(renderText, String.class, Float.TYPE, Float.TYPE, Integer.TYPE, Boolean.TYPE, Matrix4f.class, MultiBufferSource.class, Boolean.TYPE, Integer.TYPE, Integer.TYPE);
+            return !java.lang.reflect.Modifier.isPrivate(method.getModifiers());
+        } catch (Throwable ignored) {
+            return false;
+        }
+    }
+    
     public static class ConfigErrorScreen extends Screen {
         private final Component message;
         private final Screen parent;
@@ -227,8 +250,8 @@ public class ConfigManagerImpl implements ConfigManager {
         @Override
         public void render(PoseStack matrices, int mouseX, int mouseY, float delta) {
             this.renderBackground(matrices);
-            this.drawCenteredString(matrices, this.font, this.title, this.width / 2, 90, 16777215);
-            this.drawCenteredString(matrices, this.font, this.message, this.width / 2, 110, 16777215);
+            drawCenteredString(matrices, this.font, this.title, this.width / 2, 90, 16777215);
+            drawCenteredString(matrices, this.font, this.message, this.width / 2, 110, 16777215);
             super.render(matrices, mouseX, mouseY, delta);
         }
         

+ 9 - 1
RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/ScreenHelper.java

@@ -260,7 +260,15 @@ public class ScreenHelper implements ClientModInitializer, REIHelper {
     public void onInitializeClient() {
         ClothClientHooks.SCREEN_INIT_PRE.register((client, screen, screenHooks) -> {
             if ((!RoughlyEnoughItemsState.getErrors().isEmpty() || !RoughlyEnoughItemsState.getWarnings().isEmpty()) && !(screen instanceof WarningAndErrorScreen)) {
-                WarningAndErrorScreen warningAndErrorScreen = WarningAndErrorScreen.INSTANCE.get();
+                WarningAndErrorScreen warningAndErrorScreen = new WarningAndErrorScreen("initialization", RoughlyEnoughItemsState.getWarnings(), RoughlyEnoughItemsState.getErrors(), (parent) -> {
+                    if (RoughlyEnoughItemsState.getErrors().isEmpty()) {
+                        RoughlyEnoughItemsState.clear();
+                        RoughlyEnoughItemsState.continues();
+                        Minecraft.getInstance().setScreen(parent);
+                    } else {
+                        Minecraft.getInstance().stop();
+                    }
+                });
                 warningAndErrorScreen.setParent(screen);
                 try {
                     if (client.screen != null) client.screen.removed();

+ 1 - 1
src/main/resources/assets/roughlyenoughitems/lang/en_us.json

@@ -39,7 +39,7 @@
   "text.rei.config": "Config",
   "text.rei.config_tooltip": "Open Config Screen\n§7Shift-Click to toggle cheat mode",
   "text.rei.config.optifine.title": "Failed to open REI config screen",
-  "text.rei.config.optifine.description": "The configuration screen is incompatible with OptiFine / OptiFabric.",
+  "text.rei.config.optifine.description": "The configuration screen is incompatible with your instance of OptiFine / OptiFabric.",
   "text.rei.cheat_items": "Gave [{item_name}§f] x{item_count} to {player_name}.",
   "text.rei.failed_cheat_items": "§cFailed to give items.",
   "ordering.rei.ascending": "Ascending",