From 41758c5fb4042ad72bd885e2201bc9a71f49f02e Mon Sep 17 00:00:00 2001 From: apple502j <33279053+apple502j@users.noreply.github.com> Date: Fri, 30 Aug 2024 00:33:05 +0900 Subject: [PATCH] Fix entity type builder --- .../builder/v1/entity/FabricEntityType.java | 9 --------- .../v1/entity/FabricEntityTypeBuilder.java | 15 ++++++++------- .../object/builder/EntityTypeBuilderMixin.java | 10 +++------- .../object/builder/FabricEntityTypeTest.java | 9 ++++++--- .../builder/EntityTypeBuilderGenericsTest.java | 16 ++++++++++------ 5 files changed, 27 insertions(+), 32 deletions(-) diff --git a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/entity/FabricEntityType.java b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/entity/FabricEntityType.java index 69ed4d52b1..3ec2aff1c9 100644 --- a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/entity/FabricEntityType.java +++ b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/entity/FabricEntityType.java @@ -51,15 +51,6 @@ default EntityType.Builder alwaysUpdateVelocity(boolean alwaysUpdateVelocity) throw new AssertionError("Implemented in Mixin"); } - /** - * Build the entity type from the builder. Same as {@link EntityType.Builder#build(String)} but without an id. - * - * @return the entity type instance - */ - default EntityType build() { - throw new AssertionError("Implemented in Mixin"); - } - /** * Creates an entity type builder for a living entity. * diff --git a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/entity/FabricEntityTypeBuilder.java b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/entity/FabricEntityTypeBuilder.java index d0bcb2964d..845ff0785d 100644 --- a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/entity/FabricEntityTypeBuilder.java +++ b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/entity/FabricEntityTypeBuilder.java @@ -33,6 +33,7 @@ import net.minecraft.entity.SpawnRestriction; import net.minecraft.entity.attribute.DefaultAttributeContainer; import net.minecraft.entity.mob.MobEntity; +import net.minecraft.registry.RegistryKey; import net.minecraft.resource.featuretoggle.FeatureFlag; import net.minecraft.world.Heightmap; import net.minecraft.world.World; @@ -306,10 +307,10 @@ public FabricEntityTypeBuilder requires(FeatureFlag... requiredFeatures) { * Creates the entity type. * * @return a new {@link EntityType} - * @deprecated use {@link EntityType.Builder#build()} + * @deprecated use {@link EntityType.Builder#build(net.minecraft.registry.RegistryKey)} */ @Deprecated - public EntityType build() { + public EntityType build(RegistryKey> key) { EntityType.Builder builder = EntityType.Builder.create(this.factory, this.spawnGroup) .allowSpawningInside(specificSpawnBlocks.toArray(Block[]::new)) .maxTrackingRange(this.trackRange) @@ -340,7 +341,7 @@ public EntityType build() { builder = builder.alwaysUpdateVelocity(this.forceTrackedVelocityUpdates); } - return builder.build(null); + return builder.build(key); } /** @@ -476,8 +477,8 @@ public FabricEntityTypeBuilder.Living defaultAttributes(Supplier build() { - final EntityType type = super.build(); + public EntityType build(RegistryKey> key) { + final EntityType type = super.build(key); if (this.defaultAttributeBuilder != null) { FabricDefaultAttributeRegistry.register(type, this.defaultAttributeBuilder.get()); @@ -617,8 +618,8 @@ public FabricEntityTypeBuilder.Mob spawnRestriction(SpawnLocation spawnLocati } @Override - public EntityType build() { - EntityType type = super.build(); + public EntityType build(RegistryKey> key) { + EntityType type = super.build(key); if (this.spawnPredicate != null) { SpawnRestriction.register(type, this.spawnLocation, this.restrictionHeightmap, this.spawnPredicate); diff --git a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/mixin/object/builder/EntityTypeBuilderMixin.java b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/mixin/object/builder/EntityTypeBuilderMixin.java index 286dfdde15..9c2265449b 100644 --- a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/mixin/object/builder/EntityTypeBuilderMixin.java +++ b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/mixin/object/builder/EntityTypeBuilderMixin.java @@ -34,6 +34,7 @@ import net.minecraft.entity.EntityType; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.mob.MobEntity; +import net.minecraft.registry.RegistryKey; import net.fabricmc.fabric.api.object.builder.v1.entity.FabricEntityType; import net.fabricmc.fabric.impl.object.builder.FabricEntityTypeImpl; @@ -41,7 +42,7 @@ @Mixin(EntityType.Builder.class) public abstract class EntityTypeBuilderMixin implements FabricEntityType.Builder, FabricEntityTypeImpl.Builder { @Shadow - public abstract EntityType build(String id); + public abstract EntityType build(RegistryKey> registryKey); @Unique private Boolean alwaysUpdateVelocity = null; @@ -57,13 +58,8 @@ public EntityType.Builder alwaysUpdateVelocity(boolean forceTrackedVelocityUp return (EntityType.Builder) (Object) this; } - @Override - public EntityType build() { - return build(null); - } - @Inject(method = "build", at = @At("RETURN")) - private void applyChildBuilders(String id, CallbackInfoReturnable> cir) { + private void applyChildBuilders(RegistryKey> registryKey, CallbackInfoReturnable> cir) { if (!(cir.getReturnValue() instanceof FabricEntityTypeImpl entityType)) { throw new IllegalStateException(); } diff --git a/fabric-object-builder-api-v1/src/test/java/net/fabricmc/fabric/test/object/builder/FabricEntityTypeTest.java b/fabric-object-builder-api-v1/src/test/java/net/fabricmc/fabric/test/object/builder/FabricEntityTypeTest.java index fa8f464c7e..d82d17f745 100644 --- a/fabric-object-builder-api-v1/src/test/java/net/fabricmc/fabric/test/object/builder/FabricEntityTypeTest.java +++ b/fabric-object-builder-api-v1/src/test/java/net/fabricmc/fabric/test/object/builder/FabricEntityTypeTest.java @@ -36,6 +36,9 @@ import net.minecraft.entity.attribute.EntityAttributes; import net.minecraft.entity.mob.MobEntity; import net.minecraft.entity.passive.PigEntity; +import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.RegistryKeys; +import net.minecraft.util.Identifier; import net.minecraft.world.Heightmap; import net.fabricmc.fabric.api.object.builder.v1.entity.FabricEntityType; @@ -51,7 +54,7 @@ static void beforeAll() { void buildEntityType() { EntityType type = EntityType.Builder.create(SpawnGroup.MISC) .alwaysUpdateVelocity(true) - .build(); + .build(RegistryKey.of(RegistryKeys.ENTITY_TYPE, Identifier.of("test", "test"))); assertNotNull(type); assertTrue(type.alwaysUpdateVelocity()); @@ -61,7 +64,7 @@ void buildEntityType() { void buildLivingEntityType() { EntityType type = FabricEntityType.Builder.createLiving((t, w) -> null, SpawnGroup.MISC, living -> living .defaultAttributes(FabricEntityTypeTest::createAttributes) - ).build(); + ).build(RegistryKey.of(RegistryKeys.ENTITY_TYPE, Identifier.of("test", "test2"))); assertNotNull(type); assertNotNull(DefaultAttributeRegistry.get(type)); @@ -72,7 +75,7 @@ void buildMobEntityType() { EntityType type = FabricEntityType.Builder.createMob((t, w) -> null, SpawnGroup.MISC, mob -> mob .spawnRestriction(SpawnLocationTypes.ON_GROUND, Heightmap.Type.MOTION_BLOCKING_NO_LEAVES, PigEntity::canMobSpawn) .defaultAttributes(FabricEntityTypeTest::createAttributes) - ).build(); + ).build(RegistryKey.of(RegistryKeys.ENTITY_TYPE, Identifier.of("test", "test3"))); assertNotNull(type); assertEquals(SpawnLocationTypes.ON_GROUND, SpawnRestriction.getLocation(type)); diff --git a/fabric-object-builder-api-v1/src/testmod/java/net/fabricmc/fabric/test/object/builder/EntityTypeBuilderGenericsTest.java b/fabric-object-builder-api-v1/src/testmod/java/net/fabricmc/fabric/test/object/builder/EntityTypeBuilderGenericsTest.java index 121a0aae79..bfb35cd22d 100644 --- a/fabric-object-builder-api-v1/src/testmod/java/net/fabricmc/fabric/test/object/builder/EntityTypeBuilderGenericsTest.java +++ b/fabric-object-builder-api-v1/src/testmod/java/net/fabricmc/fabric/test/object/builder/EntityTypeBuilderGenericsTest.java @@ -25,31 +25,35 @@ import net.minecraft.entity.SpawnGroup; import net.minecraft.entity.mob.MobEntity; import net.minecraft.item.ItemStack; +import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.RegistryKeys; import net.minecraft.util.Arm; +import net.minecraft.util.Identifier; import net.minecraft.world.World; import net.fabricmc.fabric.api.object.builder.v1.entity.FabricEntityTypeBuilder; // This test is intentionally not an entrypoint to verify the generics of the entity type builder propagate properly final class EntityTypeBuilderGenericsTest { - static EntityType ENTITY_1 = FabricEntityTypeBuilder.create().build(); - static EntityType LIVING_ENTITY_1 = FabricEntityTypeBuilder.createLiving().build(); + static RegistryKey> DUMMY = RegistryKey.of(RegistryKeys.ENTITY_TYPE, Identifier.of("test", "dummy")); + static EntityType ENTITY_1 = FabricEntityTypeBuilder.create().build(DUMMY); + static EntityType LIVING_ENTITY_1 = FabricEntityTypeBuilder.createLiving().build(DUMMY); static EntityType TEST_ENTITY_1 = FabricEntityTypeBuilder.createLiving() .entityFactory(TestEntity::new) .spawnGroup(SpawnGroup.CREATURE) - .build(); + .build(DUMMY); static EntityType OLD_TEST = FabricEntityTypeBuilder.createLiving() .entityFactory(TestEntity::new) .spawnGroup(SpawnGroup.CREATURE) - .build(); + .build(DUMMY); static EntityType OLD_MOB = FabricEntityTypeBuilder.createMob() .disableSaving() .entityFactory(TestMob::new) - .build(); + .build(DUMMY); static EntityType MOB_TEST = FabricEntityTypeBuilder.createMob() .disableSaving() .entityFactory(TestMob::new) - .build(); + .build(DUMMY); private static class TestEntity extends LivingEntity { protected TestEntity(EntityType entityType, World world) {