Jump to content

Item disappears after player crafts the item with custom IRecipe implementation.


AntonBespoiasov

Recommended Posts

public class OreFinderBindOre extends IForgeRegistryEntry.Impl<IRecipe> implements IRecipe
{	
	public OreFinderBindOre()
	{
		setRegistryName(References.MOD_ID + ":ore_finder_bind_ore");
	}
	

	@Override
	public boolean matches(InventoryCrafting inv, World worldIn)
	{
		ItemStack oreFinder = null;
		ItemStack ore = null;
		
		for (int i = 0; i < inv.getSizeInventory(); i++)
		{
			ItemStack cur = inv.getStackInSlot(i);
			
			if (cur.getItem() == Main.ORE_FINDER)
			{
				if (oreFinder != null) return false;
				oreFinder = cur;
			} else if (cur.getItem() instanceof ItemBlock)
			{
				if (ore == null) ore = new ItemStack(cur.getItem(), 1, cur.getItemDamage());
				else if (ore.isItemEqual(cur)) ore.grow(1);
				else return false;
			} else 
			{
				return false;
			}
		}
		
		return ore.getCount() == 8 && oreFinder.getTagCompound() == null; 
	}

	@Override
	public ItemStack getCraftingResult(InventoryCrafting inv)
	{
		ItemStack oreFinder = null;
		ItemBlock ore = null;	
		
		Main.LOGGER.info("Executed the line");
		
		for (int i = 0; i < inv.getSizeInventory(); i++) // Fetches ingredients
		{
			ItemStack cur = inv.getStackInSlot(i);
			
			if (cur.getItem() == Main.ORE_FINDER)
			{
				oreFinder = cur;
			} else if (cur.getItem() instanceof ItemBlock)
			{
				ore = (ItemBlock)cur.getItem();
			}
		}
		
		NBTTagCompound nbt = oreFinder.getTagCompound();
		
		if (nbt == null || !nbt.hasKey("Ore"))
		{
			nbt = new NBTTagCompound();
			nbt.setInteger("Ore", Block.getIdFromBlock(ore.getBlock())); // Set the ore in the ore finder
		}
		oreFinder.setTagCompound(nbt);
		
		return oreFinder;
	}

	@Override
	public boolean canFit(int width, int height)
	{
		return width * height >= 9;
	}

	public boolean isDynamic()
    {
        return true;
    }
	
	@Override
	public ItemStack getRecipeOutput()
	{
		return ItemStack.EMPTY;
	}
	
	public NonNullList<ItemStack> getRemainingItems(InventoryCrafting inv)
    {
        NonNullList<ItemStack> nonnulllist = NonNullList.<ItemStack>withSize(inv.getSizeInventory(), ItemStack.EMPTY);

        for (int i = 0; i < nonnulllist.size(); ++i)
        {
            ItemStack itemstack = inv.getStackInSlot(i);
            nonnulllist.set(i, net.minecraftforge.common.ForgeHooks.getContainerItem(itemstack));
        }

        return nonnulllist;
    }
}

The above code is the implementation of IRecipe.

The Ore finder has NBT tag "Ore" that stores ore. The ore finder is like compass but it points to closest block which ID is stored in the "Ore" tag.

The purpose of the implementation of IRecipe is to say which block to find. You put 8 of the blocks and an Ore finder on the crafting grid. It puts ID of the blocks in the "Ore" tag, consumes blocks and gives as result Ore finder with the new NBT tag.

The problem is when the player takes the result from recipe output slot puts it in his inventory and then closes crafting inventory the item disappears. Why does it happen. Maybe server-client async but I checked vanilla implementations of IRecipe and they didn't use any messages and packets. Thanks in advance for solving such a personal problem.

Edited by AntonBespoiasov
Updated code
Link to comment
Share on other sites

Override getRemainingItems.

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

15 minutes ago, Draco18s said:

Override getRemainingItems.

I overrode getRemainingItems with code from net.minecraft.item.crafting.RecipeRepairItem. The code simply tries to get container from items that lie on the crafting grid. But it didn't help. Also I solved the NBT problem. 

New code is in the first post of the topic with the implementation of getRemainingItems and new NBT setting code.

Edited by AntonBespoiasov
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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