Jump to content

Recommended Posts

Posted

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

Posted

Sorry about the code, is there a tutorial on how to fix that?

 

So, I expect I should initialize my creative tab in my init method? I'll try that now. Thx

catch(Exception e)

{

 

}

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

Posted

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

Posted

Okay so I changed my init method to a preinit and the FMLInitializationEvents to FMLPreInitializationEvents. It appears to be more complicated than I thought. Thanks for your contribution!

catch(Exception e)

{

 

}

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

  • 4 months later...
Posted

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

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I tried do download the essential mod to my mod pack but i didnt work. I paly on 1.21 and it should work. I use neoforge for my modding. The weird things is my friend somehow added the mod to his modpack and many others that I somehow can´t. Is there anything i can do? 
    • Thanks, I've now installed a slightly newer version and the server is at least starting up now.
    • i have the same issue. Found 1 Create mod class dependency(ies) in createdeco-1.3.3-1.19.2.jar, which are missing from the current create-1.19.2-0.5.1.i.jar Found 11 Create mod class dependency(ies) in createaddition-fabric+1.19.2-20230723a.jar, which are missing from the current create-1.19.2-0.5.1.i.jar Detailed walkthrough of mods which rely on missing Create mod classes: Mod: createaddition-fabric+1.19.2-20230723a.jar Missing classes of create: com/simibubi/create/compat/jei/category/sequencedAssembly/JeiSequencedAssemblySubCategory com/simibubi/create/compat/recipeViewerCommon/SequencedAssemblySubCategoryType com/simibubi/create/compat/rei/CreateREI com/simibubi/create/compat/rei/EmptyBackground com/simibubi/create/compat/rei/ItemIcon com/simibubi/create/compat/rei/category/CreateRecipeCategory com/simibubi/create/compat/rei/category/WidgetUtil com/simibubi/create/compat/rei/category/animations/AnimatedBlazeBurner com/simibubi/create/compat/rei/category/animations/AnimatedKinetics com/simibubi/create/compat/rei/category/sequencedAssembly/ReiSequencedAssemblySubCategory com/simibubi/create/compat/rei/display/CreateDisplay Mod: createdeco-1.3.3-1.19.2.jar Missing classes of create: com/simibubi/create/content/kinetics/fan/SplashingRecipe
    • The crash points to moonlight lib - try other builds or make a test without this mod and the mods requiring it
    • Do you have shaders enabled? There is an issue with the mod simpleclouds - remove this mod or disable shaders, if enabled  
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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