Skip to content

Commit

Permalink
Replace FabricEntityTypeBuilder with EntityTypeBuilder + iface injection
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Mar 28, 2024
1 parent f7c07d0 commit e9cc9cd
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -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<T extends Entity> {
EntityType.Builder<T> alwaysUpdateVelocity(boolean alwaysUpdateVelocity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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-&gt;client entity tracking values.
*
* @param <T> Entity class.
*/
@Deprecated
public class FabricEntityTypeBuilder<T extends Entity> {
private SpawnGroup spawnGroup;
private EntityType.EntityFactory<T> factory;
Expand All @@ -58,7 +55,8 @@ public class FabricEntityTypeBuilder<T extends Entity> {
private EntityDimensions dimensions = EntityDimensions.changing(-1.0f, -1.0f);
private ImmutableSet<Block> specificSpawnBlocks = ImmutableSet.of();

private FeatureSet requiredFeatures = FeatureFlags.VANILLA_FEATURES;
@Nullable
private FeatureFlag[] requiredFeatures = null;

protected FabricEntityTypeBuilder(SpawnGroup spawnGroup, EntityType.EntityFactory<T> factory) {
this.spawnGroup = spawnGroup;
Expand Down Expand Up @@ -262,7 +260,7 @@ public FabricEntityTypeBuilder<T> specificSpawnBlocks(Block... blocks) {
* @return this builder for chaining
*/
public FabricEntityTypeBuilder<T> requires(FeatureFlag... requiredFeatures) {
this.requiredFeatures = FeatureFlags.FEATURE_MANAGER.featureSetOf(requiredFeatures);
this.requiredFeatures = requiredFeatures;
return this;
}

Expand All @@ -272,9 +270,38 @@ public FabricEntityTypeBuilder<T> requires(FeatureFlag... requiredFeatures) {
* @return a new {@link EntityType}
*/
public EntityType<T> build() {
// Modded DFU is a dream, currently not possible without screwing it up.
EntityType.Builder<T> 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);
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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<T extends Entity> implements FabricEntityType.Builder<T> {
@Unique
private Boolean alwaysUpdateVelocity = null;

@Override
public EntityType.Builder<T> alwaysUpdateVelocity(boolean forceTrackedVelocityUpdates) {
alwaysUpdateVelocity = forceTrackedVelocityUpdates;
return (EntityType.Builder<T>) (Object) this;
}

@Inject(method = "build", at = @At("RETURN"))
public void build(String id, CallbackInfoReturnable<EntityType<T>> 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<Type<?>> original) {
if (id == null) {
return null;
}

return original.call(typeReference, id);
}
}
Original file line number Diff line number Diff line change
@@ -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<Boolean> cir) {
if (alwaysUpdateVelocity != null) {
cir.setReturnValue(alwaysUpdateVelocity);
}
}

@Override
public void fabric_setAlwaysUpdateVelocity(Boolean alwaysUpdateVelocity) {
this.alwaysUpdateVelocity = alwaysUpdateVelocity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"DefaultAttributeRegistryAccessor",
"DefaultAttributeRegistryMixin",
"DetectorRailBlockMixin",
"EntityTypeBuilderMixin",
"EntityTypeMixin",
"PersistentStateManagerMixin",
"TradeOffersTypeAwareBuyForOneEmeraldFactoryMixin"
],
Expand Down

0 comments on commit e9cc9cd

Please sign in to comment.