Jump to content

Recommended Posts

Posted

Hello!

I saw on different posts that registering items like this i not the best way to do it

@SubscribeEvent
public static void onItemRegister(RegistryEvent.Register<Item> event)
{
	event.getRegistry().registerAll(ModItems.ITEMS.toArray(new Item[0]));
}

I saw how VoidWalker was registering his items, and it seems more complicated and to be adding more code than what i currently have

What are the downsides of doing what i do and how can i optimize it?

Posted
  On 5/25/2019 at 8:06 PM, Seynox said:

it seems more complicated and to be adding more code than what i currently have

Expand  

It isn't though.

Just instantinate your items in the registry event. Your code currently instantinates them in a static initializer which is bad.

In my case I just like to keep a reference to all my items with ObjectHolder annotation, but you don't need that.

Thus if you don't keep the references and use the correct way of instantinating items in the registry event and unlike me use an actual loop/stream to register your models you are writing less code than what you have right now.

Not to say that using static intializers will cause issues so you must instantinate your items in the registry event anyway.

  • Thanks 1
Posted

Okay so now that the Items and blocks are registered, i need to find a way to register the models, so here's what i did :

@SubscribeEvent
public static void onModelRegister(ModelRegistryEvent event)
{
	
	for(Item item : Item.REGISTRY)
	{
		if(item.getRegistryName().getResourceDomain().equals(Reference.MODID))
		ModelLoader.setCustomModelResourceLocation(item, 0, new ModelResourceLocation(item.getRegistryName(), "inventory"));
	}
	
}

Is it fine if i use the Resource Domain to register the model like this? (It's working perfectly but i don't know if that's a great way to do it)

Posted
  On 5/25/2019 at 9:26 PM, Seynox said:

I just saw how Botania registered his Items and it's really close to what i already have and it's not using the ArrayList to register things, is it good?

 

Expand  

No, it still uses static initializers which is bad and you should never do this. Actually go and complain to them that they do this since it may break their mod, may break others's mods, is against forge's guidelines, prevents forge from doing things like reloadable registries and prevents other modders from overriding their items.

So yeah, if the mod is popular it doesn't mean it'w code is good.

Again, don't ever use static initializers for registry entries. Instantinate your stuff in the registry event directry.

 

  On 5/25/2019 at 10:32 PM, Seynox said:

Is it fine if i use the Resource Domain to register the model like this? (It's working perfectly but i don't know if that's a great way to do it)

Expand  

This is absolutely fine

Posted

Okay, so i registered everything like this (The string after is just to set the unlocalized name) :

	@SubscribeEvent
	public static void onItemRegister(RegistryEvent.Register<Item> event)
	{
		event.getRegistry().registerAll
		(
				
				new ItemExemple0("item_exemple0"),
				new ItemExemple1("item_exemple1"),
				new ItemExemple2("item_exemple2")
          );
    }

Everything is working, and i still have those lines

public static final Item ITEM_EXEMPLE = new ItemExemple("item_exemple");

Is it problematic if i use them? Not for the registering of course, but in general? For exemple :

ItemStack stack = new ItemStack(ITEM_EXEMPLE);

instead of

ItemStack stack = new ItemStack(new ItemExemple("item_exemple"));

 

Posted

I don't really know hpw to explain this, but basically

new ItemExemple("item_exemple") != new ItemExemple("item_exemple"). These are two separate objects.

So you are registering the items correctly, and those are the items that are in the game and are interacted with. 

Then you have these lines

  On 5/25/2019 at 11:44 PM, Seynox said:
public static final Item ITEM_EXEMPLE = new ItemExemple("item_exemple");

 

Expand  

that now define a new item that isn't registered and thus isn't actually in game.

So as a result using this

  On 5/25/2019 at 11:44 PM, Seynox said:
ItemStack stack = new ItemStack(ITEM_EXEMPLE);

 

Expand  

Will crash the game.

However since you MUST use the instance you've registered this

  On 5/25/2019 at 11:44 PM, Seynox said:
ItemStack stack = new ItemStack(new ItemExemple("item_exemple"));

 

Expand  

Will also crash the game.

 

If you need a reference to your item instances use an ObjectHolder annotation.

Posted
  On 5/26/2019 at 12:51 AM, V0idWa1k3r said:

I don't really know hpw to explain this, but basically

new ItemExemple("item_exemple") != new ItemExemple("item_exemple"). These are two separate objects.

So you are registering the items correctly, and those are the items that are in the game and are interacted with. 

Then you have these lines

that now define a new item that isn't registered and thus isn't actually in game.

So as a result using this

Will crash the game.

However since you MUST use the instance you've registered this

Will also crash the game.

 

If you need a reference to your item instances use an ObjectHolder annotation.

Expand  

So, for example, if i make an item like this, it's not gonna work?

@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand)
{
	ItemStack itemstack = player.getHeldItem(hand);
  
	world.spawnEntity(new EntityItem(world, posX, posY, posZ, new ItemStack(new ExempleItem())));
  
	return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemstack);
}

 

Posted (edited)

So everything is working except the blocks that are really weird

I have 2 custom blocks and both have the same problem : When i place the block, the block doesnt have any textures/sounds/particles, but still have the effects and properties i have set in their class, but if they are placed with a command or a structure, the block is working perfectly (When i try to pick the working block (With the middle click), nothing happen, but when i do it on the glitchy one, i get the item)

 

EDIT : Forgot to mention that they appear normal in hand and in inventory

Edited by Seynox
Posted
  On 5/26/2019 at 2:25 AM, V0idWa1k3r said:

Thins means that the ItemBlock holds a different instance of the Block compared to the one you've registered.

Expand  
  Reveal hidden contents

That's how i register the blocks

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.