Add builder bifunction variant for EntityType DR registration (#2770)

This commit is contained in:
0x189D7997
2026-10-12 02:46:00 +08:00
committed by GitHub
parent ab7294afb1
commit 6102b96cc1
2 changed files with 26 additions and 1 deletions
@@ -13,6 +13,7 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
@@ -806,11 +807,27 @@ public class DeferredRegister<T> {
* @param category The category of the entity, typically {@link MobCategory#MISC} for non-living entities, or one of the others for living entities.
* @return A {@link DeferredHolder} which reflects the data that will be registered.
* @param <E> the type of the entity
* @see #registerEntityType(String, EntityType.EntityFactory, MobCategory, BiFunction)
* @see #registerEntityType(String, EntityType.EntityFactory, MobCategory, UnaryOperator)
*/
public <E extends Entity> DeferredHolder<EntityType<?>, EntityType<E>> registerEntityType(String name, EntityType.EntityFactory<E> factory, MobCategory category) {
return this.registerEntityType(name, factory, category, UnaryOperator.identity());
}
/// Convenience method that constructs a builder for use in the operator. Use this to avoid inference issues.
///
/// @param name The name for this entity type. It will automatically have the [namespace][#getNamespace()] prefixed.
/// @param factory The factory used to typically construct the entity when using an existing helper from the type.
/// @param category The category of the entity, typically [MobCategory#MISC] for non-living entities, or one of the others for living entities.
/// @param builder The bifunction that is passed `factory` and `category` and returns an [EntityType.Builder] which is built upon registration.
/// @return A [DeferredHolder] which reflects the data that will be registered.
/// @param <E> the type of the entity
/// @see #registerEntityType(String, EntityType.EntityFactory, MobCategory)
/// @see #registerEntityType(String, EntityType.EntityFactory, MobCategory, UnaryOperator)
public <E extends Entity> DeferredHolder<EntityType<?>, EntityType<E>> registerEntityType(String name, EntityType.EntityFactory<E> factory, MobCategory category, BiFunction<EntityType.EntityFactory<E>, MobCategory, EntityType.Builder<E>> builder) {
return this.register(name, key -> builder.apply(factory, category).build(ResourceKey.create(Registries.ENTITY_TYPE, key)));
}
/**
* Convenience method that constructs a builder for use in the operator. Use this to avoid inference issues.
*
@@ -820,9 +837,11 @@ public class DeferredRegister<T> {
* @param builder The unary operator, which is passed a new builder for user operators, then builds it upon registration.
* @return A {@link DeferredHolder} which reflects the data that will be registered.
* @param <E> the type of the entity
* @see #registerEntityType(String, EntityType.EntityFactory, MobCategory)
* @see #registerEntityType(String, EntityType.EntityFactory, MobCategory, BiFunction)
*/
public <E extends Entity> DeferredHolder<EntityType<?>, EntityType<E>> registerEntityType(String name, EntityType.EntityFactory<E> factory, MobCategory category, UnaryOperator<EntityType.Builder<E>> builder) {
return this.register(name, key -> builder.apply(EntityType.Builder.of(factory, category)).build(ResourceKey.create(Registries.ENTITY_TYPE, key)));
return this.registerEntityType(name, factory, category, (fact, cat) -> builder.apply(EntityType.Builder.of(fact, cat)));
}
}
@@ -5,6 +5,7 @@
package net.neoforged.testframework.registration;
import java.util.function.BiFunction;
import java.util.function.UnaryOperator;
import net.minecraft.core.Registry;
import net.minecraft.resources.Identifier;
@@ -27,6 +28,11 @@ public class DeferredEntityTypes extends DeferredRegister.Entities {
return new DeferredEntityTypeBuilder(ResourceKey.create(registryKey, key), helper);
}
@Override
public <E extends Entity> DeferredEntityTypeBuilder<E, EntityType<E>> registerEntityType(String name, EntityType.EntityFactory<E> factory, MobCategory category, BiFunction<EntityType.EntityFactory<E>, MobCategory, EntityType.Builder<E>> builder) {
return (DeferredEntityTypeBuilder<E, EntityType<E>>) super.registerEntityType(name, factory, category, builder);
}
@Override
public <E extends Entity> DeferredEntityTypeBuilder<E, EntityType<E>> registerEntityType(String name, EntityType.EntityFactory<E> factory, MobCategory category, UnaryOperator<EntityType.Builder<E>> builder) {
return (DeferredEntityTypeBuilder<E, EntityType<E>>) super.registerEntityType(name, factory, category, builder);