Jump to content

Item not appearing in creative inventory [solved]


Earthcomputer

Recommended Posts

I have an item, called booPowder, which for some reason is not appearing in the creative inventory - the only way to obtain it is with the give command. This item is the icon of the creative tab MCMario.CREATIVE_TAB, the tab it should appear in, proving that its texture and everything is linked properly. I also have a block, called mushroomPortalFrame, which displays perfectly.

Here is my code:

 

 

MCMario.java:

[embed=425,349]package uk.co.ijay.mcmario;

 

import net.minecraft.creativetab.CreativeTabs;

import net.minecraft.item.Item;

import net.minecraftforge.fml.common.Mod;

import net.minecraftforge.fml.common.Mod.EventHandler;

import net.minecraftforge.fml.common.Mod.Instance;

import net.minecraftforge.fml.common.event.FMLInitializationEvent;

import uk.co.ijay.mcmario.block.MCMarioBlocks;

import uk.co.ijay.mcmario.item.MCMarioItems;

 

@Mod(modid = MCMario.ID, name = MCMario.NAME, version = MCMario.VERSION)

public class MCMario {

 

public static final boolean ACTIVE = true;

 

public static final String ID = "mcmario";

public static final String NAME = (ACTIVE) ? "Minecraft Mario" : "Minecraft Mario (Inactive)";

public static final String VERSION = "0.0.1";

 

@Instance(MCMario.ID)

public static MCMario instance;

 

public static final int mushroomDimension = 0xFEDCBA;

public static final CreativeTab CREATIVE_TAB = new CreativeTab("mCMario", MCMarioItems.booPowder);

 

@EventHandler

public void init(FMLInitializationEvent e) throws Exception {

if(!ACTIVE)

return;

MCMarioBlocks.registerBlocks(e);

MCMarioItems.registerItems(e);

}

 

private static class CreativeTab extends CreativeTabs {

private Item item;

public CreativeTab(String string, Item item) {

super(string);

this.item = item;

}

 

@Override

public Item getTabIconItem() {

return item;

}

}

 

}

[/embed]

MCMarioBlocks.java:

[embed=425,349]package uk.co.ijay.mcmario.block;

 

import net.minecraft.block.Block;

import net.minecraft.block.material.Material;

import net.minecraft.client.Minecraft;

import net.minecraft.client.resources.model.ModelResourceLocation;

import net.minecraft.item.Item;

import net.minecraftforge.fml.common.event.FMLInitializationEvent;

import net.minecraftforge.fml.common.registry.GameRegistry;

import net.minecraftforge.fml.relauncher.Side;

import uk.co.ijay.mcmario.MCMario;

import uk.co.ijay.mcmods.IJMCModHelper;

 

public class MCMarioBlocks {

 

public static void registerBlocks(FMLInitializationEvent e)

throws Exception {

registerBlock(e, mushroomPortalFrame, "mushroom_portal_frame");

}

 

private static void registerBlock(FMLInitializationEvent e, Block block,

String name) throws Exception {

registerBlock(e, block, 0, name);

}

 

private static void registerBlock(FMLInitializationEvent e, Block block,

int meta, String name) throws Exception {

GameRegistry.registerBlock(block, name);

if (e.getSide().equals(Side.CLIENT)) {

Minecraft

.getMinecraft()

.getRenderItem()

.getItemModelMesher()

.register(

Item.getItemFromBlock(block),

meta,

new ModelResourceLocation(IJMCModHelper

.ensureNamespaced(name), "inventory"));

}

}

 

public static final Block mushroomPortalFrame = new IJMCModHelper.Block(

Material.plants).setUnlocalizedName("mushroom_portal_frame")

.setBlockUnbreakable().setCreativeTab(MCMario.CREATIVE_TAB);

        // Block unused yet

public static final Block mushroomPortal = new BlockMushroomPortal()

.setUnlocalizedName("mushroom_portal");

 

}

[/embed]

MCMarioItems.java:

[embed=425,349]package uk.co.ijay.mcmario.item;

 

import net.minecraft.client.Minecraft;

import net.minecraft.client.resources.model.ModelResourceLocation;

import net.minecraft.item.Item;

import net.minecraftforge.fml.common.event.FMLInitializationEvent;

import net.minecraftforge.fml.common.registry.GameRegistry;

import net.minecraftforge.fml.relauncher.Side;

import uk.co.ijay.mcmario.MCMario;

import uk.co.ijay.mcmods.IJMCModHelper;

 

public class MCMarioItems {

 

public static void registerItems(FMLInitializationEvent e) {

registerItem(e, booPowder, "boo_powder");

}

 

private static void registerItem(FMLInitializationEvent e, Item item,

String name) {

registerItem(e, item, 0, name);

}

 

private static void registerItem(FMLInitializationEvent e, Item item,

int meta, String name) {

GameRegistry.registerItem(item, name);

if (e.getSide().equals(Side.CLIENT)) {

Minecraft

.getMinecraft()

.getRenderItem()

.getItemModelMesher()

.register(

item,

meta,

new ModelResourceLocation(IJMCModHelper

.ensureNamespaced(name), "inventory"));

}

}

 

public static final Item booPowder = new ItemBooPowder()

.setUnlocalizedName("boo_powder").setCreativeTab(

MCMario.CREATIVE_TAB);

 

}

[/embed]

IJMCModHelper.java:

[embed=425,349]package uk.co.ijay.mcmods;

 

import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

 

import net.minecraft.block.material.Material;

import net.minecraftforge.fml.common.Loader;

import net.minecraftforge.fml.common.ModContainer;

 

/**

* A helper class for making Minecraft mods

*

* @author IJay

*

*/

public class IJMCModHelper {

 

/**

* Helper method to get the active modid

*

* @return the active modid

*/

public static String getActiveModId() {

ModContainer mc = Loader.instance().activeModContainer();

return (mc == null) ? "minecraft" : mc.getModId();

}

 

/**

* Helper method to check whether the name is properly namespaced

*

* @param name

*            - the name to check

* @return whether the name is properly namespaced

*/

public static boolean isProperlyNamespaced(String name) {

return (name.indexOf(":") >= 0 && name.replaceFirst(":", "").indexOf(

":") == -1);

}

 

/**

* Adds a namespace even if there is already one existent

*

* @param name

*            - the name to add a namespace to

* @return the name with the namespace added

*/

public static String addNamespace(String name) {

return getActiveModId() + ":" + name;

}

 

/**

* Ensures name is properly namespaced. If it is, it returns the argument

* untouched. If there is no namespace, adds a namespace with the active

* modid. If there are too many namespaces, it removes all but one of them.

*

* @param name

*            - the name to ensure namespaced

* @return the name namespaced

*/

public static String ensureNamespaced(String name) {

if (isProperlyNamespaced(name))

return name;

if (name.replaceFirst(":", "").indexOf(":") >= 0)

return name.substring(name.indexOf(":") + 1);

return addNamespace(name);

}

       

        // The below reflection methods are still under development. Pretend they're not there: this is not what this post is about

/**

* As many say, Minecraft is a poorly coded game. This is a helper method to

* get those normally inaccessible static fields you want to get your hands

* on. This method is not very fast, so use sparingly.

*

* @param clazz

*            - the class the static field belongs to

* @param fieldName

*            - the name of the static field

* @return the value of the field in Object form

* @throws NoSuchFieldException

*            when there is no such field

* @throws IllegalAccessException

*            when unable to access the field

*/

public static Object getPrivateStaticField(Class<?> clazz, String fieldName)

throws NoSuchFieldException, IllegalAccessException {

Field field = clazz.getDeclaredField(fieldName);

if (!field.isAccessible())

field.setAccessible(true);

return field.get(null);

}

 

/**

* As many say, Minecraft is a poorly coded game. This is a helper method to

* get those normally inaccessible fields you want to get your hands on.

* This is not very fast, so use sparingly.

*

* @param obj

*            - the Object to get the field of

* @param fieldName

*            - the name of the field

* @return the value of the field in Object form

* @throws NoSuchFieldException

*            when there is no such field

* @throws IllegalAccessException

*            when unable to access the field

*/

public static Object getPrivateField(Object obj, String fieldName)

throws NoSuchFieldException, IllegalAccessException {

Class<?> clazz = obj.getClass();

Field field = clazz.getDeclaredField(fieldName);

if (!field.isAccessible())

field.setAccessible(true);

return field.get(obj);

}

 

/**

* As many say, Minecraft is a poorly coded game. This is a helper method to

* invoke those normally inaccessible static methods you want to get your

* hands on. This is not very fast, so use sparingly.

*

* @param clazz

*            - the class the static method belongs to

* @param methodName

*            - the name of the method

* @param args

*            - the arguments to pass to the method

* @return what the method returns in Object form

* @throws NoSuchMethodException

*            when there is no such method

* @throws IllegalAccessException

*            when unable to access the method

*/

public static Object invokePrivateStaticMethod(Class<?> clazz,

String methodName, Object... args) throws NoSuchMethodException,

IllegalAccessException {

Method[] methods = clazz.getDeclaredMethods();

outside: for (Method method : methods) {

if (!method.getName().equals(methodName))

continue;

Class<?>[] types = method.getParameterTypes();

if (types.length != args.length)

continue;

for (int i = 0; i < types.length; i++) {

if (!types.isInstance(args))

continue outside;

}

if (!method.isAccessible())

method.setAccessible(true);

try {

return method.invoke(null, args);

} catch (InvocationTargetException e) {

break;

} catch (NullPointerException e) {

break;

}

}

String m = clazz.getName() + "." + methodName + "(";

for (int i = 0; i < args.length; i++) {

if (i != 0)

m += ", ";

m += args.getClass().getName();

}

m += ")";

throw new NoSuchMethodException(m);

}

 

/**

* As many say, Minecraft is a poorly coded game. This is a helper method to

* invoke those normally inaccessible methods you want to get your hands on.

* This is not very fast, so use sparingly.

*

* @param target

*            - the instance to run the method from

* @param methodName

*            - the name of the method

* @param args

*            - the arguments to pass to the method

* @return what the method returns in Object form

* @throws NoSuchMethodException

*            when there is no such method

* @throws IllegalAccessException

*            when unable to access the method

* @throws InvocationTargetException

*            when, for whatever reason, the target is invalid

*/

public static Object invokePrivateMethod(Object target, String methodName,

Object... args) throws NoSuchMethodException,

IllegalAccessException, InvocationTargetException {

Class<?> clazz = target.getClass();

Method[] methods = clazz.getDeclaredMethods();

outside: for (Method method : methods) {

if (!method.getName().equals(methodName))

continue;

Class<?>[] types = method.getParameterTypes();

if (types.length != args.length)

continue;

for (int i = 0; i < types.length; i++) {

if (!types.isInstance(args))

continue outside;

}

if (!method.isAccessible())

method.setAccessible(true);

return method.invoke(target, args);

}

String m = clazz.getName() + "." + methodName + "(";

for (int i = 0; i < args.length; i++) {

if (i != 0)

m += ", ";

m += args.getClass().getName();

}

m += ")";

throw new NoSuchMethodException(m);

}

 

/**

* It can be rather annoying that the Block constructor is protected, so

* just use this instead

*

* @author IJay

*

*/

public static class Block extends net.minecraft.block.Block {

 

public Block(Material material) {

super(material);

}

 

}

}

[/embed]

assets.mcmario.blockstates.mushroom_portal_frame.json:

[embed=425,349]# THIS IS THE BLOCKSTATE FILE. IT GOES IN THE assets/mcmario/blockstates/ folder.

# Generated using sheenrox82's JSON File Generator for Minecraft 1.8.

 

{

    "variants": {

        "normal": { "model": "mcmario:mushroom_portal_frame" }

    }

}

[/embed]

assets.mcmario.models.block.mushroom_portal_frame.json:

[embed=425,349]# THIS IS THE BLOCK MDOEL FILE. IT GOES IN THE assets/mcmario/models/block/ folder.

# Generated using sheenrox82's JSON File Generator for Minecraft 1.8.

 

{

    "parent": "block/cube_all",

    "textures": {

        "all": "mcmario:blocks/mushroom_portal_frame"

    }

}

[/embed]

assets.mcmario.models.item.boo_powder.json:

[embed=425,349]# THIS IS THE ITEM FILE. IT GOES IN THE assets/mcmario/models/item/ folder.

# Generated using sheenrox82's JSON File Generator for Minecraft 1.8.

 

{

    "parent": "builtin/generated",

    "textures": {

        "layer0": "mcmario:items/boo_powder"

    },

    "display": {

        "thirdperson": {

            "rotation": [ -90, 0, 0 ],

            "translation": [ 0, 1, -3 ],

            "scale": [ 0.55, 0.55, 0.55 ]

        },

        "firstperson": {

            "rotation": [ 0, -135, 25 ],

            "translation": [ 0, 4, 2 ],

            "scale": [ 1.7, 1.7, 1.7 ]

        }

    }

}

[/embed]

assets.mcmario.models.item.mushroom_portal_frame.json:

[embed=425,349]# THIS IS THE BLOCK ITEM MDOEL FILE. IT GOES IN THE assets/mcmario/models/item/ folder.

# Generated using sheenrox82's JSON File Generator for Minecraft 1.8.

 

{

    "parent": "mcmario:block/mushroom_portal_frame",

    "display": {

        "thirdperson": {

            "rotation": [ 10, -45, 170 ],

            "translation": [ 0, 1.5, -2.75 ],

            "scale": [ 0.375, 0.375, 0.375 ]

        }

    }

}

[/embed]

 

 

Any suggestions as to why the item is not appearing in the creative inventory?

catch(Exception e)

{

 

}

Yay, Pokémon exception handling, gotta catch 'em all (and then do nothing with 'em).

Link to comment
Share on other sites

Yes I know what a static initializer is, I just overlooked that problem. To my understanding as long as the creative tab is instantiated before it is set to the item, it will work, I'm just compiling and running now to test...

catch(Exception e)

{

 

}

Yay, Pokémon exception handling, gotta catch 'em all (and then do nothing with 'em).

Link to comment
Share on other sites

  • 4 months later...

Yes, it's solved now. One thing I've learnt is to avoid using static initializers and instead use methods, as otherwise you can get bugs which are very hard to debug.

catch(Exception e)

{

 

}

Yay, Pokémon exception handling, gotta catch 'em all (and then do nothing with 'em).

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.