HangingEntityItem .java

net.minecraft.world.item.HangingEntityItem (class): members: TOOLTIP_RANDOM_VARIANT, appendHoverText, mayPlace, useOn, builder, type. imports: Painting.

net.minecraft.world.itemclass99 linescommonextends Item3.7 KB

API Surface (3)

InteractionResult useOn(final UseOnContext context)
boolean mayPlace(final Player player, final Direction direction, final ItemStack itemStack, final BlockPos blockPos)
void appendHoverText(final ItemStack itemStack, final Item.TooltipContext context, final TooltipDisplay display, final Consumer<Component> builder, final TooltipFlag tooltipFlag)

Fields & Constants (3)

TOOLTIP_RANDOM_VARIANT
type
builder

String literals (2)

"painting.random"
"painting.dimensions"

Full Source

HangingEntityItem.java99 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item;
2
3
import java.util.Optional;
4
import java.util.function.Consumer;
5
import net.minecraft.ChatFormatting;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.Direction;
8
import net.minecraft.core.Holder;
9
import net.minecraft.core.component.DataComponents;
10
import net.minecraft.network.chat.Component;
11
import net.minecraft.world.InteractionResult;
12
import net.minecraft.world.entity.EntityType;
13
import net.minecraft.world.entity.EntityTypes;
14
import net.minecraft.world.entity.decoration.GlowItemFrame;
15
import net.minecraft.world.entity.decoration.HangingEntity;
16
import net.minecraft.world.entity.decoration.ItemFrame;
17
import net.minecraft.world.entity.decoration.painting.Painting;
18
import net.minecraft.world.entity.decoration.painting.PaintingVariant;
19
import net.minecraft.world.entity.player.Player;
20
import net.minecraft.world.item.component.TooltipDisplay;
21
import net.minecraft.world.item.context.UseOnContext;
22
import net.minecraft.world.level.Level;
23
import net.minecraft.world.level.gameevent.GameEvent;
24
25
public class HangingEntityItem extends Item {
26
	private static final Component TOOLTIP_RANDOM_VARIANT = Component.translatable("painting.random").withStyle(ChatFormatting.GRAY);
27
	private final EntityType<? extends HangingEntity> type;
28
29
	public HangingEntityItem(final EntityType<? extends HangingEntity> type, final Item.Properties properties) {
30
		super(properties);
31
		this.type = type;
32
	}
33
34
	@Override
35
	public InteractionResult useOn(final UseOnContext context) {
36
		BlockPos pos = context.getClickedPos();
37
		Direction clickedFace = context.getClickedFace();
38
		BlockPos blockPos = pos.relative(clickedFace);
39
		Player player = context.getPlayer();
40
		ItemStack itemInHand = context.getItemInHand();
41
		if (player != null && !this.mayPlace(player, clickedFace, itemInHand, blockPos)) {
42
			return InteractionResult.FAIL;
43
		}
44
45
		Level level = context.getLevel();
46
		HangingEntity entity;
47
		if (this.type == EntityTypes.PAINTING) {
48
			Optional<Painting> painting = Painting.create(level, blockPos, clickedFace);
49
			if (painting.isEmpty()) {
50
				return InteractionResult.CONSUME;
51
			}
52
53
			entity = painting.get();
54
		} else if (this.type == EntityTypes.ITEM_FRAME) {
55
			entity = new ItemFrame(level, blockPos, clickedFace);
56
		} else {
57
			if (this.type != EntityTypes.GLOW_ITEM_FRAME) {
58
				return InteractionResult.SUCCESS;
59
			}
60
61
			entity = new GlowItemFrame(level, blockPos, clickedFace);
62
		}
63
64
		EntityType.<HangingEntity>createDefaultStackConfig(level, itemInHand, player).apply(entity);
65
		if (entity.survives()) {
66
			if (!level.isClientSide()) {
67
				entity.playPlacementSound();
68
				level.gameEvent(player, GameEvent.ENTITY_PLACE, entity.position());
69
				level.addFreshEntity(entity);
70
			}
71
72
			itemInHand.shrink(1);
73
			return InteractionResult.SUCCESS;
74
		} else {
75
			return InteractionResult.CONSUME;
76
		}
77
	}
78
79
	protected boolean mayPlace(final Player player, final Direction direction, final ItemStack itemStack, final BlockPos blockPos) {
80
		return !direction.getAxis().isVertical() && player.mayUseItemAt(blockPos, direction, itemStack);
81
	}
82
83
	@Override
84
	public void appendHoverText(
85
		final ItemStack itemStack, final Item.TooltipContext context, final TooltipDisplay display, final Consumer<Component> builder, final TooltipFlag tooltipFlag
86
	) {
87
		if (this.type == EntityTypes.PAINTING && display.shows(DataComponents.PAINTING_VARIANT)) {
88
			Holder<PaintingVariant> variant = itemStack.get(DataComponents.PAINTING_VARIANT);
89
			if (variant != null) {
90
				variant.value().title().ifPresent(builder);
91
				variant.value().author().ifPresent(builder);
92
				builder.accept(Component.translatable("painting.dimensions", variant.value().width(), variant.value().height()));
93
			} else if (tooltipFlag.isCreative()) {
94
				builder.accept(TOOLTIP_RANDOM_VARIANT);
95
			}
96
		}
97
	}
98
}
99

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