2025-11-24 22:52:51 +03:00

98 lines
4.2 KiB
Java

/*
* Decompiled with CFR 0.152.
*/
package net.minecraft.world.item;
import java.util.Optional;
import java.util.function.Consumer;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.decoration.GlowItemFrame;
import net.minecraft.world.entity.decoration.HangingEntity;
import net.minecraft.world.entity.decoration.ItemFrame;
import net.minecraft.world.entity.decoration.Painting;
import net.minecraft.world.entity.decoration.PaintingVariant;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.component.TooltipDisplay;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.gameevent.GameEvent;
public class HangingEntityItem
extends Item {
private static final Component TOOLTIP_RANDOM_VARIANT = Component.translatable("painting.random").withStyle(ChatFormatting.GRAY);
private final EntityType<? extends HangingEntity> type;
public HangingEntityItem(EntityType<? extends HangingEntity> type, Item.Properties properties) {
super(properties);
this.type = type;
}
@Override
public InteractionResult useOn(UseOnContext context) {
HangingEntity entity;
BlockPos pos = context.getClickedPos();
Direction clickedFace = context.getClickedFace();
BlockPos blockPos = pos.relative(clickedFace);
Player player = context.getPlayer();
ItemStack itemInHand = context.getItemInHand();
if (player != null && !this.mayPlace(player, clickedFace, itemInHand, blockPos)) {
return InteractionResult.FAIL;
}
Level level = context.getLevel();
if (this.type == EntityType.PAINTING) {
Optional<Painting> painting = Painting.create(level, blockPos, clickedFace);
if (painting.isEmpty()) {
return InteractionResult.CONSUME;
}
entity = painting.get();
} else if (this.type == EntityType.ITEM_FRAME) {
entity = new ItemFrame(level, blockPos, clickedFace);
} else if (this.type == EntityType.GLOW_ITEM_FRAME) {
entity = new GlowItemFrame(level, blockPos, clickedFace);
} else {
return InteractionResult.SUCCESS;
}
EntityType.createDefaultStackConfig(level, itemInHand, player).accept(entity);
if (entity.survives()) {
if (!level.isClientSide()) {
entity.playPlacementSound();
level.gameEvent((Entity)player, GameEvent.ENTITY_PLACE, entity.position());
level.addFreshEntity(entity);
}
itemInHand.shrink(1);
return InteractionResult.SUCCESS;
}
return InteractionResult.CONSUME;
}
protected boolean mayPlace(Player player, Direction direction, ItemStack itemStack, BlockPos blockPos) {
return !direction.getAxis().isVertical() && player.mayUseItemAt(blockPos, direction, itemStack);
}
@Override
public void appendHoverText(ItemStack itemStack, Item.TooltipContext context, TooltipDisplay display, Consumer<Component> builder, TooltipFlag tooltipFlag) {
if (this.type == EntityType.PAINTING && display.shows(DataComponents.PAINTING_VARIANT)) {
Holder<PaintingVariant> variant = itemStack.get(DataComponents.PAINTING_VARIANT);
if (variant != null) {
variant.value().title().ifPresent(builder);
variant.value().author().ifPresent(builder);
builder.accept(Component.translatable("painting.dimensions", variant.value().width(), variant.value().height()));
} else if (tooltipFlag.isCreative()) {
builder.accept(TOOLTIP_RANDOM_VARIANT);
}
}
}
}