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 new file mode 100644 index 0000000000..bc13d5e37c --- /dev/null +++ b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/entity/FabricEntityType.java @@ -0,0 +1,10 @@ +package net.fabricmc.fabric.api.object.builder.v1.entity; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityType; + +public interface FabricEntityType { + interface Builder { + EntityType.Builder alwaysUpdateVelocity(boolean alwaysUpdateVelocity); + } +} 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 843e5bbbfb..41a9c958e6 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 @@ -32,19 +32,16 @@ import net.minecraft.entity.attribute.DefaultAttributeContainer; import net.minecraft.entity.mob.MobEntity; import net.minecraft.resource.featuretoggle.FeatureFlag; -import net.minecraft.resource.featuretoggle.FeatureFlags; -import net.minecraft.resource.featuretoggle.FeatureSet; import net.minecraft.world.Heightmap; import net.minecraft.world.World; -import net.fabricmc.fabric.impl.object.builder.FabricEntityType; - /** * Extended version of {@link EntityType.Builder} with added registration for * server->client entity tracking values. * * @param Entity class. */ +@Deprecated public class FabricEntityTypeBuilder { private SpawnGroup spawnGroup; private EntityType.EntityFactory factory; @@ -58,7 +55,8 @@ public class FabricEntityTypeBuilder { private EntityDimensions dimensions = EntityDimensions.changing(-1.0f, -1.0f); private ImmutableSet specificSpawnBlocks = ImmutableSet.of(); - private FeatureSet requiredFeatures = FeatureFlags.VANILLA_FEATURES; + @Nullable + private FeatureFlag[] requiredFeatures = null; protected FabricEntityTypeBuilder(SpawnGroup spawnGroup, EntityType.EntityFactory factory) { this.spawnGroup = spawnGroup; @@ -262,7 +260,7 @@ public FabricEntityTypeBuilder specificSpawnBlocks(Block... blocks) { * @return this builder for chaining */ public FabricEntityTypeBuilder requires(FeatureFlag... requiredFeatures) { - this.requiredFeatures = FeatureFlags.FEATURE_MANAGER.featureSetOf(requiredFeatures); + this.requiredFeatures = requiredFeatures; return this; } @@ -272,9 +270,38 @@ public FabricEntityTypeBuilder requires(FeatureFlag... requiredFeatures) { * @return a new {@link EntityType} */ public EntityType build() { - // Modded DFU is a dream, currently not possible without screwing it up. + EntityType.Builder builder = EntityType.Builder.create(this.factory, this.spawnGroup) + .allowSpawningInside(specificSpawnBlocks.toArray(Block[]::new)) + .maxTrackingRange(this.trackRange) + .trackingTickInterval(this.trackedUpdateRate) + .setDimensions(this.dimensions.width, this.dimensions.height); + + if (!this.saveable) { + builder = builder.disableSaving(); + } + + if (!this.summonable) { + builder = builder.disableSummon(); + } + + if (this.fireImmune) { + builder = builder.makeFireImmune(); + } + + if (this.spawnableFarFromPlayer) { + builder = builder.spawnableFarFromPlayer(); + } + + if (this.requiredFeatures != null) { + builder = builder.requires(this.requiredFeatures); + } + + if (this.forceTrackedVelocityUpdates != null) { + // TODO iface injection with loom 1.6 + //builder = builder.alwaysUpdateVelocity(this.forceTrackedVelocityUpdates); + } - return new FabricEntityType<>(this.factory, this.spawnGroup, this.saveable, this.summonable, this.fireImmune, this.spawnableFarFromPlayer, this.specificSpawnBlocks, dimensions, trackRange, trackedUpdateRate, forceTrackedVelocityUpdates, this.requiredFeatures); + return builder.build(null); } /** diff --git a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/impl/object/builder/FabricEntityType.java b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/impl/object/builder/FabricEntityType.java deleted file mode 100644 index c76c8e721d..0000000000 --- a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/impl/object/builder/FabricEntityType.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.impl.object.builder; - -import com.google.common.collect.ImmutableSet; - -import net.minecraft.block.Block; -import net.minecraft.entity.Entity; -import net.minecraft.entity.EntityDimensions; -import net.minecraft.entity.EntityType; -import net.minecraft.entity.SpawnGroup; -import net.minecraft.resource.featuretoggle.FeatureSet; - -public class FabricEntityType extends EntityType { - private final Boolean alwaysUpdateVelocity; - - public FabricEntityType(EntityType.EntityFactory factory, SpawnGroup spawnGroup, boolean bl, boolean summonable, boolean fireImmune, boolean spawnableFarFromPlayer, ImmutableSet spawnBlocks, EntityDimensions entityDimensions, int maxTrackDistance, int trackTickInterval, Boolean alwaysUpdateVelocity, FeatureSet featureSet) { - super(factory, spawnGroup, bl, summonable, fireImmune, spawnableFarFromPlayer, spawnBlocks, entityDimensions, maxTrackDistance, trackTickInterval, featureSet); - this.alwaysUpdateVelocity = alwaysUpdateVelocity; - } - - @Override - public boolean alwaysUpdateVelocity() { - if (alwaysUpdateVelocity != null) { - return alwaysUpdateVelocity; - } - - return super.alwaysUpdateVelocity(); - } -} diff --git a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/impl/object/builder/FabricEntityTypeImpl.java b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/impl/object/builder/FabricEntityTypeImpl.java new file mode 100644 index 0000000000..316abc9486 --- /dev/null +++ b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/impl/object/builder/FabricEntityTypeImpl.java @@ -0,0 +1,21 @@ +/* + * 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.impl.object.builder; + +public interface FabricEntityTypeImpl { + void fabric_setAlwaysUpdateVelocity(Boolean alwaysUpdateVelocity); +} 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 new file mode 100644 index 0000000000..a9a104bcfb --- /dev/null +++ b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/mixin/object/builder/EntityTypeBuilderMixin.java @@ -0,0 +1,64 @@ +/* + * 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.mixin.object.builder; + +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; +import com.mojang.datafixers.DSL; +import com.mojang.datafixers.types.Type; +import org.jetbrains.annotations.Nullable; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityType; + +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 implements FabricEntityType.Builder { + @Unique + private Boolean alwaysUpdateVelocity = null; + + @Override + public EntityType.Builder alwaysUpdateVelocity(boolean forceTrackedVelocityUpdates) { + alwaysUpdateVelocity = forceTrackedVelocityUpdates; + return (EntityType.Builder) (Object) this; + } + + @Inject(method = "build", at = @At("RETURN")) + public void build(String id, CallbackInfoReturnable> cir) { + if (!(cir.getReturnValue() instanceof FabricEntityTypeImpl entityType)) { + throw new IllegalStateException(); + } + + entityType.fabric_setAlwaysUpdateVelocity(alwaysUpdateVelocity); + } + + @WrapOperation(method = "build", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/Util;getChoiceType(Lcom/mojang/datafixers/DSL$TypeReference;Ljava/lang/String;)Lcom/mojang/datafixers/types/Type;")) + private @Nullable Type allowNullId(DSL.TypeReference typeReference, String id, Operation> original) { + if (id == null) { + return null; + } + + return original.call(typeReference, id); + } +} diff --git a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/mixin/object/builder/EntityTypeMixin.java b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/mixin/object/builder/EntityTypeMixin.java new file mode 100644 index 0000000000..4d493d6d61 --- /dev/null +++ b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/mixin/object/builder/EntityTypeMixin.java @@ -0,0 +1,45 @@ +/* + * 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.mixin.object.builder; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import net.minecraft.entity.EntityType; + +import net.fabricmc.fabric.impl.object.builder.FabricEntityTypeImpl; + +@Mixin(EntityType.class) +public abstract class EntityTypeMixin implements FabricEntityTypeImpl { + @Unique + private Boolean alwaysUpdateVelocity; + + @Inject(method = "alwaysUpdateVelocity", at = @At("HEAD"), cancellable = true) + public void alwaysUpdateVelocity(CallbackInfoReturnable cir) { + if (alwaysUpdateVelocity != null) { + cir.setReturnValue(alwaysUpdateVelocity); + } + } + + @Override + public void fabric_setAlwaysUpdateVelocity(Boolean alwaysUpdateVelocity) { + this.alwaysUpdateVelocity = alwaysUpdateVelocity; + } +} diff --git a/fabric-object-builder-api-v1/src/main/resources/fabric-object-builder-v1.mixins.json b/fabric-object-builder-api-v1/src/main/resources/fabric-object-builder-v1.mixins.json index a5c4f877c0..0896cce89f 100644 --- a/fabric-object-builder-api-v1/src/main/resources/fabric-object-builder-v1.mixins.json +++ b/fabric-object-builder-api-v1/src/main/resources/fabric-object-builder-v1.mixins.json @@ -8,6 +8,8 @@ "DefaultAttributeRegistryAccessor", "DefaultAttributeRegistryMixin", "DetectorRailBlockMixin", + "EntityTypeBuilderMixin", + "EntityTypeMixin", "PersistentStateManagerMixin", "TradeOffersTypeAwareBuyForOneEmeraldFactoryMixin" ],