| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.function.BiConsumer;
|
| 5 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Direction;
|
| 8 | import net.minecraft.core.component.DataComponents;
|
| 9 | import net.minecraft.core.particles.ParticleTypes;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.server.level.ServerPlayer;
|
| 12 | import net.minecraft.sounds.SoundEvents;
|
| 13 | import net.minecraft.sounds.SoundSource;
|
| 14 | import net.minecraft.stats.Stats;
|
| 15 | import net.minecraft.tags.BlockTags;
|
| 16 | import net.minecraft.tags.EnchantmentTags;
|
| 17 | import net.minecraft.util.Mth;
|
| 18 | import net.minecraft.util.Prediction;
|
| 19 | import net.minecraft.util.RandomSource;
|
| 20 | import net.minecraft.util.Util;
|
| 21 | import net.minecraft.world.Containers;
|
| 22 | import net.minecraft.world.InteractionHand;
|
| 23 | import net.minecraft.world.InteractionResult;
|
| 24 | import net.minecraft.world.entity.Entity;
|
| 25 | import net.minecraft.world.entity.animal.bee.Bee;
|
| 26 | import net.minecraft.world.entity.boss.wither.WitherBoss;
|
| 27 | import net.minecraft.world.entity.item.ItemEntity;
|
| 28 | import net.minecraft.world.entity.item.PrimedTnt;
|
| 29 | import net.minecraft.world.entity.monster.Creeper;
|
| 30 | import net.minecraft.world.entity.player.Player;
|
| 31 | import net.minecraft.world.entity.projectile.hurtingprojectile.WitherSkull;
|
| 32 | import net.minecraft.world.entity.vehicle.minecart.MinecartTNT;
|
| 33 | import net.minecraft.world.item.Item;
|
| 34 | import net.minecraft.world.item.ItemStack;
|
| 35 | import net.minecraft.world.item.Items;
|
| 36 | import net.minecraft.world.item.component.BlockItemStateProperties;
|
| 37 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 38 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 39 | import net.minecraft.world.level.Explosion;
|
| 40 | import net.minecraft.world.level.Level;
|
| 41 | import net.minecraft.world.level.LevelReader;
|
| 42 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 43 | import net.minecraft.world.level.block.entity.BeehiveBlockEntity;
|
| 44 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 45 | import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
| 46 | import net.minecraft.world.level.block.entity.BlockEntityType;
|
| 47 | import net.minecraft.world.level.block.entity.BlockEntityTypes;
|
| 48 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 49 | import net.minecraft.world.level.block.state.BlockState;
|
| 50 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 51 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 52 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 53 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 54 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 55 | import net.minecraft.world.level.gamerules.GameRules;
|
| 56 | import net.minecraft.world.level.storage.loot.BuiltInLootTables;
|
| 57 | import net.minecraft.world.level.storage.loot.LootParams;
|
| 58 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 59 | import net.minecraft.world.phys.AABB;
|
| 60 | import net.minecraft.world.phys.BlockHitResult;
|
| 61 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 62 | import org.jspecify.annotations.Nullable;
|
| 63 |
|
| 64 | public class BeehiveBlock extends BaseEntityBlock {
|
| 65 | public static final EnumProperty<Direction> FACING = HorizontalDirectionalBlock.FACING;
|
| 66 | public static final IntegerProperty HONEY_LEVEL = BlockStateProperties.LEVEL_HONEY;
|
| 67 | public static final int MAX_HONEY_LEVELS = 5;
|
| 68 |
|
| 69 | public BeehiveBlock(final BlockBehaviour.Properties properties) {
|
| 70 | super(properties);
|
| 71 | this.registerDefaultState(this.stateDefinition.any().setValue(HONEY_LEVEL, 0).setValue(FACING, Direction.NORTH));
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | protected boolean hasAnalogOutputSignal(final BlockState state) {
|
| 76 | return true;
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
|
| 81 | return state.getValue(HONEY_LEVEL);
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public void playerDestroy(
|
| 86 | final ServerLevel level,
|
| 87 | final ServerPlayer player,
|
| 88 | final BlockPos pos,
|
| 89 | final BlockState state,
|
| 90 | final @Nullable BlockEntity blockEntity,
|
| 91 | final ItemStack destroyedWith
|
| 92 | ) {
|
| 93 | super.playerDestroy(level, player, pos, state, blockEntity, destroyedWith);
|
| 94 | if (blockEntity instanceof BeehiveBlockEntity beehiveBlockEntity) {
|
| 95 | if (!EnchantmentHelper.hasTag(destroyedWith, EnchantmentTags.PREVENTS_BEE_SPAWNS_WHEN_MINING)) {
|
| 96 | beehiveBlockEntity.emptyAllLivingFromHive(player, state, BeehiveBlockEntity.BeeReleaseStatus.EMERGENCY);
|
| 97 | Containers.updateNeighboursAfterDestroy(state, level, pos);
|
| 98 | this.angerNearbyBees(level, pos);
|
| 99 | }
|
| 100 |
|
| 101 | CriteriaTriggers.BEE_NEST_DESTROYED.trigger(player, state, destroyedWith, beehiveBlockEntity.getOccupantCount());
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | protected void onExplosionHit(
|
| 107 | final BlockState state, final ServerLevel level, final BlockPos pos, final Explosion explosion, final BiConsumer<ItemStack, BlockPos> onHit
|
| 108 | ) {
|
| 109 | super.onExplosionHit(state, level, pos, explosion, onHit);
|
| 110 | this.angerNearbyBees(level, pos);
|
| 111 | }
|
| 112 |
|
| 113 | private void angerNearbyBees(final Level level, final BlockPos pos) {
|
| 114 | AABB areaAroundBeehive = new AABB(pos).inflate(8.0, 6.0, 8.0);
|
| 115 | List<Bee> beesToAnger = level.getEntitiesOfClass(Bee.class, areaAroundBeehive);
|
| 116 | if (!beesToAnger.isEmpty()) {
|
| 117 | List<Player> playersToBeAngryAt = level.getEntitiesOfClass(Player.class, areaAroundBeehive);
|
| 118 | if (playersToBeAngryAt.isEmpty()) {
|
| 119 | return;
|
| 120 | }
|
| 121 |
|
| 122 | for (Bee bee : beesToAnger) {
|
| 123 | if (bee.getTarget() == null) {
|
| 124 | Player angerTarget = Util.getRandom(playersToBeAngryAt, level.getRandom());
|
| 125 | bee.setTarget(angerTarget);
|
| 126 | }
|
| 127 | }
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | public static void dropHoneycomb(
|
| 132 | final ServerLevel level,
|
| 133 | final ItemStack tool,
|
| 134 | final BlockState blockState,
|
| 135 | final @Nullable BlockEntity blockEntity,
|
| 136 | final @Nullable Entity entity,
|
| 137 | final BlockPos pos
|
| 138 | ) {
|
| 139 | dropFromBlockInteractLootTable(
|
| 140 | level, BuiltInLootTables.HARVEST_BEEHIVE, pos, blockState, blockEntity, tool, entity, (serverLevel, stack) -> popResource(serverLevel, pos, stack)
|
| 141 | );
|
| 142 | }
|
| 143 |
|
| 144 | @Override
|
| 145 | protected InteractionResult useItemOn(
|
| 146 | final ItemStack itemStack,
|
| 147 | final BlockState state,
|
| 148 | final Level level,
|
| 149 | final BlockPos pos,
|
| 150 | final Player player,
|
| 151 | final InteractionHand hand,
|
| 152 | final BlockHitResult hitResult
|
| 153 | ) {
|
| 154 | int honeyLevel = state.getValue(HONEY_LEVEL);
|
| 155 | boolean hiveEmptied = false;
|
| 156 | if (honeyLevel >= 5) {
|
| 157 | Item item = itemStack.getItem();
|
| 158 | if (level instanceof ServerLevel serverLevel && itemStack.is(Items.SHEARS)) {
|
| 159 | dropHoneycomb(serverLevel, itemStack, state, level.getBlockEntity(pos), player, pos);
|
| 160 | level.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.BEEHIVE_SHEAR, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 161 | itemStack.hurtAndBreak(1, player, hand.asEquipmentSlot());
|
| 162 | hiveEmptied = true;
|
| 163 | level.gameEvent(player, GameEvent.SHEAR, pos);
|
| 164 | } else if (itemStack.is(Items.GLASS_BOTTLE)) {
|
| 165 | itemStack.shrink(1);
|
| 166 | level.playSound(player, player.getX(), player.getY(), player.getZ(), SoundEvents.BOTTLE_FILL, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 167 | if (itemStack.isEmpty()) {
|
| 168 | player.setItemInHand(hand, new ItemStack(Items.HONEY_BOTTLE));
|
| 169 | } else if (!player.getInventory().add(new ItemStack(Items.HONEY_BOTTLE))) {
|
| 170 | player.drop(new ItemStack(Items.HONEY_BOTTLE), false, Prediction.PREDICTED);
|
| 171 | }
|
| 172 |
|
| 173 | hiveEmptied = true;
|
| 174 | level.gameEvent(player, GameEvent.FLUID_PICKUP, pos);
|
| 175 | }
|
| 176 |
|
| 177 | if (!level.isClientSide() && hiveEmptied) {
|
| 178 | player.awardStat(Stats.ITEM_USED.get(item));
|
| 179 | }
|
| 180 | }
|
| 181 |
|
| 182 | if (hiveEmptied) {
|
| 183 | if (!CampfireBlock.isSmokeyPos(level, pos)) {
|
| 184 | if (this.hiveContainsBees(level, pos)) {
|
| 185 | this.angerNearbyBees(level, pos);
|
| 186 | }
|
| 187 |
|
| 188 | this.releaseBeesAndResetHoneyLevel(level, state, pos, player, BeehiveBlockEntity.BeeReleaseStatus.EMERGENCY);
|
| 189 | } else {
|
| 190 | this.resetHoneyLevel(level, state, pos);
|
| 191 | }
|
| 192 |
|
| 193 | return InteractionResult.SUCCESS;
|
| 194 | } else {
|
| 195 | return super.useItemOn(itemStack, state, level, pos, player, hand, hitResult);
|
| 196 | }
|
| 197 | }
|
| 198 |
|
| 199 | private boolean hiveContainsBees(final Level level, final BlockPos pos) {
|
| 200 | return level.getBlockEntity(pos) instanceof BeehiveBlockEntity beehiveBlockEntity ? !beehiveBlockEntity.isEmpty() : false;
|
| 201 | }
|
| 202 |
|
| 203 | public void releaseBeesAndResetHoneyLevel(
|
| 204 | final Level level, final BlockState state, final BlockPos pos, final @Nullable Player player, final BeehiveBlockEntity.BeeReleaseStatus beeReleaseStatus
|
| 205 | ) {
|
| 206 | this.resetHoneyLevel(level, state, pos);
|
| 207 | if (level.getBlockEntity(pos) instanceof BeehiveBlockEntity beehiveBlockEntity) {
|
| 208 | beehiveBlockEntity.emptyAllLivingFromHive(player, state, beeReleaseStatus);
|
| 209 | }
|
| 210 | }
|
| 211 |
|
| 212 | public void resetHoneyLevel(final Level level, final BlockState state, final BlockPos pos) {
|
| 213 | level.setBlockAndUpdate(pos, state.setValue(HONEY_LEVEL, 0));
|
| 214 | }
|
| 215 |
|
| 216 | @Override
|
| 217 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 218 | if (state.getValue(HONEY_LEVEL) >= 5) {
|
| 219 | for (int i = 0; i < random.nextInt(1) + 1; i++) {
|
| 220 | this.trySpawnDripParticles(level, pos, state);
|
| 221 | }
|
| 222 | }
|
| 223 | }
|
| 224 |
|
| 225 | private void trySpawnDripParticles(final Level level, final BlockPos pos, final BlockState state) {
|
| 226 | if (state.getFluidState().isEmpty() && !(level.getRandom().nextFloat() < 0.3F)) {
|
| 227 | VoxelShape collisionShape = state.getCollisionShape(level, pos);
|
| 228 | double topSideHeight = collisionShape.max(Direction.Axis.Y);
|
| 229 | if (topSideHeight >= 1.0 && !state.is(BlockTags.IMPERMEABLE)) {
|
| 230 | double bottomSideHeight = collisionShape.min(Direction.Axis.Y);
|
| 231 | if (bottomSideHeight > 0.0) {
|
| 232 | this.spawnParticle(level, pos, collisionShape, pos.getY() + bottomSideHeight - 0.05);
|
| 233 | } else {
|
| 234 | BlockPos below = pos.below();
|
| 235 | BlockState belowState = level.getBlockState(below);
|
| 236 | VoxelShape belowShape = belowState.getCollisionShape(level, below);
|
| 237 | double belowTopSideHeight = belowShape.max(Direction.Axis.Y);
|
| 238 | if ((belowTopSideHeight < 1.0 || !belowState.isCollisionShapeFullBlock(level, below)) && belowState.getFluidState().isEmpty()) {
|
| 239 | this.spawnParticle(level, pos, collisionShape, pos.getY() - 0.05);
|
| 240 | }
|
| 241 | }
|
| 242 | }
|
| 243 | }
|
| 244 | }
|
| 245 |
|
| 246 | private void spawnParticle(final Level level, final BlockPos pos, final VoxelShape dripShape, final double height) {
|
| 247 | this.spawnFluidParticle(
|
| 248 | level,
|
| 249 | pos.getX() + dripShape.min(Direction.Axis.X),
|
| 250 | pos.getX() + dripShape.max(Direction.Axis.X),
|
| 251 | pos.getZ() + dripShape.min(Direction.Axis.Z),
|
| 252 | pos.getZ() + dripShape.max(Direction.Axis.Z),
|
| 253 | height
|
| 254 | );
|
| 255 | }
|
| 256 |
|
| 257 | private void spawnFluidParticle(final Level level, final double x1, final double x2, final double z1, final double z2, final double y) {
|
| 258 | level.addParticle(
|
| 259 | ParticleTypes.DRIPPING_HONEY, Mth.lerp(level.getRandom().nextDouble(), x1, x2), y, Mth.lerp(level.getRandom().nextDouble(), z1, z2), 0.0, 0.0, 0.0
|
| 260 | );
|
| 261 | }
|
| 262 |
|
| 263 | @Override
|
| 264 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 265 | return this.defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite());
|
| 266 | }
|
| 267 |
|
| 268 | @Override
|
| 269 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 270 | builder.add(HONEY_LEVEL, FACING);
|
| 271 | }
|
| 272 |
|
| 273 | @Override
|
| 274 | public @Nullable BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 275 | return new BeehiveBlockEntity(worldPosition, blockState);
|
| 276 | }
|
| 277 |
|
| 278 | @Override
|
| 279 | public <T extends BlockEntity> @Nullable BlockEntityTicker<T> getTicker(final Level level, final BlockState blockState, final BlockEntityType<T> type) {
|
| 280 | return level.isClientSide() ? null : createTickerHelper(type, BlockEntityTypes.BEEHIVE, BeehiveBlockEntity::serverTick);
|
| 281 | }
|
| 282 |
|
| 283 | @Override
|
| 284 | public BlockState playerWillDestroy(final Level level, final BlockPos pos, final BlockState state, final Player player) {
|
| 285 | if (level instanceof ServerLevel serverLevel
|
| 286 | && player.preventsBlockDrops()
|
| 287 | && serverLevel.getGameRules().get(GameRules.BLOCK_DROPS)
|
| 288 | && level.getBlockEntity(pos) instanceof BeehiveBlockEntity beehiveBlockEntity) {
|
| 289 | int honeyLevel = state.getValue(HONEY_LEVEL);
|
| 290 | boolean hasBees = !beehiveBlockEntity.isEmpty();
|
| 291 | if (hasBees || honeyLevel > 0) {
|
| 292 | ItemStack itemStack = new ItemStack(this);
|
| 293 | itemStack.applyComponents(beehiveBlockEntity.collectComponents());
|
| 294 | itemStack.set(DataComponents.BLOCK_STATE, BlockItemStateProperties.EMPTY.with(HONEY_LEVEL, honeyLevel));
|
| 295 | ItemEntity entity = new ItemEntity(level, pos.getX(), pos.getY(), pos.getZ(), itemStack);
|
| 296 | entity.setDefaultPickUpDelay();
|
| 297 | level.addFreshEntity(entity);
|
| 298 | }
|
| 299 | }
|
| 300 |
|
| 301 | return super.playerWillDestroy(level, pos, state, player);
|
| 302 | }
|
| 303 |
|
| 304 | @Override
|
| 305 | protected List<ItemStack> getDrops(final BlockState state, final LootParams.Builder params) {
|
| 306 | Entity entity = params.getOptionalParameter(LootContextParams.THIS_ENTITY);
|
| 307 | if (entity instanceof PrimedTnt
|
| 308 | || entity instanceof Creeper
|
| 309 | || entity instanceof WitherSkull
|
| 310 | || entity instanceof WitherBoss
|
| 311 | || entity instanceof MinecartTNT) {
|
| 312 | BlockEntity blockEntity = params.getOptionalParameter(LootContextParams.BLOCK_ENTITY);
|
| 313 | if (blockEntity instanceof BeehiveBlockEntity beehiveBlockEntity) {
|
| 314 | beehiveBlockEntity.emptyAllLivingFromHive(null, state, BeehiveBlockEntity.BeeReleaseStatus.EMERGENCY);
|
| 315 | }
|
| 316 | }
|
| 317 |
|
| 318 | return super.getDrops(state, params);
|
| 319 | }
|
| 320 |
|
| 321 | @Override
|
| 322 | protected ItemStack getCloneItemStack(final LevelReader level, final BlockPos pos, final BlockState state, final boolean includeData) {
|
| 323 | ItemStack itemStack = super.getCloneItemStack(level, pos, state, includeData);
|
| 324 | if (includeData) {
|
| 325 | itemStack.set(DataComponents.BLOCK_STATE, BlockItemStateProperties.EMPTY.with(HONEY_LEVEL, state.getValue(HONEY_LEVEL)));
|
| 326 | }
|
| 327 |
|
| 328 | return itemStack;
|
| 329 | }
|
| 330 |
|
| 331 | @Override
|
| 332 | protected BlockState updateShape(
|
| 333 | final BlockState state,
|
| 334 | final LevelReader level,
|
| 335 | final ScheduledTickAccess ticks,
|
| 336 | final BlockPos pos,
|
| 337 | final Direction directionToNeighbour,
|
| 338 | final BlockPos neighbourPos,
|
| 339 | final BlockState neighbourState,
|
| 340 | final RandomSource random
|
| 341 | ) {
|
| 342 | if (level.getBlockState(neighbourPos).getBlock() instanceof FireBlock && level.getBlockEntity(pos) instanceof BeehiveBlockEntity beehiveBlockEntity) {
|
| 343 | beehiveBlockEntity.emptyAllLivingFromHive(null, state, BeehiveBlockEntity.BeeReleaseStatus.EMERGENCY);
|
| 344 | }
|
| 345 |
|
| 346 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 347 | }
|
| 348 |
|
| 349 | @Override
|
| 350 | public BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 351 | return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
|
| 352 | }
|
| 353 |
|
| 354 | @Override
|
| 355 | public BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 356 | return state.rotate(mirror.getRotation(state.getValue(FACING)));
|
| 357 | }
|
| 358 | }
|
| 359 |
|