diff options
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java')
| -rw-r--r-- | src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java | 212 |
1 files changed, 89 insertions, 123 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java b/src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java index 0f4e58bf..064b1fa7 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java @@ -1,14 +1,13 @@ package io.github.moulberry.notenoughupdates; -import com.google.common.collect.Lists; import com.google.gson.*; import io.github.moulberry.notenoughupdates.auction.APIManager; import io.github.moulberry.notenoughupdates.miscgui.GuiItemRecipe; import io.github.moulberry.notenoughupdates.overlays.CraftingOverlay; -import io.github.moulberry.notenoughupdates.util.Constants; -import io.github.moulberry.notenoughupdates.util.HypixelApi; -import io.github.moulberry.notenoughupdates.util.SBInfo; -import io.github.moulberry.notenoughupdates.util.Utils; +import io.github.moulberry.notenoughupdates.recipes.CraftingRecipe; +import io.github.moulberry.notenoughupdates.recipes.Ingredient; +import io.github.moulberry.notenoughupdates.recipes.NeuRecipe; +import io.github.moulberry.notenoughupdates.util.*; import net.minecraft.client.Minecraft; import net.minecraft.client.settings.KeyBinding; import net.minecraft.init.Blocks; @@ -17,8 +16,8 @@ import net.minecraft.inventory.ContainerChest; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.*; -import net.minecraft.network.play.client.C0DPacketCloseWindow; import net.minecraft.util.ResourceLocation; +import net.minecraftforge.fml.common.ProgressManager; import org.apache.commons.io.FileUtils; import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.Display; @@ -68,18 +67,26 @@ public class NEUManager { private static String GIT_COMMITS_URL; - private final HashMap<String, Set<String>> usagesMap = new HashMap<>(); + // TODO: private final Map<String, NeuItem> + + private final Set<NeuRecipe> recipes = new HashSet<>(); + private final HashMap<String, Set<NeuRecipe>> recipesMap = new HashMap<>(); + private final HashMap<String, Set<NeuRecipe>> usagesMap = new HashMap<>(); public String latestRepoCommit = null; public File configLocation; public File repoLocation; public File configFile; + public HotmInformation hotm; public NEUManager(NotEnoughUpdates neu, File configLocation) { this.neu = neu; this.configLocation = configLocation; this.auctionManager = new APIManager(this); + this.hotm = new HotmInformation(neu); + + GIT_COMMITS_URL = neu.config.hidden.repoCommitsURL; gson = new GsonBuilder().setPrettyPrinting().create(); @@ -261,14 +268,17 @@ public class NEUManager { if (items.exists()) { File[] itemFiles = new File(repoLocation, "items").listFiles(); if (itemFiles != null) { + ProgressManager.ProgressBar bar = ProgressManager.push("Loading recipes", itemFiles.length); for (File f : itemFiles) { String internalname = f.getName().substring(0, f.getName().length() - 5); + bar.step(internalname); synchronized (itemMap) { if (!itemMap.containsKey(internalname)) { loadItem(internalname); } } } + ProgressManager.pop(bar); } } @@ -283,14 +293,17 @@ public class NEUManager { if (items.exists()) { File[] itemFiles = new File(repoLocation, "items").listFiles(); if (itemFiles != null) { + ProgressManager.ProgressBar bar = ProgressManager.push("Loading items", itemFiles.length); for (File f : itemFiles) { String internalname = f.getName().substring(0, f.getName().length() - 5); + bar.step(internalname); synchronized (itemMap) { if (!itemMap.containsKey(internalname)) { loadItem(internalname); } } } + ProgressManager.pop(bar); } } @@ -325,23 +338,17 @@ public class NEUManager { itemMap.put(internalName, json); if (json.has("recipe")) { - synchronized (usagesMap) { - JsonObject recipe = json.get("recipe").getAsJsonObject(); - - String[] x = {"1", "2", "3"}; - String[] y = {"A", "B", "C"}; - for (int i = 0; i < 9; i++) { - String name = y[i / 3] + x[i % 3]; - String itemS = recipe.get(name).getAsString(); - if (itemS != null && itemS.split(":").length == 2) { - itemS = itemS.split(":")[0]; - } - - if (!usagesMap.containsKey(itemS)) { - usagesMap.put(itemS, new HashSet<>()); - } - usagesMap.get(itemS).add(internalName); - } + JsonObject recipe = json.getAsJsonObject("recipe"); + NeuRecipe neuRecipe = NeuRecipe.parseRecipe(this, recipe, json); + if (neuRecipe != null) + registerNeuRecipe(neuRecipe); + } + if (json.has("recipes")) { + for (JsonElement element : json.getAsJsonArray("recipes")) { + JsonObject recipe = element.getAsJsonObject(); + NeuRecipe neuRecipe = NeuRecipe.parseRecipe(this, recipe, json); + if (neuRecipe != null) + registerNeuRecipe(neuRecipe); } } @@ -392,6 +399,24 @@ public class NEUManager { } } + public void registerNeuRecipe(NeuRecipe recipe) { + recipes.add(recipe); + for (Ingredient output : recipe.getOutputs()) { + recipesMap.computeIfAbsent(output.getInternalItemId(), ignored -> new HashSet<>()).add(recipe); + } + for (Ingredient input : recipe.getIngredients()) { + usagesMap.computeIfAbsent(input.getInternalItemId(), ignored -> new HashSet<>()).add(recipe); + } + } + + public Set<NeuRecipe> getRecipesFor(String internalName) { + return recipesMap.getOrDefault(internalName, Collections.emptySet()); + } + + public Set<NeuRecipe> getUsagesFor(String internalName) { + return usagesMap.getOrDefault(internalName, Collections.emptySet()); + } + /** * Searches a string for a query. This method is used to mimic the behaviour of the * more complex map-based search function. This method is used for the chest-item-search feature. @@ -820,27 +845,25 @@ public class NEUManager { ContainerChest container = null; if (Minecraft.getMinecraft().thePlayer.openContainer instanceof ContainerChest) container = (ContainerChest) Minecraft.getMinecraft().thePlayer.openContainer; - if (item.has("recipe") && container != null && container.getLowerChestInventory().getDisplayName().getUnformattedText().equals("Craft Item")) { - CraftingOverlay.updateItem(item); - } else if (item.has("useneucraft") && item.get("useneucraft").getAsBoolean()) { - displayGuiItemRecipe(item.get("internalname").getAsString(), ""); - } else if (item.has("clickcommand")) { - String clickcommand = item.get("clickcommand").getAsString(); - - if (clickcommand.equals("viewrecipe")) { - neu.sendChatMessage( - "/" + clickcommand + " " + - item.get("internalname").getAsString().split(";")[0]); - viewItemAttemptID = item.get("internalname").getAsString(); - viewItemAttemptTime = System.currentTimeMillis(); - } else if (clickcommand.equals("viewpotion")) { - neu.sendChatMessage( - "/" + clickcommand + " " + - item.get("internalname").getAsString().split(";")[0].toLowerCase()); - viewItemAttemptID = item.get("internalname").getAsString(); - viewItemAttemptTime = System.currentTimeMillis(); + String internalName = item.get("internalname").getAsString(); + Set<NeuRecipe> recipesFor = getRecipesFor(internalName); + if (container != null && container.getLowerChestInventory().getDisplayName().getUnformattedText().equals("Craft Item")) { + Optional<NeuRecipe> recipe = recipesFor.stream().filter(it -> it instanceof CraftingRecipe).findAny(); + if (recipe.isPresent()) { + CraftingOverlay.updateItem((CraftingRecipe) recipe.get()); + return; } } + if (!item.has("clickcommand")) return; + String clickcommand = item.get("clickcommand").getAsString(); + switch (clickcommand.intern()) { + case "viewrecipe": + displayGuiItemRecipe(internalName, null); + break; + case "viewoption": + neu.sendChatMessage("/viewpotion " + internalName.split(";")[0].toLowerCase(Locale.ROOT)); + } + displayGuiItemRecipe(internalName, ""); } /** @@ -920,90 +943,24 @@ public class NEUManager { loadItem(internalname); } - /** - * Constructs a GuiItemUsages from the recipe usage data (see #usagesMap) of a given item - */ public boolean displayGuiItemUsages(String internalName) { - List<ItemStack[]> craftMatrices = new ArrayList<>(); - List<JsonObject> results = new ArrayList<>(); - - if (!usagesMap.containsKey(internalName)) { - return false; - } - - for (String internalNameResult : usagesMap.get(internalName)) { - JsonObject item = getItemInformation().get(internalNameResult); - results.add(item); - - if (item != null && item.has("recipe")) { - JsonObject recipe = item.get("recipe").getAsJsonObject(); - - ItemStack[] craftMatrix = new ItemStack[9]; - - String[] x = {"1", "2", "3"}; - String[] y = {"A", "B", "C"}; - for (int i = 0; i < 9; i++) { - String name = y[i / 3] + x[i % 3]; - String itemS = recipe.get(name).getAsString(); - int count = 1; - if (itemS != null && itemS.split(":").length == 2) { - count = Integer.parseInt(itemS.split(":")[1]); - itemS = itemS.split(":")[0]; - } - JsonObject craft = getItemInformation().get(itemS); - if (craft != null) { - ItemStack stack = jsonToStack(craft); - stack.stackSize = count; - craftMatrix[i] = stack; - } - } - - craftMatrices.add(craftMatrix); - } - } - - if (craftMatrices.size() > 0) { - Minecraft.getMinecraft().displayGuiScreen(new GuiItemRecipe("Item Usages", craftMatrices, results, this)); - return true; - } - return false; + if (!usagesMap.containsKey(internalName)) return false; + Set<NeuRecipe> usages = usagesMap.get(internalName); + if (usages.isEmpty()) return false; + Utils.sendCloseScreenPacket(); + Minecraft.getMinecraft().displayGuiScreen( + new GuiItemRecipe("Item Usages", new ArrayList<>(usages), this)); + return true; } - /** - * Constructs a GuiItemRecipeOld from the recipe data of a given item. - */ public boolean displayGuiItemRecipe(String internalName, String text) { - JsonObject item = getItemInformation().get(internalName); - if (item != null && item.has("recipe")) { - JsonObject recipe = item.get("recipe").getAsJsonObject(); - - ItemStack[] craftMatrix = new ItemStack[9]; - - String[] x = {"1", "2", "3"}; - String[] y = {"A", "B", "C"}; - for (int i = 0; i < 9; i++) { - String name = y[i / 3] + x[i % 3]; - String itemS = recipe.get(name).getAsString(); - int count = 1; - if (itemS != null && itemS.split(":").length == 2) { - count = Integer.parseInt(itemS.split(":")[1]); - itemS = itemS.split(":")[0]; - } - JsonObject craft = getItemInformation().get(itemS); - if (craft != null) { - ItemStack stack = jsonToStack(craft); - stack.stackSize = count; - craftMatrix[i] = stack; - } - } - - Minecraft.getMinecraft().thePlayer.sendQueue.addToSendQueue(new C0DPacketCloseWindow( - Minecraft.getMinecraft().thePlayer.openContainer.windowId)); - Minecraft.getMinecraft().displayGuiScreen(new GuiItemRecipe(text != null ? text : "Item Recipe", - Lists.<ItemStack[]>newArrayList(craftMatrix), Lists.newArrayList(item), this)); - return true; - } - return false; + if (!recipesMap.containsKey(internalName)) return false; + Set<NeuRecipe> recipes = recipesMap.get(internalName); + if (recipes.isEmpty()) return false; + Utils.sendCloseScreenPacket(); + Minecraft.getMinecraft().displayGuiScreen( + new GuiItemRecipe(text != null ? text : "Item Recipe", new ArrayList<>(recipes), this)); + return true; } /** @@ -1207,6 +1164,15 @@ public class NEUManager { writeJson(json, file); } + public JsonObject readJsonDefaultDir(String filename) throws IOException { + File f = new File(new File(repoLocation, "items"), filename); + if (f.exists() && f.isFile() && f.canRead()) + try (Reader reader = new InputStreamReader(new FileInputStream(f), StandardCharsets.UTF_8)) { + return gson.fromJson(reader, JsonObject.class); + } // rethrow io exceptions + return null; + } + public TreeMap<String, JsonObject> getItemInformation() { return itemMap; } |
