Consumable .java

net.minecraft.world.item.component.Consumable (record, 178 lines): members: consumeSeconds, animation, sound, hasConsumeParticles, onConsumeEffects, CODEC.

net.minecraft.world.item.componentrecord178 linescommon6.9 KB

API Surface (15)

record Consumable(float consumeSeconds, ItemUseAnimation animation, Holder<SoundEvent> sound, boolean hasConsumeParticles, List<ConsumeEffect> onConsumeEffects)
InteractionResult startConsuming(final LivingEntity user, final ItemStack stack, final InteractionHand hand)
ItemStack onConsume(final Level level, final LivingEntity user, final ItemStack stack)
boolean canConsume(final LivingEntity user, final ItemStack stack)
int consumeTicks()
void emitParticlesAndSounds(final RandomSource random, final LivingEntity user, final ItemStack itemStack, final int particleCount)
boolean shouldEmitParticlesAndSounds(final int useItemRemainingTicks)
Consumable.Builder builder()
Consumable.Builder consumeSeconds(final float consumeSeconds)
Consumable.Builder animation(final ItemUseAnimation animation)
Consumable.Builder sound(final Holder<SoundEvent> sound)
Consumable.Builder soundAfterConsume(final Holder<SoundEvent> soundAfterConsume)
Consumable.Builder hasConsumeParticles(final boolean hasConsumeParticles)
Consumable.Builder onConsume(final ConsumeEffect effect)
Consumable build()

Fields & Constants (5)

DEFAULT_CONSUME_SECONDS
CONSUME_EFFECTS_INTERVAL
CONSUME_EFFECTS_START_FRACTION
CODEC
STREAM_CODEC

String literals (5)

"consume_seconds"
"animation"
"sound"
"has_consume_particles"
"on_consume_effects"

Full Source

Consumable.java178 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item.component;
2
3
import com.mojang.serialization.Codec;
4
import com.mojang.serialization.codecs.RecordCodecBuilder;
5
import java.util.ArrayList;
6
import java.util.List;
7
import net.minecraft.advancements.triggers.CriteriaTriggers;
8
import net.minecraft.core.Holder;
9
import net.minecraft.core.component.DataComponents;
10
import net.minecraft.network.RegistryFriendlyByteBuf;
11
import net.minecraft.network.codec.ByteBufCodecs;
12
import net.minecraft.network.codec.StreamCodec;
13
import net.minecraft.server.level.ServerPlayer;
14
import net.minecraft.sounds.SoundEvent;
15
import net.minecraft.sounds.SoundEvents;
16
import net.minecraft.stats.Stats;
17
import net.minecraft.util.ExtraCodecs;
18
import net.minecraft.util.Mth;
19
import net.minecraft.util.RandomSource;
20
import net.minecraft.world.InteractionHand;
21
import net.minecraft.world.InteractionResult;
22
import net.minecraft.world.entity.LivingEntity;
23
import net.minecraft.world.entity.player.Player;
24
import net.minecraft.world.food.FoodProperties;
25
import net.minecraft.world.item.ItemStack;
26
import net.minecraft.world.item.ItemUseAnimation;
27
import net.minecraft.world.item.consume_effects.ConsumeEffect;
28
import net.minecraft.world.item.consume_effects.PlaySoundConsumeEffect;
29
import net.minecraft.world.level.Level;
30
import net.minecraft.world.level.gameevent.GameEvent;
31
32
public record Consumable(
33
	float consumeSeconds, ItemUseAnimation animation, Holder<SoundEvent> sound, boolean hasConsumeParticles, List<ConsumeEffect> onConsumeEffects
34
) {
35
	public static final float DEFAULT_CONSUME_SECONDS = 1.6F;
36
	private static final int CONSUME_EFFECTS_INTERVAL = 4;
37
	private static final float CONSUME_EFFECTS_START_FRACTION = 0.21875F;
38
	public static final Codec<Consumable> CODEC = RecordCodecBuilder.create(
39
		i -> i.group(
40
				ExtraCodecs.NON_NEGATIVE_FLOAT.optionalFieldOf("consume_seconds", 1.6F).forGetter(Consumable::consumeSeconds),
41
				ItemUseAnimation.CODEC.optionalFieldOf("animation", ItemUseAnimation.EAT).forGetter(Consumable::animation),
42
				SoundEvent.CODEC.optionalFieldOf("sound", SoundEvents.GENERIC_EAT).forGetter(Consumable::sound),
43
				Codec.BOOL.optionalFieldOf("has_consume_particles", true).forGetter(Consumable::hasConsumeParticles),
44
				ConsumeEffect.CODEC.listOf().optionalFieldOf("on_consume_effects", List.of()).forGetter(Consumable::onConsumeEffects)
45
			)
46
			.apply(i, Consumable::new)
47
	);
48
	public static final StreamCodec<RegistryFriendlyByteBuf, Consumable> STREAM_CODEC = StreamCodec.composite(
49
		ByteBufCodecs.FLOAT,
50
		Consumable::consumeSeconds,
51
		ItemUseAnimation.STREAM_CODEC,
52
		Consumable::animation,
53
		SoundEvent.STREAM_CODEC,
54
		Consumable::sound,
55
		ByteBufCodecs.BOOL,
56
		Consumable::hasConsumeParticles,
57
		ConsumeEffect.STREAM_CODEC.apply(ByteBufCodecs.list()),
58
		Consumable::onConsumeEffects,
59
		Consumable::new
60
	);
61
62
	public InteractionResult startConsuming(final LivingEntity user, final ItemStack stack, final InteractionHand hand) {
63
		if (!this.canConsume(user, stack)) {
64
			return InteractionResult.FAIL;
65
		} else {
66
			boolean consumesOverTime = this.consumeTicks() > 0;
67
			if (consumesOverTime) {
68
				user.startUsingItem(hand);
69
				return InteractionResult.CONSUME;
70
			} else {
71
				ItemStack result = this.onConsume(user.level(), user, stack);
72
				return InteractionResult.CONSUME.heldItemTransformedTo(result);
73
			}
74
		}
75
	}
76
77
	public ItemStack onConsume(final Level level, final LivingEntity user, final ItemStack stack) {
78
		RandomSource random = user.getRandom();
79
		this.emitParticlesAndSounds(random, user, stack, 16);
80
		if (user instanceof ServerPlayer serverPlayer) {
81
			serverPlayer.awardStat(Stats.ITEM_USED.get(stack.getItem()));
82
			CriteriaTriggers.CONSUME_ITEM.trigger(serverPlayer, stack);
83
		}
84
85
		stack.getAllOfType(ConsumableListener.class).forEach(component -> component.onConsume(level, user, stack, this));
86
		if (!level.isClientSide()) {
87
			this.onConsumeEffects.forEach(action -> action.apply(level, stack, user));
88
		}
89
90
		user.gameEvent(this.animation == ItemUseAnimation.DRINK ? GameEvent.DRINK : GameEvent.EAT);
91
		stack.consume(1, user);
92
		return stack;
93
	}
94
95
	public boolean canConsume(final LivingEntity user, final ItemStack stack) {
96
		FoodProperties foodProperties = stack.get(DataComponents.FOOD);
97
		return foodProperties != null && user instanceof Player player ? player.canEat(foodProperties.canAlwaysEat()) : true;
98
	}
99
100
	public int consumeTicks() {
101
		return (int)(this.consumeSeconds * 20.0F);
102
	}
103
104
	public void emitParticlesAndSounds(final RandomSource random, final LivingEntity user, final ItemStack itemStack, final int particleCount) {
105
		float eatVolume = random.nextBoolean() ? 0.5F : 1.0F;
106
		float eatPitch = random.triangle(1.0F, 0.2F);
107
		float drinkVolume = 0.5F;
108
		float drinkPitch = Mth.randomBetween(random, 0.9F, 1.0F);
109
		float consumableVolume = this.animation == ItemUseAnimation.DRINK ? 0.5F : eatVolume;
110
		float consumablePitch = this.animation == ItemUseAnimation.DRINK ? drinkPitch : eatPitch;
111
		if (this.hasConsumeParticles) {
112
			user.spawnItemParticles(itemStack, particleCount);
113
		}
114
115
		SoundEvent consumeSound = user instanceof Consumable.OverrideConsumeSound override ? override.getConsumeSound(itemStack) : this.sound.value();
116
		user.playSound(consumeSound, consumableVolume, consumablePitch);
117
	}
118
119
	public boolean shouldEmitParticlesAndSounds(final int useItemRemainingTicks) {
120
		int itemUsedForTicks = this.consumeTicks() - useItemRemainingTicks;
121
		int waitTicksBeforeUseEffects = (int)(this.consumeTicks() * 0.21875F);
122
		boolean isValidTime = itemUsedForTicks > waitTicksBeforeUseEffects;
123
		return isValidTime && useItemRemainingTicks % 4 == 0;
124
	}
125
126
	public static Consumable.Builder builder() {
127
		return new Consumable.Builder();
128
	}
129
130
	public static class Builder {
131
		private float consumeSeconds = 1.6F;
132
		private ItemUseAnimation animation = ItemUseAnimation.EAT;
133
		private Holder<SoundEvent> sound = SoundEvents.GENERIC_EAT;
134
		private boolean hasConsumeParticles = true;
135
		private final List<ConsumeEffect> onConsumeEffects = new ArrayList<>();
136
137
		private Builder() {
138
		}
139
140
		public Consumable.Builder consumeSeconds(final float consumeSeconds) {
141
			this.consumeSeconds = consumeSeconds;
142
			return this;
143
		}
144
145
		public Consumable.Builder animation(final ItemUseAnimation animation) {
146
			this.animation = animation;
147
			return this;
148
		}
149
150
		public Consumable.Builder sound(final Holder<SoundEvent> sound) {
151
			this.sound = sound;
152
			return this;
153
		}
154
155
		public Consumable.Builder soundAfterConsume(final Holder<SoundEvent> soundAfterConsume) {
156
			return this.onConsume(new PlaySoundConsumeEffect(soundAfterConsume));
157
		}
158
159
		public Consumable.Builder hasConsumeParticles(final boolean hasConsumeParticles) {
160
			this.hasConsumeParticles = hasConsumeParticles;
161
			return this;
162
		}
163
164
		public Consumable.Builder onConsume(final ConsumeEffect effect) {
165
			this.onConsumeEffects.add(effect);
166
			return this;
167
		}
168
169
		public Consumable build() {
170
			return new Consumable(this.consumeSeconds, this.animation, this.sound, this.hasConsumeParticles, this.onConsumeEffects);
171
		}
172
	}
173
174
	public interface OverrideConsumeSound {
175
		SoundEvent getConsumeSound(final ItemStack itemStack);
176
	}
177
}
178

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