Use the id of the food group as the name rather than the tag

This commit is contained in:
Sollace 2024-04-02 08:30:42 +01:00
parent 3bc1628fd4
commit a87257de83
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
7 changed files with 160 additions and 94 deletions

View file

@ -30,23 +30,23 @@ public record DietProfile(
float defaultMultiplier,
float foragingMultiplier,
List<Multiplier> multipliers,
List<Effect> effects,
Optional<Effect> defaultEffect
List<FoodGroupEffects> effects,
Optional<FoodGroupEffects> defaultEffect
) {
public static final DietProfile EMPTY = new DietProfile(1, 1, List.of(), List.of(), Optional.empty());
public static final Codec<DietProfile> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.FLOAT.fieldOf("default_multiplier").forGetter(DietProfile::defaultMultiplier),
Codec.FLOAT.fieldOf("foraging_multiplier").forGetter(DietProfile::foragingMultiplier),
Codec.list(Multiplier.CODEC).fieldOf("multipliers").forGetter(DietProfile::multipliers),
Codec.list(Effect.PROFILE_CODEC).fieldOf("effects").forGetter(DietProfile::effects),
Effect.CODEC.optionalFieldOf("default_effect").forGetter(DietProfile::defaultEffect)
Codec.list(FoodGroupEffects.CODEC).fieldOf("effects").forGetter(DietProfile::effects),
FoodGroupEffects.CODEC.optionalFieldOf("default_effect").forGetter(DietProfile::defaultEffect)
).apply(instance, DietProfile::new));
public DietProfile(PacketByteBuf buffer) {
this(buffer.readFloat(), buffer.readFloat(),
buffer.readList(Multiplier::new),
buffer.readList(b -> new Effect(b, FoodGroupKey.LOOKUP)),
buffer.readOptional(b -> new Effect(b, FoodGroupKey.LOOKUP))
buffer.readList(b -> new FoodGroupEffects(b, FoodGroupKey.LOOKUP)),
buffer.readOptional(b -> new FoodGroupEffects(b, FoodGroupKey.LOOKUP))
);
}
@ -81,7 +81,7 @@ public record DietProfile(
}
public Optional<Effect> findEffect(ItemStack stack) {
return effects.stream().filter(m -> m.test(stack)).findFirst().or(this::defaultEffect);
return effects.stream().filter(m -> m.test(stack)).findFirst().or(this::defaultEffect).map(Effect.class::cast);
}
static boolean isForaged(ItemStack stack) {

View file

@ -37,10 +37,10 @@ public class DietsLoader implements IdentifiableResourceReloadListener {
Map<Identifier, Effect> foodGroups = new HashMap<>();
for (var group : loadData(manager, prepareExecutor, "diets/food_groups").entrySet()) {
try {
Effect.CODEC.parse(JsonOps.INSTANCE, group.getValue())
FoodGroup.CODEC.parse(JsonOps.INSTANCE, group.getValue())
.resultOrPartial(error -> LOGGER.error("Could not load food group {}: {}", group.getKey(), error))
.ifPresent(value -> {
foodGroups.put(group.getKey(), value);
foodGroups.put(group.getKey(), new FoodGroup(group.getKey(), value));
});
} catch (Throwable t) {
LOGGER.error("Could not load food effects {}", group.getKey(), t);

View file

@ -2,60 +2,31 @@ package com.minelittlepony.unicopia.diet;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.item.FoodComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.util.UseAction;
import net.minecraft.util.Util;
public record Effect(
List<FoodGroupKey> tags,
Optional<FoodComponent> foodComponent,
Ailment ailment
) implements Predicate<ItemStack> {
public static final Effect EMPTY = new Effect(List.of(), Optional.empty(), Ailment.EMPTY);
public static final Codec<Effect> CODEC = RecordCodecBuilder.create(instance -> instance.group(
FoodGroupKey.TAG_CODEC.listOf().fieldOf("tags").forGetter(Effect::tags),
FoodAttributes.CODEC.optionalFieldOf("food_component").forGetter(Effect::foodComponent),
Ailment.CODEC.fieldOf("ailment").forGetter(Effect::ailment)
).apply(instance, Effect::new));
public static final Codec<Effect> PROFILE_CODEC = RecordCodecBuilder.create(instance -> instance.group(
FoodGroupKey.CODEC.listOf().fieldOf("tags").forGetter(Effect::tags),
FoodAttributes.CODEC.optionalFieldOf("food_component").forGetter(Effect::foodComponent),
Ailment.CODEC.fieldOf("ailment").forGetter(Effect::ailment)
).apply(instance, Effect::new));
public interface Effect extends Predicate<ItemStack> {
Effect EMPTY = new FoodGroupEffects(List.of(), Optional.empty(), Ailment.EMPTY);
public Effect(PacketByteBuf buffer, Function<Identifier, FoodGroupKey> lookup) {
this(buffer.readList(b -> lookup.apply(b.readIdentifier())), buffer.readOptional(FoodAttributes::read), new Ailment(buffer));
}
List<FoodGroupKey> tags();
public void afflict(Pony pony, ItemStack stack) {
ailment().effects().afflict(pony.asEntity(), stack);
}
Optional<FoodComponent> foodComponent();
public void appendTooltip(ItemStack stack, List<Text> tooltip, TooltipContext context) {
int size = tooltip.size();
tags.forEach(tag -> {
if (tag.contains(stack)) {
tooltip.add(Text.literal(" ").append(Text.translatable(Util.createTranslationKey("tag", tag.id()))).formatted(Formatting.GRAY));
}
});
if (tooltip.size() == size) {
Ailment ailment();
default void appendTooltip(ItemStack stack, List<Text> tooltip, TooltipContext context) {
if (!test(stack)) {
if (stack.isFood()) {
tooltip.add(Text.literal(" ").append(Text.translatable("tag.unicopia.food_types.misc")).formatted(Formatting.GRAY));
tooltip.add(Text.literal(" ").append(Text.translatable("food_group.unicopia.misc")).formatted(Formatting.GRAY));
} else if (stack.getUseAction() == UseAction.DRINK) {
tooltip.add(Text.literal(" ").append(Text.translatable("tag.unicopia.food_types.drinks")).formatted(Formatting.GRAY));
tooltip.add(Text.literal(" ").append(Text.translatable("food_group.unicopia.drinks")).formatted(Formatting.GRAY));
}
}
@ -67,14 +38,14 @@ public record Effect(
}
}
public void toBuffer(PacketByteBuf buffer) {
buffer.writeCollection(tags, (b, t) -> b.writeIdentifier(t.id()));
buffer.writeOptional(foodComponent, FoodAttributes::write);
ailment.toBuffer(buffer);
default void toBuffer(PacketByteBuf buffer) {
buffer.writeCollection(tags(), (b, t) -> b.writeIdentifier(t.id()));
buffer.writeOptional(foodComponent(), FoodAttributes::write);
ailment().toBuffer(buffer);
}
@Override
public boolean test(ItemStack stack) {
return tags.stream().anyMatch(tag -> tag.contains(stack));
default boolean test(ItemStack stack) {
return tags().stream().anyMatch(tag -> tag.contains(stack));
}
}
}

View file

@ -0,0 +1,48 @@
package com.minelittlepony.unicopia.diet;
import java.util.List;
import java.util.Optional;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.item.FoodComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.util.Util;
public record FoodGroup(
Identifier id,
List<FoodGroupKey> tags,
Optional<FoodComponent> foodComponent,
Ailment ailment) implements Effect {
public static final Codec<FoodGroupEffects> CODEC = RecordCodecBuilder.create(instance -> instance.group(
FoodGroupKey.TAG_CODEC.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 FoodGroup(Identifier id, Effect effect) {
this(id, effect.tags(), effect.foodComponent(), effect.ailment());
}
public FoodGroup(PacketByteBuf buffer) {
this(buffer.readIdentifier(), new FoodGroupEffects(buffer, FoodGroupKey.TAG_ID_LOOKUP));
}
@Override
public void appendTooltip(ItemStack stack, List<Text> tooltip, TooltipContext context) {
tooltip.add(Text.literal(" ").append(Text.translatable(Util.createTranslationKey("food_group", id()))).formatted(Formatting.GRAY));
Effect.super.appendTooltip(stack, tooltip, context);
}
@Override
public void toBuffer(PacketByteBuf buffer) {
buffer.writeIdentifier(id());
Effect.super.toBuffer(buffer);
}
}

View file

@ -0,0 +1,42 @@
package com.minelittlepony.unicopia.diet;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.item.FoodComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
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 {
public static final Codec<FoodGroupEffects> CODEC = RecordCodecBuilder.create(instance -> instance.group(
FoodGroupKey.CODEC.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 FoodGroupEffects(PacketByteBuf buffer, Function<Identifier, FoodGroupKey> lookup) {
this(buffer.readList(b -> lookup.apply(b.readIdentifier())), buffer.readOptional(FoodAttributes::read), new Ailment(buffer));
}
@Override
public void appendTooltip(ItemStack stack, List<Text> tooltip, TooltipContext context) {
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);
}
}

View file

@ -1,8 +1,11 @@
package com.minelittlepony.unicopia.diet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.jetbrains.annotations.Nullable;
@ -47,12 +50,15 @@ public class PonyDiets implements DietView {
}
public PonyDiets(PacketByteBuf buffer) {
this(buffer.readMap(b -> b.readRegistryValue(Race.REGISTRY), DietProfile::new), buffer.readMap(PacketByteBuf::readIdentifier, b -> new Effect(b, FoodGroupKey.TAG_ID_LOOKUP)));
this(
buffer.readMap(b -> b.readRegistryValue(Race.REGISTRY), DietProfile::new),
buffer.readCollection(ArrayList::new, FoodGroup::new).stream().collect(Collectors.toMap(FoodGroup::id, Function.identity()))
);
}
public void toBuffer(PacketByteBuf buffer) {
buffer.writeMap(diets, (b, r) -> b.writeRegistryValue(Race.REGISTRY, r), (b, e) -> e.toBuffer(b));
buffer.writeMap(effects, PacketByteBuf::writeIdentifier, (b, e) -> e.toBuffer(b));
buffer.writeCollection(effects.values(), (b, e) -> e.toBuffer(b));
}
private DietProfile getDiet(Pony pony) {
@ -77,7 +83,7 @@ public class PonyDiets implements DietView {
@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));
Pony.of(entity).ifPresent(pony -> getEffects(stack, pony).ailment().effects().afflict(pony.asEntity(), stack));
}
}

View file

@ -619,42 +619,41 @@
"unicopia.diet.hunger": "Hunger Ratio: %s%%",
"unicopia.diet.saturation": "Saturation Ratio: %s%%",
"tag.unicopia.food_types.rotten_meat": "Rotting Meat",
"tag.unicopia.food_types.raw_meat": "Fresh Meat",
"tag.unicopia.food_types.cooked_meat": "Prepared Meat",
"tag.c.raw_fish": "Fresh Fish",
"tag.c.cooked_fish": "Prepared Fish",
"tag.c.raw_insect": "Bugs & Insects",
"tag.unicopia.food_types.cooked_insect": "Cooked Bugs & Insects",
"tag.unicopia.food_types.nuts_and_seeds": "Nuts & Seeds",
"tag.unicopia.container_with_love": "Love",
"tag.unicopia.food_types.rocks": "Rocks",
"tag.unicopia.food_types.pinecone": "Nuts & Seeds",
"tag.unicopia.food_types.bat_ponys_delight": "Bat Pony Treats",
"tag.unicopia.food_types.cooked_sea_vegitable": "Prepared Fish Food",
"tag.unicopia.food_types.raw_sea_vegitable": "Fresh Fish Food",
"tag.unicopia.food_types.shells": "Sea Shells",
"tag.unicopia.food_types.shelly": "Sea Shells",
"tag.unicopia.food_types.candy": "Candy",
"tag.unicopia.food_types.desserts": "Desserts",
"tag.unicopia.food_types.fruit": "Fruit",
"tag.unicopia.food_types.baked_goods": "Baked Goods",
"tag.unicopia.food_types.misc": "Misc",
"tag.unicopia.food_types.fruits_and_vegetables": "Fruits & Vegetables",
"tag.unicopia.food_types.drinks": "Drinks",
"tag.minecraft.leaves": "Leaves",
"tag.unicopia.food_types.forage_edible_filling": "Bulky Plant Matter",
"tag.unicopia.food_types.forage_edible": "Plant Matter",
"tag.unicopia.food_types.forage_nauseating": "Nauseating",
"tag.unicopia.food_types.forage_prickly": "Prickly",
"tag.unicopia.food_types.forage_risky": "Unsafe",
"tag.unicopia.food_types.forage_strengthening": "Strength Enhancing",
"tag.unicopia.food_types.forage_severely_prickly": "Very Prickly",
"tag.unicopia.food_types.forage_severely_nauseating": "Sickening",
"tag.unicopia.food_types.forage_radioactive": "Glowy",
"tag.unicopia.food_types.forage_dangerous": "Dangerous",
"tag.unicopia.food_types.forage_blinding": "Toxic",
"food_group.unicopia.meat.rotten": "Rotting Meat",
"food_group.unicopia.meat.raw": "Fresh Meat",
"food_group.unicopia.meat.cooked": "Prepared Meat",
"food_group.unicopia.fish.raw": "Fresh Fish",
"food_group.unicopia.fish.cooked": "Prepared Fish",
"food_group.unicopia.insect.raw": "Bugs & Insects",
"food_group.unicopia.fish.cooked": "Cooked Bugs & Insects",
"food_group.unicopia.nuts_and_seeds": "Nuts & Seeds",
"food_group.unicopia.love": "Love",
"food_group.unicopia.rocks": "Rocks",
"food_group.unicopia.pinecone": "Nuts & Seeds",
"food_group.unicopia.bat_ponys_delight": "Bat Pony Treats",
"food_group.unicopia.sea_vegitable.cooked": "Prepared Fish Food",
"food_group.unicopia.sea_vegitable.raw": "Fresh Fish Food",
"food_group.unicopia.shells": "Sea Shells",
"food_group.unicopia.shelly": "Sea Shells",
"food_group.unicopia.candy": "Candy",
"food_group.unicopia.desserts": "Desserts",
"food_group.unicopia.fruit": "Fruit",
"food_group.unicopia.baked_goods": "Baked Goods",
"food_group.unicopia.misc": "Misc",
"food_group.unicopia.fruits_and_vegetables": "Fruits & Vegetables",
"food_group.unicopia.drinks": "Drinks",
"food_group.unicopia.foraging.edible_filling": "Bulky Plant Matter",
"food_group.unicopia.foraging.edible": "Plant Matter",
"food_group.unicopia.foraging.nauseating": "Nauseating",
"food_group.unicopia.foraging.prickly": "Prickly",
"food_group.unicopia.foraging.risky": "Unsafe",
"food_group.unicopia.foraging.strengthening": "Strength Enhancing",
"food_group.unicopia.foraging.severely_prickly": "Very Prickly",
"food_group.unicopia.foraging.severely_nauseating": "Sickening",
"food_group.unicopia.foraging.radioactive": "Glowy",
"food_group.unicopia.foraging.dangerous": "Dangerous",
"food_group.unicopia.foraging.blinding": "Toxic",
"food_group.unicopia.foraging.leafy_greens": "Leafy Greens",
"toxicity.safe.name": "Safe",
"toxicity.mild.name": "Mildly Toxic",