108 lines
4.4 KiB
Java
108 lines
4.4 KiB
Java
package com.atsuishio.superbwarfare.block;
|
|
|
|
import com.atsuishio.superbwarfare.block.entity.ChargingStationBlockEntity;
|
|
import com.atsuishio.superbwarfare.init.ModBlockEntities;
|
|
import com.mojang.serialization.MapCodec;
|
|
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.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.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.NotNull;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import javax.annotation.ParametersAreNonnullByDefault;
|
|
|
|
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(Properties.of().sound(SoundType.METAL).strength(3.0f).requiresCorrectToolForDrops());
|
|
this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(SHOW_RANGE, false));
|
|
}
|
|
|
|
@Override
|
|
@ParametersAreNonnullByDefault
|
|
protected @NotNull InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hitResult) {
|
|
if (level.isClientSide) {
|
|
return InteractionResult.SUCCESS;
|
|
} else {
|
|
this.openContainer(level, pos, player);
|
|
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);
|
|
}
|
|
}
|
|
|
|
public static final MapCodec<ChargingStationBlock> CODEC = simpleCodec((prop) -> new ChargingStationBlock());
|
|
|
|
@Override
|
|
protected @NotNull MapCodec<? extends BaseEntityBlock> codec() {
|
|
return CODEC;
|
|
}
|
|
|
|
@Override
|
|
public @NotNull RenderShape getRenderShape(@NotNull BlockState pState) {
|
|
return RenderShape.MODEL;
|
|
}
|
|
|
|
@Nullable
|
|
@Override
|
|
public BlockEntity newBlockEntity(@NotNull BlockPos pPos, @NotNull BlockState pState) {
|
|
return new ChargingStationBlockEntity(pPos, pState);
|
|
}
|
|
|
|
@Nullable
|
|
@Override
|
|
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, @NotNull BlockState pState, @NotNull BlockEntityType<T> pBlockEntityType) {
|
|
if (!pLevel.isClientSide) {
|
|
return createTickerHelper(pBlockEntityType, ModBlockEntities.CHARGING_STATION.get(), ChargingStationBlockEntity::serverTick);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public void onRemove(BlockState pState, @NotNull Level pLevel, @NotNull 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);
|
|
}
|
|
}
|