Jump to content

Refill/Repair damaged Item


CrispyChicken

Recommended Posts

ok, in one of my mods i created a recipe that sets an item to data in another item,

this is how a player uses it,

put both items on the crafting table , and its done automatically the items remain on the crafting grid

 

here is the recipe

package me.el.LoonTools;

import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;

public class TrackerRecipe implements IRecipe {

public ItemStack getCraftingResult(InventoryCrafting inv) {
	return null;
}

public ItemStack getRecipeOutput() {
	return null;
}

public int getRecipeSize() {
	return 0;
}

public boolean matches(InventoryCrafting inv, World w) {
	ItemStack cr=null;
	ItemStack tr=null;
	int sn=0;
	for (int s=0;s<inv.getSizeInventory();s++){
		ItemStack i=inv.getStackInSlot(s);
		if (i!=null){
			Item it = i.getItem();
			if(it instanceof ItemCrystal){
				if (cr!=null) return false;
				cr=i;
			}else if(it instanceof ItemLocationTracker){
				if (tr!=null) return false;
				tr=i;
				sn=s;
			}else return false;
		}
	}
	if(cr==null || tr==null) return false;

	if (!cr.hasTagCompound()) return false;

	try {
		NBTTagCompound c = cr.getTagCompound();
		float x=c.getFloat("bx");
		float z=c.getFloat("bz");
		String wld=c.getString("bw");

		NBTTagCompound t;
		if (tr.hasTagCompound()){
			t=tr.getTagCompound();
			try {
				float tx = t.getFloat("bx");
				float tz = t.getFloat("bz");
				String tw=t.getString("bw");
				System.out.println(tx+" "+tz+" "+tw);
				System.out.println(x+" "+z+" "+wld);
				if (x==tx && z==tz && tw.equals(wld)) return false;
			} catch (Exception e) {
			}
		}else{
			t=new NBTTagCompound();
		}


		t.setFloat("bx", x);
		t.setFloat("bz", z);
		t.setString("bw", wld);

		ItemStack result = tr.copy();
		result.setTagCompound(t);
		result.setStackDisplayName(cr.getDisplayName());
		inv.setInventorySlotContents(sn, result);
		return false;
	} catch (Exception e) {
		return false;
	}
}
}

 

you will notice that everything is done in the matches method

you will also notice that matches always returns false, this prevents items being lost by consumption during crafting

 

Hope this helps

 

Link to comment
Share on other sites

oh, another thing you need to make sure of, is that you only make changes and replace the items on the crafting grid when necessary , otherwise you get a non exiting loop and a stack error crash

 

so in your case if the item is full / repaired , or the filling/repairing item is empty, exit with out changing either item

 

Link to comment
Share on other sites

If you, yes YOU, also want an item that is repaired in the crafting table or in the survival crafting grid then feel free to use this code

 

 

public boolean matches(InventoryCrafting inventory, World world) {
	ItemStack itemThatWillBeRepaired = null;
	ItemStack itemThatIsUsedToRepaire = null;
	for (int i=0; i < inventory.getSizeInventory(); i++){
		ItemStack itemStack = inventory.getStackInSlot(i);
			if (itemStack != null){
				if (itemStack.getItem() == YourMod.itemThatWillBeRepaired){
					if (itemThatWillBeRepaired != null)return false;
					itemThatWillBeRepaired = itemStack;
				}else if (itemStack.getItem() == Items.stick){
					if (itemThatIsUsedToRepaire != null)return false;
					itemThatIsUsedToRepaire = itemStack;
				}
			}
	}
	return itemThatWillBeRepaired != null && itemThatIsUsedToRepaire != null;
}

public int getRecipeSize() {
	return 9;
}

 

 

If anything doesnt work, please post it in comments for other users

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

Announcements



×
×
  • Create New...

Important Information

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