package com.minelittlepony.unicopia.block; import java.util.Optional; import org.jetbrains.annotations.Nullable; import net.minecraft.block.BlockEntityProvider; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntityTicker; import net.minecraft.block.entity.BlockEntityType; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; public interface BlockEntityUtil { @SuppressWarnings("unchecked") @Nullable static BlockEntityTicker checkType(BlockEntityType givenType, BlockEntityType expectedType, BlockEntityTicker ticker) { return expectedType == givenType ? (@Nullable BlockEntityTicker) ticker : null; } @SuppressWarnings({ "unchecked", "deprecation" }) static Optional getOrCreateBlockEntity(World world, BlockPos pos, BlockEntityType type) { return world.getBlockEntity(pos, type).or(() -> { BlockState state = world.getBlockState(pos); if (!(state.hasBlockEntity())) { return Optional.empty(); } BlockEntity e = ((BlockEntityProvider)state.getBlock()).createBlockEntity(pos, state); if (e == null || e.getType() != type) { return Optional.empty(); } e.setCachedState(state); world.addBlockEntity(e); return Optional.of((T)e); }); } }