Jump to content

Recommended Posts

Posted

I can't make my item display its texture. This is my code:

 

Main mod class:

@Mod(modid = TheMod.MODID, name = TheMod.NAME, version = TheMod.VERSION)
public class TheMod
{
public static final String MODID = "mymod";
public static final String VERSION = "1.9-Alpha-1.0";
public static final String NAME = "My Mod";

@SidedProxy(clientSide = ClientProxy.NAME, serverSide = CommonProxy.NAME)
public static CommonProxy proxy;

@Instance
public static TheMod instance;
public static Logger logger;

@EventHandler
public void preInit(FMLPreInitializationEvent event)
{
	logger = event.getModLog();
	proxy.preInit(event);
}

@EventHandler
public void init(FMLInitializationEvent event)
{
	proxy.init(event);
}

@EventHandler
public void postInit(FMLPostInitializationEvent event)
{
	proxy.postInit(event);
}
}

 

CommomProxy class:

public class CommonProxy
{
public static final String NAME = "mod.mymod.CommonProxy";

public void preInit(FMLPreInitializationEvent event)
{

}

public void init(FMLInitializationEvent event)
{
	Items.init(event);
}

public void postInit(FMLPostInitializationEvent event)
{

}
}

 

ClientProxy class:

public class ClientProxy extends CommonProxy
{
public static final String NAME = "mod.mymod.ClientProxy";

@Override
public void preInit(FMLPreInitializationEvent event)
{
	super.preInit(event);
}

@Override
public void init(FMLInitializationEvent event)
{
	super.init(event);
}

@Override
public void postInit(FMLPostInitializationEvent event)
{
	super.postInit(event);
}
}

 

ItemBase class:

public class ItemBase extends Item
{
public final String name;

public ItemBase(String name)
{
	setRegistryName(name);
	this.name = name;
	setUnlocalizedName(getRegistryName().toString());
}

public ItemBase init(FMLInitializationEvent event)
{
	GameRegistry.register(this);
	if(event.getSide() != Side.SERVER)
	{
		registerModel();
	}
	return this;
}

private void registerModel()
{
	ModelLoader.setCustomModelResourceLocation(this, 0, new ModelResourceLocation(TheMod.MODID + ":" + name, "inventory"));
}

@Override
public ItemBase setCreativeTab(CreativeTabs tab)
{
	super.setCreativeTab(tab);
	return this;
}
}

 

Items class:

public final class Items
{
private Items()
{

}

public static Item firstItem;

public static void preInit(FMLPreInitializationEvent event)
{

}

public static void init(FMLInitializationEvent event)
{
	firstItem = new ItemBase("first_item").init(event);
}

public static void postInit(FMLPostInitializationEvent event)
{

}
}

 

What I understand should happen is:

TheMod 

gets a chance to handle the 3 initialization events it is subscribed to.

 

1:

TheMod 

handles the pre initialization event, where the

logger

field is initialized.

2:

TheMod

handles the initialization event by invoking the sided proxy

init

method.

3: the sided proxy's

init

invokes

Items.init

method

4:

Items.init

instantiates a new

ItemBase

4.1: The constructor of

ItemBase

registers the non-prefixed name passed to it.

4.2: The constructor of

ItemBase

stores the non-prexifed name passed to it as the instance field

name

4.3: The constructor of

ItemBase

invokes

setUnlocalized

name, passing as argument the result of

getRegistryName

(which will return the name prefixed by the mod id).

5:

Items.init

invokes

ItemBase#init

on the newly created

ItemBase

instance.

5.1:

ItemBase#init

registers

this

by invoking

GameRegistry#register

5.2:

ItemBase#init

uses its event argument's

getSide

method to decide if

registerModel

should be invoked.

5.3:

registerModel

is responsible for the item having a texture. This method's body was copy pasted from somewhere else. I'm not sure what it's being done here.

 

Where is my mistake and why? Thanks.

Posted

All of this is bad:

 

	public ItemBase init(FMLInitializationEvent event)
{
	GameRegistry.register(this);
	if(event.getSide() != Side.SERVER)
	{
		registerModel();
	}
	return this;
}

private void registerModel()
{
	ModelLoader.setCustomModelResourceLocation(this, 0, new ModelResourceLocation(TheMod.MODID + ":" + name, "inventory"));
}

 

Don't register your renderers there.  You must do it in your client proxy or you will crash the dedicated server.

Because you have a common class (
BlocClearInventory

) that contains sided code (

registerRenders

references

Minecraft.getMinecraft()

).

 

As soon as the server reads your block class from the disk, it needs to make sure that the class is valid code.  This includes loading every class referenced by your class.  This means that it will then attempt to load the

Minecraft

class, which won't exist.

 

This is not a question of "where is it called from."  This is a question of "does the code exist."  Your class contains CLIENT SIDE ONLY code.  The mere existence of that code crashes the dedicated server, even if you never call the function from anywhere ever.

 

This is why the

registerRenders

code must be moved out of the block class and into the ClientProxy.

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.

Posted

I made the following changes:

 

CommonProxy class:

public void registerItemRenderer(Item item, int meta, String name)
{

}

 

 

ClientProxy class:

        @Override
public void registerItemRenderer(Item item, int meta, String name)
{
	ModelLoader.setCustomModelResourceLocation(item, meta, new ModelResourceLocation(TheMod.MODID + ":" + name, "inventory"));
}

 

ItemBase class:

public ItemBase init()
{
	GameRegistry.register(this);
	TheMod.proxy.registerItemRenderer(this, 0, name);
	return this;
}

 

I think that covers your observation, but still no texture.

Posted
Item

s,

Block

s and every other

IForgeRegistryEntry

implementation should be registered in preInit, not init.

ModelLoader

methods only work in preInit.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

Item

s,

Block

s and every other

IForgeRegistryEntry

implementation should be registered in preInit, not init.

ModelLoader

methods only work in preInit.

 

That fixed it, thanks!

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.