From 71bb1aa3397841d78693290b36a598eeecb21080 Mon Sep 17 00:00:00 2001 From: Nicholas Ignoffo Date: Mon, 21 Jan 2019 17:03:20 -0800 Subject: Migrate to pluginloader With this implementation, the loader will only be created on the client to mimic the current implementation. If plugin loader is not installed, the default plugin will still be registered so core functionality is preserved. --- .../me/shedaniel/rei/RoughlyEnoughItemsCore.java | 76 ++-------------------- .../shedaniel/rei/RoughlyEnoughItemsPlugins.java | 31 +++++++++ src/main/resources/fabric.mod.json | 3 + src/main/resources/pluginloader.json | 7 ++ src/main/resources/plugins/rei.plugin.json | 8 --- .../plugins/roughlyenoughitems.plugin.json | 6 ++ 6 files changed, 52 insertions(+), 79 deletions(-) create mode 100644 src/main/java/me/shedaniel/rei/RoughlyEnoughItemsPlugins.java create mode 100644 src/main/resources/pluginloader.json delete mode 100644 src/main/resources/plugins/rei.plugin.json create mode 100644 src/main/resources/plugins/roughlyenoughitems.plugin.json (limited to 'src/main') diff --git a/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java b/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java index efca3aa36..3770a8d9f 100644 --- a/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java +++ b/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java @@ -2,19 +2,16 @@ package me.shedaniel.rei; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.google.gson.JsonElement; -import com.google.gson.JsonParser; import me.shedaniel.rei.api.IRecipePlugin; -import me.shedaniel.rei.api.REIPluginInfo; import me.shedaniel.rei.client.ClientHelper; import me.shedaniel.rei.client.ConfigHelper; import me.shedaniel.rei.client.RecipeHelper; import me.shedaniel.rei.listeners.IListener; +import me.shedaniel.rei.plugin.DefaultPlugin; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.ModInitializer; +import net.fabricmc.api.loader.Loader; import net.fabricmc.fabric.networking.CustomPayloadPacketRegistry; -import net.fabricmc.loader.FabricLoader; -import net.fabricmc.loader.ModContainer; import net.minecraft.client.resource.language.I18n; import net.minecraft.item.ItemStack; import net.minecraft.server.network.ServerPlayerEntity; @@ -25,19 +22,10 @@ import net.minecraft.util.Identifier; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.nio.file.Files; -import java.nio.file.StandardOpenOption; -import java.util.Collection; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.jar.JarFile; import java.util.stream.Collectors; -import java.util.zip.ZipEntry; public class RoughlyEnoughItemsCore implements ClientModInitializer, ModInitializer { @@ -84,7 +72,9 @@ public class RoughlyEnoughItemsCore implements ClientModInitializer, ModInitiali @Override public void onInitializeClient() { registerREIListeners(); - discoverPlugins(); + // If pluginloader is not installed, base functionality should still remain + if (!Loader.getInstance().isModLoaded("pluginloader")) + registerPlugin(new Identifier("roughlyenoughitems", "default_plugin"), new DefaultPlugin()); configHelper = new ConfigHelper(); } @@ -105,62 +95,6 @@ public class RoughlyEnoughItemsCore implements ClientModInitializer, ModInitiali registerFabricPackets(); } - private void discoverPlugins() { - Collection modContainers = FabricLoader.INSTANCE.getModContainers(); - List pluginInfoList = Lists.newArrayList(); - JsonParser parser = new JsonParser(); - modContainers.forEach(modContainer -> { - InputStream inputStream = null; - if (modContainer.getOriginFile().isFile()) - try (JarFile file = new JarFile(modContainer.getOriginFile())) { - ZipEntry entry = file.getEntry("plugins" + File.separatorChar + "rei.plugin.json"); - if (entry != null) - inputStream = file.getInputStream(entry); - } catch (Exception e) { - RoughlyEnoughItemsCore.LOGGER.error("REI: Failed to load plugin file from " + modContainer.getInfo().getId() + ". (" + e.getLocalizedMessage() + ")"); - } - else if (modContainer.getOriginFile().isDirectory()) { - File modInfo = new File(modContainer.getOriginFile(), "plugins" + File.separatorChar + "rei.plugin.json"); - if (modInfo.exists()) - try { - inputStream = Files.newInputStream(modInfo.toPath(), StandardOpenOption.READ); - } catch (Exception e) { - RoughlyEnoughItemsCore.LOGGER.error("REI: Failed to load plugin file from " + modContainer.getInfo().getId() + ". (" + e.getLocalizedMessage() + ")"); - } - } - if (inputStream != null) - try { - JsonElement jsonElement = parser.parse(new InputStreamReader(inputStream)); - if (jsonElement != null && jsonElement.isJsonObject()) { - REIPluginInfo info = REIPluginInfo.GSON.fromJson(jsonElement, REIPluginInfo.class); - if (info != null) - pluginInfoList.add(info); - } - } catch (Exception e) { - RoughlyEnoughItemsCore.LOGGER.error("REI: Failed to load REI plugin info from " + modContainer.getInfo().getId() + ". (" + e.getLocalizedMessage() + ")"); - } finally { - try { - inputStream.close(); - } catch (IOException e) { - RoughlyEnoughItemsCore.LOGGER.error("REI: Failed to close input stream from " + modContainer.getInfo().getId() + ". (" + e.getLocalizedMessage() + ")"); - } - } - }); - pluginInfoList.stream().forEachOrdered(reiPluginInfo -> { - reiPluginInfo.getPlugins().forEach(reiPlugin -> { - try { - Identifier identifier = new Identifier(reiPlugin.getIdentifier()); - Class aClass = Class.forName(reiPlugin.getPluginClass()); - IRecipePlugin plugin = IRecipePlugin.class.cast(aClass.newInstance()); - RoughlyEnoughItemsCore.registerPlugin(identifier, plugin); - RoughlyEnoughItemsCore.LOGGER.info("REI: Registered REI plugin: " + reiPlugin.getIdentifier()); - } catch (Exception e) { - RoughlyEnoughItemsCore.LOGGER.error("REI: Failed to register REI plugin: " + reiPlugin.getIdentifier() + ". (" + e.getLocalizedMessage() + ")"); - } - }); - }); - } - private void registerFabricPackets() { CustomPayloadPacketRegistry.SERVER.register(DELETE_ITEMS_PACKET, (packetContext, packetByteBuf) -> { ServerPlayerEntity player = (ServerPlayerEntity) packetContext.getPlayer(); diff --git a/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsPlugins.java b/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsPlugins.java new file mode 100644 index 000000000..d3ff98686 --- /dev/null +++ b/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsPlugins.java @@ -0,0 +1,31 @@ +package me.shedaniel.rei; + +import info.tehnut.pluginloader.LoaderCreator; +import info.tehnut.pluginloader.PluginLoaderBuilder; +import info.tehnut.pluginloader.ValidationStrategy; +import me.shedaniel.rei.api.IRecipePlugin; +import net.fabricmc.loader.language.LanguageAdapter; +import net.fabricmc.loader.language.LanguageAdapterException; +import net.minecraft.util.Identifier; + +public class RoughlyEnoughItemsPlugins implements LoaderCreator { + + @Override + public void createLoaders() { + LanguageAdapter.Options instantiationOptions = new LanguageAdapter.Options(); + + new PluginLoaderBuilder("roughlyenoughitems") + .withValidator(ValidationStrategy.hasInterface(IRecipePlugin.class)) + .withInitializer((aClass, container) -> { + Identifier id = new Identifier(container.getOwner().getInfo().getId(), container.getInfo().getId()); + try { + IRecipePlugin plugin = (IRecipePlugin) container.getOwner().getAdapter().createInstance(aClass, instantiationOptions); + RoughlyEnoughItemsCore.registerPlugin(id, plugin); + RoughlyEnoughItemsCore.LOGGER.info("Registered plugin %s from %s", id, aClass); + } catch (LanguageAdapterException e) { + RoughlyEnoughItemsCore.LOGGER.error("Error loading plugin %s", id, e); + } + }) + .build(); + } +} diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 4aa90427e..1b6d61f4a 100755 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -14,6 +14,9 @@ "requires": { "fabric": "0.1.4.76" }, + "recommended": { + "pluginloader": "1.0.7" + }, "mixins": { "client": "roughlyenoughitems.client.json" } diff --git a/src/main/resources/pluginloader.json b/src/main/resources/pluginloader.json new file mode 100644 index 000000000..8c256add0 --- /dev/null +++ b/src/main/resources/pluginloader.json @@ -0,0 +1,7 @@ +{ + "id": "roughlyenoughitems", + "initializer": "me.shedaniel.rei.RoughlyEnoughItemsPlugins", + "data": { + "side": "CLIENT" + } +} \ No newline at end of file diff --git a/src/main/resources/plugins/rei.plugin.json b/src/main/resources/plugins/rei.plugin.json deleted file mode 100644 index ab578d972..000000000 --- a/src/main/resources/plugins/rei.plugin.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "plugins": [ - { - "identifier": "roughlyenoughitems:default_plugin", - "class": "me.shedaniel.rei.plugin.DefaultPlugin" - } - ] -} \ No newline at end of file diff --git a/src/main/resources/plugins/roughlyenoughitems.plugin.json b/src/main/resources/plugins/roughlyenoughitems.plugin.json new file mode 100644 index 000000000..8ea3542ee --- /dev/null +++ b/src/main/resources/plugins/roughlyenoughitems.plugin.json @@ -0,0 +1,6 @@ +[ + { + "id": "default_plugin", + "initializer": "me.shedaniel.rei.plugin.DefaultPlugin" + } +] \ No newline at end of file -- cgit From f281bc967d188cfdf6f0f2c7f755af37c8b0fb3f Mon Sep 17 00:00:00 2001 From: Nicholas Ignoffo Date: Mon, 21 Jan 2019 17:10:08 -0800 Subject: Remove now unnecessary REIPluginInfo class --- .../java/me/shedaniel/rei/api/REIPluginInfo.java | 34 ---------------------- 1 file changed, 34 deletions(-) delete mode 100644 src/main/java/me/shedaniel/rei/api/REIPluginInfo.java (limited to 'src/main') diff --git a/src/main/java/me/shedaniel/rei/api/REIPluginInfo.java b/src/main/java/me/shedaniel/rei/api/REIPluginInfo.java deleted file mode 100644 index e596bb31f..000000000 --- a/src/main/java/me/shedaniel/rei/api/REIPluginInfo.java +++ /dev/null @@ -1,34 +0,0 @@ -package me.shedaniel.rei.api; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.annotations.SerializedName; - -import java.util.List; - -public class REIPluginInfo { - - public static Gson GSON = new GsonBuilder().create(); - - private List plugins; - - public static class REIPlugin { - private String identifier; - @SerializedName("class") private String pluginClass; - - public String getIdentifier() { - if (identifier == null) - return "null:null"; - return identifier; - } - - public String getPluginClass() { - return pluginClass; - } - } - - public List getPlugins() { - return plugins; - } - -} -- cgit From 680a75bb732475d55d8b8f03802b95cb7521b1a1 Mon Sep 17 00:00:00 2001 From: Nicholas Ignoffo Date: Mon, 21 Jan 2019 18:05:04 -0800 Subject: Version recommendation should have the correct version --- src/main/resources/fabric.mod.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main') diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 1b6d61f4a..ca390ff56 100755 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -15,7 +15,7 @@ "fabric": "0.1.4.76" }, "recommended": { - "pluginloader": "1.0.7" + "pluginloader": "1.0.6" }, "mixins": { "client": "roughlyenoughitems.client.json" -- cgit