| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.sounds.SoundEvents;
|
| 7 | import net.minecraft.sounds.SoundSource;
|
| 8 | import net.minecraft.tags.BlockTags;
|
| 9 | import net.minecraft.world.InteractionResult;
|
| 10 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 11 | import net.minecraft.world.entity.EntityType;
|
| 12 | import net.minecraft.world.entity.EntityTypes;
|
| 13 | import net.minecraft.world.entity.PostSpawnProcessor;
|
| 14 | import net.minecraft.world.entity.decoration.Cushion;
|
| 15 | import net.minecraft.world.entity.player.Player;
|
| 16 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 17 | import net.minecraft.world.item.context.UseOnContext;
|
| 18 | import net.minecraft.world.level.Level;
|
| 19 | import net.minecraft.world.level.block.state.BlockState;
|
| 20 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 21 | import net.minecraft.world.phys.AABB;
|
| 22 | import net.minecraft.world.phys.BlockHitResult;
|
| 23 | import net.minecraft.world.phys.Vec3;
|
| 24 |
|
| 25 | public class CushionItem extends Item {
|
| 26 | private static final double COLLISION_SHAPE_RAYCAST_EPSILON = 0.001;
|
| 27 |
|
| 28 | public CushionItem(final Item.Properties properties) {
|
| 29 | super(properties);
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public InteractionResult useOn(final UseOnContext context) {
|
| 34 | UseOnContext recalculatedContext = recalculateContextForSpecialCollisionShapes(context);
|
| 35 | Level level = context.getLevel();
|
| 36 | Direction clickedFace = recalculatedContext.getClickedFace();
|
| 37 | if (clickedFace != Direction.UP) {
|
| 38 | return InteractionResult.FAIL;
|
| 39 | }
|
| 40 |
|
| 41 | BlockPlaceContext placeContext = new BlockPlaceContext(recalculatedContext);
|
| 42 | BlockPos blockPos = placeContext.getClickedPos();
|
| 43 | Vec3 entityPos = Vec3.atCenterOfWithY(blockPos, recalculatedContext.getClickLocation().y);
|
| 44 | AABB spawnAABB = EntityTypes.CUSHION.getSpawnAABB(entityPos);
|
| 45 | if (!Cushion.canBePlacedAt(level, spawnAABB)) {
|
| 46 | return InteractionResult.FAIL;
|
| 47 | }
|
| 48 |
|
| 49 | ItemStack itemStack = context.getItemInHand();
|
| 50 | if (level instanceof ServerLevel serverLevel) {
|
| 51 | if (!serverLevel.getEntitiesOfClass(Cushion.class, spawnAABB).isEmpty()) {
|
| 52 | return InteractionResult.FAIL;
|
| 53 | }
|
| 54 |
|
| 55 | PostSpawnProcessor<Cushion> entityConfig = EntityType.createDefaultStackConfig(serverLevel, itemStack, context.getPlayer());
|
| 56 | Cushion cushion = EntityTypes.CUSHION.create(serverLevel, entityConfig, blockPos, EntitySpawnReason.SPAWN_ITEM_USE, true, true);
|
| 57 | if (cushion == null) {
|
| 58 | return InteractionResult.FAIL;
|
| 59 | }
|
| 60 |
|
| 61 | cushion.snapTo(entityPos, Direction.fromYRot(placeContext.getRotation()).toYRot(), 0.0F);
|
| 62 | serverLevel.addFreshEntity(cushion);
|
| 63 | cushion.destroyIfInFire(serverLevel);
|
| 64 | level.playSound(null, cushion.getX(), cushion.getY(), cushion.getZ(), SoundEvents.CUSHION_PLACE, SoundSource.BLOCKS, 0.75F, 0.8F);
|
| 65 | cushion.gameEvent(GameEvent.ENTITY_PLACE, context.getPlayer());
|
| 66 | itemStack.consume(1, placeContext.getPlayer());
|
| 67 | }
|
| 68 |
|
| 69 | return InteractionResult.SUCCESS;
|
| 70 | }
|
| 71 |
|
| 72 | private static UseOnContext recalculateContextForSpecialCollisionShapes(final UseOnContext context) {
|
| 73 | Player player = context.getPlayer();
|
| 74 | if (player == null) {
|
| 75 | return context;
|
| 76 | }
|
| 77 |
|
| 78 | Level level = context.getLevel();
|
| 79 | BlockPos clickedPos = context.getClickedPos();
|
| 80 | BlockState clickedState = level.getBlockState(clickedPos);
|
| 81 | if (!clickedState.is(BlockTags.CUSHION_USES_COLLISION_SHAPE)) {
|
| 82 | return context;
|
| 83 | }
|
| 84 |
|
| 85 | Vec3 rayFrom = player.getEyePosition();
|
| 86 | Vec3 ray = context.getClickLocation().subtract(rayFrom);
|
| 87 | Vec3 rayTo = context.getClickLocation().add(ray.normalize().scale(0.001));
|
| 88 | BlockHitResult collisionHitResult = clickedState.getCollisionShape(level, clickedPos).clip(rayFrom, rayTo, clickedPos);
|
| 89 | return collisionHitResult == null ? context : new UseOnContext(player, context.getHand(), collisionHitResult);
|
| 90 | }
|
| 91 | }
|
| 92 |
|