Jump to content

[1.7.10] NEI + My Mod


HalestormXV

Recommended Posts

So one huge request I keep getting is to add NEI into my mod so that the Crafting Recipe's that are created in my custom crafting manager show up in NEI.

 

It is a pretty basic crafting manager that uses 5x5 and I have a custom smelting manager. I want to be able to answer my community and give them what they are asking (we can't always do this but I figure NEI is beneficial anyway).

 

Problem is I cannot find anything with how to integrate NEI into your mod. Anyone have any starting points I can jump off of? I haven't started any of the code yet, so i am at the VERY beginning stages of it but it is something being sought highly by my community, and like any mod author I want to try and give them what i can. And I know I can do this, but I just need to learn how.

Link to comment
Share on other sites

Basically you need to provide a class which extends

TemplateRecipeHandler

that will take in either an output item (and reconstruct the recipe) or take in an input, and list all recipes that contain it.

 

I've only got 1-to-1 recipes myself, but this should give you an idea of what the class looks like:

 

 

package com.draco18s.ores.recipes.nei;

import java.util.ArrayList;
import java.util.List;

import org.lwjgl.opengl.GL11;

import com.draco18s.hardlib.api.HardLibAPI;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.oredict.OreDictionary;
import codechicken.nei.PositionedStack;
import codechicken.nei.api.IOverlayHandler;
import codechicken.nei.api.IRecipeOverlayRenderer;
import codechicken.nei.recipe.GuiRecipe;
import codechicken.nei.recipe.ICraftingHandler;
import codechicken.nei.recipe.TemplateRecipeHandler;

public class SifterNEI extends TemplateRecipeHandler {

@Override
public String getRecipeName() {
	return "Sifter";
}

@Override
public String getOverlayIdentifier() {
	return "sifter";
}

@Override
public String getGuiTexture() {
	return "ores:textures/gui/sifter.png";
}

//loads recipes from output
@Override
public void loadCraftingRecipes(String outputId, Object...results) {
	if ((outputId.equals("item") || outputId.equals("sifter")) && (results.length > 0)) {
		loadCraftingRecipes((ItemStack)results[0]);
	}
}

//loads recipes from output
@Override
public void loadCraftingRecipes(ItemStack result) {
	ItemStack ingredient = HardLibAPI.recipeManager.getSiftRecipeInput(result);
	//the not-equals here is exclusion code specific to my device
	if(ingredient!=null && !ItemStack.areItemStacksEqual(ingredient, result)) {
		CachedRecipe recipe = new CachedSifterRecipe(ingredient);
		arecipes.add(recipe);
	}
}

//load recipes from partial recipe input
@Override
public void loadUsageRecipes(String inputId, Object... ingredients) {
	if(ingredients.length >= 1 && ingredients[0] instanceof ItemStack)
		loadUsageRecipes((ItemStack) ingredients[0]);
}

//load recipes from partial recipe input
@Override
public void loadUsageRecipes(ItemStack ingredient) {
	ItemStack output = HardLibAPI.recipeManager.getSiftResult(ingredient, false);
	if(output!=null && !ItemStack.areItemStacksEqual(ingredient, output)) {
		CachedRecipe recipe = new CachedSifterRecipe(HardLibAPI.recipeManager.getSiftRecipeInput(output));
		arecipes.add(recipe);
	}
}

//this is for drawing extra widgets like progress bars, smelting indicators, etc.
//I do not recall what the various numbers indicate.  First two are, IIRC, the position on the screen
@Override
public void drawExtras(int recipe) {
	drawProgressBar(74, 20, 176, 0, 17, 24, 40, 1);
}

//You will need to extend TemplateRecipeHandler.CachedRecipe, but you will need to modify the particulars.
//numbers in the positioned stacks below are location on screen.
public class CachedSifterRecipe extends TemplateRecipeHandler.CachedRecipe {
	private PositionedStack ingredient;
	private PositionedStack stack;

	public CachedSifterRecipe(Object _ingredient) {
		super();
		if(_ingredient instanceof ItemStack) {
			ItemStack ingred = ((ItemStack)_ingredient).copy();
			ingred.stackSize = 64; //specific for my machine
			ingred.stackSize = HardLibAPI.recipeManager.getSiftAmount(ingred);
			ingredient = new PositionedStack(ingred, 66, 2);
			stack = new PositionedStack(HardLibAPI.recipeManager.getSiftResult((ItemStack)_ingredient, false), 75, 47);
		}
		else if(_ingredient instanceof String) {
			ArrayList<ItemStack> list = OreDictionary.getOres((String)_ingredient);

			if (list.size() > 0) {
				ingredient = new PositionedStack(list, 66, 2);
				stack = new PositionedStack(HardLibAPI.recipeManager.getSiftResult((ItemStack)_ingredient, false), 75, 47);
			}
		} else {
			ingredient = null;
		}
	}

	@Override
	public PositionedStack getIngredient() {
		return ingredient;
	}

	@Override
	public PositionedStack getResult() {
		return stack;
	}

	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof CachedSifterRecipe)) {
			return false;
		}
		CachedSifterRecipe other = (CachedSifterRecipe)obj;
		if (ingredient == null) {
			if (other.ingredient != null)
				return false;
		}
		else if (ingredient.item == null) {
			if (other.ingredient.item != null)
				return false;
		}
		else if (!ItemStack.areItemStacksEqual(ingredient.item, other.ingredient.item)) {
			return false;
		}
		if (stack == null) {
			if (other.stack != null)
				return false;
		}
		else if (stack.item == null) {
			if (other.stack.item != null)
				return false;
		}
		else if (!ItemStack.areItemStacksEqual(stack.item, other.stack.item)) {
			return false;
		}
		return true;
	}
}
}

 

 

Then you need to register it with NEI:

 

		TemplateRecipeHandler handler = new SifterNEI();
	API.registerUsageHandler(handler);
	API.registerRecipeHandler(handler);
	API.registerGuiOverlay(GuiContainerSifter.class, "sifter");
	API.registerGuiOverlayHandler(GuiContainerSifter.class, new DefaultOverlayHandler(), "sifter");

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Alright that is helpful. Now is implementing the NEI API (or any API) different in 1.7.10 or is this tutorial (judging by the date of the post I imagine that actually was for 1.7.x? Or at least universal):

http://www.minecraftforum.net/forums/mapping-and-modding/mapping-and-modding-tutorials/1571434-tutorial-modding-with-apis

 

still an accurate way to do it. I haven't used an API for anything yet so forgive what would seem like a rudimentary question.

Link to comment
Share on other sites

My code above was for 1.7.10 and was for a very simple machine.  CoolAlias's tutorials are extra generic and cover how to go about working with an API of any sort, looks like 1.7.10 though.

 

The only thing to be aware of when programming for an API is what to do if the API is not present, ie. if someone does not have NEI installed.

 

For something like NEI, this is easy: wrap your NEI API calls in a separate class and only call methods in that class if NEI is loaded.  For other things, like when a block or item needs to implement some API interface, but you still want that item to work when the API isn't available, you need to use the @Optional annotations, found in the cpw.mods.fml.common package (

import cpw.mods.fml.common;

).

 

@Optional is essentially (a flag for) some wicked clever ASM that strips out interfaces and/or methods if the target mod isn't installed (as not stripping them out would cause ClassNotFound errors).  Lets you have TileEntities which extend IEnergyProvider from [tech mod here] and IGenerator from [some other tech mod] and work with either but require none, instead using your own blocks.

 

Here's an example:

 

public class IndustryEventHandler {
@SubscribeEvent
public void onCrafting(PlayerEvent.ItemCraftedEvent event) {
	//standard event method
}

@SubscribeEvent
@Optional.Method(modid = "HardLib")
public void itemFrameCompare(ItemFrameComparatorPowerEvent event) {
	if(event.stack == null || !(event.entity instanceof EntityItemFrame) || event.power > 0) return;
		event.power = ((EntityItemFrame)event.entity).getRotation()*2+1;
}
}

 

The

itemFrameCompare

method has an event object of type

ItemFrameComparatorPowerEvent

which I had added to my own core library, but the mod this is in has no other dependencies, so it was not set up to actually depend on that library.  So rather than go "surprise, you need my library" I marked it as optional.  The feature won't work without the library, but there was no reason to enforce the dependency for such a trivial feature.

(This event/method adds the ItemFrame Comparator function present in 1.8 to 1.7.10 in order to meet the demands of a requested feature in one of my other mods, the general-purpose orientation code was easy to write and fit in with this particular mod).

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.