diff --git a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/block/entity/FabricBlockEntityTypeBuilder.java b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/block/entity/FabricBlockEntityTypeBuilder.java index de89267ee2..0907551fb8 100644 --- a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/block/entity/FabricBlockEntityTypeBuilder.java +++ b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/block/entity/FabricBlockEntityTypeBuilder.java @@ -16,12 +16,13 @@ package net.fabricmc.fabric.api.object.builder.v1.block.entity; -import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashSet; -import java.util.List; +import java.util.Set; import com.mojang.datafixers.types.Type; +import org.jetbrains.annotations.Nullable; import net.minecraft.block.Block; import net.minecraft.block.BlockState; @@ -29,24 +30,22 @@ import net.minecraft.block.entity.BlockEntityType; import net.minecraft.util.math.BlockPos; +import net.fabricmc.fabric.impl.object.builder.ExtendedBlockEntityType; + /** - * Fabric's version of BlockEntityType.Builder with additional convenience methods. - * + * Use this builder to create a {@link BlockEntityType}. */ public final class FabricBlockEntityTypeBuilder { private final Factory factory; - private final List blocks; + private final Set blocks = new HashSet<>(); + private boolean canPotentiallyExecuteCommands = false; - private FabricBlockEntityTypeBuilder(Factory factory, List blocks) { + private FabricBlockEntityTypeBuilder(Factory factory) { this.factory = factory; - this.blocks = blocks; } public static FabricBlockEntityTypeBuilder create(Factory factory, Block... blocks) { - List blocksList = new ArrayList<>(blocks.length); - Collections.addAll(blocksList, blocks); - - return new FabricBlockEntityTypeBuilder<>(factory, blocksList); + return new FabricBlockEntityTypeBuilder(factory).addBlocks(blocks); } /** @@ -71,12 +70,39 @@ public FabricBlockEntityTypeBuilder addBlocks(Block... blocks) { return this; } + /** + * Adds supported blocks for the block entity type. + * + * @param blocks the supported blocks + * @return this builder + */ + public FabricBlockEntityTypeBuilder addBlocks(Collection blocks) { + this.blocks.addAll(blocks); + return this; + } + + /** + * Makes the built {@link BlockEntityType} return {@code true} from + * {@link BlockEntityType#canPotentiallyExecuteCommands()}. + * + * @param canPotentiallyExecuteCommands whether the block entity is able to execute commands + * @return this builder + */ + public FabricBlockEntityTypeBuilder canPotentiallyExecuteCommands(boolean canPotentiallyExecuteCommands) { + this.canPotentiallyExecuteCommands = canPotentiallyExecuteCommands; + return this; + } + public BlockEntityType build() { - return build(null); + return new ExtendedBlockEntityType<>(factory::create, new HashSet<>(blocks), canPotentiallyExecuteCommands); } - public BlockEntityType build(Type type) { - return new BlockEntityType(factory::create, new HashSet<>(blocks)); + /** + * @deprecated Use {@link #build()} instead. + */ + @Deprecated + public BlockEntityType build(@Nullable Type type) { + return build(); } @FunctionalInterface 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 index 3ec2aff1c9..b5bad39f9e 100644 --- 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 @@ -51,6 +51,16 @@ default EntityType.Builder alwaysUpdateVelocity(boolean alwaysUpdateVelocity) throw new AssertionError("Implemented in Mixin"); } + /** + * Sets whether the entity is able to execute commands. + * + * @param canPotentiallyExecuteCommands whether the entity is able to execute commands + * @return this builder + */ + default EntityType.Builder canPotentiallyExecuteCommands(boolean canPotentiallyExecuteCommands) { + throw new AssertionError("Implemented in Mixin"); + } + /** * Creates an entity type builder for a living entity. * diff --git a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/impl/object/builder/ExtendedBlockEntityType.java b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/impl/object/builder/ExtendedBlockEntityType.java new file mode 100644 index 0000000000..a6d01edb69 --- /dev/null +++ b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/impl/object/builder/ExtendedBlockEntityType.java @@ -0,0 +1,41 @@ +/* + * 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 java.util.Set; + +import net.minecraft.block.Block; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.BlockEntityType; + +public class ExtendedBlockEntityType extends BlockEntityType { + private final boolean canPotentiallyExecuteCommands; + + public ExtendedBlockEntityType(BlockEntityFactory factory, Set blocks, boolean canPotentiallyExecuteCommands) { + super(factory, blocks); + this.canPotentiallyExecuteCommands = canPotentiallyExecuteCommands; + } + + @Override + public boolean canPotentiallyExecuteCommands() { + if (canPotentiallyExecuteCommands) { + return true; + } + + return super.canPotentiallyExecuteCommands(); + } +} 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 index 3d57204b83..e2c7f067de 100644 --- 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 @@ -37,6 +37,8 @@ public interface FabricEntityTypeImpl { void fabric_setAlwaysUpdateVelocity(Boolean alwaysUpdateVelocity); + void fabric_setCanPotentiallyExecuteCommands(Boolean canPotentiallyExecuteCommands); + interface Builder { void fabric_setLivingEntityBuilder(Living livingBuilder); diff --git a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/mixin/object/builder/BlockEntityTypeMixin.java b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/mixin/object/builder/BlockEntityTypeMixin.java index 6bc9777eab..65e11ef39d 100644 --- a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/mixin/object/builder/BlockEntityTypeMixin.java +++ b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/mixin/object/builder/BlockEntityTypeMixin.java @@ -43,7 +43,9 @@ public class BlockEntityTypeMixin implements FabricBlockE @Inject(method = "", at = @At("RETURN")) private void mutableBlocks(BlockEntityType.BlockEntityFactory factory, Set blocks, CallbackInfo ci) { - this.blocks = new HashSet<>(this.blocks); + if (!(this.blocks instanceof HashSet)) { + this.blocks = new HashSet<>(this.blocks); + } } @Override 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 index 15be0c7fdf..fe8e896b56 100644 --- 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 @@ -48,6 +48,8 @@ public abstract class EntityTypeBuilderMixin implements Fabric @Unique private Boolean alwaysUpdateVelocity = null; + @Unique + private Boolean canPotentiallyExecuteCommands = null; @Unique private FabricEntityTypeImpl.Builder.Living livingBuilder = null; @@ -55,8 +57,14 @@ public abstract class EntityTypeBuilderMixin implements Fabric private FabricEntityTypeImpl.Builder.Mob mobBuilder = null; @Override - public EntityType.Builder alwaysUpdateVelocity(boolean forceTrackedVelocityUpdates) { - alwaysUpdateVelocity = forceTrackedVelocityUpdates; + public EntityType.Builder alwaysUpdateVelocity(boolean alwaysUpdateVelocity) { + this.alwaysUpdateVelocity = alwaysUpdateVelocity; + return (EntityType.Builder) (Object) this; + } + + @Override + public EntityType.Builder canPotentiallyExecuteCommands(boolean canPotentiallyExecuteCommands) { + this.canPotentiallyExecuteCommands = canPotentiallyExecuteCommands; return (EntityType.Builder) (Object) this; } @@ -67,6 +75,7 @@ private void applyChildBuilders(RegistryKey> registryKey, Callback } entityType.fabric_setAlwaysUpdateVelocity(alwaysUpdateVelocity); + entityType.fabric_setCanPotentiallyExecuteCommands(canPotentiallyExecuteCommands); if (livingBuilder != null) { livingBuilder.onBuild(castLiving(cir.getReturnValue())); 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 index 4d493d6d61..00c945b5e8 100644 --- 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 @@ -30,16 +30,30 @@ public abstract class EntityTypeMixin implements FabricEntityTypeImpl { @Unique private Boolean alwaysUpdateVelocity; + @Unique + private Boolean canPotentiallyExecuteCommands; @Inject(method = "alwaysUpdateVelocity", at = @At("HEAD"), cancellable = true) - public void alwaysUpdateVelocity(CallbackInfoReturnable cir) { + public void onAlwaysUpdateVelocity(CallbackInfoReturnable cir) { if (alwaysUpdateVelocity != null) { cir.setReturnValue(alwaysUpdateVelocity); } } + @Inject(method = "canPotentiallyExecuteCommands", at = @At("HEAD"), cancellable = true) + public void onCanPotentiallyExecuteCommands(CallbackInfoReturnable cir) { + if (canPotentiallyExecuteCommands != null) { + cir.setReturnValue(canPotentiallyExecuteCommands); + } + } + @Override public void fabric_setAlwaysUpdateVelocity(Boolean alwaysUpdateVelocity) { this.alwaysUpdateVelocity = alwaysUpdateVelocity; } + + @Override + public void fabric_setCanPotentiallyExecuteCommands(Boolean canPotentiallyExecuteCommands) { + this.canPotentiallyExecuteCommands = canPotentiallyExecuteCommands; + } }