2023-11-28 16:26:55 +01:00
|
|
|
package com.minelittlepony.unicopia.diet;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Optional;
|
|
|
|
|
2023-12-03 03:39:55 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
|
2023-11-28 16:26:55 +01:00
|
|
|
import com.minelittlepony.unicopia.Race;
|
2023-12-03 03:39:55 +01:00
|
|
|
import com.minelittlepony.unicopia.entity.effect.FoodPoisoningStatusEffect;
|
2023-11-28 16:26:55 +01:00
|
|
|
import com.minelittlepony.unicopia.entity.player.Pony;
|
2023-12-03 03:39:55 +01:00
|
|
|
import com.minelittlepony.unicopia.item.ItemDuck;
|
|
|
|
import net.minecraft.client.item.TooltipContext;
|
|
|
|
import net.minecraft.entity.LivingEntity;
|
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2023-11-28 16:26:55 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.network.PacketByteBuf;
|
2023-12-03 03:39:55 +01:00
|
|
|
import net.minecraft.text.Text;
|
|
|
|
import net.minecraft.util.Formatting;
|
|
|
|
import net.minecraft.util.Hand;
|
|
|
|
import net.minecraft.util.TypedActionResult;
|
|
|
|
import net.minecraft.world.World;
|
2023-11-28 16:26:55 +01:00
|
|
|
|
2023-12-03 03:39:55 +01:00
|
|
|
public class PonyDiets implements DietView {
|
2023-11-28 16:26:55 +01:00
|
|
|
private final Map<Race, DietProfile> diets;
|
|
|
|
private final List<Effect> effects;
|
|
|
|
|
2023-12-03 03:39:55 +01:00
|
|
|
private static PonyDiets INSTANCE = new PonyDiets(Map.of(), List.of());
|
2023-11-28 16:26:55 +01:00
|
|
|
|
2023-12-03 03:39:55 +01:00
|
|
|
public static PonyDiets getInstance() {
|
2023-11-28 16:26:55 +01:00
|
|
|
return INSTANCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void load(PonyDiets diets) {
|
|
|
|
INSTANCE = diets;
|
|
|
|
}
|
|
|
|
|
|
|
|
PonyDiets(Map<Race, DietProfile> diets, List<Effect> effects) {
|
|
|
|
this.diets = diets;
|
|
|
|
this.effects = effects;
|
|
|
|
}
|
|
|
|
|
|
|
|
public PonyDiets(PacketByteBuf buffer) {
|
|
|
|
this(buffer.readMap(b -> b.readRegistryValue(Race.REGISTRY), DietProfile::new), buffer.readList(Effect::new));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void toBuffer(PacketByteBuf buffer) {
|
|
|
|
buffer.writeMap(diets, (b, r) -> b.writeRegistryValue(Race.REGISTRY, r), (b, e) -> e.toBuffer(b));
|
|
|
|
buffer.writeCollection(effects, (b, e) -> e.toBuffer(b));
|
|
|
|
}
|
|
|
|
|
2023-12-03 03:39:55 +01:00
|
|
|
private DietProfile getDiet(Pony pony) {
|
|
|
|
return Optional.ofNullable(diets.get(pony.getObservedSpecies())).orElse(DietProfile.EMPTY);
|
|
|
|
}
|
|
|
|
|
2024-02-22 00:21:12 +01:00
|
|
|
Effect getEffects(ItemStack stack) {
|
2023-12-03 03:39:55 +01:00
|
|
|
return effects.stream().filter(effect -> effect.test(stack)).findFirst().orElse(Effect.EMPTY);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Effect getEffects(ItemStack stack, Pony pony) {
|
|
|
|
return getDiet(pony).findEffect(stack).orElseGet(() -> getEffects(stack));
|
2023-11-28 16:26:55 +01:00
|
|
|
}
|
|
|
|
|
2023-12-03 03:39:55 +01:00
|
|
|
@Override
|
|
|
|
public TypedActionResult<ItemStack> startUsing(ItemStack stack, World world, PlayerEntity user, Hand hand) {
|
|
|
|
return initEdibility(stack, user)
|
|
|
|
? FoodPoisoningStatusEffect.apply(stack, user)
|
|
|
|
: TypedActionResult.fail(stack);
|
2023-11-28 16:26:55 +01:00
|
|
|
}
|
|
|
|
|
2023-12-03 03:39:55 +01:00
|
|
|
@Override
|
|
|
|
public void finishUsing(ItemStack stack, World world, LivingEntity entity) {
|
|
|
|
if (initEdibility(stack, entity)) {
|
|
|
|
Pony.of(entity).ifPresent(pony -> getEffects(stack, pony).afflict(pony, stack));
|
|
|
|
}
|
2023-11-28 16:26:55 +01:00
|
|
|
}
|
|
|
|
|
2023-12-03 03:39:55 +01:00
|
|
|
@Override
|
|
|
|
public void appendTooltip(ItemStack stack, @Nullable PlayerEntity user, List<Text> tooltip, TooltipContext context) {
|
|
|
|
if (initEdibility(stack, user)) {
|
|
|
|
Pony pony = Pony.of(user);
|
|
|
|
|
|
|
|
tooltip.add(Text.translatable("unicopia.diet.information").formatted(Formatting.DARK_PURPLE));
|
|
|
|
getEffects(stack, pony).appendTooltip(stack, tooltip, context);
|
2024-03-03 17:05:31 +01:00
|
|
|
|
|
|
|
/*for (Race race : Race.REGISTRY) {
|
|
|
|
var diet = diets.get(race);
|
|
|
|
if (diet != null) {
|
|
|
|
tooltip.add(race.getDisplayName());
|
|
|
|
diet.appendTooltip(stack, user, tooltip, context);
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
|
2023-12-03 03:39:55 +01:00
|
|
|
getDiet(pony).appendTooltip(stack, user, tooltip, context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean initEdibility(ItemStack stack, LivingEntity user) {
|
|
|
|
ItemDuck item = (ItemDuck)stack.getItem();
|
|
|
|
item.resetFoodComponent();
|
|
|
|
return Pony.of(user).filter(pony -> {
|
|
|
|
DietProfile diet = getDiet(pony);
|
|
|
|
|
|
|
|
if (!stack.isFood() && pony.getObservedSpecies().hasIronGut()) {
|
|
|
|
diet.findEffect(stack)
|
|
|
|
.flatMap(Effect::foodComponent)
|
|
|
|
.or(() -> getEffects(stack).foodComponent())
|
|
|
|
.ifPresent(item::setFoodComponent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stack.isFood()) {
|
|
|
|
item.setFoodComponent(diet.getAdjustedFoodComponent(stack));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}).isPresent();
|
2023-11-28 16:26:55 +01:00
|
|
|
}
|
|
|
|
}
|