Allow setting canPotentiallyExecuteCommands in BE/E type builders ()

* Allow setting canPotentiallyExecuteCommands in BE/E type builders

- Add FabricBlockEntityTypeBuilder#canPotentiallyExecuteCommands
- Add FabricEntityType.Builder#canPotentiallyExecuteCommands
- Add FabricBlockEntityTypeBuilder#addBlocks(Collection)
- Deprecate FabricBlockEntityTypeBuilder#build(Type)

* Add bool to canPotentiallyExecuteCommands

---------

Co-authored-by: modmuss50 <modmuss50@gmail.com>
This commit is contained in:
PepperCode1 2024-12-18 10:22:00 -08:00 committed by GitHub
parent 4fc7e41ba7
commit 1a8f4cc489
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 123 additions and 19 deletions
fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric

View file

@ -16,12 +16,13 @@
package net.fabricmc.fabric.api.object.builder.v1.block.entity; 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.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.Set;
import com.mojang.datafixers.types.Type; import com.mojang.datafixers.types.Type;
import org.jetbrains.annotations.Nullable;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
@ -29,24 +30,22 @@ import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType; import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.util.math.BlockPos; 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<T extends BlockEntity> { public final class FabricBlockEntityTypeBuilder<T extends BlockEntity> {
private final Factory<? extends T> factory; private final Factory<? extends T> factory;
private final List<Block> blocks; private final Set<Block> blocks = new HashSet<>();
private boolean canPotentiallyExecuteCommands = false;
private FabricBlockEntityTypeBuilder(Factory<? extends T> factory, List<Block> blocks) { private FabricBlockEntityTypeBuilder(Factory<? extends T> factory) {
this.factory = factory; this.factory = factory;
this.blocks = blocks;
} }
public static <T extends BlockEntity> FabricBlockEntityTypeBuilder<T> create(Factory<? extends T> factory, Block... blocks) { public static <T extends BlockEntity> FabricBlockEntityTypeBuilder<T> create(Factory<? extends T> factory, Block... blocks) {
List<Block> blocksList = new ArrayList<>(blocks.length); return new FabricBlockEntityTypeBuilder<T>(factory).addBlocks(blocks);
Collections.addAll(blocksList, blocks);
return new FabricBlockEntityTypeBuilder<>(factory, blocksList);
} }
/** /**
@ -71,12 +70,39 @@ public final class FabricBlockEntityTypeBuilder<T extends BlockEntity> {
return this; return this;
} }
public BlockEntityType<T> build() { /**
return build(null); * Adds supported blocks for the block entity type.
*
* @param blocks the supported blocks
* @return this builder
*/
public FabricBlockEntityTypeBuilder<T> addBlocks(Collection<? extends Block> blocks) {
this.blocks.addAll(blocks);
return this;
} }
public BlockEntityType<T> build(Type<?> type) { /**
return new BlockEntityType<T>(factory::create, new HashSet<>(blocks)); * 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<T> canPotentiallyExecuteCommands(boolean canPotentiallyExecuteCommands) {
this.canPotentiallyExecuteCommands = canPotentiallyExecuteCommands;
return this;
}
public BlockEntityType<T> build() {
return new ExtendedBlockEntityType<>(factory::create, new HashSet<>(blocks), canPotentiallyExecuteCommands);
}
/**
* @deprecated Use {@link #build()} instead.
*/
@Deprecated
public BlockEntityType<T> build(@Nullable Type<?> type) {
return build();
} }
@FunctionalInterface @FunctionalInterface

View file

@ -51,6 +51,16 @@ public interface FabricEntityType {
throw new AssertionError("Implemented in Mixin"); 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<T> canPotentiallyExecuteCommands(boolean canPotentiallyExecuteCommands) {
throw new AssertionError("Implemented in Mixin");
}
/** /**
* Creates an entity type builder for a living entity. * Creates an entity type builder for a living entity.
* *

View file

@ -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<T extends BlockEntity> extends BlockEntityType<T> {
private final boolean canPotentiallyExecuteCommands;
public ExtendedBlockEntityType(BlockEntityFactory<? extends T> factory, Set<Block> blocks, boolean canPotentiallyExecuteCommands) {
super(factory, blocks);
this.canPotentiallyExecuteCommands = canPotentiallyExecuteCommands;
}
@Override
public boolean canPotentiallyExecuteCommands() {
if (canPotentiallyExecuteCommands) {
return true;
}
return super.canPotentiallyExecuteCommands();
}
}

View file

@ -37,6 +37,8 @@ import net.fabricmc.fabric.api.object.builder.v1.entity.FabricEntityType;
public interface FabricEntityTypeImpl { public interface FabricEntityTypeImpl {
void fabric_setAlwaysUpdateVelocity(Boolean alwaysUpdateVelocity); void fabric_setAlwaysUpdateVelocity(Boolean alwaysUpdateVelocity);
void fabric_setCanPotentiallyExecuteCommands(Boolean canPotentiallyExecuteCommands);
interface Builder { interface Builder {
void fabric_setLivingEntityBuilder(Living<? extends LivingEntity> livingBuilder); void fabric_setLivingEntityBuilder(Living<? extends LivingEntity> livingBuilder);

View file

@ -43,7 +43,9 @@ public class BlockEntityTypeMixin<T extends BlockEntity> implements FabricBlockE
@Inject(method = "<init>", at = @At("RETURN")) @Inject(method = "<init>", at = @At("RETURN"))
private void mutableBlocks(BlockEntityType.BlockEntityFactory<? extends T> factory, Set<Block> blocks, CallbackInfo ci) { private void mutableBlocks(BlockEntityType.BlockEntityFactory<? extends T> factory, Set<Block> blocks, CallbackInfo ci) {
this.blocks = new HashSet<>(this.blocks); if (!(this.blocks instanceof HashSet)) {
this.blocks = new HashSet<>(this.blocks);
}
} }
@Override @Override

View file

@ -48,6 +48,8 @@ public abstract class EntityTypeBuilderMixin<T extends Entity> implements Fabric
@Unique @Unique
private Boolean alwaysUpdateVelocity = null; private Boolean alwaysUpdateVelocity = null;
@Unique
private Boolean canPotentiallyExecuteCommands = null;
@Unique @Unique
private FabricEntityTypeImpl.Builder.Living<? extends LivingEntity> livingBuilder = null; private FabricEntityTypeImpl.Builder.Living<? extends LivingEntity> livingBuilder = null;
@ -55,8 +57,14 @@ public abstract class EntityTypeBuilderMixin<T extends Entity> implements Fabric
private FabricEntityTypeImpl.Builder.Mob<? extends MobEntity> mobBuilder = null; private FabricEntityTypeImpl.Builder.Mob<? extends MobEntity> mobBuilder = null;
@Override @Override
public EntityType.Builder<T> alwaysUpdateVelocity(boolean forceTrackedVelocityUpdates) { public EntityType.Builder<T> alwaysUpdateVelocity(boolean alwaysUpdateVelocity) {
alwaysUpdateVelocity = forceTrackedVelocityUpdates; this.alwaysUpdateVelocity = alwaysUpdateVelocity;
return (EntityType.Builder<T>) (Object) this;
}
@Override
public EntityType.Builder<T> canPotentiallyExecuteCommands(boolean canPotentiallyExecuteCommands) {
this.canPotentiallyExecuteCommands = canPotentiallyExecuteCommands;
return (EntityType.Builder<T>) (Object) this; return (EntityType.Builder<T>) (Object) this;
} }
@ -67,6 +75,7 @@ public abstract class EntityTypeBuilderMixin<T extends Entity> implements Fabric
} }
entityType.fabric_setAlwaysUpdateVelocity(alwaysUpdateVelocity); entityType.fabric_setAlwaysUpdateVelocity(alwaysUpdateVelocity);
entityType.fabric_setCanPotentiallyExecuteCommands(canPotentiallyExecuteCommands);
if (livingBuilder != null) { if (livingBuilder != null) {
livingBuilder.onBuild(castLiving(cir.getReturnValue())); livingBuilder.onBuild(castLiving(cir.getReturnValue()));

View file

@ -30,16 +30,30 @@ import net.fabricmc.fabric.impl.object.builder.FabricEntityTypeImpl;
public abstract class EntityTypeMixin implements FabricEntityTypeImpl { public abstract class EntityTypeMixin implements FabricEntityTypeImpl {
@Unique @Unique
private Boolean alwaysUpdateVelocity; private Boolean alwaysUpdateVelocity;
@Unique
private Boolean canPotentiallyExecuteCommands;
@Inject(method = "alwaysUpdateVelocity", at = @At("HEAD"), cancellable = true) @Inject(method = "alwaysUpdateVelocity", at = @At("HEAD"), cancellable = true)
public void alwaysUpdateVelocity(CallbackInfoReturnable<Boolean> cir) { public void onAlwaysUpdateVelocity(CallbackInfoReturnable<Boolean> cir) {
if (alwaysUpdateVelocity != null) { if (alwaysUpdateVelocity != null) {
cir.setReturnValue(alwaysUpdateVelocity); cir.setReturnValue(alwaysUpdateVelocity);
} }
} }
@Inject(method = "canPotentiallyExecuteCommands", at = @At("HEAD"), cancellable = true)
public void onCanPotentiallyExecuteCommands(CallbackInfoReturnable<Boolean> cir) {
if (canPotentiallyExecuteCommands != null) {
cir.setReturnValue(canPotentiallyExecuteCommands);
}
}
@Override @Override
public void fabric_setAlwaysUpdateVelocity(Boolean alwaysUpdateVelocity) { public void fabric_setAlwaysUpdateVelocity(Boolean alwaysUpdateVelocity) {
this.alwaysUpdateVelocity = alwaysUpdateVelocity; this.alwaysUpdateVelocity = alwaysUpdateVelocity;
} }
@Override
public void fabric_setCanPotentiallyExecuteCommands(Boolean canPotentiallyExecuteCommands) {
this.canPotentiallyExecuteCommands = canPotentiallyExecuteCommands;
}
} }