| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.server.level.ServerLevel;
|
| 5 | import net.minecraft.world.InteractionResult;
|
| 6 | import net.minecraft.world.entity.LivingEntity;
|
| 7 | import net.minecraft.world.entity.player.Player;
|
| 8 | import net.minecraft.world.item.ItemStack;
|
| 9 | import net.minecraft.world.level.Level;
|
| 10 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 11 | import net.minecraft.world.level.block.entity.StructureBlockEntity;
|
| 12 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 13 | import net.minecraft.world.level.block.state.BlockState;
|
| 14 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 15 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 16 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 17 | import net.minecraft.world.level.block.state.properties.StructureMode;
|
| 18 | import net.minecraft.world.level.redstone.Orientation;
|
| 19 | import net.minecraft.world.phys.BlockHitResult;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 |
|
| 22 | /**
|
| 23 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 24 | */
|
| 25 | public class StructureBlock extends BaseEntityBlock implements GameMasterBlock {
|
| 26 | public static final EnumProperty<StructureMode> MODE = BlockStateProperties.STRUCTUREBLOCK_MODE;
|
| 27 |
|
| 28 | /**
|
| 29 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 30 | */
|
| 31 | public StructureBlock(final BlockBehaviour.Properties properties) {
|
| 32 | super(properties);
|
| 33 | this.registerDefaultState(this.stateDefinition.any().setValue(MODE, StructureMode.LOAD));
|
| 34 | }
|
| 35 |
|
| 36 | @Override
|
| 37 | public BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 38 | return new StructureBlockEntity(worldPosition, blockState);
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 43 | if (level.getBlockEntity(pos) instanceof StructureBlockEntity structureBlockEntity) {
|
| 44 | return structureBlockEntity.usedBy(player) ? InteractionResult.SUCCESS : InteractionResult.PASS;
|
| 45 | } else {
|
| 46 | return InteractionResult.PASS;
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack) {
|
| 52 | if (!level.isClientSide()) {
|
| 53 | if (by != null && level.getBlockEntity(pos) instanceof StructureBlockEntity structureBlockEntity) {
|
| 54 | structureBlockEntity.createdBy(by);
|
| 55 | }
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 61 | builder.add(MODE);
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | protected void neighborChanged(
|
| 66 | final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
|
| 67 | ) {
|
| 68 | if (level instanceof ServerLevel serverLevel) {
|
| 69 | if (level.getBlockEntity(pos) instanceof StructureBlockEntity structureBlock) {
|
| 70 | boolean shouldTrigger = level.hasNeighborSignal(pos);
|
| 71 | boolean isPowered = structureBlock.isPowered();
|
| 72 | if (shouldTrigger && !isPowered) {
|
| 73 | structureBlock.setPowered(true);
|
| 74 | this.trigger(serverLevel, structureBlock);
|
| 75 | } else if (!shouldTrigger && isPowered) {
|
| 76 | structureBlock.setPowered(false);
|
| 77 | }
|
| 78 | }
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | private void trigger(final ServerLevel level, final StructureBlockEntity structureBlock) {
|
| 83 | switch (structureBlock.getMode()) {
|
| 84 | case SAVE:
|
| 85 | structureBlock.saveStructure(false);
|
| 86 | break;
|
| 87 | case LOAD:
|
| 88 | structureBlock.placeStructure(level);
|
| 89 | break;
|
| 90 | case CORNER:
|
| 91 | structureBlock.unloadStructure();
|
| 92 | case DATA:
|
| 93 | }
|
| 94 | }
|
| 95 | }
|
| 96 |
|