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