superb-warfare/src/main/java/com/atsuishio/superbwarfare/block/ChargingStationBlock.java
2025-01-09 21:45:01 +08:00

99 lines
4.1 KiB
Java

package com.atsuishio.superbwarfare.block;
import com.atsuishio.superbwarfare.block.entity.ChargingStationBlockEntity;
import com.atsuishio.superbwarfare.init.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.Containers;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.phys.BlockHitResult;
import org.jetbrains.annotations.Nullable;
@SuppressWarnings("deprecation")
public class ChargingStationBlock extends BaseEntityBlock {
public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING;
public static final BooleanProperty SHOW_RANGE = BooleanProperty.create("show_range");
public ChargingStationBlock() {
super(BlockBehaviour.Properties.of().sound(SoundType.METAL).strength(3.0f).requiresCorrectToolForDrops());
this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(SHOW_RANGE, false));
}
@Override
public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHit) {
if (pLevel.isClientSide) {
return InteractionResult.SUCCESS;
} else {
this.openContainer(pLevel, pPos, pPlayer);
return InteractionResult.CONSUME;
}
}
protected void openContainer(Level pLevel, BlockPos pPos, Player pPlayer) {
BlockEntity blockentity = pLevel.getBlockEntity(pPos);
if (blockentity instanceof ChargingStationBlockEntity blockEntity) {
pPlayer.openMenu(blockEntity);
}
}
@Override
public RenderShape getRenderShape(BlockState pState) {
return RenderShape.MODEL;
}
@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
return new ChargingStationBlockEntity(pPos, pState);
}
@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, BlockState pState, BlockEntityType<T> pBlockEntityType) {
if (!pLevel.isClientSide) {
return createTickerHelper(pBlockEntityType, ModBlockEntities.CHARGING_STATION.get(), ChargingStationBlockEntity::serverTick);
}
return null;
}
@Override
public void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState pNewState, boolean pMovedByPiston) {
if (!pState.is(pNewState.getBlock())) {
BlockEntity blockentity = pLevel.getBlockEntity(pPos);
if (blockentity instanceof ChargingStationBlockEntity blockEntity) {
if (pLevel instanceof ServerLevel serverLevel) {
Containers.dropContents(serverLevel, pPos, blockEntity);
}
pLevel.updateNeighbourForOutputSignal(pPos, this);
}
}
super.onRemove(pState, pLevel, pPos, pNewState, pMovedByPiston);
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) {
pBuilder.add(FACING).add(SHOW_RANGE);
}
@Nullable
@Override
public BlockState getStateForPlacement(BlockPlaceContext pContext) {
return this.defaultBlockState().setValue(FACING, pContext.getHorizontalDirection().getOpposite()).setValue(SHOW_RANGE, false);
}
}