ArmorStandItem .java

net.minecraft.world.item.ArmorStandItem (class, 62 lines): members: useOn. imports: PostSpawnProcessor, ArmorStand, UseOnContext, BlockPlaceContext.

net.minecraft.world.itemclass62 linescommonextends Item2.5 KB

API Surface (1)

InteractionResult useOn(final UseOnContext context)

Full Source

ArmorStandItem.java62 lines · Minecraft 26.3 · verbatim
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.util.Mth;
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.ArmorStand;
15
import net.minecraft.world.item.context.BlockPlaceContext;
16
import net.minecraft.world.item.context.UseOnContext;
17
import net.minecraft.world.level.Level;
18
import net.minecraft.world.level.gameevent.GameEvent;
19
import net.minecraft.world.phys.AABB;
20
import net.minecraft.world.phys.Vec3;
21
22
public class ArmorStandItem extends Item {
23
	public ArmorStandItem(final Item.Properties properties) {
24
		super(properties);
25
	}
26
27
	@Override
28
	public InteractionResult useOn(final UseOnContext context) {
29
		Direction clickedFace = context.getClickedFace();
30
		if (clickedFace == Direction.DOWN) {
31
			return InteractionResult.FAIL;
32
		}
33
34
		Level level = context.getLevel();
35
		BlockPlaceContext placeContext = new BlockPlaceContext(context);
36
		BlockPos blockPos = placeContext.getClickedPos();
37
		ItemStack itemStack = context.getItemInHand();
38
		Vec3 pos = Vec3.atBottomCenterOf(blockPos);
39
		AABB box = EntityTypes.ARMOR_STAND.getDimensions().makeBoundingBox(pos.x(), pos.y(), pos.z());
40
		if (level.noCollision(null, box) && level.getEntities(null, box).isEmpty()) {
41
			if (level instanceof ServerLevel serverLevel) {
42
				PostSpawnProcessor<ArmorStand> entityConfig = EntityType.createDefaultStackConfig(serverLevel, itemStack, context.getPlayer());
43
				ArmorStand entity = EntityTypes.ARMOR_STAND.create(serverLevel, entityConfig, blockPos, EntitySpawnReason.SPAWN_ITEM_USE, true, true);
44
				if (entity == null) {
45
					return InteractionResult.FAIL;
46
				}
47
48
				float yRot = Mth.floor((Mth.wrapDegrees(context.getRotation() - 180.0F) + 22.5F) / 45.0F) * 45.0F;
49
				entity.snapTo(entity.getX(), entity.getY(), entity.getZ(), yRot, 0.0F);
50
				serverLevel.addFreshEntityWithPassengers(entity);
51
				level.playSound(null, entity.getX(), entity.getY(), entity.getZ(), SoundEvents.ARMOR_STAND_PLACE, SoundSource.BLOCKS, 0.75F, 0.8F);
52
				entity.gameEvent(GameEvent.ENTITY_PLACE, context.getPlayer());
53
			}
54
55
			itemStack.shrink(1);
56
			return InteractionResult.SUCCESS;
57
		} else {
58
			return InteractionResult.FAIL;
59
		}
60
	}
61
}
62

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