diff --git a/src/main/java/com/portingdeadmods/nautec/NTRegistries.java b/src/main/java/com/portingdeadmods/nautec/NTRegistries.java index aebc1fed..2e7f08df 100644 --- a/src/main/java/com/portingdeadmods/nautec/NTRegistries.java +++ b/src/main/java/com/portingdeadmods/nautec/NTRegistries.java @@ -2,6 +2,7 @@ import com.portingdeadmods.nautec.api.augments.AugmentSlot; import com.portingdeadmods.nautec.api.augments.AugmentType; +import com.portingdeadmods.nautec.api.bacteria.BacteriaSerializer; import com.portingdeadmods.nautec.api.multiblocks.Multiblock; import com.portingdeadmods.nautec.api.bacteria.Bacteria; import net.minecraft.core.Registry; @@ -15,18 +16,11 @@ public final class NTRegistries { private static final ResourceKey>> AUGMENT_TYPE_KEY = ResourceKey.createRegistryKey(ResourceLocation.fromNamespaceAndPath(Nautec.MODID, "augment_type")); public static final ResourceKey> AUGMENT_SLOT_KEY = ResourceKey.createRegistryKey(ResourceLocation.fromNamespaceAndPath(Nautec.MODID, "augment_slot")); private static final ResourceKey> MULTIBLOCK_KEY = ResourceKey.createRegistryKey(ResourceLocation.fromNamespaceAndPath(Nautec.MODID, "multiblock")); + public static final ResourceKey>> BACTERIA_SERIALIZER_KEY = ResourceKey.createRegistryKey(ResourceLocation.fromNamespaceAndPath(Nautec.MODID, "bacteria_serializer")); public static final ResourceKey> BACTERIA_KEY = ResourceKey.createRegistryKey(ResourceLocation.fromNamespaceAndPath(Nautec.MODID, "bacteria")); public static final Registry> AUGMENT_TYPE = new RegistryBuilder<>(AUGMENT_TYPE_KEY).create(); public static final Registry AUGMENT_SLOT = new RegistryBuilder<>(AUGMENT_SLOT_KEY).create(); public static final Registry MULTIBLOCK = new RegistryBuilder<>(MULTIBLOCK_KEY).create(); - - @SubscribeEvent - public static void registerDatapackRegistries(DataPackRegistryEvent.NewRegistry event) { - event.dataPackRegistry( - BACTERIA_KEY, - Bacteria.CODEC, - Bacteria.CODEC - ); - } + public static final Registry> BACTERIA_SERIALIZER = new RegistryBuilder<>(BACTERIA_SERIALIZER_KEY).create(); } diff --git a/src/main/java/com/portingdeadmods/nautec/api/bacteria/Bacteria.java b/src/main/java/com/portingdeadmods/nautec/api/bacteria/Bacteria.java index 2ec2275b..fb2c7c45 100644 --- a/src/main/java/com/portingdeadmods/nautec/api/bacteria/Bacteria.java +++ b/src/main/java/com/portingdeadmods/nautec/api/bacteria/Bacteria.java @@ -1,17 +1,18 @@ package com.portingdeadmods.nautec.api.bacteria; import com.mojang.serialization.Codec; -import com.mojang.serialization.codecs.RecordCodecBuilder; -import com.portingdeadmods.nautec.Nautec; -import com.portingdeadmods.nautec.utils.codec.CodecUtils; +import com.portingdeadmods.nautec.NTRegistries; +import io.netty.buffer.ByteBuf; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.Item; import net.minecraft.world.item.Items; public interface Bacteria { + Codec CODEC = NTRegistries.BACTERIA_SERIALIZER.byNameCodec().dispatch(Bacteria::getSerializer, BacteriaSerializer::mapCodec); + StreamCodec STREAM_CODEC = ByteBufCodecs.registry(NTRegistries.BACTERIA_SERIALIZER_KEY).dispatch(Bacteria::getSerializer, BacteriaSerializer::streamCodec); + /* Bacteria! We begin with only one! Bacteria! Two's what we then become! @@ -23,60 +24,49 @@ public interface Bacteria { Bacteria! We will take over the world! */ ResourceLocation id(); - Item type(); - float growthRate(); - float mutationResistance(); - float productionRate(); - int lifespan(); - int color(); - default byte getAlpha() { - return (byte) ((color() >> 24) & 0xFF); - } + BacteriaStats stats(); - default byte getRed() { - return (byte) ((color() >> 16) & 0xFF); - } + BacteriaSerializer getSerializer(); - default byte getGreen() { - return (byte) ((color() >> 8) & 0xFF); + static Builder of() { + return new Builder(); } - default byte getBlue() { - return (byte) (color() & 0xFF); - } + class Builder { + private float growthRate; + private float mutationResistance; + private float productionRate; + private int lifespan; + private int color; + + public Builder growthRate(float growthRate) { + this.growthRate = growthRate; + return this; + } - Codec CODEC = RecordCodecBuilder.create( - instance -> instance.group( - ResourceLocation.CODEC.fieldOf("id").forGetter(Bacteria::id), - CodecUtils.ITEM_CODEC.fieldOf("type").forGetter(Bacteria::type), - Codec.FLOAT.fieldOf("growth_rate").forGetter(Bacteria::growthRate), - Codec.FLOAT.fieldOf("mutation_resistance").forGetter(Bacteria::mutationResistance), - Codec.FLOAT.fieldOf("production_rate").forGetter(Bacteria::productionRate), - Codec.INT.fieldOf("lifespan").forGetter(Bacteria::lifespan), - Codec.INT.fieldOf("color").forGetter(Bacteria::color) - ).apply(instance, BacteriaImpl::new) - ); + public Builder mutationResistance(float mutationResistance) { + this.mutationResistance = mutationResistance; + return this; + } - StreamCodec STREAM_CODEC = StreamCodec.composite( - CodecUtils.ITEM_STREAM_CODEC, - Bacteria::type, - ByteBufCodecs.FLOAT, - Bacteria::growthRate, - ByteBufCodecs.FLOAT, - Bacteria::mutationResistance, - ByteBufCodecs.FLOAT, - Bacteria::productionRate, - ByteBufCodecs.INT, - Bacteria::lifespan, - ByteBufCodecs.INT, - Bacteria::color, - (a, b, c, d, e, f) -> new BacteriaImpl(Nautec.rl("empty"), a, b, c, d, e, f) - ); + public Builder productionRate(float productionRate) { + this.productionRate = productionRate; + return this; + } + + public Builder lifespan(int lifespan) { + this.lifespan = lifespan; + return this; + } + + public Builder color(int color) { + this.color = color; + return this; + } - class Builder { public Bacteria build(ResourceLocation location) { - return new BacteriaImpl(location, Items.AIR, 0, 0, 0, 0, 0); + return new BacteriaImpl(location, new BacteriaStats(Items.AIR, growthRate, mutationResistance, productionRate, lifespan, color)); } } } diff --git a/src/main/java/com/portingdeadmods/nautec/api/bacteria/BacteriaImpl.java b/src/main/java/com/portingdeadmods/nautec/api/bacteria/BacteriaImpl.java index c26c0af4..96150dd5 100644 --- a/src/main/java/com/portingdeadmods/nautec/api/bacteria/BacteriaImpl.java +++ b/src/main/java/com/portingdeadmods/nautec/api/bacteria/BacteriaImpl.java @@ -3,32 +3,9 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.Item; -public record BacteriaImpl( - ResourceLocation id, - Item type, - float growthRate, - float mutationResistance, - float productionRate, - int lifespan, - int color) implements Bacteria { - - @Override - public byte getAlpha() { - return (byte) ((color >> 24) & 0xFF); - } - - @Override - public byte getRed() { - return (byte) ((color >> 16) & 0xFF); - } - - @Override - public byte getGreen() { - return (byte) ((color >> 8) & 0xFF); - } - +public record BacteriaImpl(ResourceLocation id, BacteriaStats stats) implements Bacteria { @Override - public byte getBlue() { - return (byte) (color & 0xFF); + public BacteriaSerializer getSerializer() { + return null; } } diff --git a/src/main/java/com/portingdeadmods/nautec/api/bacteria/BacteriaSerializer.java b/src/main/java/com/portingdeadmods/nautec/api/bacteria/BacteriaSerializer.java new file mode 100644 index 00000000..f4c909b4 --- /dev/null +++ b/src/main/java/com/portingdeadmods/nautec/api/bacteria/BacteriaSerializer.java @@ -0,0 +1,12 @@ +package com.portingdeadmods.nautec.api.bacteria; + +import com.mojang.serialization.MapCodec; +import io.netty.buffer.ByteBuf; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; + +public interface BacteriaSerializer { + MapCodec mapCodec(); + + StreamCodec streamCodec(); +} diff --git a/src/main/java/com/portingdeadmods/nautec/api/bacteria/BacteriaStats.java b/src/main/java/com/portingdeadmods/nautec/api/bacteria/BacteriaStats.java new file mode 100644 index 00000000..2c612bde --- /dev/null +++ b/src/main/java/com/portingdeadmods/nautec/api/bacteria/BacteriaStats.java @@ -0,0 +1,45 @@ +package com.portingdeadmods.nautec.api.bacteria; + +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import com.portingdeadmods.nautec.Nautec; +import com.portingdeadmods.nautec.utils.codec.CodecUtils; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.Item; + +public record BacteriaStats(Item type, + float growthRate, + float mutationResistance, + float productionRate, + int lifespan, + int color) { + public static final Codec CODEC = RecordCodecBuilder.create( + instance -> instance.group( + CodecUtils.ITEM_CODEC.fieldOf("type").forGetter(BacteriaStats::type), + Codec.FLOAT.fieldOf("growth_rate").forGetter(BacteriaStats::growthRate), + Codec.FLOAT.fieldOf("mutation_resistance").forGetter(BacteriaStats::mutationResistance), + Codec.FLOAT.fieldOf("production_rate").forGetter(BacteriaStats::productionRate), + Codec.INT.fieldOf("lifespan").forGetter(BacteriaStats::lifespan), + Codec.INT.fieldOf("color").forGetter(BacteriaStats::color) + ).apply(instance, BacteriaStats::new) + ); + + public static final StreamCodec STREAM_CODEC = StreamCodec.composite( + CodecUtils.ITEM_STREAM_CODEC, + BacteriaStats::type, + ByteBufCodecs.FLOAT, + BacteriaStats::growthRate, + ByteBufCodecs.FLOAT, + BacteriaStats::mutationResistance, + ByteBufCodecs.FLOAT, + BacteriaStats::productionRate, + ByteBufCodecs.INT, + BacteriaStats::lifespan, + ByteBufCodecs.INT, + BacteriaStats::color, + BacteriaStats::new + ); +} diff --git a/src/main/java/com/portingdeadmods/nautec/data/components/ComponentBacteriaStorage.java b/src/main/java/com/portingdeadmods/nautec/data/components/ComponentBacteriaStorage.java index 0efdece1..7ee6a8ae 100644 --- a/src/main/java/com/portingdeadmods/nautec/data/components/ComponentBacteriaStorage.java +++ b/src/main/java/com/portingdeadmods/nautec/data/components/ComponentBacteriaStorage.java @@ -2,54 +2,21 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; -import com.portingdeadmods.nautec.Nautec; +import com.portingdeadmods.nautec.NTRegistries; import com.portingdeadmods.nautec.api.bacteria.Bacteria; +import com.portingdeadmods.nautec.registries.NTBacterias; +import com.portingdeadmods.nautec.utils.BacteriaHelper; +import net.minecraft.core.HolderLookup; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.Item; -import net.minecraft.world.item.Items; import java.util.Objects; +import java.util.function.Function; public record ComponentBacteriaStorage(Bacteria bacteria, long bacteriaAmount) { - public static final ComponentBacteriaStorage EMPTY = new ComponentBacteriaStorage(new Bacteria() { - @Override - public ResourceLocation id() { - return Nautec.rl("empty"); - } - - @Override - public Item type() { - return Items.AIR; - } - - @Override - public float growthRate() { - return 0; - } - - @Override - public float mutationResistance() { - return 0; - } - - @Override - public float productionRate() { - return 0; - } - - @Override - public int lifespan() { - return 0; - } - - @Override - public int color() { - return 0; - } - }, 0); + public static final Function EMPTY = + lookup -> new ComponentBacteriaStorage(BacteriaHelper.getBacteria(lookup, NTBacterias.EMPTY), 0); public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( Bacteria.CODEC.fieldOf("bacteriaType").forGetter(ComponentBacteriaStorage::bacteria), Codec.LONG.fieldOf("bacteriaAmount").forGetter(ComponentBacteriaStorage::bacteriaAmount) diff --git a/src/main/java/com/portingdeadmods/nautec/datagen/DatapackRegistryProvider.java b/src/main/java/com/portingdeadmods/nautec/datagen/DatapackRegistryProvider.java index 2e05c8d6..3d6cf583 100644 --- a/src/main/java/com/portingdeadmods/nautec/datagen/DatapackRegistryProvider.java +++ b/src/main/java/com/portingdeadmods/nautec/datagen/DatapackRegistryProvider.java @@ -2,7 +2,7 @@ import com.portingdeadmods.nautec.NTRegistries; import com.portingdeadmods.nautec.Nautec; -import com.portingdeadmods.nautec.registries.NTBacteria; +import com.portingdeadmods.nautec.registries.NTBacterias; import com.portingdeadmods.nautec.registries.NTBlocks; import net.minecraft.core.HolderGetter; import net.minecraft.core.HolderLookup; @@ -50,7 +50,7 @@ public DatapackRegistryProvider(PackOutput output, CompletableFuture { diff --git a/src/main/java/com/portingdeadmods/nautec/registries/NTBacteria.java b/src/main/java/com/portingdeadmods/nautec/registries/NTBacterias.java similarity index 96% rename from src/main/java/com/portingdeadmods/nautec/registries/NTBacteria.java rename to src/main/java/com/portingdeadmods/nautec/registries/NTBacterias.java index 6c5a7e8f..26b11ba3 100644 --- a/src/main/java/com/portingdeadmods/nautec/registries/NTBacteria.java +++ b/src/main/java/com/portingdeadmods/nautec/registries/NTBacterias.java @@ -6,7 +6,7 @@ import net.minecraft.data.worldgen.BootstrapContext; import net.minecraft.resources.ResourceKey; -public class NTBacteria { +public class NTBacterias { public static final ResourceKey EMPTY = key("empty"); public static void bootstrap(BootstrapContext context) { diff --git a/src/main/java/com/portingdeadmods/nautec/registries/NTCreativeTabs.java b/src/main/java/com/portingdeadmods/nautec/registries/NTCreativeTabs.java index 7bd0e2e4..44af1947 100644 --- a/src/main/java/com/portingdeadmods/nautec/registries/NTCreativeTabs.java +++ b/src/main/java/com/portingdeadmods/nautec/registries/NTCreativeTabs.java @@ -38,7 +38,7 @@ public final class NTCreativeTabs { if (item.asItem() instanceof IBacteriaItem) { ItemStack stack = new ItemStack(item); - stack.set(NTDataComponents.BACTERIA, Optional.of(ComponentBacteriaStorage.EMPTY)); + stack.set(NTDataComponents.BACTERIA, Optional.of(ComponentBacteriaStorage.EMPTY.apply(params.holders()))); output.accept(item); output.accept(stack); } diff --git a/src/main/java/com/portingdeadmods/nautec/utils/BacteriaHelper.java b/src/main/java/com/portingdeadmods/nautec/utils/BacteriaHelper.java new file mode 100644 index 00000000..6ec8d8ef --- /dev/null +++ b/src/main/java/com/portingdeadmods/nautec/utils/BacteriaHelper.java @@ -0,0 +1,12 @@ +package com.portingdeadmods.nautec.utils; + +import com.portingdeadmods.nautec.NTRegistries; +import com.portingdeadmods.nautec.api.bacteria.Bacteria; +import net.minecraft.core.HolderLookup; +import net.minecraft.resources.ResourceKey; + +public final class BacteriaHelper { + public static Bacteria getBacteria(HolderLookup.Provider lookup, ResourceKey bacteriaType) { + return lookup.asGetterLookup().lookupOrThrow(NTRegistries.BACTERIA_KEY).getOrThrow(bacteriaType).value(); + } +}