aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUnknown <shekwancheung0528@gmail.com>2019-01-24 17:59:33 +0800
committerUnknown <shekwancheung0528@gmail.com>2019-01-24 17:59:33 +0800
commit40d345b6b0f0bea1e4313ad9cdb789762e160f8d (patch)
tree45a84cf1db8296bf6a22baa28282ffa8a7d7fa18
parent0f03e78725614c1de5128cdcd0b9ea4ade2e0104 (diff)
downloadRoughlyEnoughItems-2.1.0.14.tar.gz
RoughlyEnoughItems-2.1.0.14.tar.bz2
RoughlyEnoughItems-2.1.0.14.zip
Fixing search & plugin loaderv2.1.0.14
-rw-r--r--build.gradle2
-rw-r--r--src/main/java/me/shedaniel/rei/RoughlyEnoughItemsPlugin.java95
-rw-r--r--src/main/java/me/shedaniel/rei/api/IRecipePlugin.java4
-rw-r--r--src/main/java/me/shedaniel/rei/gui/widget/ItemListOverlay.java45
4 files changed, 72 insertions, 74 deletions
diff --git a/build.gradle b/build.gradle
index 946a2150a..32df55344 100644
--- a/build.gradle
+++ b/build.gradle
@@ -16,7 +16,7 @@ apply plugin: 'org.spongepowered.mixin'
apply plugin: 'java'
group 'me.shedaniel'
-version '2.1.0.13'
+version '2.1.0.14'
archivesBaseName = 'RoughlyEnoughItems'
sourceCompatibility = 1.8
diff --git a/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsPlugin.java b/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsPlugin.java
index 621730344..8f91e7e0e 100644
--- a/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsPlugin.java
+++ b/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsPlugin.java
@@ -4,7 +4,6 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
-import com.mojang.datafixers.util.Pair;
import me.shedaniel.rei.api.IRecipePlugin;
import net.minecraft.util.ResourceLocation;
import org.apache.logging.log4j.LogManager;
@@ -13,14 +12,16 @@ import org.dimdev.riftloader.ModInfo;
import org.dimdev.riftloader.RiftLoader;
import org.dimdev.riftloader.listener.InitializationListener;
-import java.io.*;
-import java.nio.file.Files;
-import java.nio.file.StandardOpenOption;
+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 {
@@ -28,10 +29,11 @@ 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());
+ RoughlyEnoughItemsPlugin.LOGGER.info("REI: Registered Plugin from %s by %s.", resourceLocation.toString(), plugin.getClass().getSimpleName());
return plugin;
}
@@ -46,66 +48,61 @@ public class RoughlyEnoughItemsPlugin implements InitializationListener {
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();
- List<Pair<ResourceLocation, String>> plugins = Lists.newArrayList();
- modInfoCollection.forEach(modContainer -> {
- JsonElement jsonArray = null;
- if (modContainer.source.isFile())
- try (JarFile file = new JarFile(modContainer.source)) {
- ZipEntry entry = file.getEntry("plugins" + File.separatorChar + "roughlyenoughitems.plugin.json");
+ 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)
- jsonArray = readFromInputStream(file.getInputStream(entry));
- } catch (Exception e) {
- RoughlyEnoughItemsPlugin.LOGGER.error("REI: Failed to read plugin file from %s. (%s)", modContainer.id, e.getLocalizedMessage());
+ loadPluginInfo(modInfo, new InputStreamReader(jarFile.getInputStream(entry)));
}
- else if (modContainer.source.isDirectory()) {
- File modInfo = new File(modContainer.source, "plugins" + File.separatorChar + "roughlyenoughitems.plugin.json");
- if (modInfo.exists())
- try {
- jsonArray = readFromInputStream(Files.newInputStream(modInfo.toPath(), StandardOpenOption.READ));
- } catch (Exception e) {
- RoughlyEnoughItemsPlugin.LOGGER.error("REI: Failed to read plugin file from %s. (%s)", modContainer.id, e.getLocalizedMessage());
- }
- }
- if (jsonArray != null && jsonArray.isJsonArray()) {
- try {
- jsonArray.getAsJsonArray().forEach(element -> {
- String id = element.getAsJsonObject().get("id").getAsString();
- String initializer = element.getAsJsonObject().get("initializer").getAsString();
- plugins.add(new Pair<>(new ResourceLocation(modContainer.id, id), initializer));
- });
- } catch (Exception e) {
- RoughlyEnoughItemsPlugin.LOGGER.error("REI: Failed to grab plugin from %s. (%s)", modContainer.id, e.getLocalizedMessage());
- }
- }
- });
- plugins.stream().forEachOrdered(pair -> {
- try {
- Class<?> aClass = Class.forName(pair.getSecond());
- IRecipePlugin plugin = IRecipePlugin.class.cast(aClass.newInstance());
- RoughlyEnoughItemsPlugin.registerPlugin(pair.getFirst(), plugin);
} catch (Exception e) {
- RoughlyEnoughItemsPlugin.LOGGER.error("REI: Failed to register plugin from %s. (%s)", pair.getFirst().toString(), e.getLocalizedMessage());
+ 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 JsonElement readFromInputStream(InputStream stream) throws IOException {
- JsonElement element = readFromReader(new InputStreamReader(stream));
- stream.close();
- return element;
+ 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 JsonElement readFromReader(Reader reader) throws IOException {
- JsonElement element = parser.parse(reader);
- reader.close();
- return element;
+ 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);
}
}
diff --git a/src/main/java/me/shedaniel/rei/api/IRecipePlugin.java b/src/main/java/me/shedaniel/rei/api/IRecipePlugin.java
index b14e598ee..13b7518bf 100644
--- a/src/main/java/me/shedaniel/rei/api/IRecipePlugin.java
+++ b/src/main/java/me/shedaniel/rei/api/IRecipePlugin.java
@@ -1,9 +1,9 @@
package me.shedaniel.rei.api;
-import me.shedaniel.rei.listeners.IListener;
-
public interface IRecipePlugin {
+ default public void onFirstLoad() {}
+
public void registerPluginCategories();
public void registerRecipes();
diff --git a/src/main/java/me/shedaniel/rei/gui/widget/ItemListOverlay.java b/src/main/java/me/shedaniel/rei/gui/widget/ItemListOverlay.java
index a6302be7a..06bf4d687 100644
--- a/src/main/java/me/shedaniel/rei/gui/widget/ItemListOverlay.java
+++ b/src/main/java/me/shedaniel/rei/gui/widget/ItemListOverlay.java
@@ -14,6 +14,7 @@ import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent;
+import org.apache.commons.lang3.StringUtils;
import java.awt.*;
import java.util.*;
@@ -67,8 +68,7 @@ public class ItemListOverlay extends Gui implements IWidget {
int j = i + page * getTotalSlotsPerPage();
if (j >= currentDisplayed.size())
break;
- widgets.add(new ItemSlotWidget((int) (startX + (i % width) * 18), (int) (startY + MathHelper.floor(i / width) * 18),
- currentDisplayed.get(j), false, true, containerGui) {
+ widgets.add(new ItemSlotWidget((int) (startX + (i % width) * 18), (int) (startY + MathHelper.floor(i / width) * 18), currentDisplayed.get(j), false, true, containerGui) {
@Override
protected void drawToolTip(ItemStack itemStack) {
EntityPlayerSP player = Minecraft.getInstance().player;
@@ -117,24 +117,27 @@ public class ItemListOverlay extends Gui implements IWidget {
});
if (!RoughlyEnoughItemsCore.getConfigHelper().isAscending())
Collections.reverse(os);
- Arrays.stream(searchTerm.split("\\|")).forEachOrdered(s -> {
- List<SearchArgument> arguments = new ArrayList<>();
- while (s.startsWith(" ")) s = s.substring(1);
- while (s.endsWith(" ")) s = s.substring(0, s.length());
- if (s.startsWith("@-") || s.startsWith("-@"))
- arguments.add(new SearchArgument(SearchArgument.ArgumentType.MOD, s.substring(2), false));
- else if (s.startsWith("@"))
- arguments.add(new SearchArgument(SearchArgument.ArgumentType.MOD, s.substring(1), true));
- else if (s.startsWith("#-") || s.startsWith("-#"))
- arguments.add(new SearchArgument(SearchArgument.ArgumentType.TOOLTIP, s.substring(2), false));
- else if (s.startsWith("#"))
- arguments.add(new SearchArgument(SearchArgument.ArgumentType.TOOLTIP, s.substring(1), true));
- else if (s.startsWith("-"))
- arguments.add(new SearchArgument(SearchArgument.ArgumentType.TEXT, s.substring(1), false));
- else
- arguments.add(new SearchArgument(SearchArgument.ArgumentType.TEXT, s, true));
- os.stream().filter(itemStack -> filterItem(itemStack, arguments)).forEachOrdered(stacks::add);
+ String[] splitSearchTerm = StringUtils.splitByWholeSeparatorPreserveAllTokens(searchTerm, "|");
+ Arrays.stream(splitSearchTerm).forEachOrdered(s -> {
+ List<SearchArgument> arguments = Lists.newArrayList();
+ Arrays.stream(StringUtils.split(s)).forEachOrdered(s1 -> {
+ if (s1.startsWith("@-") || s1.startsWith("-@"))
+ arguments.add(new SearchArgument(SearchArgument.ArgumentType.MOD, s1.substring(2), false));
+ else if (s1.startsWith("@"))
+ arguments.add(new SearchArgument(SearchArgument.ArgumentType.MOD, s1.substring(1), true));
+ else if (s1.startsWith("#-") || s1.startsWith("-#"))
+ arguments.add(new SearchArgument(SearchArgument.ArgumentType.TOOLTIP, s1.substring(2), false));
+ else if (s1.startsWith("#"))
+ arguments.add(new SearchArgument(SearchArgument.ArgumentType.TOOLTIP, s1.substring(1), true));
+ else if (s1.startsWith("-"))
+ arguments.add(new SearchArgument(SearchArgument.ArgumentType.TEXT, s1.substring(1), false));
+ else
+ arguments.add(new SearchArgument(SearchArgument.ArgumentType.TEXT, s1, true));
+ });
+ os.stream().filter(itemStack -> arguments.isEmpty() || filterItem(itemStack, arguments)).forEachOrdered(stacks::add);
});
+ if (splitSearchTerm.length == 0)
+ stacks.addAll(os);
List<ItemStack> workingItems = RoughlyEnoughItemsCore.getConfigHelper().craftableOnly() && inventoryItems.size() > 0 ? new ArrayList<>() : new LinkedList<>(ol);
if (RoughlyEnoughItemsCore.getConfigHelper().craftableOnly()) {
RecipeHelper.findCraftableByItems(inventoryItems).forEach(workingItems::add);
@@ -184,9 +187,7 @@ public class ItemListOverlay extends Gui implements IWidget {
private List<String> getStackTooltip(ItemStack itemStack) {
Minecraft client = Minecraft.getInstance();
- return itemStack.getTooltip(client.player, client.gameSettings.advancedItemTooltips ?
- ITooltipFlag.TooltipFlags.ADVANCED : ITooltipFlag.TooltipFlags.NORMAL).stream().map(
- ITextComponent::getFormattedText).collect(Collectors.toList());
+ return itemStack.getTooltip(client.player, client.gameSettings.advancedItemTooltips ? ITooltipFlag.TooltipFlags.ADVANCED : ITooltipFlag.TooltipFlags.NORMAL).stream().map(ITextComponent::getFormattedText).collect(Collectors.toList());
}
private void calculateListSize(Rectangle rect) {