Posted March 30, 201411 yr Hi, guys! I have one question. How to replace vanilla item class with my new class? Does anybody know? I seen topic where were BlockReplaceHelper, but my attempts end in failure when I tried to rewrite to fit my needs. [spoiler=BlockReplaceHelper] import com.google.common.collect.BiMap; import cpw.mods.fml.common.registry.FMLControlledNamespacedRegistry; import cpw.mods.fml.common.registry.GameData; import net.minecraft.block.Block; import net.minecraft.init.Blocks; import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.util.ObjectIntIdentityMap; import net.minecraft.util.RegistryNamespaced; import java.lang.reflect.Field; import java.lang.reflect.Modifier; public class BlockReplaceHelper{ public static boolean replaceBlock(Block toReplace, Class<? extends Block> blockClass){ Field modifiersField=null; try{ modifiersField=Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); for(Field field:Blocks.class.getDeclaredFields()){ if (Block.class.isAssignableFrom(field.getType())){ Block block=(Block)field.get(null); if (block==toReplace){ String registryName=Block.blockRegistry.getNameForObject(block); int id=Block.getIdFromBlock(block); ItemBlock item=(ItemBlock) Item.getItemFromBlock(block); System.out.println("Replacing block - "+id+"/"+registryName); Block newBlock=blockClass.newInstance(); FMLControlledNamespacedRegistry<Block> registry= GameData.blockRegistry; registry.putObject(registryName,newBlock); Field map=RegistryNamespaced.class.getDeclaredFields()[0]; map.setAccessible(true); ((ObjectIntIdentityMap)map.get(registry)).func_148746_a(newBlock,id); map=FMLControlledNamespacedRegistry.class.getDeclaredField("namedIds"); map.setAccessible(true); ((BiMap)map.get(registry)).put(registryName,id); field.setAccessible(true); int modifiers=modifiersField.getInt(field); modifiers&=~Modifier.FINAL; modifiersField.setInt(field,modifiers); field.set(null,newBlock); Field itemblock=ItemBlock.class.getDeclaredFields()[0]; itemblock.setAccessible(true); modifiers=modifiersField.getInt(itemblock); modifiers&=~Modifier.FINAL; modifiersField.setInt(itemblock,modifiers); itemblock.set(item,newBlock); System.out.println("Check field: "+field.get(null).getClass()); System.out.println("Check registry: "+Block.blockRegistry.getObjectById(id).getClass()); System.out.println("Check item: "+((ItemBlock)Item.getItemFromBlock(newBlock)).field_150939_a.getClass()); } } } }catch(Exception e){ e.printStackTrace(); return false; } return true; } } P.S.:sorry for my bad english
March 30, 201411 yr It depends on what you're goal is -- there's often a Forge hook for your needs or some other method to do what you're trying to. But if all else fails, you will need to ASM it. I suggest really learning Java before you try that. What are you trying to accomplish by replacing the vanilla class? I like to make mods, just like you. Here's one worth checking out
March 31, 201411 yr Depending on what you want to do, you could just make it so that when someone crafts a vanilla axe they get yours instead -- that would be just a matter of replacing the recipe. If yours is an extension of the vanilla axe, it should work pretty well in all code where an axe is needed. For most practical purposes it your axe would be the axes players would get. If you really want to eliminate vanilla axes entirely (e.g. villagers can never have them, etc.) that can get pretty involved. You technically can eliminate axes from the item Registry, but I suspect that would cause trouble because I expect that axes are referred to in various places through the vanilla code. Check out my tutorials here: http://jabelarminecraft.blogspot.com/
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.