2025-11-24 22:52:51 +03:00

218 lines
9.6 KiB
Java

/*
* Decompiled with CFR 0.152.
*
* Could not load the following classes:
* com.mojang.datafixers.kinds.App
* com.mojang.datafixers.kinds.Applicative
* com.mojang.serialization.MapCodec
* com.mojang.serialization.codecs.RecordCodecBuilder
* org.jspecify.annotations.Nullable
*/
package net.minecraft.world.level.block;
import com.mojang.datafixers.kinds.App;
import com.mojang.datafixers.kinds.Applicative;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import java.util.Map;
import java.util.Optional;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.stats.Stats;
import net.minecraft.world.Containers;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.monster.Shulker;
import net.minecraft.world.entity.monster.piglin.PiglinAi;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.DirectionalBlock;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.block.Rotation;
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.entity.ShulkerBoxBlockEntity;
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.EnumProperty;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jspecify.annotations.Nullable;
public class ShulkerBoxBlock
extends BaseEntityBlock {
public static final MapCodec<ShulkerBoxBlock> CODEC = RecordCodecBuilder.mapCodec(i -> i.group((App)DyeColor.CODEC.optionalFieldOf("color").forGetter(b -> Optional.ofNullable(b.color)), ShulkerBoxBlock.propertiesCodec()).apply((Applicative)i, (color, properties) -> new ShulkerBoxBlock(color.orElse(null), (BlockBehaviour.Properties)properties)));
public static final Map<Direction, VoxelShape> SHAPES_OPEN_SUPPORT = Shapes.rotateAll(Block.boxZ(16.0, 0.0, 1.0));
public static final EnumProperty<Direction> FACING = DirectionalBlock.FACING;
private final @Nullable DyeColor color;
public MapCodec<ShulkerBoxBlock> codec() {
return CODEC;
}
public ShulkerBoxBlock(@Nullable DyeColor color, BlockBehaviour.Properties properties) {
super(properties);
this.color = color;
this.registerDefaultState((BlockState)((BlockState)this.stateDefinition.any()).setValue(FACING, Direction.UP));
}
@Override
public BlockEntity newBlockEntity(BlockPos worldPosition, BlockState blockState) {
return new ShulkerBoxBlockEntity(this.color, worldPosition, blockState);
}
@Override
public <T extends BlockEntity> @Nullable BlockEntityTicker<T> getTicker(Level level, BlockState blockState, BlockEntityType<T> type) {
return ShulkerBoxBlock.createTickerHelper(type, BlockEntityType.SHULKER_BOX, ShulkerBoxBlockEntity::tick);
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hitResult) {
if (level instanceof ServerLevel) {
ShulkerBoxBlockEntity shulkerBoxBlockEntity;
ServerLevel serverLevel = (ServerLevel)level;
BlockEntity blockEntity = level.getBlockEntity(pos);
if (blockEntity instanceof ShulkerBoxBlockEntity && ShulkerBoxBlock.canOpen(state, level, pos, shulkerBoxBlockEntity = (ShulkerBoxBlockEntity)blockEntity)) {
player.openMenu(shulkerBoxBlockEntity);
player.awardStat(Stats.OPEN_SHULKER_BOX);
PiglinAi.angerNearbyPiglins(serverLevel, player, true);
}
}
return InteractionResult.SUCCESS;
}
private static boolean canOpen(BlockState state, Level level, BlockPos pos, ShulkerBoxBlockEntity blockEntity) {
if (blockEntity.getAnimationStatus() != ShulkerBoxBlockEntity.AnimationStatus.CLOSED) {
return true;
}
AABB lidOpenBoundingBox = Shulker.getProgressDeltaAabb(1.0f, state.getValue(FACING), 0.0f, 0.5f, pos.getBottomCenter()).deflate(1.0E-6);
return level.noCollision(lidOpenBoundingBox);
}
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
return (BlockState)this.defaultBlockState().setValue(FACING, context.getClickedFace());
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(FACING);
}
@Override
public BlockState playerWillDestroy(Level level, BlockPos pos, BlockState state, Player player) {
BlockEntity blockEntity = level.getBlockEntity(pos);
if (blockEntity instanceof ShulkerBoxBlockEntity) {
ShulkerBoxBlockEntity shulkerBoxBlockEntity = (ShulkerBoxBlockEntity)blockEntity;
if (!level.isClientSide() && player.preventsBlockDrops() && !shulkerBoxBlockEntity.isEmpty()) {
ItemStack itemStack = ShulkerBoxBlock.getColoredItemStack(this.getColor());
itemStack.applyComponents(blockEntity.collectComponents());
ItemEntity entity = new ItemEntity(level, (double)pos.getX() + 0.5, (double)pos.getY() + 0.5, (double)pos.getZ() + 0.5, itemStack);
entity.setDefaultPickUpDelay();
level.addFreshEntity(entity);
} else {
shulkerBoxBlockEntity.unpackLootTable(player);
}
}
return super.playerWillDestroy(level, pos, state, player);
}
@Override
protected void affectNeighborsAfterRemoval(BlockState state, ServerLevel level, BlockPos pos, boolean movedByPiston) {
Containers.updateNeighboursAfterDestroy(state, level, pos);
}
@Override
protected VoxelShape getBlockSupportShape(BlockState state, BlockGetter level, BlockPos pos) {
ShulkerBoxBlockEntity shulker;
BlockEntity entity = level.getBlockEntity(pos);
if (entity instanceof ShulkerBoxBlockEntity && !(shulker = (ShulkerBoxBlockEntity)entity).isClosed()) {
return SHAPES_OPEN_SUPPORT.get(state.getValue(FACING).getOpposite());
}
return Shapes.block();
}
@Override
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
BlockEntity entity = level.getBlockEntity(pos);
if (entity instanceof ShulkerBoxBlockEntity) {
ShulkerBoxBlockEntity shulkerBoxBlockEntity = (ShulkerBoxBlockEntity)entity;
return Shapes.create(shulkerBoxBlockEntity.getBoundingBox(state));
}
return Shapes.block();
}
@Override
protected boolean propagatesSkylightDown(BlockState state) {
return false;
}
@Override
protected boolean hasAnalogOutputSignal(BlockState state) {
return true;
}
@Override
protected int getAnalogOutputSignal(BlockState state, Level level, BlockPos pos, Direction direction) {
return AbstractContainerMenu.getRedstoneSignalFromBlockEntity(level.getBlockEntity(pos));
}
public static Block getBlockByColor(@Nullable DyeColor color) {
if (color == null) {
return Blocks.SHULKER_BOX;
}
return switch (color) {
default -> throw new MatchException(null, null);
case DyeColor.WHITE -> Blocks.WHITE_SHULKER_BOX;
case DyeColor.ORANGE -> Blocks.ORANGE_SHULKER_BOX;
case DyeColor.MAGENTA -> Blocks.MAGENTA_SHULKER_BOX;
case DyeColor.LIGHT_BLUE -> Blocks.LIGHT_BLUE_SHULKER_BOX;
case DyeColor.YELLOW -> Blocks.YELLOW_SHULKER_BOX;
case DyeColor.LIME -> Blocks.LIME_SHULKER_BOX;
case DyeColor.PINK -> Blocks.PINK_SHULKER_BOX;
case DyeColor.GRAY -> Blocks.GRAY_SHULKER_BOX;
case DyeColor.LIGHT_GRAY -> Blocks.LIGHT_GRAY_SHULKER_BOX;
case DyeColor.CYAN -> Blocks.CYAN_SHULKER_BOX;
case DyeColor.BLUE -> Blocks.BLUE_SHULKER_BOX;
case DyeColor.BROWN -> Blocks.BROWN_SHULKER_BOX;
case DyeColor.GREEN -> Blocks.GREEN_SHULKER_BOX;
case DyeColor.RED -> Blocks.RED_SHULKER_BOX;
case DyeColor.BLACK -> Blocks.BLACK_SHULKER_BOX;
case DyeColor.PURPLE -> Blocks.PURPLE_SHULKER_BOX;
};
}
public @Nullable DyeColor getColor() {
return this.color;
}
public static ItemStack getColoredItemStack(@Nullable DyeColor color) {
return new ItemStack(ShulkerBoxBlock.getBlockByColor(color));
}
@Override
protected BlockState rotate(BlockState state, Rotation rotation) {
return (BlockState)state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
}
@Override
protected BlockState mirror(BlockState state, Mirror mirror) {
return state.rotate(mirror.getRotation(state.getValue(FACING)));
}
}