Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EntityType DeferredRegister #1854

Open
wants to merge 1 commit into
base: 1.21.x
Choose a base branch
from

Conversation

ChampionAsh5357
Copy link
Contributor

@ChampionAsh5357 ChampionAsh5357 commented Jan 14, 2025

Adds a DeferredRegister subclass for EntityTypes to fix the potential generic inference issues when there are multiple constructors present and the entity factory is constructed within a lambda. This also auto resolves the builder by supplying the ResourceKey in the build method.

The following will now resolve correctly when using the registerType method:

public class ExampleEntity extends Entity {

    public ExampleEntity(EntityType<? extends ExampleEntity> type, Level level) { /*...*/ }

    public ExampleEntity(/*...*/) { /*...*/ }

    // Other methods here
}

public static final DeferredRegister.EntityTypes REGISTRAR = DeferredRegister.createEntityTypes("examplemod");

// Can also be Supplier or Holder
public static final DeferredHolder<EntityType<?>, EntityType<ExampleEntity>> EXAMPLE_ENTITY =
    REGISTRAR.registerType("example_entity", ExampleEntity::new, MobCategory.MISC, builder -> builder);

Technically, var will still throw an exception as it will once again infer that the type of the entity is Entity if there is more than one constructor. This could be fixed by just having the EntityType in the constructor just take in a wildcard rather than be bounded, but that is not best practice, so I will be considering var a non-issue.

@neoforged-automation neoforged-automation bot added the 1.21.4 Targeted at Minecraft 1.21.4 label Jan 14, 2025
@neoforged-pr-publishing
Copy link

neoforged-pr-publishing bot commented Jan 14, 2025

  • Publish PR to GitHub Packages

Last commit published: d77dd19f2f82624ef4bb7db5a8cb733434f9b4a0.

PR Publishing

The artifacts published by this PR:

Repository Declaration

In order to use the artifacts published by the PR, add the following repository to your buildscript:

repositories {
    maven {
        name 'Maven for PR #1854' // https://github.com/neoforged/NeoForge/pull/1854
        url 'https://prmaven.neoforged.net/NeoForge/pr1854'
        content {
            includeModule('net.neoforged', 'neoforge')
            includeModule('net.neoforged', 'testframework')
        }
    }
}

MDK installation

In order to setup a MDK using the latest PR version, run the following commands in a terminal.
The script works on both *nix and Windows as long as you have the JDK bin folder on the path.
The script will clone the MDK in a folder named NeoForge-pr1854.
On Powershell you will need to remove the -L flag from the curl invocation.

mkdir NeoForge-pr1854
cd NeoForge-pr1854
curl -L https://prmaven.neoforged.net/NeoForge/pr1854/net/neoforged/neoforge/21.4.59-beta-pr-1854-feat-annoying-entitytype-generics/mdk-pr1854.zip -o mdk.zip
jar xf mdk.zip
rm mdk.zip || del mdk.zip

To test a production environment, you can download the installer from here.

Copy link
Contributor

@dhyces dhyces left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine. Wonder how many specialized registers we'll end up with 🤔

/**
* Specialized DeferredRegister for {@link EntityType EntityTypes}.
*/
public static class EntityTypes extends DeferredRegister<EntityType<?>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it should be called Entities to match DataComponents above.

* @return A {@link DeferredHolder} which reflects the data that will be registered.
* @param <E> the type of the entity
*/
public <E extends Entity> DeferredHolder<EntityType<?>, EntityType<E>> registerType(String name, EntityType.EntityFactory<E> factory, MobCategory category, UnaryOperator<EntityType.Builder<E>> builder) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it should be called registerEntityType to match registerComponentType above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.21.4 Targeted at Minecraft 1.21.4
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants