Pārlūkot izejas kodu

Better plugin loading

Unknown 6 gadi atpakaļ
vecāks
revīzija
1feb326e43

+ 9 - 5
src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java

@@ -10,6 +10,7 @@ import com.google.common.collect.Maps;
 import me.shedaniel.cloth.api.ClientUtils;
 import me.shedaniel.cloth.hooks.ClothClientHooks;
 import me.shedaniel.rei.api.*;
+import me.shedaniel.rei.api.plugins.REIPluginV0;
 import me.shedaniel.rei.client.*;
 import me.shedaniel.rei.gui.ContainerScreenOverlay;
 import me.shedaniel.rei.gui.widget.ItemListOverlay;
@@ -87,10 +88,9 @@ public class RoughlyEnoughItemsCore implements ClientModInitializer {
      * @deprecated Check REI wiki
      */
     @Deprecated
-    public static REIPluginEntry registerPlugin(Identifier identifier, REIPluginEntry plugin) {
-        plugins.put(identifier, plugin);
-        RoughlyEnoughItemsCore.LOGGER.info("[REI] Registered plugin %s from %s", identifier.toString(), plugin.getClass().getSimpleName());
-        plugin.onFirstLoad(getPluginDisabler());
+    public static REIPluginEntry registerPlugin(REIPluginEntry plugin) {
+        plugins.put(plugin.getPluginIdentifier(), plugin);
+        RoughlyEnoughItemsCore.LOGGER.info("[REI] Registered plugin %s from %s", plugin.getPluginIdentifier().toString(), plugin.getClass().getSimpleName());
         return plugin;
     }
     
@@ -147,7 +147,11 @@ public class RoughlyEnoughItemsCore implements ClientModInitializer {
     private void discoverPluginEntries() {
         for(REIPluginEntry reiPlugin : FabricLoader.getInstance().getEntrypoints("rei_plugins", REIPluginEntry.class)) {
             try {
-                registerPlugin(reiPlugin.getPluginIdentifier(), reiPlugin);
+                if (!REIPluginV0.class.isAssignableFrom(reiPlugin.getClass()))
+                    throw new IllegalArgumentException("REI plugin is too old!");
+                registerPlugin(reiPlugin);
+                if (reiPlugin instanceof REIPluginV0)
+                    ((REIPluginV0) reiPlugin).onFirstLoad(getPluginDisabler());
             } catch (Exception e) {
                 e.printStackTrace();
                 RoughlyEnoughItemsCore.LOGGER.error("[REI] Can't load REI plugins from %s: %s", reiPlugin.getClass(), e.getLocalizedMessage());

+ 0 - 42
src/main/java/me/shedaniel/rei/api/REIPluginEntry.java

@@ -12,48 +12,6 @@ import net.minecraft.util.Identifier;
  */
 public interface REIPluginEntry {
     
-    /**
-     * On register of the plugin
-     *
-     * @param pluginDisabler the helper class to disable other plugins
-     */
-    default void onFirstLoad(PluginDisabler pluginDisabler) {}
-    
-    /**
-     * Registers items on the item panel
-     *
-     * @param itemRegistry the helper class
-     */
-    default void registerItems(ItemRegistry itemRegistry) {}
-    
-    /**
-     * Registers categories
-     *
-     * @param recipeHelper the helper class
-     */
-    default void registerPluginCategories(RecipeHelper recipeHelper) {}
-    
-    /**
-     * Registers displays for categories
-     *
-     * @param recipeHelper the helper class
-     */
-    default void registerRecipeDisplays(RecipeHelper recipeHelper) {}
-    
-    /**
-     * Registers bounds handlers
-     *
-     * @param displayHelper the helper class
-     */
-    default void registerBounds(DisplayHelper displayHelper) {}
-    
-    /**
-     * Register other stuff
-     *
-     * @param recipeHelper the helper class
-     */
-    default void registerOthers(RecipeHelper recipeHelper) {}
-    
     /**
      * Gets the priority of the plugin.
      *

+ 53 - 0
src/main/java/me/shedaniel/rei/api/plugins/REIPluginV0.java

@@ -0,0 +1,53 @@
+package me.shedaniel.rei.api.plugins;
+
+import me.shedaniel.rei.api.*;
+import net.fabricmc.loader.api.SemanticVersion;
+import net.fabricmc.loader.util.version.VersionParsingException;
+
+public interface REIPluginV0 extends REIPluginEntry {
+    
+    SemanticVersion getMinimumVersion() throws VersionParsingException;
+    
+    /**
+     * On register of the plugin
+     *
+     * @param pluginDisabler the helper class to disable other plugins
+     */
+    default void onFirstLoad(PluginDisabler pluginDisabler) {}
+    
+    /**
+     * Registers items on the item panel
+     *
+     * @param itemRegistry the helper class
+     */
+    default void registerItems(ItemRegistry itemRegistry) {}
+    
+    /**
+     * Registers categories
+     *
+     * @param recipeHelper the helper class
+     */
+    default void registerPluginCategories(RecipeHelper recipeHelper) {}
+    
+    /**
+     * Registers displays for categories
+     *
+     * @param recipeHelper the helper class
+     */
+    default void registerRecipeDisplays(RecipeHelper recipeHelper) {}
+    
+    /**
+     * Registers bounds handlers
+     *
+     * @param displayHelper the helper class
+     */
+    default void registerBounds(DisplayHelper displayHelper) {}
+    
+    /**
+     * Register other stuff
+     *
+     * @param recipeHelper the helper class
+     */
+    default void registerOthers(RecipeHelper recipeHelper) {}
+    
+}

+ 25 - 10
src/main/java/me/shedaniel/rei/client/RecipeHelperImpl.java

@@ -9,6 +9,10 @@ import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import me.shedaniel.rei.RoughlyEnoughItemsCore;
 import me.shedaniel.rei.api.*;
+import me.shedaniel.rei.api.plugins.REIPluginV0;
+import net.fabricmc.loader.api.FabricLoader;
+import net.fabricmc.loader.api.SemanticVersion;
+import net.fabricmc.loader.api.Version;
 import net.minecraft.client.gui.screen.ingame.AbstractContainerScreen;
 import net.minecraft.item.ItemStack;
 import net.minecraft.recipe.Recipe;
@@ -239,19 +243,30 @@ public class RecipeHelperImpl implements RecipeHelper {
         Collections.reverse(plugins);
         RoughlyEnoughItemsCore.getItemRegisterer().getModifiableItemList().clear();
         PluginDisabler pluginDisabler = RoughlyEnoughItemsCore.getPluginDisabler();
+        Version reiVersion = FabricLoader.getInstance().getModContainer("roughlyenoughitems").get().getMetadata().getVersion();
+        if (!(reiVersion instanceof SemanticVersion))
+            RoughlyEnoughItemsCore.LOGGER.warn("[REI] Roughly Enough Items is not using semantic versioning, will be ignoring plugins' minimum versions!");
         plugins.forEach(plugin -> {
             Identifier identifier = plugin.getPluginIdentifier();
             try {
-                if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_ITEMS))
-                    plugin.registerItems(RoughlyEnoughItemsCore.getItemRegisterer());
-                if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_CATEGORIES))
-                    plugin.registerPluginCategories(this);
-                if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_RECIPE_DISPLAYS))
-                    plugin.registerRecipeDisplays(this);
-                if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_BOUNDS))
-                    plugin.registerBounds(RoughlyEnoughItemsCore.getDisplayHelper());
-                if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_OTHERS))
-                    plugin.registerOthers(this);
+                if (plugin instanceof REIPluginV0) {
+                    if (reiVersion instanceof SemanticVersion)
+                        if (((REIPluginV0) plugin).getMinimumVersion().compareTo((SemanticVersion) reiVersion) > 0) {
+                            throw new IllegalStateException("Requires " + ((REIPluginV0) plugin).getMinimumVersion().getFriendlyString() + " REI version!");
+                        }
+                    if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_CATEGORIES))
+                        ((REIPluginV0) plugin).registerPluginCategories(this);
+                    if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_RECIPE_DISPLAYS))
+                        ((REIPluginV0) plugin).registerRecipeDisplays(this);
+                    if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_BOUNDS))
+                        ((REIPluginV0) plugin).registerBounds(RoughlyEnoughItemsCore.getDisplayHelper());
+                    if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_OTHERS))
+                        ((REIPluginV0) plugin).registerOthers(this);
+                    if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_ITEMS))
+                        ((REIPluginV0) plugin).registerItems(RoughlyEnoughItemsCore.getItemRegisterer());
+                } else {
+                    throw new IllegalStateException("Invaild Plugin Class!");
+                }
             } catch (Exception e) {
                 RoughlyEnoughItemsCore.LOGGER.error("[REI] " + identifier.toString() + " plugin failed to load!", e);
             }

+ 9 - 2
src/main/java/me/shedaniel/rei/plugin/DefaultAutoCraftingPlugin.java

@@ -8,12 +8,14 @@ package me.shedaniel.rei.plugin;
 import me.shedaniel.rei.RoughlyEnoughItemsCore;
 import me.shedaniel.rei.api.PluginDisabler;
 import me.shedaniel.rei.api.PluginFunction;
-import me.shedaniel.rei.api.REIPluginEntry;
 import me.shedaniel.rei.api.RecipeHelper;
+import me.shedaniel.rei.api.plugins.REIPluginV0;
 import me.shedaniel.rei.plugin.autocrafting.*;
+import net.fabricmc.loader.api.SemanticVersion;
+import net.fabricmc.loader.util.version.VersionParsingException;
 import net.minecraft.util.Identifier;
 
-public class DefaultAutoCraftingPlugin implements REIPluginEntry {
+public class DefaultAutoCraftingPlugin implements REIPluginV0 {
     
     public static final Identifier PLUGIN = new Identifier("roughlyenoughitems", "default_auto_crafting_plugin");
     
@@ -22,6 +24,11 @@ public class DefaultAutoCraftingPlugin implements REIPluginEntry {
         return PLUGIN;
     }
     
+    @Override
+    public SemanticVersion getMinimumVersion() throws VersionParsingException {
+        return SemanticVersion.parse("2.10");
+    }
+    
     @Override
     public void onFirstLoad(PluginDisabler pluginDisabler) {
         if (!RoughlyEnoughItemsCore.getConfigManager().getConfig().loadDefaultPlugin) {

+ 10 - 1
src/main/java/me/shedaniel/rei/plugin/DefaultPlugin.java

@@ -9,6 +9,7 @@ import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import me.shedaniel.rei.RoughlyEnoughItemsCore;
 import me.shedaniel.rei.api.*;
+import me.shedaniel.rei.api.plugins.REIPluginV0;
 import me.shedaniel.rei.client.ScreenHelper;
 import me.shedaniel.rei.gui.RecipeViewingScreen;
 import me.shedaniel.rei.gui.VillagerRecipeViewingScreen;
@@ -30,6 +31,9 @@ import me.shedaniel.rei.plugin.smoking.DefaultSmokingCategory;
 import me.shedaniel.rei.plugin.smoking.DefaultSmokingDisplay;
 import me.shedaniel.rei.plugin.stonecutting.DefaultStoneCuttingCategory;
 import me.shedaniel.rei.plugin.stonecutting.DefaultStoneCuttingDisplay;
+import net.fabricmc.loader.api.SemanticVersion;
+import net.fabricmc.loader.util.version.SemanticVersionImpl;
+import net.fabricmc.loader.util.version.VersionParsingException;
 import net.minecraft.block.ComposterBlock;
 import net.minecraft.client.MinecraftClient;
 import net.minecraft.client.gui.screen.ingame.*;
@@ -49,7 +53,7 @@ import java.awt.*;
 import java.util.List;
 import java.util.*;
 
-public class DefaultPlugin implements REIPluginEntry {
+public class DefaultPlugin implements REIPluginV0 {
     
     public static final Identifier CRAFTING = new Identifier("minecraft", "plugins/crafting");
     public static final Identifier SMELTING = new Identifier("minecraft", "plugins/smelting");
@@ -77,6 +81,11 @@ public class DefaultPlugin implements REIPluginEntry {
         return PLUGIN;
     }
     
+    @Override
+    public SemanticVersion getMinimumVersion() throws VersionParsingException {
+        return SemanticVersion.parse("2.10");
+    }
+    
     @Override
     public void onFirstLoad(PluginDisabler pluginDisabler) {
         if (!RoughlyEnoughItemsCore.getConfigManager().getConfig().loadDefaultPlugin) {