aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/shedaniel/rei/client
diff options
context:
space:
mode:
authorUnknown <shekwancheung0528@gmail.com>2019-01-10 23:37:34 +0800
committerUnknown <shekwancheung0528@gmail.com>2019-01-10 23:37:34 +0800
commit537dbb95f6429c03f1f0a57cb96acb242d4572ad (patch)
tree38d045d36135a79ca32011dfae7d011333ad7804 /src/main/java/me/shedaniel/rei/client
parentc29f736c5d29c7c0ae2ba189c4bc072aca12ebbd (diff)
downloadRoughlyEnoughItems-537dbb95f6429c03f1f0a57cb96acb242d4572ad.tar.gz
RoughlyEnoughItems-537dbb95f6429c03f1f0a57cb96acb242d4572ad.tar.bz2
RoughlyEnoughItems-537dbb95f6429c03f1f0a57cb96acb242d4572ad.zip
Basic Searching Works
Diffstat (limited to 'src/main/java/me/shedaniel/rei/client')
-rw-r--r--src/main/java/me/shedaniel/rei/client/ClientHelper.java20
-rw-r--r--src/main/java/me/shedaniel/rei/client/ConfigManager.java75
-rw-r--r--src/main/java/me/shedaniel/rei/client/REIConfig.java22
-rw-r--r--src/main/java/me/shedaniel/rei/client/REIItemListOrdering.java21
-rw-r--r--src/main/java/me/shedaniel/rei/client/RecipeHelper.java100
-rw-r--r--src/main/java/me/shedaniel/rei/client/SearchArgument.java36
6 files changed, 271 insertions, 3 deletions
diff --git a/src/main/java/me/shedaniel/rei/client/ClientHelper.java b/src/main/java/me/shedaniel/rei/client/ClientHelper.java
index b0bc64a98..8c1cd472e 100644
--- a/src/main/java/me/shedaniel/rei/client/ClientHelper.java
+++ b/src/main/java/me/shedaniel/rei/client/ClientHelper.java
@@ -3,7 +3,12 @@ package me.shedaniel.rei.client;
import com.google.common.collect.Lists;
import io.netty.buffer.Unpooled;
import me.shedaniel.rei.RoughlyEnoughItemsCore;
+import me.shedaniel.rei.api.IRecipeCategory;
+import me.shedaniel.rei.api.IRecipeDisplay;
+import me.shedaniel.rei.gui.ContainerGuiOverlay;
+import me.shedaniel.rei.gui.widget.RecipeViewingWidget;
import me.shedaniel.rei.listeners.ClientLoaded;
+import me.shedaniel.rei.listeners.IMixinContainerGui;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.loader.FabricLoader;
import net.minecraft.client.MinecraftClient;
@@ -19,9 +24,11 @@ import net.minecraft.util.PacketByteBuf;
import net.minecraft.util.registry.Registry;
import java.awt.*;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.stream.Collectors;
public class ClientHelper implements ClientLoaded, ClientModInitializer {
@@ -69,6 +76,10 @@ public class ClientHelper implements ClientLoaded, ClientModInitializer {
return cheating;
}
+ public static void setCheating(boolean cheating) {
+ ClientHelper.cheating = cheating;
+ }
+
public static void sendDeletePacket() {
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
MinecraftClient.getInstance().getNetworkHandler().sendPacket(new CustomPayloadServerPacket(RoughlyEnoughItemsCore.DELETE_ITEMS_PACKET, buf));
@@ -85,8 +96,11 @@ public class ClientHelper implements ClientLoaded, ClientModInitializer {
}
}
- public static boolean executeRecipeKeyBind() {
- return false;
+ public static boolean executeRecipeKeyBind(ContainerGuiOverlay overlay, ItemStack stack, IMixinContainerGui parent) {
+ Map<IRecipeCategory, List<IRecipeDisplay>> map = RecipeHelper.getRecipesFor(stack);
+ if (map.keySet().size() > 0)
+ MinecraftClient.getInstance().openGui(new RecipeViewingWidget(overlay, MinecraftClient.getInstance().window, parent, map));
+ return map.keySet().size() > 0;
}
public static boolean executeUsageKeyBind() {
@@ -118,7 +132,7 @@ public class ClientHelper implements ClientLoaded, ClientModInitializer {
@Override
public void onInitializeClient() {
- this.cheating = true;
+ this.cheating = false;
}
}
diff --git a/src/main/java/me/shedaniel/rei/client/ConfigManager.java b/src/main/java/me/shedaniel/rei/client/ConfigManager.java
new file mode 100644
index 000000000..a2d4d9890
--- /dev/null
+++ b/src/main/java/me/shedaniel/rei/client/ConfigManager.java
@@ -0,0 +1,75 @@
+package me.shedaniel.rei.client;
+
+import me.shedaniel.rei.RoughlyEnoughItemsCore;
+import net.fabricmc.loader.FabricLoader;
+import org.apache.logging.log4j.core.Core;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.nio.file.Files;
+
+public class ConfigManager {
+
+ private final File configFile;
+ private REIConfig config;
+ private boolean craftableOnly;
+
+ public ConfigManager() {
+ this.configFile = new File(FabricLoader.INSTANCE.getConfigDirectory(), "rei.json");
+ this.craftableOnly = false;
+ try {
+ loadConfig();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void saveConfig() throws IOException {
+ configFile.getParentFile().mkdirs();
+ if (!configFile.exists() && !configFile.createNewFile()) {
+ RoughlyEnoughItemsCore.LOGGER.error("REI: Failed to save config! Overwriting with default config.");
+ config = new REIConfig();
+ return;
+ }
+ FileWriter writer = new FileWriter(configFile, false);
+ try {
+ REIConfig.GSON.toJson(config, writer);
+ } finally {
+ writer.close();
+ }
+ }
+
+ public void loadConfig() throws IOException {
+ if (!configFile.exists() || !configFile.canRead()) {
+ config = new REIConfig();
+ saveConfig();
+ return;
+ }
+ boolean failed = false;
+ try {
+ config = REIConfig.GSON.fromJson(new InputStreamReader(Files.newInputStream(configFile.toPath())), REIConfig.class);
+ } catch (Exception e) {
+ failed = true;
+ }
+ if (failed || config == null) {
+ RoughlyEnoughItemsCore.LOGGER.error("REI: Failed to load config! Overwriting with default config.");
+ config = new REIConfig();
+ }
+ saveConfig();
+ }
+
+ public REIItemListOrdering getItemListOrdering() {
+ return config.itemListOrdering;
+ }
+
+ public boolean isAscending() {
+ return config.isAscending;
+ }
+
+ public boolean craftableOnly() {
+ return craftableOnly && config.enableCraftableOnlyButton;
+ }
+
+}
diff --git a/src/main/java/me/shedaniel/rei/client/REIConfig.java b/src/main/java/me/shedaniel/rei/client/REIConfig.java
new file mode 100644
index 000000000..dfc961f28
--- /dev/null
+++ b/src/main/java/me/shedaniel/rei/client/REIConfig.java
@@ -0,0 +1,22 @@
+package me.shedaniel.rei.client;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
+import java.awt.event.KeyEvent;
+
+public class REIConfig {
+
+ public static Gson GSON = new GsonBuilder()
+ .setPrettyPrinting()
+ .create();
+
+ public int recipeKeyBind = KeyEvent.VK_R;
+ public int usageKeyBind = KeyEvent.VK_U;
+ public int hideKeyBind = KeyEvent.VK_O;
+ public boolean centreSearchBox = false;
+ public REIItemListOrdering itemListOrdering = REIItemListOrdering.REGISTRY;
+ public boolean isAscending = true;
+ public boolean enableCraftableOnlyButton = true;
+
+}
diff --git a/src/main/java/me/shedaniel/rei/client/REIItemListOrdering.java b/src/main/java/me/shedaniel/rei/client/REIItemListOrdering.java
new file mode 100644
index 000000000..b0167d352
--- /dev/null
+++ b/src/main/java/me/shedaniel/rei/client/REIItemListOrdering.java
@@ -0,0 +1,21 @@
+package me.shedaniel.rei.client;
+
+import com.google.gson.annotations.SerializedName;
+
+public enum REIItemListOrdering {
+
+ @SerializedName("registry") REGISTRY("ordering.rei.registry"),
+ @SerializedName("name") NAME("ordering.rei.name"),
+ @SerializedName("item_groups") ITEM_GROUPS("ordering.rei.item_groups");
+
+ private String nameTranslationKey;
+
+ REIItemListOrdering(String nameTranslationKey) {
+ this.nameTranslationKey = nameTranslationKey;
+ }
+
+ public String getNameTranslationKey() {
+ return nameTranslationKey;
+ }
+
+}
diff --git a/src/main/java/me/shedaniel/rei/client/RecipeHelper.java b/src/main/java/me/shedaniel/rei/client/RecipeHelper.java
new file mode 100644
index 000000000..74780846c
--- /dev/null
+++ b/src/main/java/me/shedaniel/rei/client/RecipeHelper.java
@@ -0,0 +1,100 @@
+package me.shedaniel.rei.client;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import me.shedaniel.rei.RoughlyEnoughItemsCore;
+import me.shedaniel.rei.api.IRecipeCategory;
+import me.shedaniel.rei.api.IRecipeDisplay;
+import me.shedaniel.rei.api.IRecipePlugin;
+import me.shedaniel.rei.listeners.RecipeSync;
+import net.minecraft.item.ItemStack;
+import net.minecraft.recipe.RecipeManager;
+import net.minecraft.util.Identifier;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+public class RecipeHelper implements RecipeSync {
+
+ private static Map<Identifier, List<IRecipeDisplay>> recipeCategoryListMap;
+ private static List<IRecipeCategory> categories;
+ private static RecipeManager recipeManager;
+
+ public RecipeHelper() {
+ this.recipeCategoryListMap = Maps.newHashMap();
+ this.categories = Lists.newArrayList();
+ }
+
+ public static List<ItemStack> findCraftableByItems(List<ItemStack> inventoryItems) {
+ List<ItemStack> craftables = new ArrayList<>();
+ for(List<IRecipeDisplay> value : recipeCategoryListMap.values())
+ for(IRecipeDisplay recipeDisplay : value) {
+ int slotsCraftable = 0;
+ List<List<ItemStack>> requiredInput = (List<List<ItemStack>>) recipeDisplay.getRequiredItems();
+ for(List<ItemStack> slot : requiredInput) {
+ if (slot.isEmpty()) {
+ slotsCraftable++;
+ continue;
+ }
+ boolean slotDone = false;
+ for(ItemStack possibleType : inventoryItems) {
+ for(ItemStack slotPossible : slot)
+ if (ItemStack.areEqualIgnoreTags(slotPossible, possibleType)) {
+ slotsCraftable++;
+ slotDone = true;
+ break;
+ }
+ if (slotDone)
+ break;
+ }
+ }
+ if (slotsCraftable == recipeDisplay.getRequiredItems().size())
+ craftables.addAll((List<ItemStack>) recipeDisplay.getOutput());
+ }
+ return craftables.stream().distinct().collect(Collectors.toList());
+ }
+
+ public static void registerCategory(IRecipeCategory category) {
+ categories.add(category);
+ recipeCategoryListMap.put(category.getIdentifier(), Lists.newArrayList());
+ }
+
+ public static void registerRecipe(Identifier categoryIdentifier, IRecipeDisplay display) {
+ if (!recipeCategoryListMap.containsKey(categoryIdentifier))
+ return;
+ recipeCategoryListMap.get(categoryIdentifier).add(display);
+ }
+
+ public static Map<IRecipeCategory, List<IRecipeDisplay>> getRecipesFor(ItemStack stack) {
+ Map<Identifier, List<IRecipeDisplay>> categoriesMap = new HashMap<>();
+ categories.forEach(f -> categoriesMap.put(f.getIdentifier(), new LinkedList<>()));
+ for(List<IRecipeDisplay> value : recipeCategoryListMap.values())
+ for(IRecipeDisplay recipeDisplay : value)
+ for(ItemStack outputStack : (List<ItemStack>) recipeDisplay.getOutput())
+ if (ItemStack.areEqualIgnoreTags(stack, outputStack))
+ categoriesMap.get(recipeDisplay.getRecipeCategory()).add(recipeDisplay);
+ categoriesMap.keySet().removeIf(f -> categoriesMap.get(f).isEmpty());
+ Map<IRecipeCategory, List<IRecipeDisplay>> recipeCategoryListMap = Maps.newHashMap();
+ categories.forEach(category -> {
+ if (categoriesMap.containsKey(category.getIdentifier()))
+ recipeCategoryListMap.put(category, categoriesMap.get(category.getIdentifier()));
+ });
+ return recipeCategoryListMap;
+ }
+
+ public static RecipeManager getRecipeManager() {
+ return recipeManager;
+ }
+
+ @Override
+ public void recipesLoaded(RecipeManager recipeManager) {
+ this.recipeManager = recipeManager;
+ this.recipeCategoryListMap.clear();
+ this.categories.clear();
+ RoughlyEnoughItemsCore.getListeners(IRecipePlugin.class).forEach(plugin -> {
+ plugin.registerPluginCategories();
+ plugin.registerRecipes();
+ });
+ }
+
+}
diff --git a/src/main/java/me/shedaniel/rei/client/SearchArgument.java b/src/main/java/me/shedaniel/rei/client/SearchArgument.java
new file mode 100644
index 000000000..445214871
--- /dev/null
+++ b/src/main/java/me/shedaniel/rei/client/SearchArgument.java
@@ -0,0 +1,36 @@
+package me.shedaniel.rei.client;
+
+public class SearchArgument {
+
+ public enum ArgumentType {
+ TEXT, MOD, TOOLTIP
+ }
+
+ private ArgumentType argumentType;
+ private String text;
+ private boolean include;
+
+ public SearchArgument(ArgumentType argumentType, String text, boolean include) {
+ this.argumentType = argumentType;
+ this.text = text;
+ this.include = include;
+ }
+
+ public ArgumentType getArgumentType() {
+ return argumentType;
+ }
+
+ public String getText() {
+ return text;
+ }
+
+ public boolean isInclude() {
+ return include;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("Argument[%s]: name = %s, include = %b", argumentType.name(), text, include);
+ }
+
+}