| 1 | package net.minecraft.core.dispenser;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.server.level.ServerLevel;
|
| 5 | import net.minecraft.sounds.SoundEvents;
|
| 6 | import net.minecraft.sounds.SoundSource;
|
| 7 | import net.minecraft.tags.BlockTags;
|
| 8 | import net.minecraft.world.entity.Entity;
|
| 9 | import net.minecraft.world.entity.EntitySelector;
|
| 10 | import net.minecraft.world.entity.Shearable;
|
| 11 | import net.minecraft.world.item.ItemStack;
|
| 12 | import net.minecraft.world.level.block.BeehiveBlock;
|
| 13 | import net.minecraft.world.level.block.DispenserBlock;
|
| 14 | import net.minecraft.world.level.block.entity.BeehiveBlockEntity;
|
| 15 | import net.minecraft.world.level.block.state.BlockState;
|
| 16 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 17 | import net.minecraft.world.phys.AABB;
|
| 18 |
|
| 19 | public class ShearsDispenseItemBehavior extends OptionalDispenseItemBehavior {
|
| 20 | @Override
|
| 21 | protected ItemStack execute(final BlockSource source, final ItemStack dispensed) {
|
| 22 | ServerLevel level = source.level();
|
| 23 | if (!level.isClientSide()) {
|
| 24 | BlockPos pos = source.pos().relative(source.state().getValue(DispenserBlock.FACING));
|
| 25 | this.setSuccess(tryShearBeehive(level, dispensed, pos) || tryShearEntity(level, pos, dispensed));
|
| 26 | if (this.isSuccess()) {
|
| 27 | dispensed.hurtAndBreak(1, level, null, item -> {});
|
| 28 | }
|
| 29 | }
|
| 30 |
|
| 31 | return dispensed;
|
| 32 | }
|
| 33 |
|
| 34 | private static boolean tryShearBeehive(final ServerLevel level, final ItemStack tool, final BlockPos pos) {
|
| 35 | BlockState state = level.getBlockState(pos);
|
| 36 | if (state.is(BlockTags.BEEHIVES, s -> s.hasProperty(BeehiveBlock.HONEY_LEVEL) && s.getBlock() instanceof BeehiveBlock)) {
|
| 37 | int honeyLevel = state.getValue(BeehiveBlock.HONEY_LEVEL);
|
| 38 | if (honeyLevel >= 5) {
|
| 39 | level.playSound(null, pos, SoundEvents.BEEHIVE_SHEAR, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 40 | BeehiveBlock.dropHoneycomb(level, tool, state, level.getBlockEntity(pos), null, pos);
|
| 41 | ((BeehiveBlock)state.getBlock()).releaseBeesAndResetHoneyLevel(level, state, pos, null, BeehiveBlockEntity.BeeReleaseStatus.BEE_RELEASED);
|
| 42 | level.gameEvent(null, GameEvent.SHEAR, pos);
|
| 43 | return true;
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | return false;
|
| 48 | }
|
| 49 |
|
| 50 | private static boolean tryShearEntity(final ServerLevel level, final BlockPos pos, final ItemStack tool) {
|
| 51 | for (Entity entity : level.getEntitiesOfClass(Entity.class, new AABB(pos), EntitySelector.NO_SPECTATORS)) {
|
| 52 | if (entity.shearOffAllLeashConnections(null)) {
|
| 53 | return true;
|
| 54 | }
|
| 55 |
|
| 56 | if (entity.isAlive() && entity instanceof Shearable shearable && shearable.readyForShearing()) {
|
| 57 | shearable.shear(level, SoundSource.BLOCKS, tool);
|
| 58 | level.gameEvent(null, GameEvent.SHEAR, pos);
|
| 59 | return true;
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | return false;
|
| 64 | }
|
| 65 | }
|
| 66 |
|