2023-11-28 16:26:55 +01:00
|
|
|
package com.minelittlepony.unicopia.diet;
|
|
|
|
|
2023-12-03 03:39:55 +01:00
|
|
|
import java.util.List;
|
2023-11-28 16:26:55 +01:00
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.function.Predicate;
|
|
|
|
|
2023-12-03 03:39:55 +01:00
|
|
|
import com.minelittlepony.unicopia.entity.player.Pony;
|
2023-11-28 16:26:55 +01:00
|
|
|
import com.mojang.serialization.Codec;
|
|
|
|
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
|
|
|
|
2023-12-03 03:39:55 +01:00
|
|
|
import net.minecraft.client.item.TooltipContext;
|
|
|
|
import net.minecraft.item.FoodComponent;
|
2023-11-28 16:26:55 +01:00
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.network.PacketByteBuf;
|
|
|
|
import net.minecraft.registry.RegistryKeys;
|
|
|
|
import net.minecraft.registry.tag.TagKey;
|
2023-12-03 03:39:55 +01:00
|
|
|
import net.minecraft.text.Text;
|
|
|
|
import net.minecraft.util.Formatting;
|
|
|
|
import net.minecraft.util.UseAction;
|
|
|
|
import net.minecraft.util.Util;
|
2023-11-28 16:26:55 +01:00
|
|
|
|
|
|
|
public record Effect(
|
2023-12-03 03:39:55 +01:00
|
|
|
List<TagKey<Item>> tags,
|
2023-11-28 16:26:55 +01:00
|
|
|
Optional<FoodComponent> foodComponent,
|
|
|
|
Ailment ailment
|
|
|
|
) implements Predicate<ItemStack> {
|
2023-12-03 03:39:55 +01:00
|
|
|
public static final Effect EMPTY = new Effect(List.of(), Optional.empty(), Ailment.EMPTY);
|
2023-11-28 16:26:55 +01:00
|
|
|
public static final Codec<Effect> CODEC = RecordCodecBuilder.create(instance -> instance.group(
|
2023-12-03 03:39:55 +01:00
|
|
|
TagKey.unprefixedCodec(RegistryKeys.ITEM).listOf().fieldOf("tags").forGetter(Effect::tags),
|
|
|
|
FoodAttributes.CODEC.optionalFieldOf("food_component").forGetter(Effect::foodComponent),
|
2023-11-28 16:26:55 +01:00
|
|
|
Ailment.CODEC.fieldOf("ailment").forGetter(Effect::ailment)
|
|
|
|
).apply(instance, Effect::new));
|
|
|
|
|
|
|
|
public Effect(PacketByteBuf buffer) {
|
2023-12-03 03:39:55 +01:00
|
|
|
this(buffer.readList(b -> TagKey.of(RegistryKeys.ITEM, b.readIdentifier())), buffer.readOptional(FoodAttributes::read), new Ailment(buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void afflict(Pony pony, ItemStack stack) {
|
|
|
|
ailment().effects().afflict(pony.asEntity(), stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void appendTooltip(ItemStack stack, List<Text> tooltip, TooltipContext context) {
|
|
|
|
int size = tooltip.size();
|
|
|
|
tags.forEach(tag -> {
|
|
|
|
if (stack.isIn(tag)) {
|
|
|
|
tooltip.add(Text.literal(" ").append(Text.translatable(Util.createTranslationKey("tag", tag.id()))).formatted(Formatting.GRAY));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (tooltip.size() == size) {
|
|
|
|
if (stack.isFood()) {
|
2024-03-03 17:05:31 +01:00
|
|
|
tooltip.add(Text.literal(" ").append(Text.translatable("tag.unicopia.food_types.misc")).formatted(Formatting.GRAY));
|
2023-12-03 03:39:55 +01:00
|
|
|
} else if (stack.getUseAction() == UseAction.DRINK) {
|
|
|
|
tooltip.add(Text.literal(" ").append(Text.translatable("tag.unicopia.food_types.drinks")).formatted(Formatting.GRAY));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (context.isAdvanced() && stack.isFood()) {
|
|
|
|
if (!ailment().effects().isEmpty()) {
|
|
|
|
tooltip.add(Text.translatable("unicopia.diet.side_effects").formatted(Formatting.DARK_PURPLE));
|
|
|
|
ailment().effects().appendTooltip(tooltip);
|
|
|
|
}
|
|
|
|
}
|
2023-11-28 16:26:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void toBuffer(PacketByteBuf buffer) {
|
2023-12-03 03:39:55 +01:00
|
|
|
buffer.writeCollection(tags, (b, t) -> b.writeIdentifier(t.id()));
|
|
|
|
buffer.writeOptional(foodComponent, FoodAttributes::write);
|
2023-11-28 16:26:55 +01:00
|
|
|
ailment.toBuffer(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean test(ItemStack stack) {
|
2023-12-03 03:39:55 +01:00
|
|
|
return tags.stream().anyMatch(stack::isIn);
|
2023-11-28 16:26:55 +01:00
|
|
|
}
|
|
|
|
}
|