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
This commit is contained in:
i509VCB 2020-10-26 15:57:02 -05:00 committed by GitHub
parent 9580fa8f9a
commit 25b143484c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 7 deletions

View file

@ -136,10 +136,10 @@ public class FabricEntityTypeBuilder<T extends Entity> {
return this;
}
public FabricEntityTypeBuilder<T> entityFactory(EntityType.EntityFactory<T> factory) {
public <N extends T> FabricEntityTypeBuilder<N> entityFactory(EntityType.EntityFactory<N> factory) {
Objects.requireNonNull(factory, "Entity Factory cannot be null");
this.factory = factory;
return this;
this.factory = (EntityType.EntityFactory<T>) factory;
return (FabricEntityTypeBuilder<N>) this;
}
/**
@ -289,9 +289,9 @@ public class FabricEntityTypeBuilder<T extends Entity> {
}
@Override
public FabricEntityTypeBuilder.Living<T> entityFactory(EntityType.EntityFactory<T> factory) {
public <N extends T> FabricEntityTypeBuilder.Living<N> entityFactory(EntityType.EntityFactory<N> factory) {
super.entityFactory(factory);
return this;
return (Living<N>) this;
}
@Override
@ -429,9 +429,9 @@ public class FabricEntityTypeBuilder<T extends Entity> {
}
@Override
public FabricEntityTypeBuilder.Mob<T> entityFactory(EntityType.EntityFactory<T> factory) {
public <N extends T> FabricEntityTypeBuilder.Mob<N> entityFactory(EntityType.EntityFactory<N> factory) {
super.entityFactory(factory);
return this;
return (Mob<N>) this;
}
@Override

View file

@ -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> ENTITY_1 = FabricEntityTypeBuilder.create().build();
static EntityType<LivingEntity> LIVING_ENTITY_1 = FabricEntityTypeBuilder.createLiving().build();
static EntityType<TestEntity> TEST_ENTITY_1 = FabricEntityTypeBuilder.createLiving()
.entityFactory(TestEntity::new)
.spawnGroup(SpawnGroup.CREATURE)
.build();
static EntityType<TestEntity> OLD_TEST = FabricEntityTypeBuilder.<TestEntity>createLiving()
.entityFactory(TestEntity::new)
.spawnGroup(SpawnGroup.CREATURE)
.build();
static EntityType<TestMob> OLD_MOB = FabricEntityTypeBuilder.<TestMob>createMob()
.disableSaving()
.entityFactory(TestMob::new)
.build();
static EntityType<TestMob> MOB_TEST = FabricEntityTypeBuilder.createMob()
.disableSaving()
.entityFactory(TestMob::new)
.build();
private static class TestEntity extends LivingEntity {
protected TestEntity(EntityType<? extends LivingEntity> entityType, World world) {
super(entityType, world);
}
@Override
public Iterable<ItemStack> 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<? extends MobEntity> entityType, World world) {
super(entityType, world);
}
}
}