Jump to content

[1.12.2] Registering Block with Tile Entity


petersx34

Recommended Posts

Hey all, I need some help getting a tile entity to register properly. I am following McJty's modding tutorial for making a GUI and inventory that can be found on his wiki. I noticed he is using ITileEntityProvider instead of overriding createTileEntity and hasTileEntity so I've modified my code to do so. However, when trying to launch the game, I get the following error in CommonProxy on registering the ItemBlock: java.lang.NullPointerException: null
    at com.petersx34.rfutilities.proxy.CommonProxy.registerItems(CommonProxy.java:60) ~[CommonProxy.class:?]

Any guidance on this would be much appreciated. I have been struggling with it for the better half of the day.

Block Code

public class SmartCrafterBlock extends Block {
	
	public static final int GUI_ID = 1;

    public SmartCrafterBlock() {
        super(Material.WOOD);
        setUnlocalizedName(RFUtilities.MODID + ".smartcrafterblock");
        setRegistryName("smartcrafterblock");
        
        setCreativeTab(CreativeTabs.MISC);
    }

    @SideOnly(Side.CLIENT)
    public void initModel() {
        ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(this), 0, new ModelResourceLocation(getRegistryName(), "inventory"));
    }

    @Override
    public TileEntity createTileEntity(World world, IBlockState state) {
        return new SmartCrafterTileEntity();
    }
    
    @Override
    public boolean hasTileEntity(IBlockState state) {
    	return true;
    }

    @Override
    public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
        if (world.isRemote) {
            return true;
        }
        TileEntity te = world.getTileEntity(pos);
        if (!(te instanceof SmartCrafterTileEntity)) {
            return false;
        }
        player.openGui(RFUtilities.instance, GUI_ID, world, pos.getX(), pos.getY(), pos.getZ());
        return true;
    }

}

 

ModBlocks

public class ModBlocks {
	
	@GameRegistry.ObjectHolder("rfutilities:smartcrafterblock")
    public static SmartCrafterBlock smartCrafterBlock;
	
	@SideOnly(Side.CLIENT)
    public static void initModels() {
        smartCrafterBlock.initModel();
    }
	
    @SideOnly(Side.CLIENT)
    public static void initItemModels() {
        
    }

}

 

CommonProxy

@Mod.EventBusSubscriber
public class CommonProxy{
	
	public static Configuration config;
	
    public void preInit(FMLPreInitializationEvent e) {
    	File directory = e.getModConfigurationDirectory();
        config = new Configuration(new File(directory.getPath(), "rfutilities.cfg"));
        Config.readConfig();
    }

    public void init(FMLInitializationEvent e) {
    	NetworkRegistry.INSTANCE.registerGuiHandler(RFUtilities.instance, new GuiProxy());
    }

    public void postInit(FMLPostInitializationEvent e) {
    	if (config.hasChanged()) {
            config.save();
        }
    }

    @SubscribeEvent
    public static void registerBlocks(RegistryEvent.Register<Block> event) {
    	event.getRegistry().register(new SmartCrafterBlock());
    	
    	GameRegistry.registerTileEntity(SmartCrafterTileEntity.class, RFUtilities.MODID + "_smartcrafterblock");
    }

    @SubscribeEvent
    public static void registerItems(RegistryEvent.Register<Item> event) {
    	event.getRegistry().register(new ItemLightningBoots());
    	
        event.getRegistry().register(new ItemBlock(ModBlocks.smartCrafterBlock).setRegistryName(ModBlocks.smartCrafterBlock.getRegistryName()));

    }

}

 

Link to comment
Share on other sites

9 minutes ago, diesieben07 said:
  • Code style, issue 1.
  • Problematic code, issue 3 (GameRegistry.registerTileEntity).
  • Do not use @SideOnly.
  • @ObjectHolder fields are not injected until all registry events have fired.
  • This is McJty's coding style. I am following his tutorial path. If this is incorrect, do you have tutorials available?
  • GameRegistry.registerTileEntity is not deprecated
  • Bullet 1
  • Bullet 1
Link to comment
Share on other sites

7 minutes ago, diesieben07 said:

The version you are using is. Update Forge. 

I'm using the recommended MDK Forge version 14.23.4.2705

8 minutes ago, diesieben07 said:

You should abandon this tutorial. It shows bad practices.

Are there any that you recommend? The ForgeDocs don't really provide example code but rather a description. Being new to this, it is very useful to be able to compile and run working code that can then be picked apart to understand how things work.

Link to comment
Share on other sites

13 minutes ago, diesieben07 said:

The method you are using was deprecated in Build 1.12.2-14.23.3.2694.

Digging into everything a bit further, even though my console says I'm running Forge 14.23.4.2705, my gradle cache shows forge version 1.12.2-14.23.3.2655. I'll update this.

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