InstrumentItem .java

net.minecraft.world.item.InstrumentItem (class): members: getUseAnimation, getUseDuration, use, create. imports: InstrumentComponent, Stats, GameEvent.

net.minecraft.world.itemclass77 linescommonextends Item2.9 KB

API Surface (4)

ItemStack create(final Item item, final Holder<Instrument> instrument)
InteractionResult use(final Level level, final Player player, final InteractionHand hand)
int getUseDuration(final ItemStack itemStack, final LivingEntity user)
ItemUseAnimation getUseAnimation(final ItemStack itemStack)

Full Source

InstrumentItem.java77 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item;
2
3
import java.util.Optional;
4
import net.minecraft.core.Holder;
5
import net.minecraft.core.component.DataComponents;
6
import net.minecraft.sounds.SoundEvent;
7
import net.minecraft.sounds.SoundSource;
8
import net.minecraft.stats.Stats;
9
import net.minecraft.util.Mth;
10
import net.minecraft.world.InteractionHand;
11
import net.minecraft.world.InteractionResult;
12
import net.minecraft.world.entity.LivingEntity;
13
import net.minecraft.world.entity.player.Player;
14
import net.minecraft.world.item.component.InstrumentComponent;
15
import net.minecraft.world.level.Level;
16
import net.minecraft.world.level.gameevent.GameEvent;
17
18
public class InstrumentItem extends Item {
19
	public InstrumentItem(final Item.Properties properties) {
20
		super(properties);
21
	}
22
23
	public static ItemStack create(final Item item, final Holder<Instrument> instrument) {
24
		ItemStack itemStack = new ItemStack(item);
25
		itemStack.set(DataComponents.INSTRUMENT, new InstrumentComponent(instrument));
26
		return itemStack;
27
	}
28
29
	@Override
30
	public InteractionResult use(final Level level, final Player player, final InteractionHand hand) {
31
		ItemStack itemStack = player.getItemInHand(hand);
32
		Optional<? extends Holder<Instrument>> instrumentHolder = getInstrument(itemStack);
33
		if (instrumentHolder.isPresent()) {
34
			Instrument instrument = instrumentHolder.get().value();
35
			player.startUsingItem(hand);
36
			play(level, player, instrument);
37
			int durabilityDamage = instrument.durabilityDamage();
38
			if (durabilityDamage > 0) {
39
				itemStack.hurtAndBreak(durabilityDamage, player, hand.asEquipmentSlot());
40
			}
41
42
			float instrumentCooldown = instrument.useDuration();
43
			if (instrumentCooldown > 0.0F) {
44
				player.getCooldowns().addCooldown(itemStack, Mth.floor(instrumentCooldown * 20.0F));
45
			}
46
47
			player.awardStat(Stats.ITEM_USED.get(this));
48
			return InteractionResult.CONSUME;
49
		} else {
50
			return InteractionResult.FAIL;
51
		}
52
	}
53
54
	@Override
55
	public int getUseDuration(final ItemStack itemStack, final LivingEntity user) {
56
		Optional<Holder<Instrument>> instrument = getInstrument(itemStack);
57
		return instrument.<Integer>map(instrumentHolder -> Mth.floor(instrumentHolder.value().useDuration() * 20.0F)).orElse(0);
58
	}
59
60
	private static Optional<Holder<Instrument>> getInstrument(final ItemStack itemStack) {
61
		InstrumentComponent instrument = itemStack.get(DataComponents.INSTRUMENT);
62
		return instrument != null ? Optional.of(instrument.instrument()) : Optional.empty();
63
	}
64
65
	@Override
66
	public ItemUseAnimation getUseAnimation(final ItemStack itemStack) {
67
		return ItemUseAnimation.TOOT_HORN;
68
	}
69
70
	private static void play(final Level level, final Player player, final Instrument instrument) {
71
		SoundEvent soundEvent = instrument.soundEvent().value();
72
		float volume = instrument.range() / 16.0F;
73
		level.playSound(player, player, soundEvent, SoundSource.RECORDS, volume, 1.0F);
74
		level.gameEvent(GameEvent.INSTRUMENT_PLAY, player.position(), GameEvent.Context.of(player));
75
	}
76
}
77

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