| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.world.InteractionHand;
|
| 7 | import net.minecraft.world.InteractionResult;
|
| 8 | import net.minecraft.world.entity.player.Player;
|
| 9 | import net.minecraft.world.item.ItemStack;
|
| 10 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 11 | import net.minecraft.world.level.Level;
|
| 12 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 13 | import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
| 14 | import net.minecraft.world.level.block.entity.BlockEntityType;
|
| 15 | import net.minecraft.world.level.block.entity.BlockEntityTypes;
|
| 16 | import net.minecraft.world.level.block.entity.vault.VaultBlockEntity;
|
| 17 | import net.minecraft.world.level.block.entity.vault.VaultState;
|
| 18 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 19 | import net.minecraft.world.level.block.state.BlockState;
|
| 20 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 21 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 22 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 23 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 24 | import net.minecraft.world.level.block.state.properties.Property;
|
| 25 | import net.minecraft.world.phys.BlockHitResult;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 |
|
| 28 | public class VaultBlock extends BaseEntityBlock {
|
| 29 | public static final Property<VaultState> STATE = BlockStateProperties.VAULT_STATE;
|
| 30 | public static final EnumProperty<Direction> FACING = HorizontalDirectionalBlock.FACING;
|
| 31 | public static final BooleanProperty OMINOUS = BlockStateProperties.OMINOUS;
|
| 32 |
|
| 33 | public VaultBlock(final BlockBehaviour.Properties properties) {
|
| 34 | super(properties);
|
| 35 | this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(STATE, VaultState.INACTIVE).setValue(OMINOUS, false));
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public InteractionResult useItemOn(
|
| 40 | final ItemStack itemStack,
|
| 41 | final BlockState state,
|
| 42 | final Level level,
|
| 43 | final BlockPos pos,
|
| 44 | final Player player,
|
| 45 | final InteractionHand hand,
|
| 46 | final BlockHitResult hitResult
|
| 47 | ) {
|
| 48 | if (!itemStack.isEmpty() && state.getValue(STATE) == VaultState.ACTIVE) {
|
| 49 | if (level instanceof ServerLevel serverLevel) {
|
| 50 | if (!(serverLevel.getBlockEntity(pos) instanceof VaultBlockEntity vault)) {
|
| 51 | return InteractionResult.TRY_WITH_EMPTY_HAND;
|
| 52 | }
|
| 53 |
|
| 54 | VaultBlockEntity.Server.tryInsertKey(serverLevel, pos, state, vault.getConfig(), vault.getServerData(), vault.getSharedData(), player, itemStack);
|
| 55 | }
|
| 56 |
|
| 57 | return InteractionResult.SUCCESS_SERVER;
|
| 58 | } else {
|
| 59 | return InteractionResult.TRY_WITH_EMPTY_HAND;
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public @Nullable BlockEntity newBlockEntity(final BlockPos pos, final BlockState state) {
|
| 65 | return new VaultBlockEntity(pos, state);
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 70 | builder.add(FACING, STATE, OMINOUS);
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public <T extends BlockEntity> @Nullable BlockEntityTicker<T> getTicker(final Level level, final BlockState blockState, final BlockEntityType<T> type) {
|
| 75 | return level instanceof ServerLevel serverLevel
|
| 76 | ? createTickerHelper(
|
| 77 | type,
|
| 78 | BlockEntityTypes.VAULT,
|
| 79 | (innerLevel, pos, state, entity) -> VaultBlockEntity.Server.tick(
|
| 80 | serverLevel, pos, state, entity.getConfig(), entity.getServerData(), entity.getSharedData()
|
| 81 | )
|
| 82 | )
|
| 83 | : createTickerHelper(
|
| 84 | type,
|
| 85 | BlockEntityTypes.VAULT,
|
| 86 | (innerLevel, pos, state, entity) -> VaultBlockEntity.Client.tick(innerLevel, pos, state, entity.getClientData(), entity.getSharedData())
|
| 87 | );
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 92 | return this.defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite());
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | public BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 97 | return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 102 | return state.rotate(mirror.getRotation(state.getValue(FACING)));
|
| 103 | }
|
| 104 | }
|
| 105 |
|