From b0b0b7567ec0656c60f2f3e4730c0edace353fb7 Mon Sep 17 00:00:00 2001 From: Roman / Nea Date: Thu, 30 Dec 2021 14:37:02 +0100 Subject: Adding support for more recipe types and forge recipes (#40) * Foundations for support of different crafting recipe types. NeuRecipe is now a base class for a recipe which provides common concepts, such as inputs, outputs and a rendering task. GuiItemRecipe has been reworked to work with this new NeuRecipe. NeuManager now parses said recipes. This should be reworked to be a two step process (first register items, then register recipes). To keep compatibility with older repo versions, NeuRecipes are parse lenient and default to a crafting recipe. New recipes should be added in the `recipes` json field which is an array of json dictionaries, which have a `type` and other fields depending on the `type` of that recipe. This also adds support for having multiple recipes for a single item (e.g. uncrafting storage blocks). * Remove references in existing code * Recipe Generation * ring recipes * recipe generator v2 * quick forge * bugfixes and performance improvements * fix raw craft cost * reload hotm if you open the hotm tree inv * add myself to the changelog * replace quickforge formular with lookup table * do not crash anymore when opening recipes outside of skyblock * format coins differently * remove debug logs * change recipe generator so that it doesnt crash old versions --- Update Notes/2.1.md | 2 + .../notenoughupdates/ItemPriceInformation.java | 4 +- .../moulberry/notenoughupdates/NEUManager.java | 212 +++++++---------- .../notenoughupdates/NotEnoughUpdates.java | 2 + .../notenoughupdates/auction/APIManager.java | 138 ++++++----- .../notenoughupdates/commands/Commands.java | 2 +- .../notenoughupdates/miscgui/GuiItemRecipe.java | 257 +++++++++------------ .../notenoughupdates/overlays/CraftingOverlay.java | 44 ++-- .../notenoughupdates/recipes/CraftingRecipe.java | 141 +++++++++++ .../notenoughupdates/recipes/ForgeRecipe.java | 220 ++++++++++++++++++ .../notenoughupdates/recipes/Ingredient.java | 88 +++++++ .../notenoughupdates/recipes/NeuRecipe.java | 39 ++++ .../notenoughupdates/recipes/RecipeGenerator.java | 180 +++++++++++++++ .../notenoughupdates/recipes/RecipeSlot.java | 28 +++ .../moulberry/notenoughupdates/util/Debouncer.java | 34 +++ .../notenoughupdates/util/HotmInformation.java | 179 ++++++++++++++ .../notenoughupdates/util/HypixelApi.java | 36 ++- .../moulberry/notenoughupdates/util/Utils.java | 38 ++- .../notenoughupdates/textures/gui/forge_recipe.png | Bin 0 -> 889 bytes 19 files changed, 1263 insertions(+), 381 deletions(-) create mode 100644 src/main/java/io/github/moulberry/notenoughupdates/recipes/CraftingRecipe.java create mode 100644 src/main/java/io/github/moulberry/notenoughupdates/recipes/ForgeRecipe.java create mode 100644 src/main/java/io/github/moulberry/notenoughupdates/recipes/Ingredient.java create mode 100644 src/main/java/io/github/moulberry/notenoughupdates/recipes/NeuRecipe.java create mode 100644 src/main/java/io/github/moulberry/notenoughupdates/recipes/RecipeGenerator.java create mode 100644 src/main/java/io/github/moulberry/notenoughupdates/recipes/RecipeSlot.java create mode 100644 src/main/java/io/github/moulberry/notenoughupdates/util/Debouncer.java create mode 100644 src/main/java/io/github/moulberry/notenoughupdates/util/HotmInformation.java create mode 100644 src/main/resources/assets/notenoughupdates/textures/gui/forge_recipe.png diff --git a/Update Notes/2.1.md b/Update Notes/2.1.md index 325ed5ae..6c5fb48b 100644 --- a/Update Notes/2.1.md +++ b/Update Notes/2.1.md @@ -11,6 +11,7 @@ - [Donpireso replied to an sba dev's email about some of sba features and it seems to imply that blocking clicks in guis arent bannable](https://cdn.discordapp.com/attachments/823769568933576764/906101631861526559/unknown.png) - Fixed pet overlay not updating when going into /pets ### **Minor Changes:** +- Add built-in recipes for forge crafts - nea89 - Make cata xp in /pv be calculated on how many runs you have and shows master mode xp rates - Hide mine waypoints when at location setting - Lulonaut - Added some info panels to some settings in /neu @@ -42,6 +43,7 @@ - Added a warning in the tooltip when price info couldn't be found/is outdated - Lulonaut - Added custom runes and crab hat system - jani ### **Bug Fixes** +- Fix wiki pages freezing the entire game - nea89 - Made titanium overlay and waypoints work with dwarven overlay off - "fixed" divan rarity in NEUAH (scuffed) - Made etherwarp block overlay config option diff --git a/src/main/java/io/github/moulberry/notenoughupdates/ItemPriceInformation.java b/src/main/java/io/github/moulberry/notenoughupdates/ItemPriceInformation.java index 5c26e3d6..e78c117b 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/ItemPriceInformation.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/ItemPriceInformation.java @@ -152,7 +152,7 @@ public class ItemPriceInformation { } break; case 4: - if (craftCost.fromRecipe) { + if (craftCost != null && craftCost.fromRecipe) { if ((int) craftCost.craftCost == 0) { continue; } @@ -224,7 +224,7 @@ public class ItemPriceInformation { } break; case 3: - if (craftCost.fromRecipe) { + if (craftCost != null && craftCost.fromRecipe) { if ((int) craftCost.craftCost == 0) { continue; } 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> usagesMap = new HashMap<>(); + // TODO: private final Map + + private final Set recipes = new HashSet<>(); + private final HashMap> recipesMap = new HashMap<>(); + private final HashMap> 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 getRecipesFor(String internalName) { + return recipesMap.getOrDefault(internalName, Collections.emptySet()); + } + + public Set 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 recipesFor = getRecipesFor(internalName); + if (container != null && container.getLowerChestInventory().getDisplayName().getUnformattedText().equals("Craft Item")) { + Optional 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 craftMatrices = new ArrayList<>(); - List 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 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.newArrayList(craftMatrix), Lists.newArrayList(item), this)); - return true; - } - return false; + if (!recipesMap.containsKey(internalName)) return false; + Set 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 getItemInformation() { return itemMap; } diff --git a/src/main/java/io/github/moulberry/notenoughupdates/NotEnoughUpdates.java b/src/main/java/io/github/moulberry/notenoughupdates/NotEnoughUpdates.java index 23b89b44..3a6afef8 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/NotEnoughUpdates.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/NotEnoughUpdates.java @@ -15,6 +15,7 @@ import io.github.moulberry.notenoughupdates.options.NEUConfig; import io.github.moulberry.notenoughupdates.overlays.FuelBar; import io.github.moulberry.notenoughupdates.overlays.OverlayManager; import io.github.moulberry.notenoughupdates.profileviewer.ProfileViewer; +import io.github.moulberry.notenoughupdates.recipes.RecipeGenerator; import io.github.moulberry.notenoughupdates.util.SBInfo; import io.github.moulberry.notenoughupdates.util.Utils; import io.github.moulberry.notenoughupdates.util.XPInformation; @@ -131,6 +132,7 @@ public class NotEnoughUpdates { MinecraftForge.EVENT_BUS.register(this); MinecraftForge.EVENT_BUS.register(new NEUEventListener(this)); + MinecraftForge.EVENT_BUS.register(new RecipeGenerator(this)); MinecraftForge.EVENT_BUS.register(CapeManager.getInstance()); //MinecraftForge.EVENT_BUS.register(new SBGamemodes()); MinecraftForge.EVENT_BUS.register(new EnchantingSolvers()); diff --git a/src/main/java/io/github/moulberry/notenoughupdates/auction/APIManager.java b/src/main/java/io/github/moulberry/notenoughupdates/auction/APIManager.java index 254ab5cf..d0b4a7f5 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/auction/APIManager.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/auction/APIManager.java @@ -7,6 +7,8 @@ import io.github.moulberry.notenoughupdates.ItemPriceInformation; import io.github.moulberry.notenoughupdates.NEUManager; import io.github.moulberry.notenoughupdates.NotEnoughUpdates; import io.github.moulberry.notenoughupdates.miscgui.GuiPriceGraph; +import io.github.moulberry.notenoughupdates.recipes.Ingredient; +import io.github.moulberry.notenoughupdates.recipes.NeuRecipe; import io.github.moulberry.notenoughupdates.util.Constants; import io.github.moulberry.notenoughupdates.util.Utils; import net.minecraft.client.Minecraft; @@ -760,87 +762,77 @@ public class APIManager { } public CraftInfo getCraftCost(String internalname) { - return getCraftCost(internalname, 0); + return getCraftCost(internalname, new HashSet<>()); } /** * Recursively calculates the cost of crafting an item from raw materials. */ - public CraftInfo getCraftCost(String internalname, int depth) { - if (craftCost.containsKey(internalname)) { - return craftCost.get(internalname); - } else { - CraftInfo ci = new CraftInfo(); - - ci.vanillaItem = isVanillaItem(internalname); - - JsonObject auctionInfo = getItemAuctionInfo(internalname); - float lowestBin = getLowestBin(internalname); - JsonObject bazaarInfo = getBazaarInfo(internalname); - - if (bazaarInfo != null && bazaarInfo.get("curr_buy") != null) { - float bazaarInstantBuyPrice = bazaarInfo.get("curr_buy").getAsFloat(); - ci.craftCost = bazaarInstantBuyPrice; - } - //Don't use auction prices for vanilla items cuz people like to transfer money, messing up the cost of vanilla items. - if (lowestBin > 0 && !ci.vanillaItem) { - if (ci.craftCost < 0 || lowestBin < ci.craftCost) { - ci.craftCost = lowestBin; - } - } else if (auctionInfo != null && !ci.vanillaItem) { - float auctionPrice = auctionInfo.get("price").getAsFloat() / auctionInfo.get("count").getAsFloat(); - if (ci.craftCost < 0 || auctionPrice < ci.craftCost) { - ci.craftCost = auctionPrice; - } - } - - if (depth > 16) { - craftCost.put(internalname, ci); - return ci; - } - - JsonObject item = manager.getItemInformation().get(internalname); - if (item != null && item.has("recipe")) { - float craftPrice = 0; - JsonObject recipe = item.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.length() == 0) continue; - - int count = 1; - if (itemS.split(":").length == 2) { - count = Integer.parseInt(itemS.split(":")[1]); - itemS = itemS.split(":")[0]; - } - if (itemS.equals(internalname)) { //if item is used a crafting component in its own recipe, return - craftCost.put(internalname, ci); - return ci; - } - - float compCost = getCraftCost(itemS, depth + 1).craftCost * count; - if (compCost < 0) { - //If it's a custom item without a cost, return - if (!getCraftCost(itemS).vanillaItem) { - craftCost.put(internalname, ci); - return ci; + private CraftInfo getCraftCost(String internalname, Set visited) { + if (craftCost.containsKey(internalname)) return craftCost.get(internalname); + if (visited.contains(internalname)) return null; + visited.add(internalname); + + + boolean vanillaItem = isVanillaItem(internalname); + float craftCost = Float.POSITIVE_INFINITY; + + JsonObject auctionInfo = getItemAuctionInfo(internalname); + float lowestBin = getLowestBin(internalname); + JsonObject bazaarInfo = getBazaarInfo(internalname); + + if (bazaarInfo != null && bazaarInfo.get("curr_buy") != null) { + craftCost = bazaarInfo.get("curr_buy").getAsFloat(); + } + //Don't use auction prices for vanilla items cuz people like to transfer money, messing up the cost of vanilla items. + if (!vanillaItem) { + if (lowestBin > 0) { + craftCost = Math.min(lowestBin, craftCost); + } else if (auctionInfo != null) { + float auctionPrice = auctionInfo.get("price").getAsFloat() / auctionInfo.get("count").getAsInt(); + craftCost = Math.min(auctionPrice, craftCost); + } + } + + Set recipes = manager.getRecipesFor(internalname); + boolean fromRecipe = false; + if (recipes != null) + RECIPE_ITER: + for (NeuRecipe recipe : recipes) { + float craftPrice = 0; + for (Ingredient i : recipe.getIngredients()) { + if (i.isCoins()) { + craftPrice += i.getCount(); + continue; + } + CraftInfo ingredientCraftCost = getCraftCost(i.getInternalItemId(), visited); + if (ingredientCraftCost == null) + continue RECIPE_ITER; // Skip recipes with items further up the chain + craftPrice += ingredientCraftCost.craftCost * i.getCount(); + } + int resultCount = 0; + for (Ingredient item : recipe.getOutputs()) + if (item.getInternalItemId().equals(internalname)) + resultCount += item.getCount(); + + if (resultCount == 0) + continue; + float craftPricePer = craftPrice / resultCount; + if (craftPricePer < craftCost) { + fromRecipe = true; + craftCost = craftPricePer; } - } else { - craftPrice += compCost; } - } - - if (ci.craftCost < 0 || craftPrice < ci.craftCost) { - ci.craftCost = craftPrice; - ci.fromRecipe = true; - } - } - craftCost.put(internalname, ci); - return ci; + visited.remove(internalname); + if (Float.isInfinite(craftCost)) { + return null; } + CraftInfo craftInfo = new CraftInfo(); + craftInfo.vanillaItem = vanillaItem; + craftInfo.craftCost = craftCost; + craftInfo.fromRecipe = fromRecipe; + this.craftCost.put(internalname, craftInfo); + return craftInfo; } /** diff --git a/src/main/java/io/github/moulberry/notenoughupdates/commands/Commands.java b/src/main/java/io/github/moulberry/notenoughupdates/commands/Commands.java index 910870d5..0e58a6bc 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/commands/Commands.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/commands/Commands.java @@ -715,7 +715,7 @@ public class Commands { "Ok, this is actually the last message, use the command again and you'll crash I promise"}; private int devFailIndex = 0; - private static final List devTestUsers = new ArrayList<>(Arrays.asList("moulberry", "lucycoconut", "ironm00n", "ariyio", "throwpo", "dediamondpro")); + private static final List devTestUsers = new ArrayList<>(Arrays.asList("moulberry", "lucycoconut", "ironm00n", "ariyio", "throwpo", "lrg89", "dediamondpro")); SimpleCommand devTestCommand = new SimpleCommand("neudevtest", new SimpleCommand.ProcessCommandRunnable() { @Override public void processCommand(ICommandSender sender, String[] args) { diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscgui/GuiItemRecipe.java b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/GuiItemRecipe.java index 210e1da7..e820378b 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/miscgui/GuiItemRecipe.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/GuiItemRecipe.java @@ -1,33 +1,51 @@ package io.github.moulberry.notenoughupdates.miscgui; -import com.google.gson.JsonObject; +import com.google.common.collect.ImmutableList; import io.github.moulberry.notenoughupdates.NEUManager; +import io.github.moulberry.notenoughupdates.recipes.NeuRecipe; +import io.github.moulberry.notenoughupdates.recipes.RecipeSlot; import io.github.moulberry.notenoughupdates.util.Utils; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.ScaledResolution; import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.item.ItemStack; +import net.minecraft.util.MathHelper; import net.minecraft.util.ResourceLocation; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; import org.lwjgl.opengl.GL11; -import org.lwjgl.util.vector.Vector2f; import java.awt.*; import java.io.IOException; +import java.util.ArrayList; import java.util.List; public class GuiItemRecipe extends GuiScreen { - private static final ResourceLocation resourcePacksTexture = new ResourceLocation("textures/gui/resource_packs.png"); - private static final ResourceLocation craftingTableGuiTextures = new ResourceLocation("textures/gui/container/crafting_table.png"); + public static final ResourceLocation resourcePacksTexture = new ResourceLocation("textures/gui/resource_packs.png"); + + public static final int SLOT_SIZE = 16; + public static final int SLOT_SPACING = SLOT_SIZE + 2; + public static final int BUTTON_WIDTH = 7; + public static final int BUTTON_HEIGHT = 11; + public static final int BUTTON_POSITION_Y = 63; + public static final int BUTTON_POSITION_LEFT_X = 110; + public static final int BUTTON_POSITION_RIGHT_X = 147; + public static final int PAGE_STRING_X = 132; + public static final int PAGE_STRING_Y = 69; + public static final int TITLE_X = 28; + public static final int TITLE_Y = 6; + public static final int HOTBAR_SLOT_X = 8; + public static final int HOTBAR_SLOT_Y = 142; + public static final int PLAYER_INVENTORY_X = 8; + public static final int PLAYER_INVENTORY_Y = 84; - private final List craftMatrices; - private final List results; private int currentIndex = 0; private final String title; + private final List craftingRecipes; private final NEUManager manager; public int guiLeft = 0; @@ -35,159 +53,122 @@ public class GuiItemRecipe extends GuiScreen { public int xSize = 176; public int ySize = 166; - public GuiItemRecipe(String title, List craftMatrices, List results, NEUManager manager) { - this.craftMatrices = craftMatrices; - this.results = results; + public GuiItemRecipe(String title, List craftingRecipes, NEUManager manager) { + this.craftingRecipes = craftingRecipes; this.manager = manager; this.title = title; } - private String getCraftText() { - if(results.get(currentIndex).has("crafttext")) { - return results.get(currentIndex).get("crafttext").getAsString(); - } else { - return ""; - } + public NeuRecipe getCurrentRecipe() { + currentIndex = MathHelper.clamp_int(currentIndex, 0, craftingRecipes.size()); + return craftingRecipes.get(currentIndex); + } + + public boolean isWithinRect(int x, int y, int topLeftX, int topLeftY, int width, int height) { + return topLeftX <= x && x <= topLeftX + width + && topLeftY <= y && y <= topLeftY + height; + } + + private ImmutableList getAllRenderedSlots() { + return ImmutableList.builder() + .addAll(getPlayerInventory()) + .addAll(getCurrentRecipe().getSlots()).build(); } @Override public void drawScreen(int mouseX, int mouseY, float partialTicks) { drawDefaultBackground(); - - if(currentIndex < 0) { - currentIndex = 0; - } else if(currentIndex >= craftMatrices.size()) { - currentIndex = craftMatrices.size()-1; - } - FontRenderer fontRendererObj = Minecraft.getMinecraft().fontRendererObj; this.guiLeft = (width - this.xSize) / 2; this.guiTop = (height - this.ySize) / 2; GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F); - Minecraft.getMinecraft().getTextureManager().bindTexture(craftingTableGuiTextures); + + NeuRecipe currentRecipe = getCurrentRecipe(); + + Minecraft.getMinecraft().getTextureManager().bindTexture(currentRecipe.getBackground()); this.drawTexturedModalRect(guiLeft, guiTop, 0, 0, this.xSize, this.ySize); - List tooltipToRender = null; - for(int index=0; index <= 45; index++) { - Vector2f pos = getPositionForIndex(index); - Utils.drawItemStack(getStackForIndex(index), (int)pos.x, (int)pos.y); - - if(mouseX > pos.x && mouseX < pos.x+16) { - if(mouseY > pos.y && mouseY < pos.y+16) { - ItemStack stack = getStackForIndex(index); - if(stack != null) { - tooltipToRender = stack.getTooltip(Minecraft.getMinecraft().thePlayer, false); - } - } - } + currentRecipe.drawExtraBackground(this); + + List slots = getAllRenderedSlots(); + for (RecipeSlot slot : slots) { + Utils.drawItemStack(slot.getItemStack(), slot.getX(this), slot.getY(this)); } - if(craftMatrices.size() > 1) { - int guiX = mouseX - guiLeft; - int guiY = mouseY - guiTop; + if (craftingRecipes.size() > 1) drawArrows(mouseX, mouseY); - int buttonWidth = 7; - int buttonHeight = 11; + Utils.drawStringScaledMaxWidth(title, fontRendererObj, guiLeft + TITLE_X, guiTop + TITLE_Y, false, xSize - 38, 0x404040); - boolean leftSelected = false; - boolean rightSelected = false; + currentRecipe.drawExtraInfo(this); - if(guiY > + 63 && guiY < + 63 + buttonHeight) { - if(guiX > + 110 && guiX < 110 + buttonWidth) { - leftSelected = true; - } else if(guiX > 147 && guiX < 147 + buttonWidth) { - rightSelected = true; - } + for (RecipeSlot slot : slots) { + if (isWithinRect(mouseX, mouseY, slot.getX(this), slot.getY(this), SLOT_SIZE, SLOT_SIZE)) { + if (slot.getItemStack() == null) continue; + Utils.drawHoveringText(slot.getItemStack().getTooltip(Minecraft.getMinecraft().thePlayer, false), mouseX, mouseY, width, height, -1, fontRendererObj); } - - Minecraft.getMinecraft().getTextureManager().bindTexture(resourcePacksTexture); - //Left arrow - Utils.drawTexturedRect(guiLeft+110, guiTop+63, 7, 11, 34/256f, 48/256f, - 5/256f + (leftSelected ? 32/256f : 0), 27/256f + (leftSelected ? 32/256f : 0)); - //Right arrow - Utils.drawTexturedRect(guiLeft+147, guiTop+63, 7, 11, 10/256f, 24/256f, - 5/256f + (rightSelected ? 32/256f : 0), 27/256f + (rightSelected ? 32/256f : 0)); - GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); - - String str = (currentIndex+1)+"/"+craftMatrices.size(); - Utils.drawStringCenteredScaledMaxWidth(str, fontRendererObj, guiLeft+132, guiTop+69, - false, 24, Color.BLACK.getRGB()); } + currentRecipe.drawHoverInformation(this, mouseX, mouseY); + } - Utils.drawStringCenteredScaledMaxWidth(getCraftText(), fontRendererObj, guiLeft+132, guiTop+25, - false, 75, 4210752); + private void drawArrows(int mouseX, int mouseY) { + boolean leftSelected = isWithinRect(mouseX - guiLeft, mouseY - guiTop, BUTTON_POSITION_LEFT_X, BUTTON_POSITION_Y, BUTTON_WIDTH, BUTTON_HEIGHT); + boolean rightSelected = isWithinRect(mouseX - guiLeft, mouseY - guiTop, BUTTON_POSITION_RIGHT_X, BUTTON_POSITION_Y, BUTTON_WIDTH, BUTTON_HEIGHT); - Utils.drawStringScaledMaxWidth(title, fontRendererObj, guiLeft+28, guiTop+6, title.contains("\u00a7"), xSize-38, 4210752); + Minecraft.getMinecraft().getTextureManager().bindTexture(resourcePacksTexture); - if(tooltipToRender != null) { - Utils.drawHoveringText(tooltipToRender, mouseX, mouseY, width, height, -1, fontRendererObj); - } - } + Utils.drawTexturedRect(guiLeft + BUTTON_POSITION_LEFT_X, guiTop + BUTTON_POSITION_Y, BUTTON_WIDTH, BUTTON_HEIGHT, + 34 / 256f, 48 / 256f, + leftSelected ? 37 / 256f : 5 / 256f, leftSelected ? 59 / 256f : 27 / 256f + ); + Utils.drawTexturedRect(guiLeft + BUTTON_POSITION_RIGHT_X, guiTop + BUTTON_POSITION_Y, BUTTON_WIDTH, BUTTON_HEIGHT, + 10 / 256f, 24 / 256f, + rightSelected ? 37 / 256f : 5 / 256f, rightSelected ? 59 / 256f : 27 / 256f + ); + GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); - public ItemStack getStackForIndex(int index) { - if(index == 0) { - return manager.jsonToStack(results.get(currentIndex)); - } else if(index >= 1 && index <= 9) { - return craftMatrices.get(currentIndex)[index-1]; - } else { - return Minecraft.getMinecraft().thePlayer.inventory.getStackInSlot(index-10); - } + String selectedPage = (currentIndex + 1) + "/" + craftingRecipes.size(); + + Utils.drawStringCenteredScaledMaxWidth(selectedPage, fontRendererObj, + guiLeft + PAGE_STRING_X, guiTop + PAGE_STRING_Y, false, 24, Color.BLACK.getRGB()); } - public Vector2f getPositionForIndex(int index) { - //0 = result - //1-9 = craft matrix - //10-18 = hotbar - //19-45 = player inv - - if(index == 0) { - return new Vector2f(guiLeft+124, guiTop+35); - } else if(index >= 1 && index <= 9) { - index -= 1; - int x = index % 3; - int y = index / 3; - return new Vector2f(guiLeft+30 + x*18, guiTop+17 + y * 18); - } else if(index >= 10 && index <= 18) { - index -= 10; - return new Vector2f(guiLeft+8 + index*18, guiTop+142); - } else if(index >= 19 && index <= 45) { - index -= 19; - int x = index % 9; - int y = index / 9; - return new Vector2f(guiLeft+8 + x*18, guiTop+84 + y*18); + public List getPlayerInventory() { + List slots = new ArrayList<>(); + ItemStack[] inventory = Minecraft.getMinecraft().thePlayer.inventory.mainInventory; + int hotbarSize = InventoryPlayer.getHotbarSize(); + for (int i = 0; i < inventory.length; i++) { + ItemStack item = inventory[i]; + if (item == null || item.stackSize == 0) continue; + int row = i / hotbarSize; + int col = i % hotbarSize; + if (row == 0) + slots.add(new RecipeSlot(HOTBAR_SLOT_X + i * SLOT_SPACING, HOTBAR_SLOT_Y, item)); + else + slots.add(new RecipeSlot(PLAYER_INVENTORY_X + col * SLOT_SPACING, PLAYER_INVENTORY_Y + (row - 1) * SLOT_SPACING, item)); } - return null; + return slots; } @Override public void handleKeyboardInput() throws IOException { super.handleKeyboardInput(); - if(!Keyboard.getEventKeyState()) return; - ScaledResolution scaledResolution = new ScaledResolution(Minecraft.getMinecraft()); int width = scaledResolution.getScaledWidth(); int height = scaledResolution.getScaledHeight(); int mouseX = Mouse.getX() * width / Minecraft.getMinecraft().displayWidth; int mouseY = height - Mouse.getY() * height / Minecraft.getMinecraft().displayHeight - 1; - int keyPressed = Keyboard.getEventKey() == 0 ? Keyboard.getEventCharacter()+256 : Keyboard.getEventKey(); - for(int index=0; index <= 45; index++) { - Vector2f pos = getPositionForIndex(index); - if(mouseX > pos.x && mouseX < pos.x+16) { - if(mouseY > pos.y && mouseY < pos.y+16) { - ItemStack stack = getStackForIndex(index); - if(stack != null) { - if(keyPressed == manager.keybindViewRecipe.getKeyCode()) { - manager.displayGuiItemRecipe(manager.getInternalNameForItem(stack), ""); - } else if(keyPressed == manager.keybindViewUsages.getKeyCode()) { - manager.displayGuiItemUsages(manager.getInternalNameForItem(stack)); - } - } - return; + for (RecipeSlot slot : getAllRenderedSlots()) { + if (isWithinRect(mouseX, mouseY, slot.getX(this), slot.getY(this), SLOT_SIZE, SLOT_SIZE)) { + ItemStack itemStack = slot.getItemStack(); + if (keyPressed == manager.keybindViewRecipe.getKeyCode()) { // TODO: rework this so it doesnt skip recipe chains + manager.displayGuiItemRecipe(manager.getInternalNameForItem(itemStack), ""); + } else if (keyPressed == manager.keybindViewUsages.getKeyCode()) { + manager.displayGuiItemUsages(manager.getInternalNameForItem(itemStack)); } } } @@ -197,37 +178,25 @@ public class GuiItemRecipe extends GuiScreen { protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException { super.mouseClicked(mouseX, mouseY, mouseButton); - int guiX = mouseX - guiLeft; - int guiY = mouseY - guiTop; - - int buttonWidth = 7; - int buttonHeight = 11; - - if(guiY > + 63 && guiY < + 63 + buttonHeight) { - if(guiX > + 110 && guiX < 110 + buttonWidth) { - currentIndex--; - Utils.playPressSound(); - return; - } else if(guiX > 147 && guiX < 147 + buttonWidth) { - currentIndex++; - Utils.playPressSound(); - return; - } + if (isWithinRect(mouseX - guiLeft, mouseY - guiTop, BUTTON_POSITION_LEFT_X, BUTTON_POSITION_Y, BUTTON_WIDTH, BUTTON_HEIGHT)) { + currentIndex = currentIndex == 0 ? 0 : currentIndex - 1; + Utils.playPressSound(); + return; + } + + if (isWithinRect(mouseX - guiLeft, mouseY - guiTop, BUTTON_POSITION_RIGHT_X, BUTTON_POSITION_Y, BUTTON_WIDTH, BUTTON_HEIGHT)) { + currentIndex = currentIndex == craftingRecipes.size() - 1 ? currentIndex : currentIndex + 1; + Utils.playPressSound(); + return; } - for(int index=0; index <= 45; index++) { - Vector2f pos = getPositionForIndex(index); - if(mouseX > pos.x && mouseX < pos.x+16) { - if(mouseY > pos.y && mouseY < pos.y+16) { - ItemStack stack = getStackForIndex(index); - if(stack != null) { - if(mouseButton == 0) { - manager.displayGuiItemRecipe(manager.getInternalNameForItem(stack), ""); - } else if(mouseButton == 1) { - manager.displayGuiItemUsages(manager.getInternalNameForItem(stack)); - } - } - return; + for (RecipeSlot slot : getAllRenderedSlots()) { + if (isWithinRect(mouseX, mouseY, slot.getX(this), slot.getY(this), SLOT_SIZE, SLOT_SIZE)) { + ItemStack itemStack = slot.getItemStack(); + if (mouseButton == 0) { + manager.displayGuiItemRecipe(manager.getInternalNameForItem(itemStack), ""); + } else if (mouseButton == 1) { + manager.displayGuiItemUsages(manager.getInternalNameForItem(itemStack)); } } } diff --git a/src/main/java/io/github/moulberry/notenoughupdates/overlays/CraftingOverlay.java b/src/main/java/io/github/moulberry/notenoughupdates/overlays/CraftingOverlay.java index 532f3324..c0cbef0f 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/overlays/CraftingOverlay.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/overlays/CraftingOverlay.java @@ -3,6 +3,8 @@ package io.github.moulberry.notenoughupdates.overlays; import com.google.gson.JsonObject; import io.github.moulberry.notenoughupdates.NEUManager; import io.github.moulberry.notenoughupdates.NotEnoughUpdates; +import io.github.moulberry.notenoughupdates.recipes.CraftingRecipe; +import io.github.moulberry.notenoughupdates.recipes.Ingredient; import io.github.moulberry.notenoughupdates.util.Utils; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; @@ -17,7 +19,7 @@ import org.lwjgl.input.Mouse; import java.util.List; public class CraftingOverlay { - private static ItemStack[] items = new ItemStack[9]; + private static final ItemStack[] items = new ItemStack[9]; private static final NEUManager manager = NotEnoughUpdates.INSTANCE.manager; public static boolean shouldRender = false; private static String text = null; @@ -56,34 +58,6 @@ public class CraftingOverlay { } } - public static void updateItem(JsonObject item) { - items = new ItemStack[9]; - text = null; - 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 = item.getAsJsonObject("recipe").get(name).getAsString(); - if (itemS != null && !itemS.equals("")) { - int count = 1; - if (itemS.split(":").length == 2) { - count = Integer.parseInt(itemS.split(":")[1]); - itemS = itemS.split(":")[0]; - } - JsonObject craft = manager.getItemInformation().get(itemS); - if (craft != null) { - ItemStack stack = manager.jsonToStack(craft); - stack.stackSize = count; - items[i] = stack; - } - } - } - if (item.has("crafttext")) { - text = item.get("crafttext").getAsString(); - } - shouldRender = true; - } - public static void keyInput() { if (!Keyboard.getEventKeyState() || Keyboard.getEventKey() != Keyboard.KEY_U && Keyboard.getEventKey() != Keyboard.KEY_R) return; @@ -114,4 +88,16 @@ public class CraftingOverlay { } } } + + public static void updateItem(CraftingRecipe recipe) { + for (int i = 0; i < 9; i++) { + Ingredient ingredient = recipe.getInputs()[i]; + if (ingredient == null) { + items[i] = null; + } else { + items[i] = ingredient.getItemStack(); + } + } + text = recipe.getCraftText(); + } } diff --git a/src/main/java/io/github/moulberry/notenoughupdates/recipes/CraftingRecipe.java b/src/main/java/io/github/moulberry/notenoughupdates/recipes/CraftingRecipe.java new file mode 100644 index 00000000..00e70462 --- /dev/null +++ b/src/main/java/io/github/moulberry/notenoughupdates/recipes/CraftingRecipe.java @@ -0,0 +1,141 @@ +package io.github.moulberry.notenoughupdates.recipes; + +import com.google.common.collect.Sets; +import com.google.gson.JsonObject; +import io.github.moulberry.notenoughupdates.NEUManager; +import io.github.moulberry.notenoughupdates.miscgui.GuiItemRecipe; +import io.github.moulberry.notenoughupdates.util.Utils; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.item.ItemStack; +import net.minecraft.util.ResourceLocation; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; + +public class CraftingRecipe implements NeuRecipe { + + public static final ResourceLocation BACKGROUND = new ResourceLocation("textures/gui/container/crafting_table.png"); + + private static final int EXTRA_STRING_X = 132; + private static final int EXTRA_STRING_Y = 25; + + private final NEUManager manager; + private final Ingredient[] inputs; + private final String extraText; + private final Ingredient outputIngredient; + private List slots; + + public CraftingRecipe(NEUManager manager, Ingredient[] inputs, Ingredient output, String extra) { + this.manager = manager; + this.inputs = inputs; + this.outputIngredient = output; + this.extraText = extra; + if (inputs.length != 9) + throw new IllegalArgumentException("Cannot construct crafting recipe with non standard crafting grid size"); + } + + @Override + public Set getIngredients() { + Set ingredients = Sets.newHashSet(inputs); + ingredients.remove(null); + return ingredients; + } + + @Override + public Set getOutputs() { + return Collections.singleton(getOutput()); + } + + public Ingredient getOutput() { + return outputIngredient; + } + + public Ingredient[] getInputs() { + return inputs; + } + + + @Override + public List getSlots() { + if (slots != null) return slots; + slots = new ArrayList<>(); + for (int x = 0; x < 3; x++) { + for (int y = 0; y < 3; y++) { + Ingredient input = inputs[x + y * 3]; + if (input == null) continue; + ItemStack item = input.getItemStack(); + if (item == null) continue; + slots.add(new RecipeSlot(30 + x * GuiItemRecipe.SLOT_SPACING, 17 + y * GuiItemRecipe.SLOT_SPACING, item)); + } + } + slots.add(new RecipeSlot(124, 35, outputIngredient.getItemStack())); + return slots; + } + + public String getCraftText() { + return extraText; + } + + @Override + public ResourceLocation getBackground() { + return BACKGROUND; + } + + @Override + public void drawExtraInfo(GuiItemRecipe gui) { + FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj; + + String craftingText = getCraftText(); + if (craftingText != null) + Utils.drawStringCenteredScaledMaxWidth(craftingText, fontRenderer, + gui.guiLeft + EXTRA_STRING_X, gui.guiTop + EXTRA_STRING_Y, false, 75, 0x404040); + } + + @Override + public JsonObject serialize() { + JsonObject object = new JsonObject(); + object.addProperty("type", "crafting"); + object.addProperty("count", outputIngredient.getCount()); + object.addProperty("overrideOutputId", outputIngredient.getInternalItemId()); + for (int i = 0; i < 9; i++) { + Ingredient ingredient = inputs[i]; + if (ingredient == null) continue; + String[] x = {"1", "2", "3"}; + String[] y = {"A", "B", "C"}; + String name = x[i / 3] + y[i % 3]; + object.addProperty(name, ingredient.serialize()); + } + if(extraText != null) + object.addProperty("crafttext", extraText); + return object; + } + + public static CraftingRecipe parseCraftingRecipe(NEUManager manager, JsonObject recipe, JsonObject outputItem) { + Ingredient[] craftMatrix = new Ingredient[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]; + if (!recipe.has(name)) continue; + String item = recipe.get(name).getAsString(); + if (item == null || item.isEmpty()) continue; + craftMatrix[i] = new Ingredient(manager, item); + } + int resultCount = 1; + if (recipe.has("count")) + resultCount = recipe.get("count").getAsInt(); + String extra = null; + if (outputItem.has("crafttext")) + extra = outputItem.get("crafttext").getAsString(); + if (recipe.has("crafttext")) + extra = recipe.get("crafttext").getAsString(); + String outputItemId = outputItem.get("internalname").getAsString(); + if (recipe.has("overrideOutputId")) + outputItemId = recipe.get("overrideOutputId").getAsString(); + return new CraftingRecipe(manager, craftMatrix, new Ingredient(manager, outputItemId, resultCount), extra); + } +} diff --git a/src/main/java/io/github/moulberry/notenoughupdates/recipes/ForgeRecipe.java b/src/main/java/io/github/moulberry/notenoughupdates/recipes/ForgeRecipe.java new file mode 100644 index 00000000..5cbb4afe --- /dev/null +++ b/src/main/java/io/github/moulberry/notenoughupdates/recipes/ForgeRecipe.java @@ -0,0 +1,220 @@ +package io.github.moulberry.notenoughupdates.recipes; + +import com.google.common.collect.Sets; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import io.github.moulberry.notenoughupdates.NEUManager; +import io.github.moulberry.notenoughupdates.miscgui.GuiItemRecipe; +import io.github.moulberry.notenoughupdates.util.HotmInformation; +import io.github.moulberry.notenoughupdates.util.Utils; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.item.ItemStack; +import net.minecraft.util.EnumChatFormatting; +import net.minecraft.util.ResourceLocation; + +import java.util.*; + +public class ForgeRecipe implements NeuRecipe { + + private static final ResourceLocation BACKGROUND = new ResourceLocation("notenoughupdates", "textures/gui/forge_recipe.png"); + + private static final int SLOT_IMAGE_U = 176; + private static final int SLOT_IMAGE_V = 0; + private static final int SLOT_IMAGE_SIZE = 18; + private static final int SLOT_PADDING = 1; + private static final int EXTRA_INFO_MAX_WIDTH = 75; + public static final int EXTRA_INFO_X = 132; + public static final int EXTRA_INFO_Y = 25; + + public enum ForgeType { + REFINING, ITEM_FORGING + } + + private final NEUManager manager; + private final List inputs; + private final Ingredient output; + private final int hotmLevel; + private final int timeInSeconds; // TODO: quick forge + private List slots; + + public ForgeRecipe(NEUManager manager, List inputs, Ingredient output, int durationInSeconds, int hotmLevel) { + this.manager = manager; + this.inputs = inputs; + this.output = output; + this.hotmLevel = hotmLevel; + this.timeInSeconds = durationInSeconds; + } + + public List getInputs() { + return inputs; + } + + public Ingredient getOutput() { + return output; + } + + public int getHotmLevel() { + return hotmLevel; + } + + public int getTimeInSeconds() { + return timeInSeconds; + } + + @Override + public ResourceLocation getBackground() { + return BACKGROUND; + } + + @Override + public Set getIngredients() { + return Sets.newHashSet(inputs); + } + + @Override + public Set getOutputs() { + return Collections.singleton(output); + } + + @Override + public List getSlots() { + if (slots != null) return slots; + slots = new ArrayList<>(); + for (int i = 0; i < inputs.size(); i++) { + Ingredient input = inputs.get(i); + ItemStack itemStack = input.getItemStack(); + if (itemStack == null) continue; + int[] slotCoordinates = getSlotCoordinates(i, inputs.size()); + slots.add(new RecipeSlot(slotCoordinates[0], slotCoordinates[1], itemStack)); + } + slots.add(new RecipeSlot(124, 35, output.getItemStack())); + return slots; + } + + @Override + public void drawExtraBackground(GuiItemRecipe gui) { + Minecraft.getMinecraft().getTextureManager().bindTexture(BACKGROUND); + for (int i = 0; i < inputs.size(); i++) { + int[] slotCoordinates = getSlotCoordinates(i, inputs.size()); + gui.drawTexturedModalRect( + gui.guiLeft + slotCoordinates[0] - SLOT_PADDING, gui.guiTop + slotCoordinates[1] - SLOT_PADDING, + SLOT_IMAGE_U, SLOT_IMAGE_V, + SLOT_IMAGE_SIZE, SLOT_IMAGE_SIZE); + } + } + + @Override + public void drawExtraInfo(GuiItemRecipe gui) { + FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj; + if (timeInSeconds > 0) + Utils.drawStringCenteredScaledMaxWidth(formatDuration(timeInSeconds), fontRenderer, gui.guiLeft + EXTRA_INFO_X, gui.guiTop + EXTRA_INFO_Y, false, EXTRA_INFO_MAX_WIDTH, 0xff00ff); + } + + @Override + public void drawHoverInformation(GuiItemRecipe gui, int mouseX, int mouseY) { + manager.hotm.getInformationOnCurrentProfile().ifPresent(hotmTree -> { + if (timeInSeconds > 0 && gui.isWithinRect( + mouseX, mouseY, + gui.guiLeft + EXTRA_INFO_X - EXTRA_INFO_MAX_WIDTH / 2, + gui.guiTop + EXTRA_INFO_Y - 8, + EXTRA_INFO_MAX_WIDTH, 16 + )) { + int qf = hotmTree.getLevel("forge_time"); + int reducedTime = getReducedTime(qf); + if (qf > 0) { + + Utils.drawHoveringText(Arrays.asList(EnumChatFormatting.YELLOW + formatDuration(reducedTime) + " with Quick Forge (Level " + qf + ")"), mouseX, mouseY, gui.width, gui.height, 500, Minecraft.getMinecraft().fontRendererObj); + } + } + }); + } + + public int getReducedTime(int quickForgeUpgradeLevel) { + return HotmInformation.getQuickForgeMultiplier(quickForgeUpgradeLevel) * timeInSeconds / 1000; + } + + @Override + public JsonObject serialize() { + JsonObject object = new JsonObject(); + JsonArray ingredients = new JsonArray(); + for (Ingredient input : inputs) { + ingredients.add(new JsonPrimitive(input.serialize())); + } + object.addProperty("type", "forge"); + object.add("inputs", ingredients); + object.addProperty("count", output.getCount()); + object.addProperty("overrideOutputId", output.getInternalItemId()); + if (hotmLevel >= 0) + object.addProperty("hotmLevel", hotmLevel); + if (timeInSeconds >= 0) + object.addProperty("duration", timeInSeconds); + return object; + } + + static ForgeRecipe parseForgeRecipe(NEUManager manager, JsonObject recipe, JsonObject output) { + List ingredients = new ArrayList<>(); + for (JsonElement element : recipe.getAsJsonArray("inputs")) { + String ingredientString = element.getAsString(); + ingredients.add(new Ingredient(manager, ingredientString)); + } + String internalItemId = output.get("internalname").getAsString(); + if (recipe.has("overrideOutputId")) + internalItemId = recipe.get("overrideOutputId").getAsString(); + int resultCount = 1; + if (recipe.has("count")) { + resultCount = recipe.get("count").getAsInt(); + } + int duration = -1; + if (recipe.has("duration")) { + duration = recipe.get("duration").getAsInt(); + } + int hotmLevel = -1; + if (recipe.has("hotmLevel")) { + hotmLevel = recipe.get("hotmLevel").getAsInt(); + } + return new ForgeRecipe(manager, ingredients, new Ingredient(manager, internalItemId, resultCount), duration, hotmLevel); + } + + private static final int RECIPE_CENTER_X = 40; + private static final int RECIPE_CENTER_Y = 34; + private static final int SLOT_DISTANCE_FROM_CENTER = 22; + private static final int RECIPE_FALLBACK_X = 20; + private static final int RECIPE_FALLBACK_Y = 15; + + static int[] getSlotCoordinates(int slotNumber, int totalSlotCount) { + if (totalSlotCount > 6) { + return new int[]{ + RECIPE_FALLBACK_X + (slotNumber % 4) * GuiItemRecipe.SLOT_SPACING, + RECIPE_FALLBACK_Y + (slotNumber / 4) * GuiItemRecipe.SLOT_SPACING, + }; + } + if (totalSlotCount == 1) { + return new int[] { + RECIPE_CENTER_X - GuiItemRecipe.SLOT_SIZE / 2, + RECIPE_CENTER_Y - GuiItemRecipe.SLOT_SIZE / 2 + }; + } + double rad = Math.PI * 2 * slotNumber / totalSlotCount; + int x = (int) (Math.cos(rad) * SLOT_DISTANCE_FROM_CENTER); + int y = (int) (Math.sin(rad) * SLOT_DISTANCE_FROM_CENTER); + return new int[]{RECIPE_CENTER_X + x, RECIPE_CENTER_Y + y}; + } + + static String formatDuration(int seconds) { + int minutes = seconds / 60; + seconds %= 60; + int hours = minutes / 60; + minutes %= 60; + int days = hours / 24; + hours %= 24; + StringBuilder sB = new StringBuilder(); + if (days != 0) sB.append(days).append("d "); + if (hours != 0) sB.append(hours).append("h "); + if (minutes != 0) sB.append(minutes).append("m "); + if (seconds != 0) sB.append(seconds).append("s "); + return sB.substring(0, sB.length() - 1); + } +} diff