From 9f9fe782dcd2476a58093f39b529a74605c6f8b6 Mon Sep 17 00:00:00 2001 From: glowredman <35727266+glowredman@users.noreply.github.com> Date: Sun, 20 Dec 2020 02:35:52 +0100 Subject: [PATCH] Add NEI handling --- .../java/glowredman/wherearetheores/WATO.java | 57 ++++++ .../wherearetheores/nei/NEIWATOConfig.java | 27 +++ .../nei/WATORecipeHandler.java | 184 ++++++++++++++++++ .../wherearetheores/proxy/ClientProxy.java | 4 +- .../assets/wherearetheores/lang/en_US.lang | 1 + 5 files changed, 271 insertions(+), 2 deletions(-) create mode 100644 src/main/java/glowredman/wherearetheores/nei/NEIWATOConfig.java create mode 100644 src/main/java/glowredman/wherearetheores/nei/WATORecipeHandler.java create mode 100644 src/main/resources/assets/wherearetheores/lang/en_US.lang diff --git a/src/main/java/glowredman/wherearetheores/WATO.java b/src/main/java/glowredman/wherearetheores/WATO.java index e0c4d83..5314a71 100644 --- a/src/main/java/glowredman/wherearetheores/WATO.java +++ b/src/main/java/glowredman/wherearetheores/WATO.java @@ -6,8 +6,13 @@ import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.Mod.Instance; import cpw.mods.fml.common.SidedProxy; +import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; +import cpw.mods.fml.common.registry.GameRegistry; import glowredman.wherearetheores.proxy.CommonProxy; +import net.minecraft.init.Items; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; @Mod(acceptedMinecraftVersions = "1.7.10", dependencies = "required-after:NotEnoughItems", modid = WATO.MODID, name = WATO.MODNAME, version = WATO.VERSION) public class WATO { @@ -30,4 +35,56 @@ public static void preInit(FMLPreInitializationEvent event) { proxy.preInit(event); } + @EventHandler + public static void postInit(FMLPostInitializationEvent event) { + proxy.postInit(event); + } + + /************* + * UTILITIES * + ************/ + + /** + * Decodes the given String to an ItemStack. Examples: "apple", "coal:1", + * "IC2:dust", "IC2:dust:4". + */ + public static ItemStack findItem(String item) { + String[] parts = item.split(":"); + switch (parts.length) { + + // The item is from minecraft and has no meta value. + case 1: + return GameRegistry.findItemStack("minecraft", item, 1); + case 2: + + // The item is from Minecraft and has a meta value. + try { + ItemStack stack = GameRegistry.findItemStack("minecraft", parts[0], 1); + Items.apple.setDamage(stack, Integer.parseInt(parts[1])); + return stack; + + // The item is not from Minecraft and has no meta value. + } catch (Exception e) { + return GameRegistry.findItemStack(parts[0], parts[1], 1); + } + + // The item is not from Minecraft and has a meta value. + case 3: + ItemStack stack = GameRegistry.findItemStack(parts[0], parts[1], 1); + Items.apple.setDamage(stack, Integer.parseInt(parts[2])); + return stack; + default: + WATO.logger.error("Unable to translate \"" + item + "\" to an ItemStack!"); + return null; + } + } + + public static long getUSIID(ItemStack stack) { + return getUSIID(Item.getIdFromItem(stack.getItem()), stack.getItemDamage()); + } + + public static long getUSIID(int itemID, int meta) { + return itemID * Short.MAX_VALUE + meta; + } + } diff --git a/src/main/java/glowredman/wherearetheores/nei/NEIWATOConfig.java b/src/main/java/glowredman/wherearetheores/nei/NEIWATOConfig.java new file mode 100644 index 0000000..1d9cfc8 --- /dev/null +++ b/src/main/java/glowredman/wherearetheores/nei/NEIWATOConfig.java @@ -0,0 +1,27 @@ +package glowredman.wherearetheores.nei; + +import codechicken.nei.api.API; +import codechicken.nei.api.IConfigureNEI; +import glowredman.wherearetheores.WATO; + +public class NEIWATOConfig implements IConfigureNEI { + + @Override + public String getName() { + return WATO.MODNAME; + } + + @Override + public String getVersion() { + return WATO.VERSION; + } + + @Override + public void loadConfig() { + + API.registerRecipeHandler(new WATORecipeHandler()); + API.registerUsageHandler(new WATORecipeHandler()); + + } + +} diff --git a/src/main/java/glowredman/wherearetheores/nei/WATORecipeHandler.java b/src/main/java/glowredman/wherearetheores/nei/WATORecipeHandler.java new file mode 100644 index 0000000..2a38f88 --- /dev/null +++ b/src/main/java/glowredman/wherearetheores/nei/WATORecipeHandler.java @@ -0,0 +1,184 @@ +package glowredman.wherearetheores.nei; + +import java.awt.Point; +import java.awt.Rectangle; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.lwjgl.opengl.GL11; + +import codechicken.lib.gui.GuiDraw; +import codechicken.nei.PositionedStack; +import codechicken.nei.recipe.TemplateRecipeHandler; +import glowredman.wherearetheores.WATO; +import glowredman.wherearetheores.config.ConfigHandler; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.resources.I18n; +import net.minecraft.item.ItemStack; +import net.minecraftforge.oredict.OreDictionary; + +public class WATORecipeHandler extends TemplateRecipeHandler { + + public static final int WIDTH = 170; + public static final int HEIGHT = 160; + public static final int TEXT_OFFSET_X = 4; + public static final int TEXT_OFFSET_Y = 20; + public static final int TEXT_SPACING = 9; + public static final String OUTPUT_ID = "wato.ore"; + + @Override + public void loadCraftingRecipes(String outputId, Object... results) { + if (outputId.equals(OUTPUT_ID)) { + + for (Entry>> e : ConfigHandler.config.entrySet()) { + String ore = e.getKey(); + Map> dimensionInfo = e.getValue(); + + // decode ore: + List decodedOres = new ArrayList(); + if (ore.startsWith("[")) { + for (String item : ore.substring(1).split(";")) { + decodedOres.add(WATO.findItem(item)); + } + } else { + decodedOres = OreDictionary.getOres(ore); + } + this.arecipes.add(new CachedOreRecipe(dimensionInfo, decodedOres)); + } + } else { + super.loadCraftingRecipes(outputId, results); + } + } + + @Override + public void loadCraftingRecipes(ItemStack result) { + if (ConfigHandler.possibleItems.containsKey(WATO.getUSIID(result))) { + String key = ConfigHandler.possibleItems.get(WATO.getUSIID(result)); + + // decode ore: + List decodedOres = new ArrayList(); + if (key.startsWith("[")) { + key.replaceFirst("[", ""); + for (String item : key.split(";")) { + decodedOres.add(WATO.findItem(item)); + } + } else { + decodedOres = OreDictionary.getOres(key); + } + + this.arecipes.add(new CachedOreRecipe(ConfigHandler.config.get(key), decodedOres)); + } else { + super.loadCraftingRecipes(result); + } + } + + @Override + public void drawBackground(int recipe) { + GL11.glColor4f(1, 1, 1, 1); + } + + @Override + public void drawForeground(int recipe) { + GL11.glColor4f(1, 1, 1, 1); + GL11.glDisable(GL11.GL_LIGHTING); + drawExtras(recipe); + } + + @Override + public void drawExtras(int recipe) { + CachedOreRecipe cRecipe = (CachedOreRecipe) arecipes.get(recipe); + List dimensions = new ArrayList(); + dimensions.addAll(cRecipe.dimensionInfo.keySet()); + FontRenderer font = GuiDraw.fontRenderer; + for (int i = 0; i < dimensions.size(); i++) { + String dim = dimensions.get(i); + GuiDraw.drawString(dim, (WIDTH - font.getStringWidth(dim)) / 2, TEXT_OFFSET_Y + i * TEXT_SPACING, 0x404040, + false); + } + drawTooltip(cRecipe); + } + + @Override + public int recipiesPerPage() { + return 1; + } + + @Override + public String getRecipeName() { + return I18n.format("gui.nei.wato"); + } + + @Override + public String getGuiTexture() { + return null; + } + + @Override + public void loadTransferRects() { + int stringLenght = GuiDraw.getStringWidth(I18n.format("gui.nei.wato")); + transferRects.add(new RecipeTransferRect( + new Rectangle((WIDTH - stringLenght + 1) / 2, -12, stringLenght + 2, 10), OUTPUT_ID)); + } + + /** + * Draws a tooltip with details about the dimension the mouse is hovering over. + */ + public void drawTooltip(CachedOreRecipe recipe) { + Point mouse = GuiDraw.getMousePosition(); + Point mouseRel = new Point(mouse.x - getXOffset(), mouse.y - getYOffset()); + + // Check, if the mouse is inside the GUI (horizontally) + if (mouseRel.x <= WIDTH && mouseRel.x >= 0) { + + // Get the line, the mouse is hovering over + int line = (int) Math.floor((mouseRel.y - TEXT_OFFSET_Y) / TEXT_SPACING); + + // Check, if the mouse position is inside the text area (vertically) + if (line < recipe.dimensionInfo.size() && line >= 0) { + String dim = (String) recipe.dimensionInfo.keySet().toArray()[line]; + + // Don't draw the tooltip, if it wouldn't contain text + if (recipe.dimensionInfo.get(dim).size() > 0) { + GuiDraw.drawMultilineTip(mouseRel.x + 8, mouseRel.y - TEXT_OFFSET_Y + 32, + recipe.dimensionInfo.get(dim)); + } + } + } + } + + private int getXOffset() { + return (GuiDraw.displaySize().width - WIDTH) / 2; + } + + private int getYOffset() { + return (GuiDraw.displaySize().height - HEIGHT) / 2 + 13; + } + + public class CachedOreRecipe extends CachedRecipe { + + public Map> dimensionInfo; + public PositionedStack ore; + public int numVariants; + + public CachedOreRecipe(Map> dimensionInfo, List ores) { + this.dimensionInfo = dimensionInfo; + this.ore = new PositionedStack(ores, (WIDTH - 18) / 2, 1); + this.numVariants = ores.size(); + } + + @Override + public PositionedStack getIngredient() { + ore.setPermutationToRender((cycleticks / 20) % numVariants); + return ore; + } + + @Override + public PositionedStack getResult() { + return null; + } + + } + +} diff --git a/src/main/java/glowredman/wherearetheores/proxy/ClientProxy.java b/src/main/java/glowredman/wherearetheores/proxy/ClientProxy.java index 8e167e5..c7f5f82 100644 --- a/src/main/java/glowredman/wherearetheores/proxy/ClientProxy.java +++ b/src/main/java/glowredman/wherearetheores/proxy/ClientProxy.java @@ -5,13 +5,13 @@ import glowredman.wherearetheores.config.ConfigHandler; public class ClientProxy extends CommonProxy { - + @Override public void preInit(FMLPreInitializationEvent event) { super.preInit(event); ConfigHandler.init(event); } - + @Override public void postInit(FMLPostInitializationEvent event) { super.postInit(event); diff --git a/src/main/resources/assets/wherearetheores/lang/en_US.lang b/src/main/resources/assets/wherearetheores/lang/en_US.lang new file mode 100644 index 0000000..4ea07f2 --- /dev/null +++ b/src/main/resources/assets/wherearetheores/lang/en_US.lang @@ -0,0 +1 @@ +gui.nei.wato=Ores \ No newline at end of file