75 lines
3.3 KiB
Java
75 lines
3.3 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* com.mojang.datafixers.kinds.App
|
|
* com.mojang.datafixers.kinds.Applicative
|
|
* com.mojang.serialization.Codec
|
|
* com.mojang.serialization.codecs.RecordCodecBuilder
|
|
*/
|
|
package net.minecraft.world.food;
|
|
|
|
import com.mojang.datafixers.kinds.App;
|
|
import com.mojang.datafixers.kinds.Applicative;
|
|
import com.mojang.serialization.Codec;
|
|
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
|
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.food.FoodConstants;
|
|
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(i -> i.group((App)ExtraCodecs.NON_NEGATIVE_INT.fieldOf("nutrition").forGetter(FoodProperties::nutrition), (App)Codec.FLOAT.fieldOf("saturation").forGetter(FoodProperties::saturation), (App)Codec.BOOL.optionalFieldOf("can_always_eat", (Object)false).forGetter(FoodProperties::canAlwaysEat)).apply((Applicative)i, 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 level, LivingEntity user, ItemStack stack, Consumable consumable) {
|
|
RandomSource random = user.getRandom();
|
|
level.playSound(null, user.getX(), user.getY(), user.getZ(), consumable.sound().value(), SoundSource.NEUTRAL, 1.0f, random.triangle(1.0f, 0.4f));
|
|
if (user instanceof Player) {
|
|
Player player = (Player)user;
|
|
player.getFoodData().eat(this);
|
|
level.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.PLAYER_BURP, SoundSource.PLAYERS, 0.5f, Mth.randomBetween(random, 0.9f, 1.0f));
|
|
}
|
|
}
|
|
|
|
public static class Builder {
|
|
private int nutrition;
|
|
private float saturationModifier;
|
|
private boolean canAlwaysEat;
|
|
|
|
public Builder nutrition(int nutrition) {
|
|
this.nutrition = nutrition;
|
|
return this;
|
|
}
|
|
|
|
public Builder saturationModifier(float saturationModifier) {
|
|
this.saturationModifier = saturationModifier;
|
|
return this;
|
|
}
|
|
|
|
public Builder alwaysEdible() {
|
|
this.canAlwaysEat = true;
|
|
return this;
|
|
}
|
|
|
|
public FoodProperties build() {
|
|
float saturation = FoodConstants.saturationByModifier(this.nutrition, this.saturationModifier);
|
|
return new FoodProperties(this.nutrition, saturation, this.canAlwaysEat);
|
|
}
|
|
}
|
|
}
|
|
|