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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • The version of what? I just imported the MDK into Eclipse. I haven't made any edits to the Java code in it or even any other files. I haven't even gotten to changing the modid from "examplemod" yet.
    • Captain WebGenesis is a Cyber skilled hacker with special abilities and skills that enact penetration to help victims of Crypto Fraudulent activities who are faced with cyber challenges get back their lost funds. Simply file your complaint through the website (https://captainwebgenesis. com) and have all your crypto recovered back.
    • The crash log keeps saying something about the tick rate. Can someone please help me fix?   ---- Minecraft Crash Report ---- // Everything's going to plan. No, really, that was supposed to happen. Time: 2024-06-21 16:24:55 Description: Exception in server tick loop java.lang.NullPointerException: Cannot invoke "com.cobblemon.mod.common.api.conditional.RegistryLikeCondition.fits(Object, net.minecraft.core.Registry)" because "condition" is null at com.cobblemon.mod.common.api.spawning.condition.SpawningCondition.fits(SpawningCondition.java:85) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.condition.AreaTypeSpawningCondition.fits(AreaTypeSpawningCondition.java:30) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.condition.GroundedTypeSpawningCondition.fits(GroundedTypeSpawningCondition.java:28) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.condition.GroundedTypeSpawningCondition.fits(GroundedTypeSpawningCondition.java:24) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.condition.SpawningCondition.isSatisfiedBy(SpawningCondition.java:68) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.detail.SpawnDetail.isSatisfiedBy(SpawnDetail.java:78) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.spawner.Spawner$DefaultImpls.getMatchingSpawns(Spawner.java:42) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.spawner.TickingSpawner.getMatchingSpawns(TickingSpawner.java:31) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.selection.FlatContextWeightedSelector.getSelectionData(FlatContextWeightedSelector.java:79) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.selection.FlatContextWeightedSelector.select(FlatContextWeightedSelector.java:106) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.spawner.AreaSpawner.run(AreaSpawner.java:94) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.spawner.TickingSpawner.tick(TickingSpawner.java:71) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.SpawnerManager.onServerTick(SpawnerManager.java:58) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.events.ServerTickHandler.onTick(ServerTickHandler.java:20) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.Cobblemon$initialize$7.invoke(Cobblemon.java:368) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.Cobblemon$initialize$7.invoke(Cobblemon.java:368) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.reactive.ObservableSubscription.handle(ObservableSubscription.java:16) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.reactive.SimpleObservable.emit(SimpleObservable.java:39) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.forge.event.ForgePlatformEventHandler.onTick(ForgePlatformEventHandler.kt:172) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.forge.event.__ForgePlatformEventHandler_onTick_ServerTickEvent.invoke(.dynamic) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading,pl:eventbus:B} at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:73) ~[eventbus-6.0.5.jar%2352!/:?] {} at net.minecraftforge.eventbus.EventBus.post(EventBus.java:315) ~[eventbus-6.0.5.jar%2352!/:?] {} at net.minecraftforge.eventbus.EventBus.post(EventBus.java:296) ~[eventbus-6.0.5.jar%2352!/:?] {} at net.minecraftforge.event.ForgeEventFactory.onPostServerTick(ForgeEventFactory.java:950) ~[forge-1.20.1-47.2.17-universal.jar%23138!/:?] {re:classloading} at net.minecraft.server.MinecraftServer.m_5705_(MinecraftServer.java:835) ~[server-1.20.1-20230612.114412-srg.jar%23133!/:?] {re:computing_frames,pl:accesstransformer:B,re:classloading,pl:accesstransformer:B,re:mixin,pl:accesstransformer:B} at net.minecraft.server.MinecraftServer.m_130011_(MinecraftServer.java:661) ~[server-1.20.1-20230612.114412-srg.jar%23133!/:?] {re:computing_frames,pl:accesstransformer:B,re:classloading,pl:accesstransformer:B,re:mixin,pl:accesstransformer:B} at net.minecraft.server.MinecraftServer.m_206580_(MinecraftServer.java:251) ~[server-1.20.1-20230612.114412-srg.jar%23133!/:?] {re:computing_frames,pl:accesstransformer:B,re:classloading,pl:accesstransformer:B,re:mixin,pl:accesstransformer:B} at java.lang.Thread.run(Thread.java:840) ~[?:?] {re:mixin}
    • okay i've managed to fix it. looks like i made the stupidest error known to man. i hadn't installed forge to the client, which i assumed only affected the launcher which wouldn't affect me because i used a third party one. evidently not. the more you know
    • tried a fresh install of above and beyond with java 8 installed. got a popup that says "Error: Unable to access jarfile forge-1.16.5-36.2.8.jar". every good programmer knows getting a different error means you've made progress!
  • Topics

×
×
  • Create New...

Important Information

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