Jump to content

Item.getItemFromBlock() returns AIR


NathanPB
 Share

Recommended Posts

Hello, I'm new with Forge (I started this week) and I got a problem:
 

@Override
    public ItemStack getResult() {
        ItemStack stack = new ItemStack(Item.getItemFromBlock(new TestBlock()));
        System.out.println("Item: "+Item.getItemFromBlock(new TestBlock()).getUnlocalizedName());
        System.out.println("Block: "+new TestBlock().getUnlocalizedName());
        System.out.println("Stack: "+new ItemStack(Item.getItemFromBlock(new TestBlock())));
        return stack;
    }

In the console, I saw:
 

[13:45:48] [Client thread/INFO]: [STDOUT]: Item: tile.air
[13:45:48] [Client thread/INFO]: [STDOUT]: Block: tile.test_block
[13:45:48] [Client thread/INFO]: [STDOUT]: Stack: 1xtile.air@0


It is being registered:
 

    public static void register(Block block){
        if(block != null) {
            GameRegistry.register(block);
            GameRegistry.register(new ItemBlock(block).setRegistryName(block.getRegistryName()));
            blocks.add(block);
            System.out.println("[SPELLING] Registered "+block.getRegistryName());
        }
    }
[13:45:43] [Client thread/INFO]: [STDOUT]: [SPELLING] Registered testmod:test_block



The method to register the block and recipes  went called in CommonProxy:
 

public class CommonProxy {

    public void preInit(FMLPreInitializationEvent e){
        ItemManager.autoregister();
        BlockManager.autoregister();
    }
    public void init(FMLInitializationEvent e){
        RecipeManager.autoregister();
    }
    public void postInit(FMLPostInitializationEvent e){

    }
}

 

PS: Item/Block/RecipeManager.autoregister() are only an automated form to register my stuff!
From BlockManager, e.g.:
Its working fine, I think it isn't the problem
 

 public static void autoregister() {
        try {
            for (Class<?> c :  new Reflections("me.nathanpb.TestMod.Blocks").getTypesAnnotatedWith(SpellingBlock.class)) {
                Block block = (Block)c.newInstance();
                register(block);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

Link to comment
Share on other sites

First of all, Blocks are singletons. You should only ever have 1 instance of each block, and not call new TestBlock() each time you want an instance. Store a single instance in a static field somewhere to reference later. That alone will fix a lot of your issues if not all.

 

Also, Reflection for Block registering? You'd be better of with the RegistryEvents (http://mcforge.readthedocs.io/en/latest/concepts/registries/).

  • Like 2

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

7 minutes ago, larsgerrits said:

First of all, Blocks are singletons. You should only ever have 1 instance of each block, and not call new TestBlock() each time you want an instance. Store a single instance in a static field somewhere to reference later. That alone will fix a lot of your issues if not all.

 

Also, Reflection for Block registering? You'd be better of with the RegistryEvents (http://mcforge.readthedocs.io/en/latest/concepts/registries/).

Lol thanks, I didn't know about block instances, I will try to store all in a HashMap<Class, Block>:
 

    public static HashMap<Class, Block> instances = new HashMap<>();

I think it will work...

About the Reflections to register, I like this, I can register a new Block simply using @SpellingBlock =) It works fine and save me from oblivion =)

EDIT:
It worked, thanks =)

 

Edited by NathanPB
Link to comment
Share on other sites

Ew. What will you do when you want to use the same class for more than one block? 

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

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

 Share



×
×
  • Create New...

Important Information

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