From d77dd19f2f82624ef4bb7db5a8cb733434f9b4a0 Mon Sep 17 00:00:00 2001 From: ChampionAsh5357 Date: Tue, 14 Jan 2025 11:10:09 -0500 Subject: [PATCH] feat(registrar): Add entity type deferredregister --- .../neoforge/registries/DeferredRegister.java | 38 +++++++++++++++++++ .../registration/DeferredEntityTypes.java | 13 ++++--- .../entity/EntityDataSerializerTest.java | 4 +- .../neoforge/debug/entity/EntityTests.java | 12 +++--- .../debug/entity/vehicle/CustomBoatTest.java | 10 ++--- .../neoforge/debug/item/ItemTests.java | 3 +- 6 files changed, 59 insertions(+), 21 deletions(-) diff --git a/src/main/java/net/neoforged/neoforge/registries/DeferredRegister.java b/src/main/java/net/neoforged/neoforge/registries/DeferredRegister.java index 4d44f562f98..d7477b8f256 100644 --- a/src/main/java/net/neoforged/neoforge/registries/DeferredRegister.java +++ b/src/main/java/net/neoforged/neoforge/registries/DeferredRegister.java @@ -25,6 +25,9 @@ import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.TagKey; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.MobCategory; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.Item; import net.minecraft.world.level.block.Block; @@ -170,6 +173,18 @@ public static DataComponents createDataComponents(ResourceKey> registryKey; private final String namespace; private final Map, Supplier> entries = new LinkedHashMap<>(); @@ -648,6 +663,29 @@ public DeferredHolder, DataComponentType> registerCo } } + /** + * Specialized DeferredRegister for {@link EntityType EntityTypes}. + */ + public static class EntityTypes extends DeferredRegister> { + protected EntityTypes(String namespace) { + super(Registries.ENTITY_TYPE, namespace); + } + + /** + * 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 {@linkplain #getNamespace() namespace} 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 {@link MobCategory#MISC} for non-living entities, or one of the others for living entities. + * @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 the type of the entity + */ + public DeferredHolder, EntityType> registerType(String name, EntityType.EntityFactory factory, MobCategory category, UnaryOperator> builder) { + return this.register(name, key -> builder.apply(EntityType.Builder.of(factory, category)).build(ResourceKey.create(Registries.ENTITY_TYPE, key))); + } + } + private static class RegistryHolder implements Supplier> { private final ResourceKey> registryKey; private Registry registry = null; diff --git a/testframework/src/main/java/net/neoforged/testframework/registration/DeferredEntityTypes.java b/testframework/src/main/java/net/neoforged/testframework/registration/DeferredEntityTypes.java index f23c3b06ed8..49161bce89d 100644 --- a/testframework/src/main/java/net/neoforged/testframework/registration/DeferredEntityTypes.java +++ b/testframework/src/main/java/net/neoforged/testframework/registration/DeferredEntityTypes.java @@ -5,20 +5,20 @@ package net.neoforged.testframework.registration; -import java.util.function.Supplier; +import java.util.function.UnaryOperator; import net.minecraft.core.Registry; -import net.minecraft.core.registries.Registries; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.MobCategory; import net.neoforged.neoforge.registries.DeferredRegister; -public class DeferredEntityTypes extends DeferredRegister> { +public class DeferredEntityTypes extends DeferredRegister.EntityTypes { private final RegistrationHelper helper; public DeferredEntityTypes(String namespace, RegistrationHelper helper) { - super(Registries.ENTITY_TYPE, namespace); + super(namespace); this.helper = helper; } @@ -27,7 +27,8 @@ protected > DeferredEntityTypeBuilder createHolder(Resou return new DeferredEntityTypeBuilder(ResourceKey.create(registryKey, key), helper); } - public DeferredEntityTypeBuilder> registerType(String name, Supplier> sup) { - return (DeferredEntityTypeBuilder>) super.register(name, rl -> sup.get().build(ResourceKey.create(getRegistryKey(), rl))); + @Override + public DeferredEntityTypeBuilder> registerType(String name, EntityType.EntityFactory factory, MobCategory category, UnaryOperator> builder) { + return (DeferredEntityTypeBuilder>) super.registerType(name, factory, category, builder); } } diff --git a/tests/src/main/java/net/neoforged/neoforge/debug/entity/EntityDataSerializerTest.java b/tests/src/main/java/net/neoforged/neoforge/debug/entity/EntityDataSerializerTest.java index aec385e238b..b8a5d8c4243 100644 --- a/tests/src/main/java/net/neoforged/neoforge/debug/entity/EntityDataSerializerTest.java +++ b/tests/src/main/java/net/neoforged/neoforge/debug/entity/EntityDataSerializerTest.java @@ -54,8 +54,8 @@ static void register(final TestFramework framework) { @EmptyTemplate(floor = true) @TestHolder(description = "Tests if custom EntityDataSerializers are properly handled") static void customEntityDataSerializer(final DynamicTest test, final RegistrationHelper reg) { - var testEntity = reg.entityTypes().registerType("serializer_test_entity", () -> EntityType.Builder.of(TestEntity::new, MobCategory.CREATURE) - .sized(1, 1)).withRenderer(() -> TestEntityRenderer::new); + var testEntity = reg.entityTypes().registerType("serializer_test_entity", TestEntity::new, MobCategory.CREATURE, builder -> builder.sized(1, 1)) + .withRenderer(() -> TestEntityRenderer::new); test.onGameTest(helper -> { var entity = helper.spawn(testEntity.get(), 1, 2, 1); diff --git a/tests/src/main/java/net/neoforged/neoforge/debug/entity/EntityTests.java b/tests/src/main/java/net/neoforged/neoforge/debug/entity/EntityTests.java index 73cc7cac7fc..6b6f07f2737 100644 --- a/tests/src/main/java/net/neoforged/neoforge/debug/entity/EntityTests.java +++ b/tests/src/main/java/net/neoforged/neoforge/debug/entity/EntityTests.java @@ -41,12 +41,12 @@ public class EntityTests { @EmptyTemplate @TestHolder(description = "Tests if custom fence gates without wood types work, allowing for the use of the vanilla block for non-wooden gates") static void customSpawnLogic(final DynamicTest test, final RegistrationHelper reg) { - final var usingForgeAdvancedSpawn = reg.entityTypes().registerType("complex_spawn", () -> EntityType.Builder.of(CustomComplexSpawnEntity::new, MobCategory.AMBIENT) - .sized(1, 1)).withLang("Custom complex spawn egg").withRenderer(() -> NoopRenderer::new); - final var usingCustomPayloadsSpawn = reg.entityTypes().registerType("adapted_spawn", () -> EntityType.Builder.of(AdaptedSpawnEntity::new, MobCategory.AMBIENT) - .sized(1, 1)).withLang("Adapted complex spawn egg").withRenderer(() -> NoopRenderer::new); - final var simpleSpawn = reg.entityTypes().registerType("simple_spawn", () -> EntityType.Builder.of(SimpleEntity::new, MobCategory.AMBIENT) - .sized(1, 1)).withLang("Simple spawn egg").withRenderer(() -> NoopRenderer::new); + final var usingForgeAdvancedSpawn = reg.entityTypes().registerType("complex_spawn", CustomComplexSpawnEntity::new, MobCategory.AMBIENT, builder -> builder.sized(1, 1)) + .withLang("Custom complex spawn egg").withRenderer(() -> NoopRenderer::new); + final var usingCustomPayloadsSpawn = reg.entityTypes().registerType("adapted_spawn", AdaptedSpawnEntity::new, MobCategory.AMBIENT, builder -> builder.sized(1, 1)) + .withLang("Adapted complex spawn egg").withRenderer(() -> NoopRenderer::new); + final var simpleSpawn = reg.entityTypes().registerType("simple_spawn", SimpleEntity::new, MobCategory.AMBIENT, builder -> builder.sized(1, 1)) + .withLang("Simple spawn egg").withRenderer(() -> NoopRenderer::new); reg.eventListeners().accept((Consumer) event -> event.registrar("1") .playToClient(EntityTests.CustomSyncPayload.TYPE, CustomSyncPayload.STREAM_CODEC, (payload, context) -> {})); diff --git a/tests/src/main/java/net/neoforged/neoforge/debug/entity/vehicle/CustomBoatTest.java b/tests/src/main/java/net/neoforged/neoforge/debug/entity/vehicle/CustomBoatTest.java index 4ee4b21656a..abf34b7d47a 100644 --- a/tests/src/main/java/net/neoforged/neoforge/debug/entity/vehicle/CustomBoatTest.java +++ b/tests/src/main/java/net/neoforged/neoforge/debug/entity/vehicle/CustomBoatTest.java @@ -6,6 +6,7 @@ package net.neoforged.neoforge.debug.entity.vehicle; import java.util.function.Supplier; +import java.util.function.UnaryOperator; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.MobCategory; import net.minecraft.world.entity.vehicle.Boat; @@ -13,7 +14,6 @@ import net.minecraft.world.entity.vehicle.ChestRaft; import net.minecraft.world.entity.vehicle.Raft; import net.minecraft.world.item.BoatItem; -import net.minecraft.world.item.Item; import net.neoforged.testframework.DynamicTest; import net.neoforged.testframework.Test; import net.neoforged.testframework.annotation.ForEachTest; @@ -32,10 +32,10 @@ public class CustomBoatTest { @EmptyTemplate @TestHolder(description = "Tests that custom boat types work") static void customBoatType(final DynamicTest test, final RegistrationHelper reg) { - Supplier> paperBoat = reg.entityTypes().registerType("paper_boat", () -> EntityType.Builder.of((type, level) -> new Boat(type, level, (Supplier) () -> paperBoatItem.get()), MobCategory.MISC)); - Supplier> paperChestBoat = reg.entityTypes().registerType("paper_chest_boat", () -> EntityType.Builder.of((type, level) -> new ChestBoat(type, level, (Supplier) () -> paperChestBoatItem.get()), MobCategory.MISC)); - Supplier> paperRaft = reg.entityTypes().registerType("paper_raft", () -> EntityType.Builder.of((type, level) -> new Raft(type, level, (Supplier) () -> paperRaftItem.get()), MobCategory.MISC)); - Supplier> paperChestRaft = reg.entityTypes().registerType("paper_chest_raft", () -> EntityType.Builder.of((type, level) -> new ChestRaft(type, level, (Supplier) () -> paperChestRaftItem.get()), MobCategory.MISC)); + Supplier> paperBoat = reg.entityTypes().registerType("paper_boat", (type, level) -> new Boat(type, level, () -> paperBoatItem.get()), MobCategory.MISC, UnaryOperator.identity()); + Supplier> paperChestBoat = reg.entityTypes().registerType("paper_chest_boat", (type, level) -> new ChestBoat(type, level, () -> paperChestBoatItem.get()), MobCategory.MISC, UnaryOperator.identity()); + Supplier> paperRaft = reg.entityTypes().registerType("paper_raft", (type, level) -> new Raft(type, level, () -> paperRaftItem.get()), MobCategory.MISC, UnaryOperator.identity()); + Supplier> paperChestRaft = reg.entityTypes().registerType("paper_chest_raft", (type, level) -> new ChestRaft(type, level, () -> paperChestRaftItem.get()), MobCategory.MISC, UnaryOperator.identity()); paperBoatItem = reg.items().registerItem("paper_boat", props -> new BoatItem(paperBoat.get(), props)) .withLang("Paper Boat"); diff --git a/tests/src/main/java/net/neoforged/neoforge/debug/item/ItemTests.java b/tests/src/main/java/net/neoforged/neoforge/debug/item/ItemTests.java index 94028ab5ebc..025edf997c6 100644 --- a/tests/src/main/java/net/neoforged/neoforge/debug/item/ItemTests.java +++ b/tests/src/main/java/net/neoforged/neoforge/debug/item/ItemTests.java @@ -120,8 +120,7 @@ public ItemStack execute(BlockSource p_302435_, ItemStack p_123562_) { "Tests if the forge spawn egg works" }) static void forgeSpawnEggTest(final DynamicTest test, final RegistrationHelper reg) { - final var testEntity = reg.entityTypes().registerType("test_entity", () -> EntityType.Builder.of(Pig::new, MobCategory.CREATURE) - .sized(1, 1)) + final var testEntity = reg.entityTypes().registerType("test_entity", Pig::new, MobCategory.CREATURE, builder -> builder.sized(1, 1)) .withAttributes(Pig::createAttributes) .withRenderer(() -> PigRenderer::new) .withLang("Test Pig spawn egg");