From e76feffd3c251a97fbfa4176c3b42e2b106dc672 Mon Sep 17 00:00:00 2001 From: thepigcat Date: Sat, 21 Dec 2024 13:32:51 +0100 Subject: [PATCH] bacteria overhaul --- .../103d9f3f36b01595f1aa5172191e60eff02e6924 | 12 +- .../4d58d921702f57799841730d3d537abc5ea398a2 | 4 +- .../nautec/models/item/bio_reactor_part.json | 3 + .../nautec/nautec/bacteria/cyanobacteria.json | 6 +- .../data/nautec/nautec/bacteria/empty.json | 11 +- .../nautec/nautec/bacteria/halobacteria.json | 6 +- .../nautec/nautec/bacteria/methanogens.json | 6 +- .../nautec/nautec/bacteria/thermophiles.json | 6 +- .../portingdeadmods/nautec/NTRegistries.java | 21 ++- .../com/portingdeadmods/nautec/Nautec.java | 2 + .../portingdeadmods/nautec/NautecClient.java | 6 +- .../nautec/api/bacteria/Bacteria.java | 51 +++++-- .../nautec/api/bacteria/BacteriaInstance.java | 72 +++++++++ .../nautec/api/bacteria/BacteriaStats.java | 45 ++++++ .../api/bacteria/BacteriaStatsSerializer.java | 11 ++ .../api/bacteria/BaseBacteriaStats.java | 35 ----- .../blockentities/ContainerBlockEntity.java | 15 +- .../bacteria/BacteriaStorage.java | 76 ++++++---- .../bacteria/IBacteriaStorage.java | 18 ++- .../bacteria/ItemBacteriaWrapper.java | 21 +-- .../content/bacteria/BacteriaStats.java | 118 --------------- .../content/bacteria/EmptyBacteria.java | 48 ++++++ .../content/bacteria/SimpleBacteria.java | 27 ++-- .../content/bacteria/SimpleBacteriaStats.java | 138 ++++++++++++++++++ .../blockentities/MutatorBlockEntity.java | 14 +- .../nautec/content/items/PetriDishItem.java | 6 +- .../nautec/data/NTDataComponentsUtils.java | 2 +- .../components/ComponentBacteriaStorage.java | 31 ++-- .../registries/NTBacteriaSerializers.java | 2 + .../NTBacteriaStatsSerializers.java | 17 +++ .../nautec/registries/NTBacterias.java | 7 +- .../nautec/registries/NTCreativeTabs.java | 9 +- 32 files changed, 536 insertions(+), 310 deletions(-) create mode 100644 src/generated/resources/assets/nautec/models/item/bio_reactor_part.json create mode 100644 src/main/java/com/portingdeadmods/nautec/api/bacteria/BacteriaInstance.java create mode 100644 src/main/java/com/portingdeadmods/nautec/api/bacteria/BacteriaStats.java create mode 100644 src/main/java/com/portingdeadmods/nautec/api/bacteria/BacteriaStatsSerializer.java delete mode 100644 src/main/java/com/portingdeadmods/nautec/api/bacteria/BaseBacteriaStats.java delete mode 100644 src/main/java/com/portingdeadmods/nautec/content/bacteria/BacteriaStats.java create mode 100644 src/main/java/com/portingdeadmods/nautec/content/bacteria/EmptyBacteria.java create mode 100644 src/main/java/com/portingdeadmods/nautec/content/bacteria/SimpleBacteriaStats.java create mode 100644 src/main/java/com/portingdeadmods/nautec/registries/NTBacteriaStatsSerializers.java diff --git a/src/generated/resources/.cache/103d9f3f36b01595f1aa5172191e60eff02e6924 b/src/generated/resources/.cache/103d9f3f36b01595f1aa5172191e60eff02e6924 index 4b327f0..fe2b7fb 100644 --- a/src/generated/resources/.cache/103d9f3f36b01595f1aa5172191e60eff02e6924 +++ b/src/generated/resources/.cache/103d9f3f36b01595f1aa5172191e60eff02e6924 @@ -1,9 +1,9 @@ -// 1.21.1 2024-12-19T15:16:06.7633778 Registries -4cc74761a420946768852a3c4e5fba64de831555 data/nautec/nautec/bacteria/cyanobacteria.json -2ceb7d9533a35c6cb05ba68915ecaf868def152a data/nautec/nautec/bacteria/empty.json -ce644e1811997ff67d2f7df856532017272a7b22 data/nautec/nautec/bacteria/halobacteria.json -36808ce13b558579e40af12e305adf733c501003 data/nautec/nautec/bacteria/methanogens.json -165e8d4b95ee84a6c951b35561ba6808bf60c49c data/nautec/nautec/bacteria/thermophiles.json +// 1.21.1 2024-12-21T13:29:07.948008452 Registries +731c134d2749f365fc9a6d44d8d6e400e77eec49 data/nautec/nautec/bacteria/cyanobacteria.json +6e0101d77b1f0382041425952ff4b8544733f2e2 data/nautec/nautec/bacteria/empty.json +81c72e6a3cfb78f800e627cdad79edbb044de4a1 data/nautec/nautec/bacteria/halobacteria.json +09d49475a6a9fffaf92674fc3f0e4df1f76ea94d data/nautec/nautec/bacteria/methanogens.json +f8693e008a409782cc34263ff0b2c2c61c99a3cc data/nautec/nautec/bacteria/thermophiles.json d3949aab832fc1b72c2512c35586c2682dea4d4d data/nautec/worldgen/configured_feature/prismarine_sand_beach.json e9f0c0ce377bc6f731c3b36673e845152a53327d data/nautec/worldgen/configured_feature/prismarine_sand_ocean.json 685d14b2486e0da05c931e69fe78236dd9523cbf data/nautec/worldgen/placed_feature/prismarine_sand_beach.json diff --git a/src/generated/resources/.cache/4d58d921702f57799841730d3d537abc5ea398a2 b/src/generated/resources/.cache/4d58d921702f57799841730d3d537abc5ea398a2 index 3454d0b..3c712ad 100644 --- a/src/generated/resources/.cache/4d58d921702f57799841730d3d537abc5ea398a2 +++ b/src/generated/resources/.cache/4d58d921702f57799841730d3d537abc5ea398a2 @@ -1,4 +1,4 @@ -// 1.21.1 2024-12-18T15:31:36.54862117 Item Models: nautec +// 1.21.1 2024-12-21T13:22:19.561016732 Item Models: nautec 53692692f57914fc54eb6ebf416ce1cb7789e57b assets/nautec/models/item/air_bottle.json cc6ee3887ec6e0c53216dc681ac7b32b34182253 assets/nautec/models/item/anchor.json 2e336fce57acffa1d0eb621baa30a26f665b4924 assets/nautec/models/item/aquarine_steel_axe.json @@ -26,6 +26,8 @@ a95dff0d56139e74443ee5069394ccbe31bc875a assets/nautec/models/item/atlantic_gold f616877025b33a5d417689bd59cba3385b11e5c9 assets/nautec/models/item/augmentation_station_extension.json 8d57f0feda40d2aa883370a346a266cf9d149895 assets/nautec/models/item/bacterial_containment_shield.json cd68bca3502f5a671aa5c4bbd828397047b19b8b assets/nautec/models/item/bio_reactor.json +53fad2021e8228bda58efa92544f377f7543832b assets/nautec/models/item/bio_reactor_part.json +cc14d169c8083ba22e2cf9eeae1842532d664950 assets/nautec/models/item/breaker.json b1e3c9080ae5a0f78b06fa90f3c5da5b3805f842 assets/nautec/models/item/broken_whisk.json cf1524c962ddbbf1cb5e8782d764e71888a03eb1 assets/nautec/models/item/brown_polymer.json 888a9dd92c350f3174e3cb52f714096944855bd5 assets/nautec/models/item/brown_polymer_block.json diff --git a/src/generated/resources/assets/nautec/models/item/bio_reactor_part.json b/src/generated/resources/assets/nautec/models/item/bio_reactor_part.json new file mode 100644 index 0000000..0fad492 --- /dev/null +++ b/src/generated/resources/assets/nautec/models/item/bio_reactor_part.json @@ -0,0 +1,3 @@ +{ + "parent": "nautec:block/bio_reactor_part" +} \ No newline at end of file diff --git a/src/generated/resources/data/nautec/nautec/bacteria/cyanobacteria.json b/src/generated/resources/data/nautec/nautec/bacteria/cyanobacteria.json index 03b1bc9..e8b1575 100644 --- a/src/generated/resources/data/nautec/nautec/bacteria/cyanobacteria.json +++ b/src/generated/resources/data/nautec/nautec/bacteria/cyanobacteria.json @@ -1,12 +1,12 @@ { "type": "nautec:simple", + "resource": "minecraft:iron_ingot", "stats": { - "colony_size": 0.0, + "type": "nautec:simple", "color": -13434881, "growth_rate": 0.0, "lifespan": 0, "mutation_resistance": 0.0, - "production_rate": 0.0, - "resource": "minecraft:iron_ingot" + "production_rate": 0.0 } } \ No newline at end of file diff --git a/src/generated/resources/data/nautec/nautec/bacteria/empty.json b/src/generated/resources/data/nautec/nautec/bacteria/empty.json index ee4bd21..87fc783 100644 --- a/src/generated/resources/data/nautec/nautec/bacteria/empty.json +++ b/src/generated/resources/data/nautec/nautec/bacteria/empty.json @@ -1,12 +1,3 @@ { - "type": "nautec:simple", - "stats": { - "colony_size": 0.0, - "color": 0, - "growth_rate": 0.0, - "lifespan": 0, - "mutation_resistance": 0.0, - "production_rate": 0.0, - "resource": "minecraft:air" - } + "type": "nautec:empty" } \ No newline at end of file diff --git a/src/generated/resources/data/nautec/nautec/bacteria/halobacteria.json b/src/generated/resources/data/nautec/nautec/bacteria/halobacteria.json index e23a7fc..28e0c3d 100644 --- a/src/generated/resources/data/nautec/nautec/bacteria/halobacteria.json +++ b/src/generated/resources/data/nautec/nautec/bacteria/halobacteria.json @@ -1,12 +1,12 @@ { "type": "nautec:simple", + "resource": "minecraft:gold_ingot", "stats": { - "colony_size": 0.0, + "type": "nautec:simple", "color": -6912, "growth_rate": 0.0, "lifespan": 0, "mutation_resistance": 0.0, - "production_rate": 0.0, - "resource": "minecraft:gold_ingot" + "production_rate": 0.0 } } \ No newline at end of file diff --git a/src/generated/resources/data/nautec/nautec/bacteria/methanogens.json b/src/generated/resources/data/nautec/nautec/bacteria/methanogens.json index 82c2146..e8dd36c 100644 --- a/src/generated/resources/data/nautec/nautec/bacteria/methanogens.json +++ b/src/generated/resources/data/nautec/nautec/bacteria/methanogens.json @@ -1,12 +1,12 @@ { "type": "nautec:simple", + "resource": "minecraft:cooked_beef", "stats": { - "colony_size": 0.0, + "type": "nautec:simple", "color": -1328403, "growth_rate": 0.0, "lifespan": 0, "mutation_resistance": 0.0, - "production_rate": 0.0, - "resource": "minecraft:cooked_beef" + "production_rate": 0.0 } } \ No newline at end of file diff --git a/src/generated/resources/data/nautec/nautec/bacteria/thermophiles.json b/src/generated/resources/data/nautec/nautec/bacteria/thermophiles.json index 3d5847a..234f0e0 100644 --- a/src/generated/resources/data/nautec/nautec/bacteria/thermophiles.json +++ b/src/generated/resources/data/nautec/nautec/bacteria/thermophiles.json @@ -1,12 +1,12 @@ { "type": "nautec:simple", + "resource": "minecraft:lava_bucket", "stats": { - "colony_size": 0.0, + "type": "nautec:simple", "color": -65536, "growth_rate": 0.0, "lifespan": 0, "mutation_resistance": 0.0, - "production_rate": 0.0, - "resource": "minecraft:lava_bucket" + "production_rate": 0.0 } } \ No newline at end of file diff --git a/src/main/java/com/portingdeadmods/nautec/NTRegistries.java b/src/main/java/com/portingdeadmods/nautec/NTRegistries.java index 2e7f08d..22d0610 100644 --- a/src/main/java/com/portingdeadmods/nautec/NTRegistries.java +++ b/src/main/java/com/portingdeadmods/nautec/NTRegistries.java @@ -3,24 +3,31 @@ 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.bacteria.BacteriaStatsSerializer; import com.portingdeadmods.nautec.api.multiblocks.Multiblock; import com.portingdeadmods.nautec.api.bacteria.Bacteria; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; -import net.neoforged.bus.api.SubscribeEvent; -import net.neoforged.neoforge.registries.DataPackRegistryEvent; import net.neoforged.neoforge.registries.RegistryBuilder; 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")); + 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_STATS_SERIALIZER_KEY = + ResourceKey.createRegistryKey(ResourceLocation.fromNamespaceAndPath(Nautec.MODID, "bacteria_stats_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(); public static final Registry> BACTERIA_SERIALIZER = new RegistryBuilder<>(BACTERIA_SERIALIZER_KEY).create(); + public static final Registry> BACTERIA_STATS_SERIALIZER = new RegistryBuilder<>(BACTERIA_STATS_SERIALIZER_KEY).create(); } diff --git a/src/main/java/com/portingdeadmods/nautec/Nautec.java b/src/main/java/com/portingdeadmods/nautec/Nautec.java index 6198525..402b018 100644 --- a/src/main/java/com/portingdeadmods/nautec/Nautec.java +++ b/src/main/java/com/portingdeadmods/nautec/Nautec.java @@ -59,6 +59,7 @@ public Nautec(IEventBus modEventBus, ModContainer modContainer) { event.register(NTRegistries.AUGMENT_SLOT); event.register(NTRegistries.AUGMENT_TYPE); event.register(NTRegistries.BACTERIA_SERIALIZER); + event.register(NTRegistries.BACTERIA_STATS_SERIALIZER); }); modEventBus.addListener(DataPackRegistryEvent.NewRegistry.class, event -> { @@ -82,6 +83,7 @@ public Nautec(IEventBus modEventBus, ModContainer modContainer) { NTStructures.STRUCTURES.register(modEventBus); NTLootModifier.LOOT_MODIFIERS.register(modEventBus); NTBacteriaSerializers.SERIALIZERS.register(modEventBus); + NTBacteriaStatsSerializers.SERIALIZERS.register(modEventBus); NTFluids.HELPER.register(modEventBus); diff --git a/src/main/java/com/portingdeadmods/nautec/NautecClient.java b/src/main/java/com/portingdeadmods/nautec/NautecClient.java index fae1f55..6b03158 100644 --- a/src/main/java/com/portingdeadmods/nautec/NautecClient.java +++ b/src/main/java/com/portingdeadmods/nautec/NautecClient.java @@ -33,6 +33,7 @@ import net.minecraft.client.renderer.FogRenderer; import net.minecraft.client.renderer.entity.ThrownTridentRenderer; import net.minecraft.client.renderer.item.ItemProperties; +import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.resources.ResourceManagerReloadListener; import net.minecraft.util.FastColor; @@ -225,8 +226,9 @@ private void onFMLClientSetupEvent(final FMLClientSetupEvent event) { private void registerColorHandlers(RegisterColorHandlersEvent.Item event) { event.register((stack, layer) -> { - Bacteria bacteria = BacteriaHelper.getBacteria(Minecraft.getInstance().level.registryAccess(), stack.get(NTDataComponents.BACTERIA).bacteria()); - return layer == 1 ? bacteria.stats().color() : -1; + ResourceKey bacteriaType = stack.get(NTDataComponents.BACTERIA).bacteriaInstance().getBacteria(); + Bacteria bacteria = BacteriaHelper.getBacteria(Minecraft.getInstance().level.registryAccess(), bacteriaType); + return layer == 1 ? bacteria.initialStats().color() : -1; }, NTItems.PETRI_DISH); event.register(new DynamicFluidContainerModel.Colors(), NTFluids.SALT_WATER.getBucket()); } 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 dc0334b..3b819fd 100644 --- a/src/main/java/com/portingdeadmods/nautec/api/bacteria/Bacteria.java +++ b/src/main/java/com/portingdeadmods/nautec/api/bacteria/Bacteria.java @@ -2,25 +2,23 @@ import com.mojang.serialization.Codec; import com.portingdeadmods.nautec.NTRegistries; -import com.portingdeadmods.nautec.content.bacteria.BacteriaStats; -import com.portingdeadmods.nautec.content.bacteria.SimpleBacteria; +import com.portingdeadmods.nautec.utils.codec.CodecUtils; 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.ResourceKey; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.Items; +import net.minecraft.world.item.Item; /** -Bacteria! We begin with only one! -Bacteria! Two's what we then become! -Bacteria! Each of us becomes two more! -Bacteria! We are stronger than before! -Bacteria! We keep growing at this rate! -Bacteria! No longer shall we wait! -Bacteria! The plan now unfolds! -Bacteria! We will take over the world! + * Bacteria! We begin with only one! + * Bacteria! Two's what we then become! + * Bacteria! Each of us becomes two more! + * Bacteria! We are stronger than before! + * Bacteria! We keep growing at this rate! + * Bacteria! No longer shall we wait! + * Bacteria! The plan now unfolds! + * Bacteria! We will take over the world! */ public interface Bacteria { Codec> BACTERIA_TYPE_CODEC = ResourceKey.codec(NTRegistries.BACTERIA_KEY); @@ -29,7 +27,34 @@ 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); - BaseBacteriaStats stats(); + Resource resource(); + + BacteriaStats initialStats(); BacteriaSerializer getSerializer(); + + interface Resource { + Codec codec(); + + StreamCodec streamCodec(); + + record ItemResource(Item item) implements Resource { + public static final Codec CODEC = CodecUtils.ITEM_CODEC.xmap(ItemResource::new, ItemResource::item); + public static final StreamCodec STREAM_CODEC = CodecUtils.ITEM_STREAM_CODEC.map(ItemResource::new, ItemResource::item); + + @Override + public Codec codec() { + return CODEC; + } + + @Override + public StreamCodec streamCodec() { + return STREAM_CODEC; + } + } + } + + interface Builder { + T build(); + } } diff --git a/src/main/java/com/portingdeadmods/nautec/api/bacteria/BacteriaInstance.java b/src/main/java/com/portingdeadmods/nautec/api/bacteria/BacteriaInstance.java new file mode 100644 index 0000000..25f4b5f --- /dev/null +++ b/src/main/java/com/portingdeadmods/nautec/api/bacteria/BacteriaInstance.java @@ -0,0 +1,72 @@ +package com.portingdeadmods.nautec.api.bacteria; + +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import com.portingdeadmods.nautec.content.bacteria.SimpleBacteriaStats; +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.ResourceKey; + +public final class BacteriaInstance { + public static final BacteriaInstance EMPTY = new BacteriaInstance(NTBacterias.EMPTY, SimpleBacteriaStats.EMPTY); + public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( + Bacteria.BACTERIA_TYPE_CODEC.fieldOf("bacteria").forGetter(BacteriaInstance::getBacteria), + Codec.LONG.fieldOf("amount").forGetter(BacteriaInstance::getAmount), + BacteriaStats.CODEC.fieldOf("stats").forGetter(BacteriaInstance::getStats) + ).apply(instance, BacteriaInstance::new)); + public static final StreamCodec STREAM_CODEC = StreamCodec.composite( + Bacteria.BACTERIA_TYPE_STREAM_CODEC, + BacteriaInstance::getBacteria, + ByteBufCodecs.VAR_LONG, + BacteriaInstance::getAmount, + BacteriaStats.STREAM_CODEC, + BacteriaInstance::getStats, + BacteriaInstance::new + ); + + private final ResourceKey bacteria; + private long amount; + private BacteriaStats stats; + + public BacteriaInstance(ResourceKey bacteria, HolderLookup.Provider lookup) { + this(bacteria, 1, BacteriaHelper.getBacteria(lookup, bacteria).initialStats()); + } + + public BacteriaInstance(ResourceKey bacteria, BacteriaStats stats) { + this(bacteria, 1, stats); + } + + public BacteriaInstance(ResourceKey bacteria, long amount, BacteriaStats stats) { + this.bacteria = bacteria; + this.amount = amount; + this.stats = stats; + } + + public void setAmount(long amount) { + this.amount = amount; + } + + public long getAmount() { + return this.amount; + } + + public ResourceKey getBacteria() { + return bacteria; + } + + public void setStats(BacteriaStats stats) { + this.stats = stats; + } + + public BacteriaStats getStats() { + return this.stats; + } + + public BacteriaInstance copy() { + return new BacteriaInstance(this.bacteria, amount, this.stats.copy()); + } +} 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 0000000..550c23f --- /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.portingdeadmods.nautec.NTRegistries; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.chat.Component; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; + +import java.util.List; + +public interface BacteriaStats { + Codec CODEC = + NTRegistries.BACTERIA_STATS_SERIALIZER.byNameCodec().dispatch(BacteriaStats::getSerializer, BacteriaStatsSerializer::mapCodec); + StreamCodec STREAM_CODEC = + ByteBufCodecs.registry(NTRegistries.BACTERIA_STATS_SERIALIZER_KEY).dispatch(BacteriaStats::getSerializer, BacteriaStatsSerializer::streamCodec); + + + float growthRate(); + + float mutationResistance(); + + float productionRate(); + + int lifespan(); + + int color(); + + BacteriaStats copy(); + + List statsTooltip(); + + BacteriaStatsSerializer getSerializer(); + + // Mutator + BacteriaStats rollGrowthRate(); + BacteriaStats rollMutationResistance(); + BacteriaStats rollProductionRate(); + BacteriaStats rollLifespan(); + BacteriaStats rollStats(); + + // Incubator + BacteriaStats grow(); + BacteriaStats shrink(); +} diff --git a/src/main/java/com/portingdeadmods/nautec/api/bacteria/BacteriaStatsSerializer.java b/src/main/java/com/portingdeadmods/nautec/api/bacteria/BacteriaStatsSerializer.java new file mode 100644 index 0000000..881f30f --- /dev/null +++ b/src/main/java/com/portingdeadmods/nautec/api/bacteria/BacteriaStatsSerializer.java @@ -0,0 +1,11 @@ +package com.portingdeadmods.nautec.api.bacteria; + +import com.mojang.serialization.MapCodec; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; + +public interface BacteriaStatsSerializer { + MapCodec mapCodec(); + + StreamCodec streamCodec(); +} \ No newline at end of file diff --git a/src/main/java/com/portingdeadmods/nautec/api/bacteria/BaseBacteriaStats.java b/src/main/java/com/portingdeadmods/nautec/api/bacteria/BaseBacteriaStats.java deleted file mode 100644 index 0205d49..0000000 --- a/src/main/java/com/portingdeadmods/nautec/api/bacteria/BaseBacteriaStats.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.portingdeadmods.nautec.api.bacteria; - -import net.minecraft.network.chat.Component; -import net.minecraft.world.item.Item; - -import java.util.List; - -public interface BaseBacteriaStats { - Item resource(); - - float growthRate(); - - float mutationResistance(); - - float productionRate(); - - float colonySize(); - - int lifespan(); - - int color(); - - List statsTooltip(); - - // Mutator - BaseBacteriaStats rollGrowthRate(); - BaseBacteriaStats rollMutationResistance(); - BaseBacteriaStats rollProductionRate(); - BaseBacteriaStats rollLifespan(); - BaseBacteriaStats rollStats(); - - // Incubator - BaseBacteriaStats grow(); - BaseBacteriaStats shrink(); -} diff --git a/src/main/java/com/portingdeadmods/nautec/api/blockentities/ContainerBlockEntity.java b/src/main/java/com/portingdeadmods/nautec/api/blockentities/ContainerBlockEntity.java index 50fef27..91c9a94 100644 --- a/src/main/java/com/portingdeadmods/nautec/api/blockentities/ContainerBlockEntity.java +++ b/src/main/java/com/portingdeadmods/nautec/api/blockentities/ContainerBlockEntity.java @@ -39,7 +39,9 @@ import java.util.List; import java.util.Map; import java.util.function.BiPredicate; +import java.util.function.Function; import java.util.function.Predicate; +import java.util.function.UnaryOperator; public abstract class ContainerBlockEntity extends BlockEntity { private @Nullable ItemStackHandler itemHandler; @@ -150,8 +152,15 @@ protected final void addItemHandler(int slots, int slotLimit) { protected final void addItemHandler(int slots, BiPredicate validation) { addItemHandler(slots, 64, validation); } + protected final void addItemHandler(int slots, UnaryOperator slotLimit) { + addItemHandler(slots, slotLimit, (slot, itemStack) -> true); + } protected final void addItemHandler(int slots, int slotLimit, BiPredicate validation) { + addItemHandler(slots, slot -> slotLimit, validation); + } + + protected final void addItemHandler(int slots, UnaryOperator slotLimit, BiPredicate validation) { this.itemHandler = new ItemStackHandler(slots) { @Override protected void onContentsChanged(int slot) { @@ -168,7 +177,7 @@ public boolean isItemValid(int slot, @NotNull ItemStack stack) { @Override public int getSlotLimit(int slot) { - return slotLimit; + return slotLimit.apply(slot); } @Override @@ -227,8 +236,8 @@ public void onEnergyChanged() { this.powerStorage.setPowerCapacity(powerCapacity); } - protected final void addBacteriaStorage() { - this.bacteriaStorage = new BacteriaStorage(); + protected final void addBacteriaStorage(int slots) { + this.bacteriaStorage = new BacteriaStorage(slots); } private void update() { diff --git a/src/main/java/com/portingdeadmods/nautec/capabilities/bacteria/BacteriaStorage.java b/src/main/java/com/portingdeadmods/nautec/capabilities/bacteria/BacteriaStorage.java index 27b17b0..295a189 100644 --- a/src/main/java/com/portingdeadmods/nautec/capabilities/bacteria/BacteriaStorage.java +++ b/src/main/java/com/portingdeadmods/nautec/capabilities/bacteria/BacteriaStorage.java @@ -1,10 +1,13 @@ package com.portingdeadmods.nautec.capabilities.bacteria; import com.mojang.datafixers.util.Pair; +import com.mojang.serialization.Codec; import com.mojang.serialization.DataResult; +import com.mojang.serialization.codecs.RecordCodecBuilder; import com.portingdeadmods.nautec.NTRegistries; import com.portingdeadmods.nautec.Nautec; import com.portingdeadmods.nautec.api.bacteria.Bacteria; +import com.portingdeadmods.nautec.api.bacteria.BacteriaInstance; import net.minecraft.core.HolderLookup; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.NbtOps; @@ -13,51 +16,66 @@ import net.neoforged.neoforge.common.util.INBTSerializable; import org.jetbrains.annotations.UnknownNullability; -public class BacteriaStorage implements IBacteriaStorage, INBTSerializable { - private ResourceKey bacteria; - private long bacteriaAmount; +import java.util.ArrayList; +import java.util.List; - @Override - public void setBacteria(ResourceKey bacteria) { +public class BacteriaStorage implements IBacteriaStorage, INBTSerializable { + private static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( + BacteriaInstance.CODEC.listOf().fieldOf("bacteria").forGetter(BacteriaStorage::getBacteria), + Codec.INT.fieldOf("slots").forGetter(BacteriaStorage::getBacteriaSlots) + ).apply(instance, BacteriaStorage::new)); + + private List bacteria; + private int slots; + + public BacteriaStorage(int slots) { + this.bacteria = new ArrayList<>(slots); + this.slots = slots; + } + + private BacteriaStorage(List bacteria, int slots) { this.bacteria = bacteria; + this.slots = slots; } @Override - public ResourceKey getBacteria() { - return this.bacteria; + public @UnknownNullability Tag serializeNBT(HolderLookup.Provider provider) { + DataResult tagDataResult = CODEC.encodeStart(NbtOps.INSTANCE, this); + if (tagDataResult.isSuccess()) { + return tagDataResult.result().get(); + } + Nautec.LOGGER.error("Error encoding BacteriaStorage: {}", tagDataResult.error().get().message()); + return null; } @Override - public void setBacteriaAmount(long bacteriaAmount) { - this.bacteriaAmount = bacteriaAmount; + public void deserializeNBT(HolderLookup.Provider provider, Tag nbt) { + DataResult> dataResult = CODEC.decode(NbtOps.INSTANCE, nbt); + if (dataResult.isSuccess()) { + BacteriaStorage newThis = dataResult.getOrThrow().getFirst(); + this.bacteria = newThis.bacteria; + this.slots = newThis.slots; + } else { + Nautec.LOGGER.error("Error decoding BacteriaStorage: {}", dataResult.error().get().message()); + } + } + + public List getBacteria() { + return bacteria; } @Override - public long getBacteriaAmount() { - return this.bacteriaAmount; + public void setBacteria(int slot, BacteriaInstance bacteriaInstance) { + this.bacteria.set(slot, bacteriaInstance); } @Override - public @UnknownNullability CompoundTag serializeNBT(HolderLookup.Provider provider) { - CompoundTag tag = new CompoundTag(); - tag.putLong("bacteriaAmount", this.bacteriaAmount); - DataResult tagDataResult = ResourceKey.codec(NTRegistries.BACTERIA_KEY).encodeStart(NbtOps.INSTANCE, this.bacteria); - if (tagDataResult.isSuccess()) { - tag.put("bacteriaType", tagDataResult.getOrThrow()); - } else { - Nautec.LOGGER.error("Failed to encode bacteria, {}", tagDataResult.error().get().message()); - } - return tag; + public BacteriaInstance getBacteria(int slot) { + return this.bacteria.get(slot); } @Override - public void deserializeNBT(HolderLookup.Provider provider, CompoundTag nbt) { - this.bacteriaAmount = nbt.getLong("bacteriaAmount"); - DataResult, Tag>> decodedBacteria = ResourceKey.codec(NTRegistries.BACTERIA_KEY).decode(NbtOps.INSTANCE, nbt.get("bacteriaType")); - if (decodedBacteria.isSuccess()) { - this.bacteria = decodedBacteria.result().get().getFirst(); - } else { - Nautec.LOGGER.error("Failed to decode bacteria, {}", decodedBacteria.error().get().message()); - } + public int getBacteriaSlots() { + return slots; } } diff --git a/src/main/java/com/portingdeadmods/nautec/capabilities/bacteria/IBacteriaStorage.java b/src/main/java/com/portingdeadmods/nautec/capabilities/bacteria/IBacteriaStorage.java index a19d4a5..2025b6b 100644 --- a/src/main/java/com/portingdeadmods/nautec/capabilities/bacteria/IBacteriaStorage.java +++ b/src/main/java/com/portingdeadmods/nautec/capabilities/bacteria/IBacteriaStorage.java @@ -1,14 +1,20 @@ package com.portingdeadmods.nautec.capabilities.bacteria; -import com.portingdeadmods.nautec.api.bacteria.Bacteria; -import net.minecraft.resources.ResourceKey; +import com.portingdeadmods.nautec.api.bacteria.BacteriaStats; +import com.portingdeadmods.nautec.api.bacteria.BacteriaInstance; + +import java.util.function.UnaryOperator; public interface IBacteriaStorage { - void setBacteria(ResourceKey bacteria); + void setBacteria(int slot, BacteriaInstance bacteriaInstance); + + BacteriaInstance getBacteria(int slot); - ResourceKey getBacteria(); + int getBacteriaSlots(); - void setBacteriaAmount(long bacteriaAmount); + default void modifyStats(int slot, UnaryOperator statsModifier) { + BacteriaInstance bacteriaInstance = getBacteria(slot); + BacteriaStats newStats = statsModifier.apply(bacteriaInstance.getStats()); - long getBacteriaAmount(); + } } diff --git a/src/main/java/com/portingdeadmods/nautec/capabilities/bacteria/ItemBacteriaWrapper.java b/src/main/java/com/portingdeadmods/nautec/capabilities/bacteria/ItemBacteriaWrapper.java index 6a6d528..d1ca714 100644 --- a/src/main/java/com/portingdeadmods/nautec/capabilities/bacteria/ItemBacteriaWrapper.java +++ b/src/main/java/com/portingdeadmods/nautec/capabilities/bacteria/ItemBacteriaWrapper.java @@ -1,32 +1,25 @@ package com.portingdeadmods.nautec.capabilities.bacteria; -import com.portingdeadmods.nautec.api.bacteria.Bacteria; +import com.portingdeadmods.nautec.api.bacteria.BacteriaInstance; import com.portingdeadmods.nautec.data.components.ComponentBacteriaStorage; import net.minecraft.core.component.DataComponentType; -import net.minecraft.resources.ResourceKey; import net.minecraft.world.item.ItemStack; -import java.util.Optional; import java.util.function.Supplier; public record ItemBacteriaWrapper(Supplier> componentType, ItemStack itemStack) implements IBacteriaStorage { @Override - public void setBacteria(ResourceKey bacteria) { - itemStack.set(componentType, new ComponentBacteriaStorage(bacteria, getBacteriaAmount())); + public void setBacteria(int slot, BacteriaInstance bacteriaInstance) { + itemStack.set(componentType, new ComponentBacteriaStorage(bacteriaInstance)); } @Override - public ResourceKey getBacteria() { - return itemStack.get(componentType).bacteria(); + public BacteriaInstance getBacteria(int slot) { + return itemStack.get(componentType).bacteriaInstance(); } @Override - public void setBacteriaAmount(long bacteriaAmount) { - itemStack.set(componentType, new ComponentBacteriaStorage(getBacteria(), bacteriaAmount)); - } - - @Override - public long getBacteriaAmount() { - return itemStack.get(componentType).bacteriaAmount(); + public int getBacteriaSlots() { + return 1; } } diff --git a/src/main/java/com/portingdeadmods/nautec/content/bacteria/BacteriaStats.java b/src/main/java/com/portingdeadmods/nautec/content/bacteria/BacteriaStats.java deleted file mode 100644 index 1ac197e..0000000 --- a/src/main/java/com/portingdeadmods/nautec/content/bacteria/BacteriaStats.java +++ /dev/null @@ -1,118 +0,0 @@ -package com.portingdeadmods.nautec.content.bacteria; - -import com.mojang.serialization.Codec; -import com.mojang.serialization.codecs.RecordCodecBuilder; -import com.portingdeadmods.nautec.api.bacteria.BaseBacteriaStats; -import com.portingdeadmods.nautec.utils.ComponentUtils; -import com.portingdeadmods.nautec.utils.RNGUtils; -import com.portingdeadmods.nautec.utils.codec.CodecUtils; -import net.minecraft.network.RegistryFriendlyByteBuf; -import net.minecraft.network.chat.Component; -import net.minecraft.network.codec.ByteBufCodecs; -import net.minecraft.network.codec.StreamCodec; -import net.minecraft.world.item.Item; -import net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs; - -import static com.portingdeadmods.nautec.NTConfig.*; - -import java.util.List; - -public record BacteriaStats(Item resource, - float growthRate, - float mutationResistance, - float productionRate, - float colonySize, - int lifespan, - int color) implements BaseBacteriaStats { - public static final Codec CODEC = RecordCodecBuilder.create( - instance -> instance.group( - CodecUtils.ITEM_CODEC.fieldOf("resource").forGetter(BacteriaStats::resource), - 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.FLOAT.fieldOf("colony_size").forGetter(BacteriaStats::colonySize), - Codec.INT.fieldOf("lifespan").forGetter(BacteriaStats::lifespan), - Codec.INT.fieldOf("color").forGetter(BacteriaStats::color) - ).apply(instance, BacteriaStats::new) - ); - - public static final StreamCodec STREAM_CODEC = NeoForgeStreamCodecs.composite( - CodecUtils.ITEM_STREAM_CODEC, - BacteriaStats::resource, - ByteBufCodecs.FLOAT, - BacteriaStats::growthRate, - ByteBufCodecs.FLOAT, - BacteriaStats::mutationResistance, - ByteBufCodecs.FLOAT, - BacteriaStats::productionRate, - ByteBufCodecs.FLOAT, - BacteriaStats::colonySize, - ByteBufCodecs.INT, - BacteriaStats::lifespan, - ByteBufCodecs.INT, - BacteriaStats::color, - BacteriaStats::new - ); - - @Override - public List statsTooltip() { - return List.of( - ComponentUtils.stringStatShow("Resource", resource.getDefaultInstance().getDisplayName().getString()), - ComponentUtils.countableStatShow("Growth Rate", growthRate, bacteriaGrowthRateCap), - ComponentUtils.countableStatShow("Mutation Resistance", mutationResistance, bacteriaMutationResistanceCap), - ComponentUtils.countableStatShow("Production Rate", productionRate, bacteriaProductionRateCap), - ComponentUtils.countableStatShow("Colony Size", colonySize, bacteriaColonySizeCap), - ComponentUtils.countableStatShow("Lifespan", lifespan, bacteriaLifespanCap) - ); - } - - @Override - public BaseBacteriaStats rollGrowthRate() { - float newGR = growthRate + (RNGUtils.floatInRangeOf(growthRate) / (10 * (1 + mutationResistance))); - newGR = Math.min(newGR, bacteriaGrowthRateCap); - - return new BacteriaStats(resource, newGR, mutationResistance, productionRate, colonySize, lifespan, color); - } - - @Override - public BaseBacteriaStats rollMutationResistance() { - float newMR = mutationResistance + RNGUtils.biasedInRange(0, 0.1f, mutationResistance) / 10 * (bacteriaMutationResistanceCap - mutationResistance); - - return new BacteriaStats(resource, growthRate, newMR, productionRate, colonySize, lifespan, color); - } - - @Override - public BaseBacteriaStats rollProductionRate() { - float newPR = productionRate + RNGUtils.floatInRangeOf(productionRate) / 10 * (1 + mutationResistance); - newPR = Math.min(newPR, bacteriaProductionRateCap); - - return new BacteriaStats(resource, growthRate, mutationResistance, newPR, colonySize, lifespan, color); - } - - @Override - public BaseBacteriaStats rollLifespan() { - int newLS = lifespan + (int) (RNGUtils.uniformRandInt(-10, 10) / (1 + mutationResistance)); - newLS = Math.min(newLS, bacteriaLifespanCap); - - return new BacteriaStats(resource, growthRate, mutationResistance, productionRate, colonySize, newLS, color); - } - - @Override - public BaseBacteriaStats rollStats() { - return rollGrowthRate().rollMutationResistance().rollProductionRate().rollLifespan(); - } - - @Override - public BaseBacteriaStats grow() { - float newCS = colonySize + RNGUtils.floatInRangeOf(0, growthRate); - newCS = Math.min(newCS, bacteriaColonySizeCap); - - return new BacteriaStats(resource, growthRate, mutationResistance, productionRate, newCS, lifespan, color); - } - - @Override - public BaseBacteriaStats shrink() { - float newCS = colonySize - colonySize * RNGUtils.uniformRandFloat(25) - 1; - return new BacteriaStats(resource, growthRate, mutationResistance, productionRate, newCS, lifespan, color); - } -} diff --git a/src/main/java/com/portingdeadmods/nautec/content/bacteria/EmptyBacteria.java b/src/main/java/com/portingdeadmods/nautec/content/bacteria/EmptyBacteria.java new file mode 100644 index 0000000..24592dd --- /dev/null +++ b/src/main/java/com/portingdeadmods/nautec/content/bacteria/EmptyBacteria.java @@ -0,0 +1,48 @@ +package com.portingdeadmods.nautec.content.bacteria; + +import com.mojang.serialization.MapCodec; +import com.portingdeadmods.nautec.api.bacteria.Bacteria; +import com.portingdeadmods.nautec.api.bacteria.BacteriaSerializer; +import com.portingdeadmods.nautec.api.bacteria.BacteriaStats; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.world.item.Items; + +public class EmptyBacteria implements Bacteria, Bacteria.Builder { + public static final EmptyBacteria INSTANCE = new EmptyBacteria(); + public static final BacteriaSerializer SERIALIZER = new BacteriaSerializer<>() { + @Override + public MapCodec mapCodec() { + return MapCodec.unit(EmptyBacteria.INSTANCE); + } + + @Override + public StreamCodec streamCodec() { + return StreamCodec.unit(EmptyBacteria.INSTANCE); + } + }; + public static final Resource.ItemResource RESOURCE = new Resource.ItemResource(Items.AIR); + + private EmptyBacteria() { + } + + @Override + public Resource resource() { + return RESOURCE; + } + + @Override + public BacteriaStats initialStats() { + return SimpleBacteriaStats.EMPTY; + } + + @Override + public BacteriaSerializer getSerializer() { + return SERIALIZER; + } + + @Override + public EmptyBacteria build() { + return EmptyBacteria.INSTANCE; + } +} diff --git a/src/main/java/com/portingdeadmods/nautec/content/bacteria/SimpleBacteria.java b/src/main/java/com/portingdeadmods/nautec/content/bacteria/SimpleBacteria.java index 7dc5275..8064eab 100644 --- a/src/main/java/com/portingdeadmods/nautec/content/bacteria/SimpleBacteria.java +++ b/src/main/java/com/portingdeadmods/nautec/content/bacteria/SimpleBacteria.java @@ -4,13 +4,13 @@ import com.mojang.serialization.codecs.RecordCodecBuilder; import com.portingdeadmods.nautec.api.bacteria.Bacteria; import com.portingdeadmods.nautec.api.bacteria.BacteriaSerializer; +import com.portingdeadmods.nautec.api.bacteria.BacteriaStats; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.codec.StreamCodec; -import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.Item; import net.minecraft.world.item.Items; -public record SimpleBacteria(BacteriaStats stats) implements Bacteria { +public record SimpleBacteria(Resource.ItemResource resource, BacteriaStats initialStats) implements Bacteria { public static Builder of() { return new Builder(); } @@ -23,11 +23,14 @@ public BacteriaSerializer getSerializer() { public static final class Serializer implements BacteriaSerializer { public static final Serializer INSTANCE = new Serializer(); public static final MapCodec MAP_CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group( - BacteriaStats.CODEC.fieldOf("stats").forGetter(SimpleBacteria::stats) + Resource.ItemResource.CODEC.fieldOf("resource").forGetter(SimpleBacteria::resource), + BacteriaStats.CODEC.fieldOf("stats").forGetter(SimpleBacteria::initialStats) ).apply(instance, SimpleBacteria::new)); public static final StreamCodec STREAM_CODEC = StreamCodec.composite( + Resource.ItemResource.STREAM_CODEC, + SimpleBacteria::resource, BacteriaStats.STREAM_CODEC, - SimpleBacteria::stats, + SimpleBacteria::initialStats, SimpleBacteria::new ); @@ -45,17 +48,16 @@ public StreamCodec streamCodec() { } } - public static class Builder { - private Item resource = Items.AIR; + public static class Builder implements Bacteria.Builder { + private Resource.ItemResource resource = new Resource.ItemResource(Items.AIR); private float growthRate; private float mutationResistance; private float productionRate; - private float colonySize; private int lifespan; private int color; public Builder resource(Item resource) { - this.resource = resource; + this.resource = new Resource.ItemResource(resource); return this; } @@ -74,11 +76,6 @@ public Builder productionRate(float productionRate) { return this; } - public Builder colonySize(float colonySize) { - this.colonySize = colonySize; - return this; - } - public Builder lifespan(int lifespan) { this.lifespan = lifespan; return this; @@ -89,8 +86,8 @@ public Builder color(int color) { return this; } - public Bacteria build(ResourceLocation location) { - return new SimpleBacteria(new BacteriaStats(resource, growthRate, mutationResistance, productionRate, colonySize, lifespan, color)); + public SimpleBacteria build() { + return new SimpleBacteria(resource, new SimpleBacteriaStats(growthRate, mutationResistance, productionRate, lifespan, color)); } } } diff --git a/src/main/java/com/portingdeadmods/nautec/content/bacteria/SimpleBacteriaStats.java b/src/main/java/com/portingdeadmods/nautec/content/bacteria/SimpleBacteriaStats.java new file mode 100644 index 0000000..1874dbf --- /dev/null +++ b/src/main/java/com/portingdeadmods/nautec/content/bacteria/SimpleBacteriaStats.java @@ -0,0 +1,138 @@ +package com.portingdeadmods.nautec.content.bacteria; + +import com.mojang.serialization.Codec; +import com.mojang.serialization.MapCodec; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import com.portingdeadmods.nautec.api.bacteria.BacteriaStats; +import com.portingdeadmods.nautec.api.bacteria.BacteriaStatsSerializer; +import com.portingdeadmods.nautec.utils.ComponentUtils; +import com.portingdeadmods.nautec.utils.RNGUtils; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.chat.Component; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; + +import static com.portingdeadmods.nautec.NTConfig.*; + +import java.util.List; + +public record SimpleBacteriaStats(float growthRate, + float mutationResistance, + float productionRate, + int lifespan, + int color) implements BacteriaStats { + public static final SimpleBacteriaStats EMPTY = new SimpleBacteriaStats(0, 0, 0, 0, -1); + public static final MapCodec CODEC = RecordCodecBuilder.mapCodec( + instance -> instance.group( + Codec.FLOAT.fieldOf("growth_rate").forGetter(SimpleBacteriaStats::growthRate), + Codec.FLOAT.fieldOf("mutation_resistance").forGetter(SimpleBacteriaStats::mutationResistance), + Codec.FLOAT.fieldOf("production_rate").forGetter(SimpleBacteriaStats::productionRate), + Codec.INT.fieldOf("lifespan").forGetter(SimpleBacteriaStats::lifespan), + Codec.INT.fieldOf("color").forGetter(SimpleBacteriaStats::color) + ).apply(instance, SimpleBacteriaStats::new) + ); + public static final StreamCodec STREAM_CODEC = StreamCodec.composite( + ByteBufCodecs.FLOAT, + SimpleBacteriaStats::growthRate, + ByteBufCodecs.FLOAT, + SimpleBacteriaStats::mutationResistance, + ByteBufCodecs.FLOAT, + SimpleBacteriaStats::productionRate, + ByteBufCodecs.INT, + SimpleBacteriaStats::lifespan, + ByteBufCodecs.INT, + SimpleBacteriaStats::color, + SimpleBacteriaStats::new + ); + + @Override + public BacteriaStats copy() { + return new SimpleBacteriaStats(growthRate, mutationResistance, productionRate, lifespan, color); + } + + @Override + public List statsTooltip() { + return List.of( + ComponentUtils.countableStatShow("Growth Rate", growthRate, bacteriaGrowthRateCap), + ComponentUtils.countableStatShow("Mutation Resistance", mutationResistance, bacteriaMutationResistanceCap), + ComponentUtils.countableStatShow("Production Rate", productionRate, bacteriaProductionRateCap), + ComponentUtils.countableStatShow("Lifespan", lifespan, bacteriaLifespanCap) + ); + } + + @Override + public BacteriaStatsSerializer getSerializer() { + return Serializer.INSTANCE; + } + + @Override + public BacteriaStats rollGrowthRate() { + float newGR = growthRate + (RNGUtils.floatInRangeOf(growthRate) / (10 * (1 + mutationResistance))); + newGR = Math.min(newGR, bacteriaGrowthRateCap); + + return new SimpleBacteriaStats(newGR, mutationResistance, productionRate, lifespan, color); + } + + @Override + public BacteriaStats rollMutationResistance() { + float newMR = mutationResistance + RNGUtils.biasedInRange(0, 0.1f, mutationResistance) / 10 * (bacteriaMutationResistanceCap - mutationResistance); + + return new SimpleBacteriaStats(growthRate, newMR, productionRate, lifespan, color); + } + + @Override + public BacteriaStats rollProductionRate() { + float newPR = productionRate + RNGUtils.floatInRangeOf(productionRate) / 10 * (1 + mutationResistance); + newPR = Math.min(newPR, bacteriaProductionRateCap); + + return new SimpleBacteriaStats(growthRate, mutationResistance, newPR, lifespan, color); + } + + @Override + public BacteriaStats rollLifespan() { + int newLS = lifespan + (int) (RNGUtils.uniformRandInt(-10, 10) / (1 + mutationResistance)); + newLS = Math.min(newLS, bacteriaLifespanCap); + + return new SimpleBacteriaStats(growthRate, mutationResistance, productionRate, newLS, color); + } + + @Override + public BacteriaStats rollStats() { + return rollGrowthRate().rollMutationResistance().rollProductionRate().rollLifespan(); + } + + // TODO: Might wanna move this to SimpleBacteriaInstance + @Override + public BacteriaStats grow() { +// float newCS = colonySize + RNGUtils.floatInRangeOf(0, growthRate); +// newCS = Math.min(newCS, bacteriaColonySizeCap); +// +// return new BacteriaStats(growthRate, mutationResistance, productionRate, newCS, lifespan, color); + return this; + } + + @Override + public BacteriaStats shrink() { +// float newCS = colonySize - colonySize * RNGUtils.uniformRandFloat(25) - 1; +// return new BacteriaStats(growthRate, mutationResistance, productionRate, newCS, lifespan, color); + return this; + } + + public static final class Serializer implements BacteriaStatsSerializer { + public static final Serializer INSTANCE = new Serializer(); + + private Serializer() { + } + + @Override + public MapCodec mapCodec() { + return CODEC; + } + + @Override + public StreamCodec streamCodec() { + return STREAM_CODEC; + } + } + +} diff --git a/src/main/java/com/portingdeadmods/nautec/content/blockentities/MutatorBlockEntity.java b/src/main/java/com/portingdeadmods/nautec/content/blockentities/MutatorBlockEntity.java index c3576de..a692acf 100644 --- a/src/main/java/com/portingdeadmods/nautec/content/blockentities/MutatorBlockEntity.java +++ b/src/main/java/com/portingdeadmods/nautec/content/blockentities/MutatorBlockEntity.java @@ -62,13 +62,13 @@ public void commonTick() { ItemStack result = extracted.copy(); - Bacteria bacteria = BacteriaHelper.getBacteria(getLevel().getServer().registryAccess(), extracted.get(NTDataComponents.BACTERIA).bacteria()); - bacteria.stats().rollStats(); - - result.set(NTDataComponents.BACTERIA, new ComponentBacteriaStorage( - NTBacterias.EMPTY, - 1 - )); +// Bacteria bacteria = BacteriaHelper.getBacteria(getLevel().getServer().registryAccess(), extracted.get(NTDataComponents.BACTERIA).bacteria()); +// bacteria.stats().rollStats(); +// +// result.set(NTDataComponents.BACTERIA, new ComponentBacteriaStorage( +// NTBacterias.EMPTY, +// 1 +// )); getItemHandler().insertItem(1, result, false); } else { diff --git a/src/main/java/com/portingdeadmods/nautec/content/items/PetriDishItem.java b/src/main/java/com/portingdeadmods/nautec/content/items/PetriDishItem.java index d842af4..f10d714 100644 --- a/src/main/java/com/portingdeadmods/nautec/content/items/PetriDishItem.java +++ b/src/main/java/com/portingdeadmods/nautec/content/items/PetriDishItem.java @@ -1,6 +1,5 @@ package com.portingdeadmods.nautec.content.items; -import com.portingdeadmods.nautec.NTRegistries; import com.portingdeadmods.nautec.api.bacteria.Bacteria; import com.portingdeadmods.nautec.api.items.IBacteriaItem; import com.portingdeadmods.nautec.data.NTDataComponents; @@ -9,7 +8,6 @@ import com.portingdeadmods.nautec.utils.Utils; import net.minecraft.ChatFormatting; import net.minecraft.network.chat.Component; -import net.minecraft.network.chat.ComponentContents; import net.minecraft.network.chat.MutableComponent; import net.minecraft.resources.ResourceKey; import net.minecraft.world.item.Item; @@ -25,7 +23,7 @@ public PetriDishItem(Properties properties) { @Override public void appendHoverText(ItemStack stack, TooltipContext context, List tooltipComponents, TooltipFlag tooltipFlag) { - ResourceKey bacteriaType = stack.get(NTDataComponents.BACTERIA).bacteria(); + ResourceKey bacteriaType = stack.get(NTDataComponents.BACTERIA).bacteriaInstance().getBacteria(); Bacteria bacteria = BacteriaHelper.getBacteria(context.registries(), bacteriaType); if (bacteria != null) { tooltipComponents.add(Component.literal("Name: ").append(Utils.registryTranslation(bacteriaType)).withStyle(ChatFormatting.WHITE)); @@ -33,7 +31,7 @@ public void appendHoverText(ItemStack stack, TooltipContext context, List bacteria, long bacteriaAmount) { - public static final ComponentBacteriaStorage EMPTY = new ComponentBacteriaStorage(NTBacterias.EMPTY, 0); - public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( - Bacteria.BACTERIA_TYPE_CODEC.fieldOf("bacteriaType").forGetter(ComponentBacteriaStorage::bacteria), - Codec.LONG.fieldOf("bacteriaAmount").forGetter(ComponentBacteriaStorage::bacteriaAmount) - ).apply(instance, ComponentBacteriaStorage::new)); - public static final StreamCodec STREAM_CODEC = StreamCodec.composite( - Bacteria.BACTERIA_TYPE_STREAM_CODEC, - ComponentBacteriaStorage::bacteria, - ByteBufCodecs.VAR_LONG, - ComponentBacteriaStorage::bacteriaAmount, - ComponentBacteriaStorage::new - ); +public record ComponentBacteriaStorage(BacteriaInstance bacteriaInstance) { + public static final ComponentBacteriaStorage EMPTY = new ComponentBacteriaStorage(BacteriaInstance.EMPTY); + + public static final Codec CODEC = + BacteriaInstance.CODEC.xmap(ComponentBacteriaStorage::new, ComponentBacteriaStorage::bacteriaInstance); + public static final StreamCodec STREAM_CODEC = + BacteriaInstance.STREAM_CODEC.map(ComponentBacteriaStorage::new, ComponentBacteriaStorage::bacteriaInstance); @Override public boolean equals(Object o) { - if (!(o instanceof ComponentBacteriaStorage(Bacteria bacteria1, long amount))) return false; - return bacteriaAmount == amount && Objects.equals(bacteria, bacteria1); + if (!(o instanceof ComponentBacteriaStorage(BacteriaInstance instance))) return false; + return Objects.equals(bacteriaInstance, instance); } @Override public int hashCode() { - return Objects.hash(bacteria, bacteriaAmount); + return Objects.hashCode(bacteriaInstance); } } diff --git a/src/main/java/com/portingdeadmods/nautec/registries/NTBacteriaSerializers.java b/src/main/java/com/portingdeadmods/nautec/registries/NTBacteriaSerializers.java index 03b6bab..16c490a 100644 --- a/src/main/java/com/portingdeadmods/nautec/registries/NTBacteriaSerializers.java +++ b/src/main/java/com/portingdeadmods/nautec/registries/NTBacteriaSerializers.java @@ -3,6 +3,7 @@ import com.portingdeadmods.nautec.NTRegistries; import com.portingdeadmods.nautec.Nautec; import com.portingdeadmods.nautec.api.bacteria.BacteriaSerializer; +import com.portingdeadmods.nautec.content.bacteria.EmptyBacteria; import com.portingdeadmods.nautec.content.bacteria.SimpleBacteria; import net.neoforged.neoforge.registries.DeferredRegister; @@ -10,6 +11,7 @@ public final class NTBacteriaSerializers { public static final DeferredRegister> SERIALIZERS = DeferredRegister.create(NTRegistries.BACTERIA_SERIALIZER, Nautec.MODID); static { + SERIALIZERS.register("empty", () -> EmptyBacteria.SERIALIZER); SERIALIZERS.register("simple", () -> SimpleBacteria.Serializer.INSTANCE); } } diff --git a/src/main/java/com/portingdeadmods/nautec/registries/NTBacteriaStatsSerializers.java b/src/main/java/com/portingdeadmods/nautec/registries/NTBacteriaStatsSerializers.java new file mode 100644 index 0000000..d1c6c2b --- /dev/null +++ b/src/main/java/com/portingdeadmods/nautec/registries/NTBacteriaStatsSerializers.java @@ -0,0 +1,17 @@ +package com.portingdeadmods.nautec.registries; + +import com.portingdeadmods.nautec.NTRegistries; +import com.portingdeadmods.nautec.Nautec; +import com.portingdeadmods.nautec.api.bacteria.BacteriaSerializer; +import com.portingdeadmods.nautec.api.bacteria.BacteriaStatsSerializer; +import com.portingdeadmods.nautec.content.bacteria.SimpleBacteria; +import com.portingdeadmods.nautec.content.bacteria.SimpleBacteriaStats; +import net.neoforged.neoforge.registries.DeferredRegister; + +public final class NTBacteriaStatsSerializers { + public static final DeferredRegister> SERIALIZERS = DeferredRegister.create(NTRegistries.BACTERIA_STATS_SERIALIZER, Nautec.MODID); + + static { + SERIALIZERS.register("simple", () -> SimpleBacteriaStats.Serializer.INSTANCE); + } +} diff --git a/src/main/java/com/portingdeadmods/nautec/registries/NTBacterias.java b/src/main/java/com/portingdeadmods/nautec/registries/NTBacterias.java index 558e848..f19d882 100644 --- a/src/main/java/com/portingdeadmods/nautec/registries/NTBacterias.java +++ b/src/main/java/com/portingdeadmods/nautec/registries/NTBacterias.java @@ -3,6 +3,7 @@ import com.portingdeadmods.nautec.NTRegistries; import com.portingdeadmods.nautec.Nautec; import com.portingdeadmods.nautec.api.bacteria.Bacteria; +import com.portingdeadmods.nautec.content.bacteria.EmptyBacteria; import com.portingdeadmods.nautec.content.bacteria.SimpleBacteria; import net.minecraft.data.worldgen.BootstrapContext; import net.minecraft.resources.ResourceKey; @@ -17,7 +18,7 @@ public final class NTBacterias { public static final ResourceKey THERMOPHILES = key("thermophiles"); public static void bootstrap(BootstrapContext context) { - register(context, EMPTY, SimpleBacteria.of()); + register(context, EMPTY, EmptyBacteria.INSTANCE); register(context, CYANOBACTERIA, SimpleBacteria.of() .resource(Items.IRON_INGOT) .color(FastColor.ARGB32.color(50, 255, 255))); @@ -32,8 +33,8 @@ public static void bootstrap(BootstrapContext context) { .color(FastColor.ARGB32.color(255, 0, 0))); } - private static void register(BootstrapContext context, ResourceKey key, SimpleBacteria.Builder builder) { - context.register(key, builder.build(key.location())); + private static void register(BootstrapContext context, ResourceKey key, Bacteria.Builder builder) { + context.register(key, builder.build()); } private static ResourceKey key(String name) { diff --git a/src/main/java/com/portingdeadmods/nautec/registries/NTCreativeTabs.java b/src/main/java/com/portingdeadmods/nautec/registries/NTCreativeTabs.java index e964362..4eda672 100644 --- a/src/main/java/com/portingdeadmods/nautec/registries/NTCreativeTabs.java +++ b/src/main/java/com/portingdeadmods/nautec/registries/NTCreativeTabs.java @@ -9,6 +9,7 @@ import com.portingdeadmods.nautec.capabilities.NTCapabilities; import com.portingdeadmods.nautec.capabilities.power.IPowerStorage; import com.portingdeadmods.nautec.compat.modonomicon.ModonomiconCompat; +import com.portingdeadmods.nautec.api.bacteria.BacteriaInstance; import com.portingdeadmods.nautec.data.NTDataComponents; import com.portingdeadmods.nautec.data.components.ComponentBacteriaStorage; import net.minecraft.core.HolderLookup; @@ -46,8 +47,8 @@ public final class NTCreativeTabs { if (lookup.isPresent()) { Stream> resourceKeyStream = lookup.get().listElementIds(); resourceKeyStream.forEach(elem -> { - addPetriDish(output, item, elem, false); - addPetriDish(output, item, elem, true); + addPetriDish(output, params.holders(), item, elem, false); + addPetriDish(output, params.holders(), item, elem, true); }); } } @@ -68,10 +69,10 @@ public final class NTCreativeTabs { }) .build()); - private static void addPetriDish(CreativeModeTab.Output output, ItemLike item, ResourceKey elem, boolean analyzed) { + private static void addPetriDish(CreativeModeTab.Output output, HolderLookup.Provider lookup, ItemLike item, ResourceKey elem, boolean analyzed) { if (elem != NTBacterias.EMPTY) { ItemStack stack = new ItemStack(item); - stack.set(NTDataComponents.BACTERIA, new ComponentBacteriaStorage(elem, 1)); + stack.set(NTDataComponents.BACTERIA, new ComponentBacteriaStorage(new BacteriaInstance(elem, lookup))); stack.set(NTDataComponents.ANALYZED, analyzed); output.accept(stack); }