Jump to content

Recommended Posts

Posted

I registered my IRecipe instead of a stick recipe, but in the recipe book it's empty, the stick icon itself is there.
As you can see in the screenshot, the recipe icon is there, but supposedly I can do it, although I have no resources. When you click on the crafting icon, there is no reaction.
How to make a normal recipe in the book?

image.thumb.png.aed8203edd297e9e0147f724af4c0454.png 

Posted
17 hours ago, Animefan8888 said:

Show your code.

package ru.fruten.ntc;

import java.lang.reflect.Field;
import java.util.ArrayList;

import com.google.common.base.Throwables;
import com.google.common.collect.Lists;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ContainerPlayer;
import net.minecraft.inventory.ContainerWorkbench;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.inventory.SlotCrafting;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.ReflectionHelper;

public class NewStickIRecipe implements IRecipe {
	
	private static IRecipe rec;
	
	@Override
	public IRecipe setRegistryName(ResourceLocation name) {
		return NewStickIRecipe.rec;
	}

	@Override
	public ResourceLocation getRegistryName() {
		return NewStickIRecipe.rec.getRegistryName();
	}

	@Override
	public Class<IRecipe> getRegistryType() {
		return NewStickIRecipe.rec.getRegistryType();
	}

	@Override
	public boolean matches(InventoryCrafting inv, World worldIn) {
		return NewStickIRecipe.rec.matches(inv, worldIn) && findPlayer(inv).experienceLevel >= 1;
	}

	@Override
	public ItemStack getCraftingResult(InventoryCrafting inv) {
		return NewStickIRecipe.rec.getCraftingResult(inv);
	}

	@Override
	public boolean canFit(int width, int height) {
		return NewStickIRecipe.rec.canFit(width, height);
	}

	@Override
	public ItemStack getRecipeOutput() {
		return NewStickIRecipe.rec.getRecipeOutput();
	}
	
	private static final Field eventHandlerField = ReflectionHelper.findField(InventoryCrafting.class, "eventHandler");
	private static final Field containerPlayerPlayerField = ReflectionHelper.findField(ContainerPlayer.class, "player");
	private static final Field slotCraftingPlayerField = ReflectionHelper.findField(SlotCrafting.class, "player");
	
	private static EntityPlayer findPlayer(InventoryCrafting inv) {
        try {
            Container container = (Container) eventHandlerField.get(inv);
            if (container instanceof ContainerPlayer) {
                return (EntityPlayer) containerPlayerPlayerField.get(container);
            } else if (container instanceof ContainerWorkbench) {
                return (EntityPlayer) slotCraftingPlayerField.get(container.getSlot(0));
            } else {
                return null;
            }
        } catch (Exception e) {
            throw Throwables.propagate(e);
        }
    }
	
	public static IRecipe getRecipeFromItem(Item item) {
		ArrayList<IRecipe> recipes = Lists.newArrayList(CraftingManager.REGISTRY.iterator());
		IRecipe recipeFromItemStack;
		for (IRecipe recipe : recipes) {
			if (recipe.getRecipeOutput().getItem() == item) {
				recipeFromItemStack = recipe;
				return recipeFromItemStack;
			}
		}
		return null;
	}
	
	public static void recRecipe(IRecipe recipe) {
		rec = recipe;
	}
	
}

My IRecipe.
 

package ru.fruten.ntc.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import com.google.common.collect.Lists;

import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import net.minecraftforge.registries.ForgeRegistry;
import net.minecraftforge.registries.IForgeRegistryEntry;
import net.minecraftforge.registries.IForgeRegistryModifiable;
import ru.fruten.ntc.NewStickIRecipe;

public class AllVanillaRecipes {
	
	public static void addNewStickRecipe() {
		 ForgeRegistry<IRecipe> recipeRegistry = (ForgeRegistry<IRecipe>)ForgeRegistries.RECIPES;
		 NewStickIRecipe recipe = new NewStickIRecipe();
		 recipeRegistry.register(recipe);
	}
	
	public static void removeStickRecipe() {
        ForgeRegistry<IRecipe> recipeRegistry = (ForgeRegistry<IRecipe>)ForgeRegistries.RECIPES;
        recipeRegistry.remove(Items.STICK.getRegistryName());
   }
}

Methods for Init.

 

@EventHandler
	public static void Init(FMLInitializationEvent event) {
		NewStickIRecipe.recRecipe(NewStickIRecipe.getRecipeFromItem(Items.STICK));
		AllVanillaRecipes.removeStickRecipe();
		AllVanillaRecipes.addNewStickRecipe();
	}

Himself init. 

 

 

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.