Port FabricApiLookup to 1.17

This commit is contained in:
modmuss50 2021-03-08 21:00:44 +00:00
parent f995ab4233
commit b510623339
6 changed files with 51 additions and 35 deletions
fabric-api-lookup-api-v1/src
main/java/net/fabricmc/fabric/impl/lookup/block
testmod/java/net/fabricmc/fabric/test/lookup

View file

@ -69,7 +69,7 @@ public final class BlockApiLookupImpl<A, C> implements BlockApiLookup<A, C> {
state = world.getBlockState(pos);
}
if (state.getBlock().hasBlockEntity()) {
if (state.hasBlockEntity()) {
blockEntity = world.getBlockEntity(pos);
}
} else {
@ -106,7 +106,9 @@ public final class BlockApiLookupImpl<A, C> implements BlockApiLookup<A, C> {
@Override
public void registerSelf(BlockEntityType<?>... blockEntityTypes) {
for (BlockEntityType<?> blockEntityType : blockEntityTypes) {
BlockEntity blockEntity = blockEntityType.instantiate();
Block supportBlock = ((BlockEntityTypeAccessor) blockEntityType).getBlocks().iterator().next();
Objects.requireNonNull(supportBlock, "Could not get a support block for block entity type.");
BlockEntity blockEntity = blockEntityType.instantiate(BlockPos.ORIGIN, supportBlock.getDefaultState());
Objects.requireNonNull(blockEntity, "Instantiated block entity may not be null.");
if (!apiClass.isAssignableFrom(blockEntity.getClass())) {

View file

@ -18,18 +18,28 @@ package net.fabricmc.fabric.test.lookup;
import org.jetbrains.annotations.Nullable;
import net.minecraft.block.Block;
import net.minecraft.block.BlockEntityProvider;
import net.minecraft.block.BlockState;
import net.minecraft.block.BlockWithEntity;
import net.minecraft.block.entity.BlockEntityTicker;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.world.BlockView;
public class ChuteBlock extends Block implements BlockEntityProvider {
public class ChuteBlock extends BlockWithEntity {
public ChuteBlock(Settings settings) {
super(settings);
}
@Nullable
@Override
public @Nullable BlockEntity createBlockEntity(BlockView world) {
return new ChuteBlockEntity();
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new ChuteBlockEntity(pos, state);
}
@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) {
return world.isClient ? null : checkType(type, FabricApiLookupTest.CHUTE_BLOCK_ENTITY_TYPE, ChuteBlockEntity::serverTick);
}
}

View file

@ -18,9 +18,11 @@ package net.fabricmc.fabric.test.lookup;
import org.jetbrains.annotations.NotNull;
import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Tickable;
import net.minecraft.util.math.Direction;
import net.fabricmc.fabric.api.lookup.v1.block.BlockApiCache;
@ -29,41 +31,39 @@ import net.fabricmc.fabric.test.lookup.api.ItemExtractable;
import net.fabricmc.fabric.test.lookup.api.ItemInsertable;
import net.fabricmc.fabric.test.lookup.api.ItemUtils;
public class ChuteBlockEntity extends BlockEntity implements Tickable {
public class ChuteBlockEntity extends BlockEntity {
private int moveDelay = 0;
private BlockApiCache<ItemInsertable, @NotNull Direction> cachedInsertable = null;
private BlockApiCache<ItemExtractable, @NotNull Direction> cachedExtractable = null;
public ChuteBlockEntity() {
super(FabricApiLookupTest.CHUTE_BLOCK_ENTITY_TYPE);
public ChuteBlockEntity(BlockPos pos, BlockState state) {
super(FabricApiLookupTest.CHUTE_BLOCK_ENTITY_TYPE, pos, state);
}
@Override
public void tick() {
//noinspection ConstantConditions - Intellij intrinsics don't know that hasWorld makes getWorld evaluate to non-null
if (!this.hasWorld() || this.getWorld().isClient()) {
public static void serverTick(World world, BlockPos pos, BlockState blockState, ChuteBlockEntity blockEntity) {
if (!blockEntity.hasWorld()) {
return;
}
if (cachedInsertable == null) {
cachedInsertable = BlockApiCache.create(ItemApis.INSERTABLE, (ServerWorld) world, pos.offset(Direction.DOWN));
if (blockEntity.cachedInsertable == null) {
blockEntity.cachedInsertable = BlockApiCache.create(ItemApis.INSERTABLE, (ServerWorld) world, pos.offset(Direction.DOWN));
}
if (cachedExtractable == null) {
cachedExtractable = BlockApiCache.create(ItemApis.EXTRACTABLE, (ServerWorld) world, pos.offset(Direction.UP));
if (blockEntity.cachedExtractable == null) {
blockEntity.cachedExtractable = BlockApiCache.create(ItemApis.EXTRACTABLE, (ServerWorld) world, pos.offset(Direction.UP));
}
if (moveDelay == 0) {
ItemExtractable from = cachedExtractable.find(Direction.DOWN);
ItemInsertable to = cachedInsertable.find(Direction.UP);
if (blockEntity.moveDelay == 0) {
ItemExtractable from = blockEntity.cachedExtractable.find(Direction.DOWN);
ItemInsertable to = blockEntity.cachedInsertable.find(Direction.UP);
if (from != null && to != null) {
ItemUtils.move(from, to, 1);
}
moveDelay = 20;
blockEntity.moveDelay = 20;
}
--moveDelay;
--blockEntity.moveDelay;
}
}

View file

@ -18,18 +18,19 @@ package net.fabricmc.fabric.test.lookup;
import org.jetbrains.annotations.Nullable;
import net.minecraft.block.Block;
import net.minecraft.block.BlockEntityProvider;
import net.minecraft.block.BlockState;
import net.minecraft.block.BlockWithEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.world.BlockView;
public class CobbleGenBlock extends Block implements BlockEntityProvider {
public class CobbleGenBlock extends BlockWithEntity {
public CobbleGenBlock(Settings settings) {
super(settings);
}
@Nullable
@Override
public @Nullable BlockEntity createBlockEntity(BlockView world) {
return new CobbleGenBlockEntity();
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new CobbleGenBlockEntity(pos, state);
}
}

View file

@ -18,15 +18,17 @@ package net.fabricmc.fabric.test.lookup;
import java.util.function.Predicate;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.math.BlockPos;
import net.fabricmc.fabric.test.lookup.api.ItemExtractable;
public class CobbleGenBlockEntity extends BlockEntity implements ItemExtractable {
public CobbleGenBlockEntity() {
super(FabricApiLookupTest.COBBLE_GEN_BLOCK_ENTITY_TYPE);
public CobbleGenBlockEntity(BlockPos pos, BlockState state) {
super(FabricApiLookupTest.COBBLE_GEN_BLOCK_ENTITY_TYPE, pos, state);
}
@Override

View file

@ -30,6 +30,7 @@ import net.minecraft.util.registry.Registry;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.lookup.v1.block.BlockApiLookup;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.fabricmc.fabric.test.lookup.api.ItemApis;
import net.fabricmc.fabric.test.lookup.api.ItemInsertable;
import net.fabricmc.fabric.test.lookup.compat.InventoryExtractableProvider;
@ -53,12 +54,12 @@ public class FabricApiLookupTest implements ModInitializer {
Identifier chute = new Identifier(MOD_ID, "chute");
Registry.register(Registry.BLOCK, chute, CHUTE_BLOCK);
Registry.register(Registry.ITEM, chute, CHUTE_ITEM);
CHUTE_BLOCK_ENTITY_TYPE = Registry.register(Registry.BLOCK_ENTITY_TYPE, chute, BlockEntityType.Builder.create(ChuteBlockEntity::new, CHUTE_BLOCK).build(null));
CHUTE_BLOCK_ENTITY_TYPE = Registry.register(Registry.BLOCK_ENTITY_TYPE, chute, FabricBlockEntityTypeBuilder.create(ChuteBlockEntity::new, CHUTE_BLOCK).build());
Identifier cobbleGen = new Identifier(MOD_ID, "cobble_gen");
Registry.register(Registry.BLOCK, cobbleGen, COBBLE_GEN_BLOCK);
Registry.register(Registry.ITEM, cobbleGen, COBBLE_GEN_ITEM);
COBBLE_GEN_BLOCK_ENTITY_TYPE = Registry.register(Registry.BLOCK_ENTITY_TYPE, cobbleGen, BlockEntityType.Builder.create(CobbleGenBlockEntity::new, COBBLE_GEN_BLOCK).build(null));
COBBLE_GEN_BLOCK_ENTITY_TYPE = Registry.register(Registry.BLOCK_ENTITY_TYPE, cobbleGen, FabricBlockEntityTypeBuilder.create(CobbleGenBlockEntity::new, COBBLE_GEN_BLOCK).build());
InventoryExtractableProvider extractableProvider = new InventoryExtractableProvider();
InventoryInsertableProvider insertableProvider = new InventoryInsertableProvider();