BoatItem .java

net.minecraft.world.item.BoatItem (class, 89 lines): members: entityType, use. imports: AbstractBoat, ClipContext, HitResult, EntitySelector, Stats.

net.minecraft.world.itemclass89 linescommonextends Item3.0 KB

API Surface (1)

InteractionResult use(final Level level, final Player player, final InteractionHand hand)

Fields & Constants (1)

entityType

Full Source

BoatItem.java89 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item;
2
3
import java.util.List;
4
import net.minecraft.server.level.ServerLevel;
5
import net.minecraft.stats.Stats;
6
import net.minecraft.world.InteractionHand;
7
import net.minecraft.world.InteractionResult;
8
import net.minecraft.world.entity.Entity;
9
import net.minecraft.world.entity.EntitySelector;
10
import net.minecraft.world.entity.EntitySpawnReason;
11
import net.minecraft.world.entity.EntityType;
12
import net.minecraft.world.entity.player.Player;
13
import net.minecraft.world.entity.vehicle.boat.AbstractBoat;
14
import net.minecraft.world.level.ClipContext;
15
import net.minecraft.world.level.Level;
16
import net.minecraft.world.level.gameevent.GameEvent;
17
import net.minecraft.world.phys.AABB;
18
import net.minecraft.world.phys.HitResult;
19
import net.minecraft.world.phys.Vec3;
20
import org.jspecify.annotations.Nullable;
21
22
public class BoatItem extends Item {
23
	private final EntityType<? extends AbstractBoat> entityType;
24
25
	public BoatItem(final EntityType<? extends AbstractBoat> entityType, final Item.Properties properties) {
26
		super(properties);
27
		this.entityType = entityType;
28
	}
29
30
	@Override
31
	public InteractionResult use(final Level level, final Player player, final InteractionHand hand) {
32
		ItemStack itemStack = player.getItemInHand(hand);
33
		HitResult hitResult = getPlayerPOVHitResult(level, player, ClipContext.Fluid.ANY);
34
		if (hitResult.getType() == HitResult.Type.MISS) {
35
			return InteractionResult.PASS;
36
		}
37
38
		Vec3 viewVector = player.getViewVector(1.0F);
39
		double range = 5.0;
40
		List<Entity> entities = level.getEntities(player, player.getBoundingBox().expandTowards(viewVector.scale(5.0)).inflate(1.0), EntitySelector.CAN_BE_PICKED);
41
		if (!entities.isEmpty()) {
42
			Vec3 from = player.getEyePosition();
43
44
			for (Entity entity : entities) {
45
				AABB bb = entity.getBoundingBox().inflate(entity.getPickRadius());
46
				if (bb.contains(from)) {
47
					return InteractionResult.PASS;
48
				}
49
			}
50
		}
51
52
		if (hitResult.getType() == HitResult.Type.BLOCK) {
53
			AbstractBoat boat = this.getBoat(level, hitResult, itemStack, player);
54
			if (boat == null) {
55
				return InteractionResult.FAIL;
56
			}
57
58
			boat.setYRot(player.getYRot());
59
			if (!level.noCollision(boat, boat.getBoundingBox())) {
60
				return InteractionResult.FAIL;
61
			}
62
63
			if (!level.isClientSide()) {
64
				level.addFreshEntity(boat);
65
				level.gameEvent(player, GameEvent.ENTITY_PLACE, hitResult.getLocation());
66
				itemStack.consume(1, player);
67
			}
68
69
			player.awardStat(Stats.ITEM_USED.get(this));
70
			return InteractionResult.SUCCESS;
71
		} else {
72
			return InteractionResult.PASS;
73
		}
74
	}
75
76
	private @Nullable AbstractBoat getBoat(final Level level, final HitResult hitResult, final ItemStack itemStack, final Player player) {
77
		AbstractBoat boat = this.entityType.create(level, EntitySpawnReason.SPAWN_ITEM_USE);
78
		if (boat != null) {
79
			Vec3 location = hitResult.getLocation();
80
			boat.setInitialPos(location.x, location.y, location.z);
81
			if (level instanceof ServerLevel serverLevel) {
82
				EntityType.<AbstractBoat>createDefaultStackConfig(serverLevel, itemStack, player).apply(boat);
83
			}
84
		}
85
86
		return boat;
87
	}
88
}
89

Decompiled Minecraft 26.3 source of BoatItem (net/minecraft/world/item/BoatItem.java) with Mojang official mappings, stored verbatim on the site.