| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.component.DataComponents;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.util.RandomSource;
|
| 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.component.BlockItemStateProperties;
|
| 11 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 12 | import net.minecraft.world.level.BlockGetter;
|
| 13 | import net.minecraft.world.level.Level;
|
| 14 | import net.minecraft.world.level.LevelReader;
|
| 15 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 16 | import net.minecraft.world.level.block.entity.TestBlockEntity;
|
| 17 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 18 | import net.minecraft.world.level.block.state.BlockState;
|
| 19 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 20 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 21 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 22 | import net.minecraft.world.level.block.state.properties.TestBlockMode;
|
| 23 | import net.minecraft.world.level.redstone.Orientation;
|
| 24 | import net.minecraft.world.phys.BlockHitResult;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 |
|
| 27 | public class TestBlock extends BaseEntityBlock implements GameMasterBlock {
|
| 28 | public static final EnumProperty<TestBlockMode> MODE = BlockStateProperties.TEST_BLOCK_MODE;
|
| 29 |
|
| 30 | public TestBlock(final BlockBehaviour.Properties properties) {
|
| 31 | super(properties);
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public @Nullable BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 36 | return new TestBlockEntity(worldPosition, blockState);
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 41 | BlockItemStateProperties stateProperties = context.getItemInHand().get(DataComponents.BLOCK_STATE);
|
| 42 | BlockState toPlace = this.defaultBlockState();
|
| 43 | if (stateProperties != null) {
|
| 44 | TestBlockMode mode = stateProperties.get(MODE);
|
| 45 | if (mode != null) {
|
| 46 | toPlace = toPlace.setValue(MODE, mode);
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | return toPlace;
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 55 | builder.add(MODE);
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 60 | if (level.getBlockEntity(pos) instanceof TestBlockEntity testBlockEntity) {
|
| 61 | if (!player.canUseGameMasterBlocks()) {
|
| 62 | return InteractionResult.PASS;
|
| 63 | }
|
| 64 |
|
| 65 | if (level.isClientSide()) {
|
| 66 | player.openTestBlock(testBlockEntity);
|
| 67 | }
|
| 68 |
|
| 69 | return InteractionResult.SUCCESS;
|
| 70 | } else {
|
| 71 | return InteractionResult.PASS;
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 77 | TestBlockEntity testBlock = getServerTestBlockEntity(level, pos);
|
| 78 | if (testBlock != null) {
|
| 79 | testBlock.reset();
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | protected void neighborChanged(
|
| 85 | final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
|
| 86 | ) {
|
| 87 | TestBlockEntity testBlock = getServerTestBlockEntity(level, pos);
|
| 88 | if (testBlock != null) {
|
| 89 | if (testBlock.getMode() != TestBlockMode.START) {
|
| 90 | boolean shouldTrigger = level.hasNeighborSignal(pos);
|
| 91 | boolean isPowered = testBlock.isPowered();
|
| 92 | if (shouldTrigger && !isPowered) {
|
| 93 | testBlock.setPowered(true);
|
| 94 | testBlock.trigger();
|
| 95 | } else if (!shouldTrigger && isPowered) {
|
| 96 | testBlock.setPowered(false);
|
| 97 | }
|
| 98 | }
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | private static @Nullable TestBlockEntity getServerTestBlockEntity(final Level level, final BlockPos pos) {
|
| 103 | return level instanceof ServerLevel serverLevel && serverLevel.getBlockEntity(pos) instanceof TestBlockEntity testBlockEntity ? testBlockEntity : null;
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | public int ownSignal(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 108 | if (state.getValue(MODE) != TestBlockMode.START) {
|
| 109 | return 0;
|
| 110 | } else if (level.getBlockEntity(pos) instanceof TestBlockEntity testBlock) {
|
| 111 | return testBlock.isPowered() ? 15 : 0;
|
| 112 | } else {
|
| 113 | return 0;
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | protected ItemStack getCloneItemStack(final LevelReader level, final BlockPos pos, final BlockState state, final boolean includeData) {
|
| 119 | ItemStack itemStack = super.getCloneItemStack(level, pos, state, includeData);
|
| 120 | return setModeOnStack(itemStack, state.getValue(MODE));
|
| 121 | }
|
| 122 |
|
| 123 | public static ItemStack setModeOnStack(final ItemStack itemStack, final TestBlockMode mode) {
|
| 124 | itemStack.set(DataComponents.BLOCK_STATE, itemStack.getOrDefault(DataComponents.BLOCK_STATE, BlockItemStateProperties.EMPTY).with(MODE, mode));
|
| 125 | return itemStack;
|
| 126 | }
|
| 127 | }
|
| 128 |
|