From 25b143484c19bea2a14937264832f7cf74fc497c Mon Sep 17 00:00:00 2001 From: i509VCB Date: Mon, 26 Oct 2020 15:57:02 -0500 Subject: [PATCH] Tweak entity type builder generics to eliminate explicit type arguments (#1094) * Tweak entity type builder generics to eliminate explicit type arguments * Apply different generic on the entity factory method --- .../v1/entity/FabricEntityTypeBuilder.java | 14 ++-- .../EntityTypeBuilderGenericsTest.java | 84 +++++++++++++++++++ 2 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 fabric-object-builder-api-v1/src/testmod/java/net/fabricmc/fabric/test/object/builder/EntityTypeBuilderGenericsTest.java 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 bda6b25f1..ec29eb4e8 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 @@ -136,10 +136,10 @@ public class FabricEntityTypeBuilder { return this; } - public FabricEntityTypeBuilder entityFactory(EntityType.EntityFactory factory) { + public FabricEntityTypeBuilder entityFactory(EntityType.EntityFactory factory) { Objects.requireNonNull(factory, "Entity Factory cannot be null"); - this.factory = factory; - return this; + this.factory = (EntityType.EntityFactory) factory; + return (FabricEntityTypeBuilder) this; } /** @@ -289,9 +289,9 @@ public class FabricEntityTypeBuilder { } @Override - public FabricEntityTypeBuilder.Living entityFactory(EntityType.EntityFactory factory) { + public FabricEntityTypeBuilder.Living entityFactory(EntityType.EntityFactory factory) { super.entityFactory(factory); - return this; + return (Living) this; } @Override @@ -429,9 +429,9 @@ public class FabricEntityTypeBuilder { } @Override - public FabricEntityTypeBuilder.Mob entityFactory(EntityType.EntityFactory factory) { + public FabricEntityTypeBuilder.Mob entityFactory(EntityType.EntityFactory factory) { super.entityFactory(factory); - return this; + return (Mob) this; } @Override 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 new file mode 100644 index 000000000..121a0aae7 --- /dev/null +++ b/fabric-object-builder-api-v1/src/testmod/java/net/fabricmc/fabric/test/object/builder/EntityTypeBuilderGenericsTest.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.test.object.builder; + +import java.util.Collections; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityType; +import net.minecraft.entity.EquipmentSlot; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.SpawnGroup; +import net.minecraft.entity.mob.MobEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.util.Arm; +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 EntityType TEST_ENTITY_1 = FabricEntityTypeBuilder.createLiving() + .entityFactory(TestEntity::new) + .spawnGroup(SpawnGroup.CREATURE) + .build(); + static EntityType OLD_TEST = FabricEntityTypeBuilder.createLiving() + .entityFactory(TestEntity::new) + .spawnGroup(SpawnGroup.CREATURE) + .build(); + static EntityType OLD_MOB = FabricEntityTypeBuilder.createMob() + .disableSaving() + .entityFactory(TestMob::new) + .build(); + static EntityType MOB_TEST = FabricEntityTypeBuilder.createMob() + .disableSaving() + .entityFactory(TestMob::new) + .build(); + + private static class TestEntity extends LivingEntity { + protected TestEntity(EntityType entityType, World world) { + super(entityType, world); + } + + @Override + public Iterable getArmorItems() { + return Collections.emptyList(); + } + + @Override + public ItemStack getEquippedStack(EquipmentSlot slot) { + return ItemStack.EMPTY; + } + + @Override + public void equipStack(EquipmentSlot slot, ItemStack stack) { + } + + @Override + public Arm getMainArm() { + return Arm.RIGHT; + } + } + + private static class TestMob extends MobEntity { + protected TestMob(EntityType entityType, World world) { + super(entityType, world); + } + } +}