Add an API to add additional supported blocks to block entity types. (#4009)

(cherry picked from commit 40875a9333)
This commit is contained in:
modmuss 2024-08-07 18:39:15 +01:00 committed by modmuss50
parent 7363c44ea9
commit a551f7a45b
9 changed files with 75 additions and 140 deletions

View file

@ -18,13 +18,25 @@ package net.fabricmc.fabric.api.object.builder.v1.block.entity;
import com.mojang.datafixers.types.Type; import com.mojang.datafixers.types.Type;
import net.minecraft.block.Block;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType; import net.minecraft.block.entity.BlockEntityType;
/** /**
* General-purpose Fabric-provided extensions for {@link BlockEntityType}. * General-purpose Fabric-provided extensions for {@link BlockEntityType}.
*
* <p>Note: This interface is automatically implemented on {@link BlockEntityType} via Mixin and interface injection.
*/ */
public interface FabricBlockEntityType { public interface FabricBlockEntityType {
/**
* Adds a block to the list of blocks that this block entity type can be used with.
*
* @param block the {@link Block} to add
*/
default void addSupportedBlock(Block block) {
throw new AssertionError("Implemented in Mixin");
}
/** /**
* General-purpose Fabric-provided extensions for {@link BlockEntityType.Builder}. * General-purpose Fabric-provided extensions for {@link BlockEntityType.Builder}.
* *

View file

@ -0,0 +1,55 @@
/*
* 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 java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import com.mojang.datafixers.types.Type;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import net.minecraft.block.Block;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityType;
@Mixin(BlockEntityType.class)
public class BlockEntityTypeMixin<T extends BlockEntity> implements FabricBlockEntityType {
@Mutable
@Shadow
@Final
private Set<Block> blocks;
@Inject(method = "<init>", at = @At("RETURN"))
private void mutableBlocks(BlockEntityType.BlockEntityFactory<? extends T> factory, Set<Block> blocks, Type<?> type, CallbackInfo ci) {
this.blocks = new HashSet<>(this.blocks);
}
@Override
public void addSupportedBlock(Block block) {
Objects.requireNonNull(block, "block");
blocks.add(block);
}
}

View file

@ -6,6 +6,7 @@
"AbstractBlockAccessor", "AbstractBlockAccessor",
"AbstractBlockSettingsAccessor", "AbstractBlockSettingsAccessor",
"BlockEntityTypeBuilderMixin", "BlockEntityTypeBuilderMixin",
"BlockEntityTypeMixin",
"DefaultAttributeRegistryAccessor", "DefaultAttributeRegistryAccessor",
"DefaultAttributeRegistryMixin", "DefaultAttributeRegistryMixin",
"DetectorRailBlockMixin", "DetectorRailBlockMixin",

View file

@ -32,6 +32,7 @@
"fabric-api:module-lifecycle": "stable", "fabric-api:module-lifecycle": "stable",
"loom:injected_interfaces": { "loom:injected_interfaces": {
"net/minecraft/class_1299\u0024class_1300": ["net/fabricmc/fabric/api/object/builder/v1/entity/FabricEntityType\u0024Builder<TT;>"], "net/minecraft/class_1299\u0024class_1300": ["net/fabricmc/fabric/api/object/builder/v1/entity/FabricEntityType\u0024Builder<TT;>"],
"net/minecraft/class_2591": ["net/fabricmc/fabric/api/object/builder/v1/block/entity/FabricBlockEntityType"],
"net/minecraft/class_2591\u0024class_2592": ["net/fabricmc/fabric/api/object/builder/v1/block/entity/FabricBlockEntityType\u0024Builder<TT;>"] "net/minecraft/class_2591\u0024class_2592": ["net/fabricmc/fabric/api/object/builder/v1/block/entity/FabricBlockEntityType\u0024Builder<TT;>"]
} }
} }

View file

@ -18,7 +18,6 @@ package net.fabricmc.fabric.test.object.builder;
import net.minecraft.block.AbstractBlock; import net.minecraft.block.AbstractBlock;
import net.minecraft.block.BlockSetType; import net.minecraft.block.BlockSetType;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks; import net.minecraft.block.Blocks;
import net.minecraft.block.HangingSignBlock; import net.minecraft.block.HangingSignBlock;
import net.minecraft.block.SignBlock; import net.minecraft.block.SignBlock;
@ -26,18 +25,14 @@ import net.minecraft.block.WallHangingSignBlock;
import net.minecraft.block.WallSignBlock; import net.minecraft.block.WallSignBlock;
import net.minecraft.block.WoodType; import net.minecraft.block.WoodType;
import net.minecraft.block.entity.BlockEntityType; import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.block.entity.HangingSignBlockEntity;
import net.minecraft.block.entity.SignBlockEntity;
import net.minecraft.item.HangingSignItem; import net.minecraft.item.HangingSignItem;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.SignItem; import net.minecraft.item.SignItem;
import net.minecraft.registry.Registries; import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry; import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.fabricmc.fabric.api.object.builder.v1.block.type.BlockSetTypeBuilder; import net.fabricmc.fabric.api.object.builder.v1.block.type.BlockSetTypeBuilder;
import net.fabricmc.fabric.api.object.builder.v1.block.type.WoodTypeBuilder; import net.fabricmc.fabric.api.object.builder.v1.block.type.WoodTypeBuilder;
@ -45,34 +40,12 @@ public class TealSignTest implements ModInitializer {
public static final Identifier TEAL_TYPE_ID = ObjectBuilderTestConstants.id("teal"); public static final Identifier TEAL_TYPE_ID = ObjectBuilderTestConstants.id("teal");
public static final BlockSetType TEAL_BLOCK_SET_TYPE = BlockSetTypeBuilder.copyOf(BlockSetType.OAK).build(TEAL_TYPE_ID); public static final BlockSetType TEAL_BLOCK_SET_TYPE = BlockSetTypeBuilder.copyOf(BlockSetType.OAK).build(TEAL_TYPE_ID);
public static final WoodType TEAL_WOOD_TYPE = WoodTypeBuilder.copyOf(WoodType.OAK).build(TEAL_TYPE_ID, TEAL_BLOCK_SET_TYPE); public static final WoodType TEAL_WOOD_TYPE = WoodTypeBuilder.copyOf(WoodType.OAK).build(TEAL_TYPE_ID, TEAL_BLOCK_SET_TYPE);
public static final SignBlock TEAL_SIGN = new SignBlock(TEAL_WOOD_TYPE, AbstractBlock.Settings.copy(Blocks.OAK_SIGN)) { public static final SignBlock TEAL_SIGN = new SignBlock(TEAL_WOOD_TYPE, AbstractBlock.Settings.copy(Blocks.OAK_SIGN));
@Override public static final WallSignBlock TEAL_WALL_SIGN = new WallSignBlock(TEAL_WOOD_TYPE, AbstractBlock.Settings.copy(Blocks.OAK_SIGN));
public TealSign createBlockEntity(BlockPos pos, BlockState state) { public static final HangingSignBlock TEAL_HANGING_SIGN = new HangingSignBlock(TEAL_WOOD_TYPE, AbstractBlock.Settings.copy(Blocks.OAK_HANGING_SIGN));
return new TealSign(pos, state); public static final WallHangingSignBlock TEAL_WALL_HANGING_SIGN = new WallHangingSignBlock(TEAL_WOOD_TYPE, AbstractBlock.Settings.copy(Blocks.OAK_HANGING_SIGN));
}
};
public static final WallSignBlock TEAL_WALL_SIGN = new WallSignBlock(TEAL_WOOD_TYPE, AbstractBlock.Settings.copy(Blocks.OAK_SIGN)) {
@Override
public TealSign createBlockEntity(BlockPos pos, BlockState state) {
return new TealSign(pos, state);
}
};
public static final HangingSignBlock TEAL_HANGING_SIGN = new HangingSignBlock(TEAL_WOOD_TYPE, AbstractBlock.Settings.copy(Blocks.OAK_HANGING_SIGN)) {
@Override
public TealHangingSign createBlockEntity(BlockPos pos, BlockState state) {
return new TealHangingSign(pos, state);
}
};
public static final WallHangingSignBlock TEAL_WALL_HANGING_SIGN = new WallHangingSignBlock(TEAL_WOOD_TYPE, AbstractBlock.Settings.copy(Blocks.OAK_HANGING_SIGN)) {
@Override
public TealHangingSign createBlockEntity(BlockPos pos, BlockState state) {
return new TealHangingSign(pos, state);
}
};
public static final SignItem TEAL_SIGN_ITEM = new SignItem(new Item.Settings(), TEAL_SIGN, TEAL_WALL_SIGN); public static final SignItem TEAL_SIGN_ITEM = new SignItem(new Item.Settings(), TEAL_SIGN, TEAL_WALL_SIGN);
public static final HangingSignItem TEAL_HANGING_SIGN_ITEM = new HangingSignItem(TEAL_HANGING_SIGN, TEAL_WALL_HANGING_SIGN, new Item.Settings()); public static final HangingSignItem TEAL_HANGING_SIGN_ITEM = new HangingSignItem(TEAL_HANGING_SIGN, TEAL_WALL_HANGING_SIGN, new Item.Settings());
public static final BlockEntityType<TealSign> TEST_SIGN_BLOCK_ENTITY = FabricBlockEntityTypeBuilder.create(TealSign::new, TEAL_SIGN, TEAL_WALL_SIGN).build();
public static final BlockEntityType<TealHangingSign> TEST_HANGING_SIGN_BLOCK_ENTITY = FabricBlockEntityTypeBuilder.create(TealHangingSign::new, TEAL_HANGING_SIGN, TEAL_WALL_HANGING_SIGN).build();
@Override @Override
public void onInitialize() { public void onInitialize() {
@ -86,29 +59,7 @@ public class TealSignTest implements ModInitializer {
Registry.register(Registries.ITEM, ObjectBuilderTestConstants.id("teal_sign"), TEAL_SIGN_ITEM); Registry.register(Registries.ITEM, ObjectBuilderTestConstants.id("teal_sign"), TEAL_SIGN_ITEM);
Registry.register(Registries.ITEM, ObjectBuilderTestConstants.id("teal_hanging_sign"), TEAL_HANGING_SIGN_ITEM); Registry.register(Registries.ITEM, ObjectBuilderTestConstants.id("teal_hanging_sign"), TEAL_HANGING_SIGN_ITEM);
Registry.register(Registries.BLOCK_ENTITY_TYPE, ObjectBuilderTestConstants.id("teal_sign"), TEST_SIGN_BLOCK_ENTITY); BlockEntityType.SIGN.addSupportedBlock(TEAL_SIGN);
Registry.register(Registries.BLOCK_ENTITY_TYPE, ObjectBuilderTestConstants.id("teal_hanging_sign"), TEST_HANGING_SIGN_BLOCK_ENTITY); BlockEntityType.HANGING_SIGN.addSupportedBlock(TEAL_HANGING_SIGN);
}
public static class TealSign extends SignBlockEntity {
public TealSign(BlockPos pos, BlockState state) {
super(pos, state);
}
@Override
public BlockEntityType<?> getType() {
return TEST_SIGN_BLOCK_ENTITY;
}
}
public static class TealHangingSign extends HangingSignBlockEntity {
public TealHangingSign(BlockPos pos, BlockState state) {
super(pos, state);
}
@Override
public BlockEntityType<?> getType() {
return TEST_HANGING_SIGN_BLOCK_ENTITY;
}
} }
} }

View file

@ -20,12 +20,6 @@
"fabric-object-builder-api-v1": "*" "fabric-object-builder-api-v1": "*"
}, },
"description": "Test mod for fabric object builder API v1.", "description": "Test mod for fabric object builder API v1.",
"mixins": [
{
"config": "fabric-object-builder-api-v1-testmod.client.mixins.json",
"environment": "client"
}
],
"entrypoints": { "entrypoints": {
"main": [ "main": [
"net.fabricmc.fabric.test.object.builder.BlockEntityTypeBuilderTest", "net.fabricmc.fabric.test.object.builder.BlockEntityTypeBuilderTest",
@ -35,9 +29,6 @@
"net.fabricmc.fabric.test.object.builder.TealSignTest", "net.fabricmc.fabric.test.object.builder.TealSignTest",
"net.fabricmc.fabric.test.object.builder.PersistentStateManagerTest" "net.fabricmc.fabric.test.object.builder.PersistentStateManagerTest"
], ],
"client": [
"net.fabricmc.fabric.test.object.builder.client.TealSignClientTest"
],
"fabric-gametest": [ "fabric-gametest": [
"net.fabricmc.fabric.test.object.builder.ObjectBuilderGameTest" "net.fabricmc.fabric.test.object.builder.ObjectBuilderGameTest"
] ]

View file

@ -1,32 +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.test.object.builder.client;
import net.minecraft.client.render.block.entity.HangingSignBlockEntityRenderer;
import net.minecraft.client.render.block.entity.SignBlockEntityRenderer;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.test.object.builder.TealSignTest;
import net.fabricmc.fabric.test.object.builder.client.mixin.BlockEntityRendererFactoriesAccessor;
public class TealSignClientTest implements ClientModInitializer {
@Override
public void onInitializeClient() {
BlockEntityRendererFactoriesAccessor.callRegister(TealSignTest.TEST_SIGN_BLOCK_ENTITY, SignBlockEntityRenderer::new);
BlockEntityRendererFactoriesAccessor.callRegister(TealSignTest.TEST_HANGING_SIGN_BLOCK_ENTITY, HangingSignBlockEntityRenderer::new);
}
}

View file

@ -1,33 +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.test.object.builder.client.mixin;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.client.render.block.entity.BlockEntityRendererFactories;
import net.minecraft.client.render.block.entity.BlockEntityRendererFactory;
@Mixin(BlockEntityRendererFactories.class)
public interface BlockEntityRendererFactoriesAccessor {
@Invoker
static <T extends BlockEntity> void callRegister(BlockEntityType<? extends T> type, BlockEntityRendererFactory<T> factory) {
throw new UnsupportedOperationException();
}
}

View file

@ -1,11 +0,0 @@
{
"required": true,
"package": "net.fabricmc.fabric.test.object.builder.client.mixin",
"compatibilityLevel": "JAVA_17",
"client": [
"BlockEntityRendererFactoriesAccessor"
],
"injectors": {
"defaultRequire": 1
}
}