| 1 | package net.minecraft.world.food;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 6 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 7 | import net.minecraft.network.codec.StreamCodec;
|
| 8 | import net.minecraft.sounds.SoundEvents;
|
| 9 | import net.minecraft.sounds.SoundSource;
|
| 10 | import net.minecraft.util.ExtraCodecs;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.util.RandomSource;
|
| 13 | import net.minecraft.world.entity.LivingEntity;
|
| 14 | import net.minecraft.world.entity.player.Player;
|
| 15 | import net.minecraft.world.item.ItemStack;
|
| 16 | import net.minecraft.world.item.component.Consumable;
|
| 17 | import net.minecraft.world.item.component.ConsumableListener;
|
| 18 | import net.minecraft.world.level.Level;
|
| 19 |
|
| 20 | public record FoodProperties(int nutrition, float saturation, boolean canAlwaysEat) implements ConsumableListener {
|
| 21 | public static final Codec<FoodProperties> DIRECT_CODEC = RecordCodecBuilder.create(
|
| 22 | i -> i.group(
|
| 23 | ExtraCodecs.NON_NEGATIVE_INT.fieldOf("nutrition").forGetter(FoodProperties::nutrition),
|
| 24 | Codec.FLOAT.fieldOf("saturation").forGetter(FoodProperties::saturation),
|
| 25 | Codec.BOOL.optionalFieldOf("can_always_eat", false).forGetter(FoodProperties::canAlwaysEat)
|
| 26 | )
|
| 27 | .apply(i, FoodProperties::new)
|
| 28 | );
|
| 29 | public static final StreamCodec<RegistryFriendlyByteBuf, FoodProperties> DIRECT_STREAM_CODEC = StreamCodec.composite(
|
| 30 | ByteBufCodecs.VAR_INT,
|
| 31 | FoodProperties::nutrition,
|
| 32 | ByteBufCodecs.FLOAT,
|
| 33 | FoodProperties::saturation,
|
| 34 | ByteBufCodecs.BOOL,
|
| 35 | FoodProperties::canAlwaysEat,
|
| 36 | FoodProperties::new
|
| 37 | );
|
| 38 |
|
| 39 | @Override
|
| 40 | public void onConsume(final Level level, final LivingEntity user, final ItemStack stack, final Consumable consumable) {
|
| 41 | RandomSource random = user.getRandom();
|
| 42 | level.playSound(null, user.getX(), user.getY(), user.getZ(), consumable.sound().value(), SoundSource.NEUTRAL, 1.0F, random.triangle(1.0F, 0.4F));
|
| 43 | if (user instanceof Player player) {
|
| 44 | player.getFoodData().eat(this);
|
| 45 | level.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.PLAYER_BURP, SoundSource.PLAYERS, 0.5F, Mth.randomBetween(random, 0.9F, 1.0F));
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | public static class Builder {
|
| 50 | private int nutrition;
|
| 51 | private float saturationModifier;
|
| 52 | private boolean canAlwaysEat;
|
| 53 |
|
| 54 | public FoodProperties.Builder nutrition(final int nutrition) {
|
| 55 | this.nutrition = nutrition;
|
| 56 | return this;
|
| 57 | }
|
| 58 |
|
| 59 | public FoodProperties.Builder saturationModifier(final float saturationModifier) {
|
| 60 | this.saturationModifier = saturationModifier;
|
| 61 | return this;
|
| 62 | }
|
| 63 |
|
| 64 | public FoodProperties.Builder alwaysEdible() {
|
| 65 | this.canAlwaysEat = true;
|
| 66 | return this;
|
| 67 | }
|
| 68 |
|
| 69 | public FoodProperties build() {
|
| 70 | float saturation = FoodConstants.saturationByModifier(this.nutrition, this.saturationModifier);
|
| 71 | return new FoodProperties(this.nutrition, saturation, this.canAlwaysEat);
|
| 72 | }
|
| 73 | }
|
| 74 | }
|
| 75 |
|