Skip to content

Commit

Permalink
Deprecate FabricEntityTypeBuilder & FabricBlockEntityTypeBuilder in f…
Browse files Browse the repository at this point in the history
…avour of the vanilla classes (FabricMC#3677)

* Replace FabricEntityTypeBuilder with EntityTypeBuilder + iface injection

* Finish and test entity type builder

* Deprecate FabricBlockEntityTypeBuilder

* Review fix

* Fixes based on review

* Some fixes

* Checkstyle
  • Loading branch information
modmuss50 committed Apr 10, 2024
1 parent d73f365 commit 44e6689
Show file tree
Hide file tree
Showing 13 changed files with 674 additions and 59 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
id "idea"
id "maven-publish"
id 'jacoco'
id "fabric-loom" version "1.6.3" apply false
id "fabric-loom" version "1.6.5" apply false
id "com.diffplug.spotless" version "6.20.0"
id "org.ajoberstar.grgit" version "3.1.0"
id "me.modmuss50.remotesign" version "0.4.0" apply false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.api.object.builder.v1.block.entity;

import com.mojang.datafixers.types.Type;

import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;

/**
* General-purpose Fabric-provided extensions for {@link BlockEntityType}.
*/
public interface FabricBlockEntityType {
/**
* General-purpose Fabric-provided extensions for {@link BlockEntityType.Builder}.
*
* <p>Note: This interface is automatically implemented on {@link BlockEntityType.Builder} via Mixin and interface injection.
*/
interface Builder<T extends BlockEntity> {
/**
* Builds the {@link BlockEntityType}, see {@link BlockEntityType.Builder#build(Type)}.
*
* @return the built {@link BlockEntityType}
*/
default BlockEntityType<T> build() {
throw new AssertionError("Implemented in Mixin");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
*
* <p>Alternatively, use the access widener for {@link BlockEntityType.BlockEntityFactory}
* in Fabric Transitive Access Wideners (v1).
*
* @deprecated Use {@link BlockEntityType.Builder} directly.
*/
@Deprecated
public final class FabricBlockEntityTypeBuilder<T extends BlockEntity> {
private final Factory<? extends T> factory;
private final List<Block> blocks;
Expand All @@ -43,6 +46,10 @@ private FabricBlockEntityTypeBuilder(Factory<? extends T> factory, List<Block> b
this.blocks = blocks;
}

/**
* @deprecated Use {@link BlockEntityType.Builder#create(BlockEntityType.BlockEntityFactory, Block...)}.
*/
@Deprecated
public static <T extends BlockEntity> FabricBlockEntityTypeBuilder<T> create(Factory<? extends T> factory, Block... blocks) {
List<Block> blocksList = new ArrayList<>(blocks.length);
Collections.addAll(blocksList, blocks);
Expand All @@ -55,7 +62,9 @@ public static <T extends BlockEntity> FabricBlockEntityTypeBuilder<T> create(Fac
*
* @param block the supported block
* @return this builder
* @deprecated Use {@link BlockEntityType.Builder#create(BlockEntityType.BlockEntityFactory, Block...)}.
*/
@Deprecated
public FabricBlockEntityTypeBuilder<T> addBlock(Block block) {
this.blocks.add(block);
return this;
Expand All @@ -66,22 +75,36 @@ public FabricBlockEntityTypeBuilder<T> addBlock(Block block) {
*
* @param blocks the supported blocks
* @return this builder
* @deprecated Use {@link BlockEntityType.Builder#create(BlockEntityType.BlockEntityFactory, Block...)}.
*/
@Deprecated
public FabricBlockEntityTypeBuilder<T> addBlocks(Block... blocks) {
Collections.addAll(this.blocks, blocks);
return this;
}

/**
* @deprecated Use {@link BlockEntityType.Builder#build()}.
*/
@Deprecated
public BlockEntityType<T> build() {
return build(null);
}

/**
* @deprecated Use {@link BlockEntityType.Builder#build(Type)}.
*/
@Deprecated
public BlockEntityType<T> build(Type<?> type) {
return BlockEntityType.Builder.<T>create(factory::create, blocks.toArray(new Block[0]))
.build(type);
}

/**
* @deprecated Use {@link BlockEntityType.BlockEntityFactory}.
*/
@FunctionalInterface
@Deprecated
public interface Factory<T extends BlockEntity> {
T create(BlockPos blockPos, BlockState blockState);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* 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.api.object.builder.v1.entity;

import java.util.function.Supplier;
import java.util.function.UnaryOperator;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.SpawnGroup;
import net.minecraft.entity.SpawnRestriction;
import net.minecraft.entity.attribute.DefaultAttributeContainer;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.world.Heightmap;

import net.fabricmc.fabric.impl.object.builder.FabricEntityTypeImpl;

/**
* General-purpose Fabric-provided extensions for {@link EntityType}.
*/
public interface FabricEntityType {
/**
* General-purpose Fabric-provided extensions for {@link EntityType.Builder}.
*
* <p>Note: This interface is automatically implemented on {@link EntityType.Builder} via Mixin and interface injection.
*/
interface Builder<T extends Entity> {
/**
* Sets whether the entity's velocity should always be updated.
*
* @param alwaysUpdateVelocity whether the entity's velocity should always be updated
* @return this builder
*/
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.
*
* <p>This entity's spawn group will automatically be set to {@link SpawnGroup#MISC}.
*
* @param <T> the type of entity
* @param livingBuilder a function to configure living entity specific properties
*
* @return a new living entity type builder
*/
static <T extends LivingEntity> EntityType.Builder<T> createLiving(EntityType.EntityFactory<T> factory, SpawnGroup spawnGroup, UnaryOperator<Living<T>> livingBuilder) {
return FabricEntityTypeImpl.Builder.createLiving(factory, spawnGroup, livingBuilder);
}

/**
* Creates an entity type builder for a mob entity.
*
* @param <T> the type of entity
* @param mobBuilder a function to configure mob entity specific properties
*
* @return a new mob entity type builder
*/
static <T extends MobEntity> EntityType.Builder<T> createMob(EntityType.EntityFactory<T> factory, SpawnGroup spawnGroup, UnaryOperator<Mob<T>> mobBuilder) {
return FabricEntityTypeImpl.Builder.createMob(factory, spawnGroup, mobBuilder);
}

/**
* A builder for additional properties of a living entity, use via {@link #createLiving(EntityType.EntityFactory, SpawnGroup, UnaryOperator)}.
* @param <T> the type of living entity
*/
interface Living<T extends LivingEntity> {
/**
* Sets the default attributes for a type of living entity.
*
* @param defaultAttributeBuilder a function to generate the default attribute builder from the entity type
* @return this builder for chaining
*/
Living<T> defaultAttributes(Supplier<DefaultAttributeContainer.Builder> defaultAttributeBuilder);
}

/**
* A builder for additional properties of a mob entity, use via {@link #createMob(EntityType.EntityFactory, SpawnGroup, UnaryOperator)}.
* @param <T> the type of mob entity
*/
interface Mob<T extends MobEntity> extends Living<T> {
/**
* Registers a spawn restriction for this entity.
*
* <p>This is used by mobs to determine whether Minecraft should spawn an entity within a certain context.
*
* @return this builder for chaining.
*/
Mob<T> spawnRestriction(SpawnRestriction.Location location, Heightmap.Type heightmap, SpawnRestriction.SpawnPredicate<T> spawnPredicate);

/**
* Sets the default attributes for a type of mob entity.
*
* @param defaultAttributeBuilder a function to generate the default attribute builder from the entity type
* @return this builder for chaining
*/
@Override
Mob<T> defaultAttributes(Supplier<DefaultAttributeContainer.Builder> defaultAttributeBuilder);
}
}
}
Loading

0 comments on commit 44e6689

Please sign in to comment.