Jump to content

Recommended Posts

Posted (edited)

So, I'm switching my mod to use deferred registries. I've got this method:

public static RegistryObject<Block> registerBlock(Block block) {
	return BLOCKS.register(block.getRegistryName().getPath(), () -> block);
}

(BLOCKS is my deferred registry for blocks)

This allows me to do this in another class:

public static final RegistryObject<Block> CAUTION_BLOCK = ModRegistry.registerBlock(new CautionBlock());

Which I personally think is a nice, clean way to do it.

However, when it comes to things with "type"s, e.g. entities or tile entities, I have this:

public static RegistryObject<TileEntityType<?>> registerTile(TileEntity te, ResourceLocation regName, Block... validBlocks) {
	TileEntityType<?> tetype = TileEntityType.Builder.create(() -> te.getTileEntity(), validBlocks).build(null).setRegistryName(regName);
	return TILE_TYPES.register(tetype.getRegistryName().getPath(), () -> tetype);
}

And that works! However for entities I have this:

public static RegistryObject<EntityType<?>> registerEntity(Entity entity, ResourceLocation regName, EntityClassification classification, float width, float height) {
	EntityType<?> entityType = EntityType.Builder.create((a, b) -> entity, classification).size(width, height).build(regName.toString()).setRegistryName(regName);
	return ENTITY_TYPES.register(entityType.getRegistryName().getPath(), () -> entityType);
}

And I call it like this:

public static final RegistryObject<EntityType<?>> ROBOT = ModRegistry.registerEntity(new RobotEntity(null, null), new ResourceLocation(LabKit.MODID, "robot"), EntityClassification.CREATURE, 0.6f, 0.9f);

But that produces an odd error in the console: 

java.lang.IllegalStateException: Attempted to set registry name with existing registry name! New: labkit:robot Old: labkit:robot

What am I doing wrong?

Edited by BlockyPenguin

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Posted
32 minutes ago, diesieben07 said:

This completely defeats the point of DeferredRegister. Do not do this! You must create the registry object inside the supplier (like is shown in the Javadoc for DeferredRegister).

 

This makes no sense! Your EntityType must create a new entity instance every time. You can't just create it once at startup... The same goes for the TileEntityType. That is the whole reason you register a TileEntityType and an EntityType instead of just TileEntity and Entity. There are many instances for your TE and Entity class - hence you cannot use them as the registry type.

Oh ok... how exactly does it defeat the point of DeferredRegister though? I thought the point was it stops you from doing things at the wrong time.

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Posted
1 hour ago, diesieben07 said:

Precisely that is the point. And if you do things outside of the supplier... it can no longer ensure that those things happen at the correct time (i.e. inside the registry event).

By creating your Block instance outside, you are now back to creating your block instance in a static initializer, which is exactly the thing DeferredRegister prevents you from doing.

Ah, ok, I understand :) thank you!

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

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.