| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.core.particles.BlockParticleOption;
|
| 6 | import net.minecraft.core.particles.ParticleTypes;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.sounds.SoundEvent;
|
| 9 | import net.minecraft.sounds.SoundEvents;
|
| 10 | import net.minecraft.sounds.SoundSource;
|
| 11 | import net.minecraft.world.InteractionHand;
|
| 12 | import net.minecraft.world.InteractionResult;
|
| 13 | import net.minecraft.world.entity.EntitySelector;
|
| 14 | import net.minecraft.world.entity.EquipmentSlot;
|
| 15 | import net.minecraft.world.entity.HumanoidArm;
|
| 16 | import net.minecraft.world.entity.LivingEntity;
|
| 17 | import net.minecraft.world.entity.player.Player;
|
| 18 | import net.minecraft.world.entity.projectile.ProjectileUtil;
|
| 19 | import net.minecraft.world.item.context.UseOnContext;
|
| 20 | import net.minecraft.world.level.Level;
|
| 21 | import net.minecraft.world.level.block.BrushableBlock;
|
| 22 | import net.minecraft.world.level.block.RenderShape;
|
| 23 | import net.minecraft.world.level.block.entity.BrushableBlockEntity;
|
| 24 | import net.minecraft.world.level.block.state.BlockState;
|
| 25 | import net.minecraft.world.phys.BlockHitResult;
|
| 26 | import net.minecraft.world.phys.HitResult;
|
| 27 | import net.minecraft.world.phys.Vec3;
|
| 28 |
|
| 29 | public class BrushItem extends Item {
|
| 30 | public static final int ANIMATION_DURATION = 10;
|
| 31 | private static final int USE_DURATION = 200;
|
| 32 |
|
| 33 | public BrushItem(final Item.Properties properties) {
|
| 34 | super(properties);
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public InteractionResult useOn(final UseOnContext context) {
|
| 39 | Player player = context.getPlayer();
|
| 40 | if (player != null && this.calculateHitResult(player).getType() == HitResult.Type.BLOCK) {
|
| 41 | player.startUsingItem(context.getHand());
|
| 42 | }
|
| 43 |
|
| 44 | return InteractionResult.CONSUME;
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public ItemUseAnimation getUseAnimation(final ItemStack itemStack) {
|
| 49 | return ItemUseAnimation.BRUSH;
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public int getUseDuration(final ItemStack itemStack, final LivingEntity user) {
|
| 54 | return 200;
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public void onUseTick(final Level level, final LivingEntity livingEntity, final ItemStack itemStack, final int ticksRemaining) {
|
| 59 | if (ticksRemaining >= 0 && livingEntity instanceof Player player) {
|
| 60 | HitResult hitResult = this.calculateHitResult(player);
|
| 61 | if (hitResult instanceof BlockHitResult blockHitResult && hitResult.getType() == HitResult.Type.BLOCK) {
|
| 62 | int timeElapsed = this.getUseDuration(itemStack, livingEntity) - ticksRemaining + 1;
|
| 63 | boolean isLastTickBeforeBackswing = timeElapsed % 10 == 5;
|
| 64 | if (isLastTickBeforeBackswing) {
|
| 65 | BlockPos pos = blockHitResult.getBlockPos();
|
| 66 | BlockState state = level.getBlockState(pos);
|
| 67 | HumanoidArm brushingArm = livingEntity.getUsedItemHand() == InteractionHand.MAIN_HAND ? player.getMainArm() : player.getMainArm().getOpposite();
|
| 68 | if (state.shouldSpawnTerrainParticles() && state.getRenderShape() != RenderShape.INVISIBLE) {
|
| 69 | this.spawnDustParticles(level, blockHitResult, state, livingEntity.getViewVector(0.0F), brushingArm);
|
| 70 | }
|
| 71 |
|
| 72 | SoundEvent brushSound;
|
| 73 | if (state.getBlock() instanceof BrushableBlock brushableBlock) {
|
| 74 | brushSound = brushableBlock.getBrushSound();
|
| 75 | } else {
|
| 76 | brushSound = SoundEvents.BRUSH_GENERIC;
|
| 77 | }
|
| 78 |
|
| 79 | level.playSound(player, pos, brushSound, SoundSource.BLOCKS);
|
| 80 | if (level instanceof ServerLevel serverLevel && level.getBlockEntity(pos) instanceof BrushableBlockEntity brushableBlockEntity) {
|
| 81 | boolean brushingUpdatedState = brushableBlockEntity.brush(level.getGameTime(), serverLevel, player, blockHitResult.getDirection(), itemStack);
|
| 82 | if (brushingUpdatedState) {
|
| 83 | EquipmentSlot equippedHand = itemStack.equals(player.getItemBySlot(EquipmentSlot.OFFHAND)) ? EquipmentSlot.OFFHAND : EquipmentSlot.MAINHAND;
|
| 84 | itemStack.hurtAndBreak(1, player, equippedHand);
|
| 85 | }
|
| 86 | }
|
| 87 | }
|
| 88 | } else {
|
| 89 | livingEntity.releaseUsingItem();
|
| 90 | }
|
| 91 | } else {
|
| 92 | livingEntity.releaseUsingItem();
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | private HitResult calculateHitResult(final Player player) {
|
| 97 | return ProjectileUtil.getHitResultOnViewVector(player, EntitySelector.CAN_BE_PICKED, player.blockInteractionRange());
|
| 98 | }
|
| 99 |
|
| 100 | private void spawnDustParticles(
|
| 101 | final Level level, final BlockHitResult hitResult, final BlockState state, final Vec3 viewVector, final HumanoidArm brushingArm
|
| 102 | ) {
|
| 103 | double deltaScale = 3.0;
|
| 104 | int flip = brushingArm == HumanoidArm.RIGHT ? 1 : -1;
|
| 105 | int particles = level.getRandom().nextInt(7, 12);
|
| 106 | BlockParticleOption particle = new BlockParticleOption(ParticleTypes.BLOCK, state);
|
| 107 | Direction hitDirection = hitResult.getDirection();
|
| 108 | BrushItem.DustParticlesDelta dustParticlesDelta = BrushItem.DustParticlesDelta.fromDirection(viewVector, hitDirection);
|
| 109 | Vec3 hitLocation = hitResult.getLocation();
|
| 110 |
|
| 111 | for (int i = 0; i < particles; i++) {
|
| 112 | level.addParticle(
|
| 113 | particle,
|
| 114 | hitLocation.x - (hitDirection == Direction.WEST ? 1.0E-6F : 0.0F),
|
| 115 | hitLocation.y,
|
| 116 | hitLocation.z - (hitDirection == Direction.NORTH ? 1.0E-6F : 0.0F),
|
| 117 | dustParticlesDelta.xd() * flip * 3.0 * level.getRandom().nextDouble(),
|
| 118 | 0.0,
|
| 119 | dustParticlesDelta.zd() * flip * 3.0 * level.getRandom().nextDouble()
|
| 120 | );
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | private record DustParticlesDelta(double xd, double yd, double zd) {
|
| 125 | private static final double ALONG_SIDE_DELTA = 1.0;
|
| 126 | private static final double OUT_FROM_SIDE_DELTA = 0.1;
|
| 127 |
|
| 128 | public static BrushItem.DustParticlesDelta fromDirection(final Vec3 viewVector, final Direction hitDirection) {
|
| 129 | double yd = 0.0;
|
| 130 |
|
| 131 | return switch (hitDirection) {
|
| 132 | case DOWN, UP -> new BrushItem.DustParticlesDelta(viewVector.z(), 0.0, -viewVector.x());
|
| 133 | case NORTH -> new BrushItem.DustParticlesDelta(1.0, 0.0, -0.1);
|
| 134 | case SOUTH -> new BrushItem.DustParticlesDelta(-1.0, 0.0, 0.1);
|
| 135 | case WEST -> new BrushItem.DustParticlesDelta(-0.1, 0.0, -1.0);
|
| 136 | case EAST -> new BrushItem.DustParticlesDelta(0.1, 0.0, 1.0);
|
| 137 | };
|
| 138 | }
|
| 139 | }
|
| 140 | }
|
| 141 |
|