Skip to content

Commit

Permalink
Add NEI handling
Browse files Browse the repository at this point in the history
  • Loading branch information
glowredman committed Dec 20, 2020
1 parent 588e08a commit 9f9fe78
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 2 deletions.
57 changes: 57 additions & 0 deletions src/main/java/glowredman/wherearetheores/WATO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}

}
27 changes: 27 additions & 0 deletions src/main/java/glowredman/wherearetheores/nei/NEIWATOConfig.java
Original file line number Diff line number Diff line change
@@ -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());

}

}
184 changes: 184 additions & 0 deletions src/main/java/glowredman/wherearetheores/nei/WATORecipeHandler.java
Original file line number Diff line number Diff line change
@@ -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<String, Map<String, List<String>>> e : ConfigHandler.config.entrySet()) {
String ore = e.getKey();
Map<String, List<String>> dimensionInfo = e.getValue();

// decode ore:
List<ItemStack> decodedOres = new ArrayList<ItemStack>();
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<ItemStack> decodedOres = new ArrayList<ItemStack>();
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<String> dimensions = new ArrayList<String>();
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<String, List<String>> dimensionInfo;
public PositionedStack ore;
public int numVariants;

public CachedOreRecipe(Map<String, List<String>> dimensionInfo, List<ItemStack> 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;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/wherearetheores/lang/en_US.lang
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gui.nei.wato=Ores

0 comments on commit 9f9fe78

Please sign in to comment.