MutableBlockEntityType

This commit is contained in:
CreepyCre 2021-04-14 20:33:29 +02:00
parent fb51ad6536
commit b1e140dfca
6 changed files with 69 additions and 13 deletions

View file

@ -0,0 +1,57 @@
package org.dimdev.dimdoors.api.block.entity;
import com.mojang.datafixers.types.Type;
import net.fabricmc.fabric.mixin.lookup.BlockEntityTypeAccessor;
import net.minecraft.block.Block;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class MutableBlockEntityType<T extends BlockEntity> extends BlockEntityType<T> {
public MutableBlockEntityType(BlockEntityFactory<? extends T> factory, Set<Block> blocks, Type<?> type) {
// ensure that the Set is mutable
super(factory, new HashSet<>(blocks), type);
}
public boolean addBlock(Block block) {
return ((BlockEntityTypeAccessor) this).getBlocks().add(block);
}
public boolean removeBlock(Block block) {
return ((BlockEntityTypeAccessor) this).getBlocks().remove(block);
}
public static final class Builder<T extends BlockEntity> {
private final BlockEntityFactory<? extends T> factory;
private final Set<Block> blocks;
private Builder(BlockEntityFactory<? extends T> factory, Set<Block> blocks) {
this.factory = factory;
this.blocks = blocks;
}
public static <T extends BlockEntity> Builder<T> create(BlockEntityFactory<? extends T> factory, Block... blocks) {
// ensure mutability
return new Builder<>(factory, new HashSet<>(Arrays.asList(blocks)));
}
public MutableBlockEntityType<T> build() {
return build(null);
}
public MutableBlockEntityType<T> build(Type<?> type) {
return new MutableBlockEntityType<>(this.factory, this.blocks, type);
}
}
// exists for convenience so that no access widener for BlockEntityType.BlockEntityFactory is necessary
@FunctionalInterface
public interface BlockEntityFactory<T extends BlockEntity> extends BlockEntityType.BlockEntityFactory<T> {
}
}

View file

@ -60,7 +60,7 @@ public class DimensionalDoorBlockRegistrar {
private void register(Identifier identifier, Block block, BiFunction<AbstractBlock.Settings, Block, ? extends Block> constructor) {
Identifier gennedId = new Identifier("dimdoors", PREFIX + identifier.getPath());
Block newBlock = Registry.register(registry, gennedId, constructor.apply(FabricBlockSettings.copy(block), block));
ModBlockEntityTypes.ENTRANCE_RIFT_BLOCKS.add(newBlock);
ModBlockEntityTypes.ENTRANCE_RIFT.addBlock(newBlock);
mappedDoorBlocks.put(gennedId, identifier);
itemRegistrar.notifyBlockMapped(block, newBlock);

View file

@ -5,7 +5,6 @@ import java.util.Optional;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import org.dimdev.dimdoors.block.CoordinateTransformerBlock;
import org.dimdev.dimdoors.block.RiftProvider;
import org.dimdev.dimdoors.block.door.data.DoorDataReader;
import org.dimdev.dimdoors.api.client.DefaultTransformation;
import org.dimdev.dimdoors.api.client.Transformer;
import org.dimdev.dimdoors.item.RiftKeyItem;

View file

@ -1,6 +1,7 @@
package org.dimdev.dimdoors.block.entity;
import org.apache.commons.lang3.ArrayUtils;
import org.dimdev.dimdoors.api.block.entity.MutableBlockEntityType;
import org.dimdev.dimdoors.block.ModBlocks;
import org.dimdev.dimdoors.block.door.data.DoorData;
@ -16,23 +17,25 @@ import java.util.HashSet;
import java.util.Set;
public class ModBlockEntityTypes {
// needed for the autogen doors
public static final Set<Block> ENTRANCE_RIFT_BLOCKS = new HashSet<>(Arrays.asList(ArrayUtils.add(DoorData.DOORS.toArray(new Block[0]), ModBlocks.DIMENSIONAL_PORTAL)));
public static final BlockEntityType<DetachedRiftBlockEntity> DETACHED_RIFT = register(
"dimdoors:detached_rift",
DetachedRiftBlockEntity::new,
ModBlocks.DETACHED_RIFT);
public static final BlockEntityType<EntranceRiftBlockEntity> ENTRANCE_RIFT = Registry.register(
Registry.BLOCK_ENTITY_TYPE,
public static final MutableBlockEntityType<EntranceRiftBlockEntity> ENTRANCE_RIFT = registerMutable(
"dimdoors:entrance_rift",
new BlockEntityType<>(EntranceRiftBlockEntity::new, ENTRANCE_RIFT_BLOCKS, null));
EntranceRiftBlockEntity::new,
ArrayUtils.add(DoorData.DOORS.toArray(new Block[0]), ModBlocks.DIMENSIONAL_PORTAL));
private static <E extends BlockEntity> BlockEntityType<E> register(String id, FabricBlockEntityTypeBuilder.Factory<E> factory, Block... blocks) {
return Registry.register(Registry.BLOCK_ENTITY_TYPE, id, FabricBlockEntityTypeBuilder.create(factory, blocks).build());
}
private static <E extends BlockEntity> MutableBlockEntityType<E> registerMutable(String id, MutableBlockEntityType.BlockEntityFactory<E> factory, Block... blocks) {
return Registry.register(Registry.BLOCK_ENTITY_TYPE, id, MutableBlockEntityType.Builder.create(factory, blocks).build());
}
public static void init() {
//just loads the class
}

View file

@ -97,10 +97,6 @@ public class DimensionalDoorItemRegistrar {
private void register(Identifier identifier, Item dimItem) {
Identifier gennedId = new Identifier("dimdoors", PREFIX + identifier.getPath());
Registry.register(registry, gennedId, dimItem);
//if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) {
//BlockRenderLayerMap.INSTANCE.putBlock(newBlock, RenderLayer.getCutout());
//}
}

View file

@ -3,4 +3,5 @@ accessWidener v1 named
# Because its package private
accessible class net/minecraft/client/render/RenderLayer$MultiPhase
accessible class net/minecraft/block/entity/BlockEntityType$BlockEntityFactory
# for MutableBlockEntityType
extendable class net/minecraft/block/entity/BlockEntityType$BlockEntityFactory