Skip to content

Commit

Permalink
Update codecs
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatGravyBoat committed Dec 21, 2023
1 parent cd9221d commit dc2a056
Show file tree
Hide file tree
Showing 29 changed files with 126 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.teamresourceful.resourcefulbees.api.data.honeycomb.OutputVariation;
import com.teamresourceful.resourcefulbees.api.registry.HoneycombRegistry;
import net.minecraft.core.HolderSet;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.level.block.Block;

Expand Down Expand Up @@ -33,7 +33,7 @@ default boolean isEntityFlower(EntityType<?> entityType) {

int maxTimeInHive();

List<MutableComponent> lore();
List<Component> lore();

default Optional<OutputVariation> getHoneycombData() {
return Optional.ofNullable(HoneycombRegistry.get().getHoneycomb(honeycomb()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.teamresourceful.resourcefullib.common.codecs.CodecExtras;

public record TraitDamageType(String type, int amplifier) {
public static final Codec<TraitDamageType> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.STRING.fieldOf("damageType").orElse("").forGetter(TraitDamageType::type),
Codec.intRange(0, Integer.MAX_VALUE).fieldOf("amplifier").orElse(0).forGetter(TraitDamageType::amplifier)
Codec.STRING.optionalFieldOf("damageType", "").forGetter(TraitDamageType::type),
CodecExtras.NON_NEGATIVE_INT.optionalFieldOf("amplifier", 0).forGetter(TraitDamageType::amplifier)
).apply(instance, TraitDamageType::new));
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.teamresourceful.resourcefullib.common.color.Color;
import com.teamresourceful.resourcefullib.common.exceptions.ValidationException;
import net.minecraft.core.HolderSet;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.valueproviders.UniformInt;
import net.minecraft.world.entity.EntityType;
Expand Down Expand Up @@ -67,7 +67,7 @@ public BeeCombatData combat(boolean isPassive, boolean removeStingerOnAttack, bo
return this.combat.create(isPassive, removeStingerOnAttack, inflictsPoison, isInvulnerable, attributes);
}

public BeeCoreData core(String honeycomb, HolderSet<Block> flowers, HolderSet<EntityType<?>> entityFlowers, int maxTimeInHive, List<MutableComponent> lore) {
public BeeCoreData core(String honeycomb, HolderSet<Block> flowers, HolderSet<EntityType<?>> entityFlowers, int maxTimeInHive, List<Component> lore) {
return this.core.create(honeycomb, flowers, entityFlowers, maxTimeInHive, lore);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.teamresourceful.resourcefullib.common.codecs.predicates.RestrictedItemPredicate;
import com.teamresourceful.resourcefullib.common.color.Color;
import net.minecraft.core.HolderSet;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.valueproviders.UniformInt;
import net.minecraft.world.entity.EntityType;
Expand Down Expand Up @@ -74,7 +74,7 @@ public interface CombatInitializer {

@FunctionalInterface
public interface CoreInitializer {
BeeCoreData create(String honeycomb, HolderSet<Block> flowers, HolderSet<EntityType<?>> entityFlowers, int maxTimeInHive, List<MutableComponent> lore);
BeeCoreData create(String honeycomb, HolderSet<Block> flowers, HolderSet<EntityType<?>> entityFlowers, int maxTimeInHive, List<Component> lore);
}

@FunctionalInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ private static ItemStack getBucketWithNbt() {
HolderSet.direct(Block::builtInRegistryHolder, Blocks.DIAMOND_BLOCK, Blocks.EMERALD_BLOCK),
HolderSet.direct(EntityType::builtInRegistryHolder, EntityType.COW, EntityType.SHEEP),
4000,
List.of(Component.literal("This is a bee template"),
Component.literal("This bee is a dummy and does not exist"),
Component.literal("Let's hope this works...")
List.of(
Component.literal("This is a bee template"),
Component.literal("This bee is a dummy and does not exist"),
Component.literal("Let's hope this works...")
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.teamresourceful.resourcefulbees.common.recipes.base.RecipeFluid;
import com.teamresourceful.resourcefulbees.common.registries.minecraft.ModRecipeSerializers;
import com.teamresourceful.resourcefulbees.common.registries.minecraft.ModRecipes;
import com.teamresourceful.resourcefullib.common.codecs.CodecExtras;
import com.teamresourceful.resourcefullib.common.recipe.CodecRecipe;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -24,8 +25,8 @@ public static Codec<HoneyGenRecipe> codec(ResourceLocation id) {
return RecordCodecBuilder.create(instance -> instance.group(
RecordCodecBuilder.point(id),
RecipeFluid.CODEC.fieldOf("honey").forGetter(HoneyGenRecipe::honey),
Codec.intRange(1, Integer.MAX_VALUE).fieldOf("energyFillRate").orElse(125).forGetter(HoneyGenRecipe::energyFillRate),
Codec.intRange(1, Integer.MAX_VALUE).fieldOf("honeyDrainRate").orElse(5).forGetter(HoneyGenRecipe::honeyDrainRate)
CodecExtras.NON_NEGATIVE_INT.optionalFieldOf("energyFillRate", 125).forGetter(HoneyGenRecipe::energyFillRate),
CodecExtras.NON_NEGATIVE_INT.optionalFieldOf("honeyDrainRate", 5).forGetter(HoneyGenRecipe::honeyDrainRate)
).apply(instance, HoneyGenRecipe::new));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public record CombatData(
});
private static final BeeCombatData DEFAULT = new CombatData(false, true, true, false, DEFAULT_ATTRIBUTES);
private static final Codec<BeeCombatData> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.BOOL.fieldOf("isPassive").orElse(false).forGetter(BeeCombatData::isPassive),
Codec.BOOL.fieldOf("removeStingerOnAttack").orElse(true).forGetter(BeeCombatData::removeStingerOnAttack),
Codec.BOOL.fieldOf("inflictsPoison").orElse(true).forGetter(BeeCombatData::inflictsPoison),
Codec.BOOL.fieldOf("isInvulnerable").orElse(false).forGetter(BeeCombatData::isInvulnerable),
Codec.unboundedMap(BuiltInRegistries.ATTRIBUTE.byNameCodec(), Codec.DOUBLE).fieldOf("attributes").orElse(DEFAULT_ATTRIBUTES).forGetter(BeeCombatData::attributes)
Codec.BOOL.optionalFieldOf("isPassive", false).forGetter(BeeCombatData::isPassive),
Codec.BOOL.optionalFieldOf("removeStingerOnAttack", true).forGetter(BeeCombatData::removeStingerOnAttack),
Codec.BOOL.optionalFieldOf("inflictsPoison", true).forGetter(BeeCombatData::inflictsPoison),
Codec.BOOL.optionalFieldOf("isInvulnerable", false).forGetter(BeeCombatData::isInvulnerable),
Codec.unboundedMap(BuiltInRegistries.ATTRIBUTE.byNameCodec(), Codec.DOUBLE).optionalFieldOf("attributes", DEFAULT_ATTRIBUTES).forGetter(BeeCombatData::attributes)
).apply(instance, CombatData::new));
public static final BeeDataSerializer<BeeCombatData> SERIALIZER = BeeDataSerializer.of(new ModResourceLocation("combat"), 1, id -> CODEC, DEFAULT);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
import com.teamresourceful.resourcefulbees.api.data.bee.base.BeeDataSerializer;
import com.teamresourceful.resourcefulbees.common.lib.constants.BeeConstants;
import com.teamresourceful.resourcefulbees.common.util.ModResourceLocation;
import com.teamresourceful.resourcefullib.common.codecs.CodecExtras;
import com.teamresourceful.resourcefullib.common.codecs.tags.HolderSetCodec;
import net.minecraft.core.HolderSet;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
Expand All @@ -25,16 +24,17 @@ public record CoreData(
HolderSet<Block> flowers,
HolderSet<EntityType<?>> entityFlowers,
int maxTimeInHive,
List<MutableComponent> lore
List<Component> lore
) implements BeeCoreData {

private static final HolderSet<Block> DEFAULT_FLOWERS = HolderSet.direct(Block::builtInRegistryHolder, Blocks.POPPY);
private static final BeeCoreData DEFAULT = new CoreData("", HolderSet.direct(), HolderSet.direct(), BeeConstants.MAX_TIME_IN_HIVE, new ArrayList<>());
private static final Codec<BeeCoreData> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.STRING.fieldOf("honeycombVariation").orElse("").forGetter(BeeCoreData::honeycomb),
HolderSetCodec.of(BuiltInRegistries.BLOCK).fieldOf("flower").orElse(HolderSet.direct(Block::builtInRegistryHolder, Blocks.POPPY)).forGetter(BeeCoreData::flowers),
HolderSetCodec.of(BuiltInRegistries.ENTITY_TYPE).fieldOf("entityFlower").orElse(HolderSet.direct()).forGetter(BeeCoreData::entityFlowers),
Codec.intRange(600, Integer.MAX_VALUE).fieldOf("maxTimeInHive").orElse(2400).forGetter(BeeCoreData::maxTimeInHive),
CodecExtras.passthrough(Component.Serializer::toJsonTree, Component.Serializer::fromJson).listOf().fieldOf("lore").orElse(Lists.newArrayList()).forGetter(BeeCoreData::lore)
Codec.STRING.optionalFieldOf("honeycombVariation", "").forGetter(BeeCoreData::honeycomb),
HolderSetCodec.of(BuiltInRegistries.BLOCK).optionalFieldOf("flower", DEFAULT_FLOWERS).forGetter(BeeCoreData::flowers),
HolderSetCodec.of(BuiltInRegistries.ENTITY_TYPE).optionalFieldOf("entityFlower", HolderSet.direct()).forGetter(BeeCoreData::entityFlowers),
Codec.intRange(600, Integer.MAX_VALUE).optionalFieldOf("maxTimeInHive", 2400).forGetter(BeeCoreData::maxTimeInHive),
ExtraCodecs.COMPONENT.listOf().optionalFieldOf("lore", Lists.newArrayList()).forGetter(BeeCoreData::lore)
).apply(instance, CoreData::new));

public static final BeeDataSerializer<BeeCoreData> SERIALIZER = BeeDataSerializer.of(new ModResourceLocation("core"), 1, id -> CODEC, DEFAULT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
import com.teamresourceful.resourcefulbees.api.data.bee.breeding.BeeBreedData;
import com.teamresourceful.resourcefulbees.api.data.bee.mutation.BeeMutationData;
import com.teamresourceful.resourcefulbees.api.data.bee.render.BeeRenderData;
import com.teamresourceful.resourcefulbees.common.lib.constants.ModConstants;
import com.teamresourceful.resourcefulbees.common.setup.data.beedata.breeding.BreedData;
import com.teamresourceful.resourcefulbees.common.setup.data.beedata.mutation.MutationData;
import com.teamresourceful.resourcefulbees.common.setup.data.beedata.rendering.RenderData;
import com.teamresourceful.resourcefulbees.common.setup.data.beedata.traits.TraitData;
import com.teamresourceful.resourcefulbees.common.lib.constants.ModConstants;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.teamresourceful.resourcefulbees.api.data.bee.base.BeeDataSerializer;
import com.teamresourceful.resourcefulbees.common.registries.minecraft.ModItems;
import com.teamresourceful.resourcefulbees.common.util.ModResourceLocation;
import com.teamresourceful.resourcefullib.common.codecs.CodecExtras;
import com.teamresourceful.resourcefullib.common.codecs.bounds.UniformedNumberCodecs;
import com.teamresourceful.resourcefullib.common.codecs.recipes.ItemStackCodec;
import net.minecraft.util.RandomSource;
Expand Down Expand Up @@ -40,12 +41,12 @@ public boolean isTradable() {
}

public static final Codec<BeekeeperTradeData> CODEC = RecordCodecBuilder.create(tradeDataInstance -> tradeDataInstance.group(
UniformedNumberCodecs.rangedUniformIntCodec(1, 64).fieldOf("amount").orElse(UniformInt.of(1,1)).forGetter(BeekeeperTradeData::amount),
ItemStackCodec.CODEC.fieldOf("secondaryItem").orElse(ItemStack.EMPTY).forGetter(BeekeeperTradeData::secondaryItem),
UniformedNumberCodecs.rangedUniformIntCodec(1, 64).fieldOf("secondaryItemCost").orElse(UniformInt.of(1, 4)).forGetter(BeekeeperTradeData::secondaryItemCost),
Codec.floatRange(0, 1).fieldOf("priceMultiplier").orElse(0.05f).forGetter(BeekeeperTradeData::priceMultiplier),
Codec.intRange(1, 64).fieldOf("maxTrades").orElse(8).forGetter(BeekeeperTradeData::maxTrades),
Codec.intRange(1, 64).fieldOf("xp").orElse(3).forGetter(BeekeeperTradeData::xp)
UniformedNumberCodecs.rangedUniformIntCodec(1, 64).optionalFieldOf("amount", UniformInt.of(1,1)).forGetter(BeekeeperTradeData::amount),
ItemStackCodec.CODEC.optionalFieldOf("secondaryItem", ItemStack.EMPTY).forGetter(BeekeeperTradeData::secondaryItem),
UniformedNumberCodecs.rangedUniformIntCodec(1, 64).optionalFieldOf("secondaryItemCost", UniformInt.of(1, 4)).forGetter(BeekeeperTradeData::secondaryItemCost),
CodecExtras.FLOAT_UNIT_INTERVAL.optionalFieldOf("priceMultiplier", 0.05f).forGetter(BeekeeperTradeData::priceMultiplier),
Codec.intRange(1, 64).optionalFieldOf("maxTrades", 8).forGetter(BeekeeperTradeData::maxTrades),
Codec.intRange(1, 64).optionalFieldOf("xp", 3).forGetter(BeekeeperTradeData::xp)
).apply(tradeDataInstance, TradeData::new));

public static final BeekeeperTradeData DEFAULT = new TradeData(UniformInt.of(0,0), ItemStack.EMPTY, UniformInt.of(0,0), 0, 0, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import com.google.common.base.Suppliers;
import com.mojang.serialization.Codec;
import com.mojang.serialization.Decoder;
import com.mojang.serialization.Encoder;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.teamresourceful.resourcefulbees.api.data.bee.CustomBeeData;
import com.teamresourceful.resourcefulbees.api.data.bee.breeding.FamilyUnit;
Expand All @@ -31,11 +28,11 @@ public static BeeFamilyUnit of(double weight, double chance, String parent1, Str
@ApiStatus.Internal
public static Codec<FamilyUnit> codec(String name) {
return RecordCodecBuilder.create(instance -> instance.group(
Codec.doubleRange(0.0d, Double.MAX_VALUE).fieldOf("weight").orElse(BeeConstants.DEFAULT_BREED_WEIGHT).forGetter(FamilyUnit::weight),
Codec.doubleRange(0.0d, 1.0d).fieldOf("chance").orElse(BeeConstants.DEFAULT_BREED_CHANCE).forGetter(FamilyUnit::chance),
Codec.STRING.fieldOf("parent1").orElse("").forGetter(unit -> unit.getParents().getParent1()),
Codec.STRING.fieldOf("parent2").orElse("").forGetter(unit -> unit.getParents().getParent2()),
MapCodec.of(Encoder.empty(), Decoder.unit(() -> name)).forGetter(FamilyUnit::getChild)
Codec.doubleRange(0.0d, Double.MAX_VALUE).optionalFieldOf("weight", BeeConstants.DEFAULT_BREED_WEIGHT).forGetter(FamilyUnit::weight),
Codec.doubleRange(0.0d, 1.0d).optionalFieldOf("chance", BeeConstants.DEFAULT_BREED_CHANCE).forGetter(FamilyUnit::chance),
Codec.STRING.optionalFieldOf("parent1", "").forGetter(unit -> unit.getParents().getParent1()),
Codec.STRING.optionalFieldOf("parent2", "").forGetter(unit -> unit.getParents().getParent2()),
RecordCodecBuilder.point(name)
).apply(instance, BeeFamilyUnit::of));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.teamresourceful.resourcefullib.common.codecs.tags.HolderSetCodec;
import net.minecraft.core.HolderSet;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
Expand All @@ -27,17 +28,18 @@ public record BreedData(
int childGrowthDelay, int breedDelay
) implements BeeBreedData {

private static final BeeBreedData DEFAULT = new BreedData(Collections.emptySet(), HolderSet.direct(Item::builtInRegistryHolder, Items.POPPY), Optional.empty(), 0, 0, 0);
private static final HolderSet<Item> DEFAULT_FEED_ITEM = HolderSet.direct(Item::builtInRegistryHolder, Items.POPPY);
private static final BeeBreedData DEFAULT = new BreedData(Collections.emptySet(), DEFAULT_FEED_ITEM, Optional.empty(), 0, 0, 0);
public static final BeeDataSerializer<BeeBreedData> SERIALIZER = BeeDataSerializer.of(new ModResourceLocation("breeding"), 1, BreedData::codec, DEFAULT);

private static Codec<BeeBreedData> codec(String name) {
return RecordCodecBuilder.create(instance -> instance.group(
CodecExtras.set(BeeFamilyUnit.codec(name)).fieldOf("parents").orElse(new HashSet<>()).forGetter(BeeBreedData::families),
HolderSetCodec.of(BuiltInRegistries.ITEM).fieldOf("feedItem").orElse(HolderSet.direct(Item::builtInRegistryHolder, Items.POPPY)).forGetter(BeeBreedData::feedItems),
CodecExtras.set(BeeFamilyUnit.codec(name)).optionalFieldOf("parents", new HashSet<>()).forGetter(BeeBreedData::families),
HolderSetCodec.of(BuiltInRegistries.ITEM).optionalFieldOf("feedItem", DEFAULT_FEED_ITEM).forGetter(BeeBreedData::feedItems),
ItemStackCodec.CODEC.optionalFieldOf("feedReturnItem").forGetter(BeeBreedData::feedReturnItem),
Codec.intRange(1, Integer.MAX_VALUE).fieldOf("feedAmount").orElse(1).forGetter(BeeBreedData::feedAmount),
Codec.intRange(Integer.MIN_VALUE, 0).fieldOf("childGrowthDelay").orElse(BeeConstants.CHILD_GROWTH_DELAY).forGetter(BeeBreedData::childGrowthDelay),
Codec.intRange(0, Integer.MAX_VALUE).fieldOf("breedDelay").orElse(BeeConstants.BREED_DELAY).forGetter(BeeBreedData::breedDelay)
CodecExtras.POSITIVE_INT.optionalFieldOf("feedAmount", 1).forGetter(BeeBreedData::feedAmount),
CodecExtras.NON_POSITIVE_INT.optionalFieldOf("childGrowthDelay", BeeConstants.CHILD_GROWTH_DELAY).forGetter(BeeBreedData::childGrowthDelay),
ExtraCodecs.NON_NEGATIVE_INT.optionalFieldOf("breedDelay", BeeConstants.BREED_DELAY).forGetter(BeeBreedData::breedDelay)
).apply(instance, BreedData::new));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.teamresourceful.resourcefulbees.common.util.ModResourceLocation;
import com.teamresourceful.resourcefullib.common.collections.WeightedCollection;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.world.level.Level;

import java.util.Map;
Expand All @@ -17,7 +18,7 @@ public record MutationData(int count, ResourceLocation id) implements BeeMutatio

private static final BeeMutationData DEFAULT = new MutationData(0, new ModResourceLocation("empty"));
private static final Codec<BeeMutationData> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.intRange(1, Integer.MAX_VALUE).fieldOf("count").orElse(10).forGetter(BeeMutationData::count),
ExtraCodecs.POSITIVE_INT.optionalFieldOf("count", 10).forGetter(BeeMutationData::count),
ResourceLocation.CODEC.fieldOf("mutation").forGetter(BeeMutationData::id)
).apply(instance, MutationData::new));

Expand Down
Loading

0 comments on commit dc2a056

Please sign in to comment.