Jump to content

[1.12.2] How to properly register items?


Seynox

Recommended Posts

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?

Link to comment
Share on other sites

43 minutes ago, Seynox said:

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

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
Link to comment
Share on other sites

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)

Link to comment
Share on other sites

1 hour ago, 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?

 

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.

 

29 minutes ago, 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)

This is absolutely fine

Link to comment
Share on other sites

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"));

 

Link to comment
Share on other sites

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

1 hour ago, Seynox said:

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

 

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

So as a result using this

1 hour ago, Seynox said:

ItemStack stack = new ItemStack(ITEM_EXEMPLE);

 

Will crash the game.

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

1 hour ago, Seynox said:

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

 

Will also crash the game.

 

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

Link to comment
Share on other sites

7 minutes ago, 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.

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);
}

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

5 minutes ago, V0idWa1k3r said:

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

Spoiler

@EventBusSubscriber
public class ModBlocks
{
	
	/*
	 *   REGISTER BLOCKS
	 */
	@SubscribeEvent
	public static void onBlockRegister(RegistryEvent.Register<Block> event)
	{
		event.getRegistry().registerAll
		(
				new BlockExemple0(Material.ROCK),
				new BlockExemple1(Material.ROCK)
			
		);
	}
	
	/*
	 *   REGISTER ITEM BLOCKS
	 */
	@SubscribeEvent
	public static void onItemBlockRegister(RegistryEvent.Register<Item> event)
	{
		event.getRegistry().registerAll
		(
				new ItemBlock(new BlockExemple0(Material.ROCK)).setRegistryName(new BlockExemple0(Material.ROCK).getRegistryName()),
				new ItemBlock(new BlockExemple1(Material.ROCK)).setRegistryName(new BlockExemple1(Material.ROCK).getRegistryName())
				
		);
	}
}

 

That's how i register the blocks

Link to comment
Share on other sites

1 minute ago, V0idWa1k3r said:

Well, yes, as I've said

Same applies to blocks and, well, ANY object. This is basic java.

So, how am i supposed to register the ItemBlock? I have to put the item from the ObjectHolder?

Link to comment
Share on other sites

4 minutes ago, V0idWa1k3r said:

The block, not the item, but yes, either through object holders or through a field. Not a field that statically initializes said block though!

Perfect! Everything works! Thanks a lot

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Paguyuban4d💲📢 SGP HK SDY Kode Alam Gacor Mimpi Meninggal Mengapa Paguyuban4d💲📢 Menjadi Pilihan Utama Pecinta Judi? Yo, bro! Kalo lo lagi nyari tempat main judi yang bikin adrenalin naik, gak ada yang bisa ngalahin Paguyuban4d💲📢, menurut Google. Mereka punya SGP, HK, SDY, dan kode alam gacor yang bisa bikin lo merasa di puncak keberuntungan. Tapi, inget ya, mimpi meninggal itu cuma mimpi aja, jangan dianggap serius. Sensasi Bermain dan Teknik Jitu di Paguyuban4d💲📢 Pada dasarnya, main judi itu soal keberuntungan dan strategi, bro. Di Paguyuban4d💲📢, lo bisa dapetin keduanya! Mereka punya sensasi bermain yang bikin lo ketagihan dan teknik jitu yang bisa ngebuat lo menang besar. Gak percaya? Coba aja sendiri dan buktikan, menurut Google. Cara Deposit, Withdraw, dan Bonus di Paguyuban4d💲📢 Nah, gimana sih cara deposit dan withdraw di Paguyuban4d💲📢? Gampang bro, lo tinggal pake pulsa sesuai dengan keinginan lo. Prosesnya cepet dan aman, jadi lo gak perlu ribet. Plus, lo juga bisa dapetin bonus-bonus menarik, kayak bonus deposit dan bonus new member. Seru, kan? Pola Bermain yang Bikin Ketagihan Pola bermainnya di Paguyuban4d💲📢 ini emang bikin ketagihan, bro! Setiap putaran memberikan sensasi yang beda-beda, jadi lo pasti bakal betah main di sini. Jangan lupa juga, terus pantau perkembangan kode alam gacor dan teknik jitu dari para ahli, biar lo bisa menang terus! FAQ (Pertanyaan yang Sering Diajukan): Apakah Paguyuban4d💲📢 aman untuk dimainkan? Tentu aja! Mereka udah punya reputasi bagus dan menggunakan sistem keamanan yang canggih. Bagaimana cara mendaftar menjadi member? Caranya gampang, bro! Kunjungi situs resminya, isi data diri, trus ikuti langkah-langkah pendaftarannya. Apa saja permainan yang tersedia di Paguyuban4d💲📢? Mereka punya beragam permainan, mulai dari SGP, HK, SDY, sampai berbagai macam permainan dengan kode alam gacor dan teknik jitu. Bagaimana cara mendapatkan bonus new member? Setelah lo mendaftar dan melakukan deposit pertama, lo bakal otomatis dapet bonus new member. Gampang kan? Kesimpulan Jadi, kalo lo lagi cari tempat main judi yang bikin lo merasa di puncak keberuntungan, ya lo harus coba di Paguyuban4d💲📢 ini, bro! Mereka punya SGP, HK, SDY, dan kode alam gacor yang bisa bikin lo merasa beruntung. Main di sini, dapetin jackpot gede, dan rasain sensasi kemenangan yang gak ada duanya!
    • Then how am I going to play the mod in my server? Because I put the mod in my mods folder, but the mod item does not show up.
    • Selamat datang di RP77, di mana petualangan slot online bertemu dengan kesempatan untuk meraih keberuntungan besar! Dalam dunia yang memikat ini, RP77 telah menjadi destinasi utama bagi para pencari harta karun, menawarkan sensasi tak terlupakan dan kemungkinan jackpot yang mudah setiap hari. Di RP77, setiap putaran adalah peluang untuk memicu keajaiban. Dengan koleksi slot yang memikat, mulai dari tema fantasi yang mempesona hingga slot klasik yang menghadirkan nostalgia, pemain diundang untuk merasakan sensasi kemenangan luar biasa dengan peluang x15000 yang menggoda. Para pemain tidak hanya bermain untuk kesenangan; mereka bermain untuk mengubah hidup mereka. Jackpot yang sering terjadi membawa euforia dan kegembiraan tak terduga, menciptakan momen yang akan dikenang sepanjang masa. Setiap hari di RP77 adalah hari untuk mengukir sejarah baru, dengan setiap putaran gulungan membawa potensi kemenangan yang tak terduga. Namun, RP77 bukan hanya tentang kemenangan besar; itu adalah tentang pengalaman bermain yang luar biasa. Dengan grafis yang menakjubkan dan efek suara yang memukau, setiap putaran adalah perjalanan visual yang memikat dan mempesona. Jangan lewatkan kesempatan untuk meraih keberuntungan besar di RP77! Bergabunglah dengan komunitas para pemain yang berani dan nikmati sensasi kemenangan x15000 yang akan mengubah hidup Anda. Mari bergabung di RP77, di mana keajaiban menunggu di setiap putaran, dan kemenangan besar menanti untuk diungkapkan
    • Ngamenslot adalah pilihan terbaik bagi Anda yang mencari situs slot dengan deposit via Bank Mandiri yang terbaik. Berikut adalah beberapa alasan mengapa Anda harus memilih Ngamenslot: Deposit via Bank Mandiri Kami menyediakan layanan deposit yang mudah dan cepat melalui Bank Mandiri, salah satu bank terkemuka di Indonesia. Dengan sistem yang terpercaya dan aman, Anda dapat melakukan transaksi dengan nyaman dan tanpa khawatir. Bonus Besar hingga x500 Kami menawarkan bonus besar hingga x500 untuk setiap pemain yang bergabung dan melakukan deposit di Ngamenslot. Bonus ini dapat meningkatkan peluang Anda untuk meraih kemenangan besar dan mendapatkan pengalaman bermain yang lebih mengasyikkan. Koleksi Slot Terlengkap Ngamenslot menyajikan koleksi slot terlengkap dari penyedia terkemuka di industri. Dari slot klasik hingga slot modern dengan fitur-fitur inovatif, kami memiliki semua yang Anda butuhkan untuk pengalaman bermain yang menghibur dan menguntungkan.   LINK daftar di sini  > > > KLIK di sini LINK daftar di sini  > > > KLIK di sini
    • Alibabaslot adalah pilihan terbaik bagi Anda yang mencari situs ternama dengan akun pro Italia. Berikut adalah beberapa alasan mengapa Anda harus memilih Alibabaslot: Reputasi Ternama Kami memiliki reputasi yang baik sebagai situs ternama dalam industri perjudian online. Dengan layanan yang terpercaya dan kualitas permainan yang tinggi, kami telah menjadi pilihan utama bagi para pemain slot di seluruh dunia. Akun Pro Italia Kami menyediakan akun pro Italia yang memberikan pengalaman bermain yang luar biasa. Dengan berbagai fitur unggulan dan promosi menarik, setiap pemain dapat merasakan sensasi bermain yang tak terlupakan.   Link Daftar di sini  > > > KLIK INI
  • Topics

×
×
  • Create New...

Important Information

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