63 lines
2.2 KiB
Java
63 lines
2.2 KiB
Java
package net.minecraft.world.item;
|
|
|
|
import net.minecraft.nbt.CompoundTag;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.entity.player.Player;
|
|
import net.minecraft.core.Direction;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.world.entity.Entity;
|
|
import net.minecraft.world.entity.decoration.ItemFrame;
|
|
import net.minecraft.world.entity.decoration.Painting;
|
|
import net.minecraft.world.InteractionResult;
|
|
import net.minecraft.world.entity.decoration.HangingEntity;
|
|
import net.minecraft.world.entity.EntityType;
|
|
|
|
public class HangingEntityItem extends Item {
|
|
private final EntityType<? extends HangingEntity> type;
|
|
|
|
public HangingEntityItem(final EntityType<? extends HangingEntity> akr, final Properties a) {
|
|
super(a);
|
|
this.type = akr;
|
|
}
|
|
|
|
@Override
|
|
public InteractionResult useOn(final UseOnContext bfw) {
|
|
final BlockPos fk3 = bfw.getClickedPos();
|
|
final Direction fp4 = bfw.getClickedFace();
|
|
final BlockPos fk4 = fk3.relative(fp4);
|
|
final Player ayg6 = bfw.getPlayer();
|
|
final ItemStack bek7 = bfw.getItemInHand();
|
|
if (ayg6 != null && !this.mayPlace(ayg6, fp4, bek7, fk4)) {
|
|
return InteractionResult.FAIL;
|
|
}
|
|
final Level bjt8 = bfw.getLevel();
|
|
HangingEntity avn9;
|
|
if (this.type == EntityType.PAINTING) {
|
|
avn9 = new Painting(bjt8, fk4, fp4);
|
|
}
|
|
else {
|
|
if (this.type != EntityType.ITEM_FRAME) {
|
|
return InteractionResult.SUCCESS;
|
|
}
|
|
avn9 = new ItemFrame(bjt8, fk4, fp4);
|
|
}
|
|
final CompoundTag jt10 = bek7.getTag();
|
|
if (jt10 != null) {
|
|
EntityType.updateCustomEntityTag(bjt8, ayg6, avn9, jt10);
|
|
}
|
|
if (avn9.survives()) {
|
|
if (!bjt8.isClientSide) {
|
|
avn9.playPlacementSound();
|
|
bjt8.addFreshEntity(avn9);
|
|
}
|
|
bek7.shrink(1);
|
|
return InteractionResult.SUCCESS;
|
|
}
|
|
return InteractionResult.CONSUME;
|
|
}
|
|
|
|
protected boolean mayPlace(final Player ayg, final Direction fp, final ItemStack bek, final BlockPos fk) {
|
|
return !fp.getAxis().isVertical() && ayg.mayUseItemAt(fk, fp, bek);
|
|
}
|
|
}
|