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;
|
2024-04-02 00:39:21 +02:00
|
|
|
import net.minecraft.util.Identifier;
|
2023-12-03 03:39:55 +01:00
|
|
|
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;
|
2024-04-02 00:39:21 +02:00
|
|
|
private final Map<Identifier, Effect> effects;
|
2023-11-28 16:26:55 +01:00
|
|
|
|
2024-04-02 00:39:21 +02:00
|
|
|
private static PonyDiets INSTANCE = new PonyDiets(Map.of(), Map.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;
|
|
|
|
}
|
|
|
|
|
2024-04-02 00:39:21 +02:00
|
|
|
@Nullable
|
|
|
|
static Effect getEffect(Identifier id) {
|
|
|
|
return INSTANCE.effects.get(id);
|
|
|
|
}
|
|
|
|
|
2023-11-28 16:26:55 +01:00
|
|
|
public static void load(PonyDiets diets) {
|
|
|
|
INSTANCE = diets;
|
|
|
|
}
|
|
|
|
|
2024-04-02 00:39:21 +02:00
|
|
|
PonyDiets(Map<Race, DietProfile> diets, Map<Identifier, Effect> effects) {
|
2023-11-28 16:26:55 +01:00
|
|
|
this.diets = diets;
|
|
|
|
this.effects = effects;
|
|
|
|
}
|
|
|
|
|
|
|
|
public PonyDiets(PacketByteBuf buffer) {
|
2024-04-02 00:39:21 +02:00
|
|
|
this(buffer.readMap(b -> b.readRegistryValue(Race.REGISTRY), DietProfile::new), buffer.readMap(PacketByteBuf::readIdentifier, b -> new Effect(b, FoodGroupKey.TAG_ID_LOOKUP)));
|
2023-11-28 16:26:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void toBuffer(PacketByteBuf buffer) {
|
|
|
|
buffer.writeMap(diets, (b, r) -> b.writeRegistryValue(Race.REGISTRY, r), (b, e) -> e.toBuffer(b));
|
2024-04-02 00:39:21 +02:00
|
|
|
buffer.writeMap(effects, PacketByteBuf::writeIdentifier, (b, e) -> e.toBuffer(b));
|
2023-11-28 16:26:55 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2024-04-02 00:39:21 +02:00
|
|
|
return effects.values().stream().filter(effect -> effect.test(stack)).findFirst().orElse(Effect.EMPTY);
|
2023-12-03 03:39:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2024-03-03 17:05:31 +01:00
|
|
|
|
2024-03-25 19:11:50 +01:00
|
|
|
if (initEdibility(stack, user)) {
|
|
|
|
if (!((ItemDuck)stack.getItem()).getOriginalFoodComponent().isEmpty() || stack.getItem().getFoodComponent() != null) {
|
|
|
|
Pony pony = Pony.of(user);
|
2024-03-03 17:05:31 +01:00
|
|
|
|
2024-03-25 19:11:50 +01:00
|
|
|
tooltip.add(Text.translatable("unicopia.diet.information").formatted(Formatting.DARK_PURPLE));
|
|
|
|
getEffects(stack, pony).appendTooltip(stack, tooltip, context);
|
|
|
|
getDiet(pony).appendTooltip(stack, user, tooltip, context);
|
|
|
|
}
|
2023-12-03 03:39:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|