aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsPlugin.java
blob: 8f91e7e0eb6cc2c5e8a90038bf89e7c36fd64494 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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 net.minecraft.util.ResourceLocation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dimdev.riftloader.ModInfo;
import org.dimdev.riftloader.RiftLoader;
import org.dimdev.riftloader.listener.InitializationListener;

import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.Reader;
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 RoughlyEnoughItemsPlugin implements InitializationListener {
    
    public static final Logger LOGGER = LogManager.getFormatterLogger("REI");
    private static final Map<ResourceLocation, IRecipePlugin> plugins = Maps.newHashMap();
    private static JsonParser parser = new JsonParser();
    private static List<ResourceLocation> disablingPlugins;
    
    public static IRecipePlugin registerPlugin(ResourceLocation resourceLocation, IRecipePlugin plugin) {
        plugins.put(resourceLocation, plugin);
        RoughlyEnoughItemsPlugin.LOGGER.info("REI: Registered Plugin from %s by %s.", resourceLocation.toString(), plugin.getClass().getSimpleName());
        return plugin;
    }
    
    public static List<IRecipePlugin> getPlugins() {
        return new LinkedList<>(plugins.values());
    }
    
    public static ResourceLocation getPluginResourceLocation(IRecipePlugin plugin) {
        for(ResourceLocation ResourceLocation : plugins.keySet())
            if (plugins.get(ResourceLocation).equals(plugin))
                return ResourceLocation;
        return null;
    }
    
    public static void disablePlugin(ResourceLocation location) {
        if (disablingPlugins.stream().noneMatch(location1 -> {return location.equals(location1);}))
            disablingPlugins.add(location);
    }
    
    @Override
    public void onInitialization() {
        discoverPlugins();
    }
    
    private void discoverPlugins() {
        LOGGER.info("REI: Discovering Plugins.");
        disablingPlugins = Lists.newArrayList();
        Collection<ModInfo> modInfoCollection = RiftLoader.instance.getMods();
        modInfoCollection.forEach(modInfo -> {
            try {
                if (modInfo.source.isDirectory()) {
                    File pluginFile = new File(modInfo.source, "plugins/roughlyenoughitems.plugin.json");
                    if (pluginFile.exists())
                        loadPluginInfo(modInfo, new FileReader(pluginFile));
                } else {
                    JarFile jarFile = new JarFile(modInfo.source);
                    ZipEntry entry = jarFile.getEntry("plugins/roughlyenoughitems.plugin.json");
                    if (entry != null)
                        loadPluginInfo(modInfo, new InputStreamReader(jarFile.getInputStream(entry)));
                }
            } catch (Exception e) {
                RoughlyEnoughItemsPlugin.LOGGER.error("REI: Failed to load plugin file from %s. (%s)", (Object) modInfo.id, (Object) e.getLocalizedMessage());
            }
        });
        plugins.forEach((location, plugin) -> plugin.onFirstLoad());
        plugins.keySet().stream().filter(location -> {
            return disablingPlugins.contains(location);
        }).collect(Collectors.toList()).forEach(location -> {
            plugins.remove(location);
            LOGGER.info("REI: Disabled REI plugin %s.", location.toString());
        });
        LOGGER.info("REI: Discovered %d REI Plugins%s", plugins.size(), (plugins.size() > 0 ? ": " + String.join(", ", plugins.keySet().stream().map(ResourceLocation::toString).collect(Collectors.toList())) : "."));
    }
    
    private void loadPluginInfo(ModInfo modInfo, Reader reader) throws Exception {
        JsonElement infoElement = parser.parse(reader);
        if (infoElement.isJsonArray())
            for(JsonElement jsonElement : infoElement.getAsJsonArray())
                parseAndRegisterPlugin(modInfo.id, jsonElement);
        else
            parseAndRegisterPlugin(modInfo.id, infoElement);
        reader.close();
    }
    
    private void parseAndRegisterPlugin(String modId, JsonElement jsonElement) throws Exception {
        ResourceLocation location = new ResourceLocation(modId, jsonElement.getAsJsonObject().getAsJsonPrimitive("id").getAsString());
        Class<?> aClass = Class.forName(jsonElement.getAsJsonObject().getAsJsonPrimitive("initializer").getAsString());
        IRecipePlugin plugin = IRecipePlugin.class.cast(aClass.newInstance());
        registerPlugin(location, plugin);
    }
    
}