Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

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

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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.