Skip to content

Commit

Permalink
Fix entity type builder
Browse files Browse the repository at this point in the history
  • Loading branch information
apple502j committed Aug 29, 2024
1 parent edb419d commit 41758c5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ default EntityType.Builder<T> 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<T> build() {
throw new AssertionError("Implemented in Mixin");
}

/**
* Creates an entity type builder for a living entity.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -306,10 +307,10 @@ public FabricEntityTypeBuilder<T> 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<T> build() {
public EntityType<T> build(RegistryKey<EntityType<?>> key) {
EntityType.Builder<T> builder = EntityType.Builder.create(this.factory, this.spawnGroup)
.allowSpawningInside(specificSpawnBlocks.toArray(Block[]::new))
.maxTrackingRange(this.trackRange)
Expand Down Expand Up @@ -340,7 +341,7 @@ public EntityType<T> build() {
builder = builder.alwaysUpdateVelocity(this.forceTrackedVelocityUpdates);
}

return builder.build(null);
return builder.build(key);
}

/**
Expand Down Expand Up @@ -476,8 +477,8 @@ public FabricEntityTypeBuilder.Living<T> defaultAttributes(Supplier<DefaultAttri

@Deprecated
@Override
public EntityType<T> build() {
final EntityType<T> type = super.build();
public EntityType<T> build(RegistryKey<EntityType<?>> key) {
final EntityType<T> type = super.build(key);

if (this.defaultAttributeBuilder != null) {
FabricDefaultAttributeRegistry.register(type, this.defaultAttributeBuilder.get());
Expand Down Expand Up @@ -617,8 +618,8 @@ public FabricEntityTypeBuilder.Mob<T> spawnRestriction(SpawnLocation spawnLocati
}

@Override
public EntityType<T> build() {
EntityType<T> type = super.build();
public EntityType<T> build(RegistryKey<EntityType<?>> key) {
EntityType<T> type = super.build(key);

if (this.spawnPredicate != null) {
SpawnRestriction.register(type, this.spawnLocation, this.restrictionHeightmap, this.spawnPredicate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@
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;

@Mixin(EntityType.Builder.class)
public abstract class EntityTypeBuilderMixin<T extends Entity> implements FabricEntityType.Builder<T>, FabricEntityTypeImpl.Builder {
@Shadow
public abstract EntityType<T> build(String id);
public abstract EntityType<T> build(RegistryKey<EntityType<?>> registryKey);

@Unique
private Boolean alwaysUpdateVelocity = null;
Expand All @@ -57,13 +58,8 @@ public EntityType.Builder<T> alwaysUpdateVelocity(boolean forceTrackedVelocityUp
return (EntityType.Builder<T>) (Object) this;
}

@Override
public EntityType<T> build() {
return build(null);
}

@Inject(method = "build", at = @At("RETURN"))
private void applyChildBuilders(String id, CallbackInfoReturnable<EntityType<T>> cir) {
private void applyChildBuilders(RegistryKey<EntityType<?>> registryKey, CallbackInfoReturnable<EntityType<T>> cir) {
if (!(cir.getReturnValue() instanceof FabricEntityTypeImpl entityType)) {
throw new IllegalStateException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,7 +54,7 @@ static void beforeAll() {
void buildEntityType() {
EntityType<Entity> type = EntityType.Builder.create(SpawnGroup.MISC)
.alwaysUpdateVelocity(true)
.build();
.build(RegistryKey.of(RegistryKeys.ENTITY_TYPE, Identifier.of("test", "test")));

assertNotNull(type);
assertTrue(type.alwaysUpdateVelocity());
Expand All @@ -61,7 +64,7 @@ void buildEntityType() {
void buildLivingEntityType() {
EntityType<LivingEntity> 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));
Expand All @@ -72,7 +75,7 @@ void buildMobEntityType() {
EntityType<MobEntity> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> ENTITY_1 = FabricEntityTypeBuilder.create().build();
static EntityType<LivingEntity> LIVING_ENTITY_1 = FabricEntityTypeBuilder.createLiving().build();
static RegistryKey<EntityType<?>> DUMMY = RegistryKey.of(RegistryKeys.ENTITY_TYPE, Identifier.of("test", "dummy"));
static EntityType<Entity> ENTITY_1 = FabricEntityTypeBuilder.create().build(DUMMY);
static EntityType<LivingEntity> LIVING_ENTITY_1 = FabricEntityTypeBuilder.createLiving().build(DUMMY);
static EntityType<TestEntity> TEST_ENTITY_1 = FabricEntityTypeBuilder.createLiving()
.entityFactory(TestEntity::new)
.spawnGroup(SpawnGroup.CREATURE)
.build();
.build(DUMMY);
static EntityType<TestEntity> OLD_TEST = FabricEntityTypeBuilder.<TestEntity>createLiving()
.entityFactory(TestEntity::new)
.spawnGroup(SpawnGroup.CREATURE)
.build();
.build(DUMMY);
static EntityType<TestMob> OLD_MOB = FabricEntityTypeBuilder.<TestMob>createMob()
.disableSaving()
.entityFactory(TestMob::new)
.build();
.build(DUMMY);
static EntityType<TestMob> MOB_TEST = FabricEntityTypeBuilder.createMob()
.disableSaving()
.entityFactory(TestMob::new)
.build();
.build(DUMMY);

private static class TestEntity extends LivingEntity {
protected TestEntity(EntityType<? extends LivingEntity> entityType, World world) {
Expand Down

0 comments on commit 41758c5

Please sign in to comment.