2024-04-02 09:30:42 +02:00
|
|
|
package com.minelittlepony.unicopia.diet;
|
|
|
|
|
2024-04-04 21:22:10 +02:00
|
|
|
import java.util.ArrayList;
|
2024-04-02 09:30:42 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Optional;
|
2024-04-04 21:22:10 +02:00
|
|
|
import com.minelittlepony.unicopia.diet.affliction.Affliction;
|
|
|
|
import com.minelittlepony.unicopia.item.UFoodComponents;
|
2024-04-02 09:30:42 +02:00
|
|
|
import com.mojang.serialization.Codec;
|
|
|
|
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
|
|
|
|
2024-09-28 23:27:46 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2024-09-26 22:36:35 +02:00
|
|
|
import net.minecraft.component.type.FoodComponent;
|
2024-04-04 21:22:10 +02:00
|
|
|
import net.minecraft.item.Item;
|
2024-04-02 09:30:42 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2024-09-28 23:27:46 +02:00
|
|
|
import net.minecraft.item.tooltip.TooltipType;
|
|
|
|
import net.minecraft.network.RegistryByteBuf;
|
|
|
|
import net.minecraft.network.codec.PacketCodec;
|
|
|
|
import net.minecraft.network.codec.PacketCodecs;
|
2024-04-04 21:22:10 +02:00
|
|
|
import net.minecraft.registry.RegistryKeys;
|
|
|
|
import net.minecraft.registry.tag.TagKey;
|
2024-04-02 09:30:42 +02:00
|
|
|
import net.minecraft.text.Text;
|
|
|
|
import net.minecraft.util.Formatting;
|
|
|
|
import net.minecraft.util.Identifier;
|
|
|
|
import net.minecraft.util.Util;
|
|
|
|
|
|
|
|
public record FoodGroupEffects(
|
|
|
|
List<FoodGroupKey> tags,
|
|
|
|
Optional<FoodComponent> foodComponent,
|
|
|
|
Ailment ailment
|
|
|
|
) implements Effect {
|
2024-09-28 23:27:46 +02:00
|
|
|
public static Codec<FoodGroupEffects> createCodec(Codec<FoodGroupKey> keyCodec) {
|
|
|
|
return RecordCodecBuilder.create(instance -> instance.group(
|
|
|
|
keyCodec.listOf().fieldOf("tags").forGetter(FoodGroupEffects::tags),
|
|
|
|
FoodAttributes.CODEC.optionalFieldOf("food_component").forGetter(FoodGroupEffects::foodComponent),
|
|
|
|
Ailment.CODEC.fieldOf("ailment").forGetter(FoodGroupEffects::ailment)
|
|
|
|
).apply(instance, FoodGroupEffects::new));
|
|
|
|
}
|
|
|
|
public static final PacketCodec<RegistryByteBuf, FoodGroupEffects> createPacketCodec(PacketCodec<ByteBuf, FoodGroupKey> keyCodec) {
|
|
|
|
return PacketCodec.tuple(
|
|
|
|
keyCodec.collect(PacketCodecs.toList()), FoodGroupEffects::tags,
|
|
|
|
PacketCodecs.optional(FoodComponent.PACKET_CODEC), FoodGroupEffects::foodComponent,
|
|
|
|
Ailment.PACKET_CODEC, FoodGroupEffects::ailment,
|
|
|
|
FoodGroupEffects::new
|
|
|
|
);
|
2024-04-02 09:30:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-09-28 23:27:46 +02:00
|
|
|
public void appendTooltip(ItemStack stack, List<Text> tooltip, TooltipType context) {
|
2024-04-02 09:30:42 +02:00
|
|
|
tags.forEach(tag -> {
|
|
|
|
if (tag.contains(stack)) {
|
|
|
|
tooltip.add(Text.literal(" ").append(Text.translatable(Util.createTranslationKey("tag", tag.id()))).formatted(Formatting.GRAY));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Effect.super.appendTooltip(stack, tooltip, context);
|
|
|
|
}
|
2024-04-04 21:22:10 +02:00
|
|
|
|
|
|
|
public static final class Builder {
|
|
|
|
private final List<FoodGroupKey> tags = new ArrayList<>();
|
|
|
|
private Optional<FoodComponent> foodComponent = Optional.empty();
|
2024-09-28 23:27:46 +02:00
|
|
|
private Ailment ailment = Ailment.EMPTY;
|
2024-04-04 21:22:10 +02:00
|
|
|
|
|
|
|
public Builder tag(Identifier tag) {
|
|
|
|
return tag(TagKey.of(RegistryKeys.ITEM, tag));
|
|
|
|
}
|
|
|
|
|
|
|
|
public Builder tag(TagKey<Item> tag) {
|
|
|
|
tags.add(FoodGroupKey.TAG_LOOKUP.apply(tag));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Builder ailment(Affliction affliction) {
|
|
|
|
ailment = new Ailment(affliction);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Builder food(int hunger, float saturation) {
|
|
|
|
return food(UFoodComponents.builder(hunger, saturation));
|
|
|
|
}
|
|
|
|
|
|
|
|
public Builder food(FoodComponent.Builder food) {
|
|
|
|
return food(food.build());
|
|
|
|
}
|
|
|
|
|
|
|
|
public Builder food(FoodComponent food) {
|
|
|
|
foodComponent = Optional.of(food);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public FoodGroupEffects build() {
|
|
|
|
return new FoodGroupEffects(tags, foodComponent, ailment);
|
|
|
|
}
|
|
|
|
}
|
2024-04-02 09:30:42 +02:00
|
|
|
}
|