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

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

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.

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

 

Posted
11 minutes ago, V0idWa1k3r said:

No, this will crash the game because you are using an instance of Item that isn't in the game.

So, how can i create a ObjectHolder?

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

Posted

Well, yes, as I've said

1 hour ago, V0idWa1k3r said:

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

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

Posted
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?

Posted
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

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.