From 9bded9c917f7be1c67ac02f135e52b17fa67d19e Mon Sep 17 00:00:00 2001 From: thepigcat Date: Sat, 14 Dec 2024 20:47:10 +0100 Subject: [PATCH] finish bacteria capability --- .../4d58d921702f57799841730d3d537abc5ea398a2 | 3 +- .../com/portingdeadmods/nautec/Nautec.java | 9 ++- .../nautec/api/items/IBacteriaItem.java | 11 ++++ .../nautec/bacteria/Bacteria.java | 17 +++++ .../nautec/capabilities/NTCapabilities.java | 7 +++ .../bacteria/BacteriaStorage.java | 61 ++++++++++++++++++ .../bacteria/IBacteriaStorage.java | 9 +++ .../bacteria/ItemBacteriaWrapper.java | 30 +++++++++ .../AugmentationStationBlockEntity.java | 1 + .../nautec/content/items/PetriDishItem.java | 11 ++++ .../nautec/data/NTDataComponents.java | 3 + .../components/ComponentBacteriaStorage.java | 63 +++++++++++++++++++ .../nautec/datagen/ItemModelProvider.java | 2 + .../nautec/registries/NTItems.java | 17 +++++ 14 files changed, 241 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/portingdeadmods/nautec/api/items/IBacteriaItem.java create mode 100644 src/main/java/com/portingdeadmods/nautec/capabilities/bacteria/BacteriaStorage.java create mode 100644 src/main/java/com/portingdeadmods/nautec/capabilities/bacteria/ItemBacteriaWrapper.java create mode 100644 src/main/java/com/portingdeadmods/nautec/content/items/PetriDishItem.java create mode 100644 src/main/java/com/portingdeadmods/nautec/data/components/ComponentBacteriaStorage.java diff --git a/src/generated/resources/.cache/4d58d921702f57799841730d3d537abc5ea398a2 b/src/generated/resources/.cache/4d58d921702f57799841730d3d537abc5ea398a2 index cca1e641..125aec6e 100644 --- a/src/generated/resources/.cache/4d58d921702f57799841730d3d537abc5ea398a2 +++ b/src/generated/resources/.cache/4d58d921702f57799841730d3d537abc5ea398a2 @@ -1,4 +1,4 @@ -// 1.21.1 2024-09-29T15:46:30.473567706 Item Models: nautec +// 1.21.1 2024-12-14T20:23:59.70087773 Item Models: nautec 53692692f57914fc54eb6ebf416ce1cb7789e57b assets/nautec/models/item/air_bottle.json 2e336fce57acffa1d0eb621baa30a26f665b4924 assets/nautec/models/item/aquarine_steel_axe.json f031500271b315d13a5f3ca96fc3a73ea8d47054 assets/nautec/models/item/aquarine_steel_axe_enabled.json @@ -55,6 +55,7 @@ d30193c75aab5ff8f4d4945df8689f575874c0f0 assets/nautec/models/item/laser_channel 644f1395dba609cfe064a66a028001e76dd20ab7 assets/nautec/models/item/laser_junction.json e2a54f360d7c345a624bfd88678359ffce2af94d assets/nautec/models/item/long_distance_laser.json 2a5b3ee493a3b20a37c3f391ebd960de088f2b7c assets/nautec/models/item/mixer.json +312565f208fcf706ea6e2f7550dadd69a7503bc2 assets/nautec/models/item/petri_dish.json e8d19ed0a058c92c28efb20a5f8887ff0b78770f assets/nautec/models/item/polished_prismarine.json 113d63fc1bd37fbe0fb477446a06eafa25ce09b1 assets/nautec/models/item/prism_monocle.json 8e22e42d27bc66bc91cc76fd66d0ff5457b585b1 assets/nautec/models/item/prismarine_crystal.json diff --git a/src/main/java/com/portingdeadmods/nautec/Nautec.java b/src/main/java/com/portingdeadmods/nautec/Nautec.java index 0d162da7..24086509 100644 --- a/src/main/java/com/portingdeadmods/nautec/Nautec.java +++ b/src/main/java/com/portingdeadmods/nautec/Nautec.java @@ -4,10 +4,13 @@ import com.portingdeadmods.nautec.api.augments.AugmentSlot; import com.portingdeadmods.nautec.api.augments.AugmentType; import com.portingdeadmods.nautec.api.blockentities.ContainerBlockEntity; +import com.portingdeadmods.nautec.api.items.IBacteriaItem; import com.portingdeadmods.nautec.api.items.ICurioItem; import com.portingdeadmods.nautec.api.items.IFluidItem; import com.portingdeadmods.nautec.api.items.IPowerItem; import com.portingdeadmods.nautec.capabilities.NTCapabilities; +import com.portingdeadmods.nautec.capabilities.bacteria.BacteriaStorage; +import com.portingdeadmods.nautec.capabilities.bacteria.ItemBacteriaWrapper; import com.portingdeadmods.nautec.capabilities.power.ItemPowerWrapper; import com.portingdeadmods.nautec.compat.duradisplay.DuraDisplayCompat; import com.portingdeadmods.nautec.content.commands.arguments.AugmentSlotArgumentType; @@ -98,8 +101,6 @@ private void onRegisterAugments(RegisterEvent event) { } } - - private void registerCapabilities(RegisterCapabilitiesEvent event) { registerItemCaps(event); registerBECaps(event); @@ -115,6 +116,10 @@ private static void registerItemCaps(RegisterCapabilitiesEvent event) { event.registerItem(Capabilities.FluidHandler.ITEM, (stack, ctx) -> new FluidHandlerItemStack(NTDataComponents.FLUID, stack, fluidItem.getFluidCapacity()), item); } + if (item instanceof IBacteriaItem) { + event.registerItem(NTCapabilities.BacteriaStorage.ITEM, (stack, ctx) -> new ItemBacteriaWrapper(NTDataComponents.BACTERIA, stack), item); + } + if (item instanceof ICurioItem curioItem) { event.registerItem(CuriosCapability.ITEM, (stack, context) -> new ICurio() { diff --git a/src/main/java/com/portingdeadmods/nautec/api/items/IBacteriaItem.java b/src/main/java/com/portingdeadmods/nautec/api/items/IBacteriaItem.java new file mode 100644 index 00000000..789ea381 --- /dev/null +++ b/src/main/java/com/portingdeadmods/nautec/api/items/IBacteriaItem.java @@ -0,0 +1,11 @@ +package com.portingdeadmods.nautec.api.items; + +import com.portingdeadmods.nautec.capabilities.NTCapabilities; +import com.portingdeadmods.nautec.capabilities.bacteria.IBacteriaStorage; +import net.minecraft.world.item.ItemStack; + +public interface IBacteriaItem { + default IBacteriaStorage getStorage(ItemStack stack) { + return stack.getCapability(NTCapabilities.BacteriaStorage.ITEM); + } +} diff --git a/src/main/java/com/portingdeadmods/nautec/bacteria/Bacteria.java b/src/main/java/com/portingdeadmods/nautec/bacteria/Bacteria.java index 11bd9d45..c30011a1 100644 --- a/src/main/java/com/portingdeadmods/nautec/bacteria/Bacteria.java +++ b/src/main/java/com/portingdeadmods/nautec/bacteria/Bacteria.java @@ -4,6 +4,9 @@ import com.mojang.serialization.MapCodec; import com.mojang.serialization.codecs.RecordCodecBuilder; 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.world.item.Item; public interface Bacteria { @@ -32,4 +35,18 @@ public interface Bacteria { Codec.INT.fieldOf("lifespan").forGetter(Bacteria::lifespan) ).apply(instance, BacteriaImpl::new) ); + + 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, + BacteriaImpl::new + ); } diff --git a/src/main/java/com/portingdeadmods/nautec/capabilities/NTCapabilities.java b/src/main/java/com/portingdeadmods/nautec/capabilities/NTCapabilities.java index 6cc619d2..b36f1aa3 100644 --- a/src/main/java/com/portingdeadmods/nautec/capabilities/NTCapabilities.java +++ b/src/main/java/com/portingdeadmods/nautec/capabilities/NTCapabilities.java @@ -1,6 +1,7 @@ package com.portingdeadmods.nautec.capabilities; import com.portingdeadmods.nautec.Nautec; +import com.portingdeadmods.nautec.capabilities.bacteria.IBacteriaStorage; import com.portingdeadmods.nautec.capabilities.power.IPowerStorage; import net.minecraft.core.Direction; import net.minecraft.resources.ResourceLocation; @@ -16,6 +17,12 @@ public static final class PowerStorage { public static final EntityCapability ENTITY = EntityCapability.createSided(create("power"), IPowerStorage.class); } + public static final class BacteriaStorage { + public static final BlockCapability BLOCK = BlockCapability.createSided(create("bacteria"), IBacteriaStorage.class); + public static final ItemCapability ITEM = ItemCapability.createVoid(create("bacteria"), IBacteriaStorage.class); + public static final EntityCapability ENTITY = EntityCapability.createSided(create("bacteria"), IBacteriaStorage.class); + } + private static ResourceLocation create(String path) { return ResourceLocation.fromNamespaceAndPath(Nautec.MODID, path); } diff --git a/src/main/java/com/portingdeadmods/nautec/capabilities/bacteria/BacteriaStorage.java b/src/main/java/com/portingdeadmods/nautec/capabilities/bacteria/BacteriaStorage.java new file mode 100644 index 00000000..56a93917 --- /dev/null +++ b/src/main/java/com/portingdeadmods/nautec/capabilities/bacteria/BacteriaStorage.java @@ -0,0 +1,61 @@ +package com.portingdeadmods.nautec.capabilities.bacteria; + +import com.mojang.datafixers.util.Pair; +import com.mojang.serialization.DataResult; +import com.portingdeadmods.nautec.Nautec; +import com.portingdeadmods.nautec.bacteria.Bacteria; +import net.minecraft.core.HolderLookup; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.NbtOps; +import net.minecraft.nbt.Tag; +import net.neoforged.neoforge.common.util.INBTSerializable; +import org.jetbrains.annotations.UnknownNullability; + +public class BacteriaStorage implements IBacteriaStorage, INBTSerializable { + private Bacteria bacteria; + private long bacteriaAmount; + + @Override + public void setBacteria(Bacteria bacteria) { + this.bacteria = bacteria; + } + + @Override + public Bacteria getBacteria() { + return this.bacteria; + } + + @Override + public void setBacteriaAmount(long bacteriaAmount) { + this.bacteriaAmount = bacteriaAmount; + } + + @Override + public long getBacteriaAmount() { + return this.bacteriaAmount; + } + + @Override + public @UnknownNullability CompoundTag serializeNBT(HolderLookup.Provider provider) { + CompoundTag tag = new CompoundTag(); + tag.putLong("bacteriaAmount", this.bacteriaAmount); + DataResult tagDataResult = Bacteria.CODEC.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; + } + + @Override + public void deserializeNBT(HolderLookup.Provider provider, CompoundTag nbt) { + this.bacteriaAmount = nbt.getLong("bacteriaAmount"); + DataResult> decodedBacteria = Bacteria.CODEC.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()); + } + } +} 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 06c11e1d..be31b1b6 100644 --- a/src/main/java/com/portingdeadmods/nautec/capabilities/bacteria/IBacteriaStorage.java +++ b/src/main/java/com/portingdeadmods/nautec/capabilities/bacteria/IBacteriaStorage.java @@ -1,4 +1,13 @@ package com.portingdeadmods.nautec.capabilities.bacteria; +import com.portingdeadmods.nautec.bacteria.Bacteria; + public interface IBacteriaStorage { + void setBacteria(Bacteria bacteria); + + Bacteria getBacteria(); + + void setBacteriaAmount(long bacteriaAmount); + + 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 new file mode 100644 index 00000000..d2343352 --- /dev/null +++ b/src/main/java/com/portingdeadmods/nautec/capabilities/bacteria/ItemBacteriaWrapper.java @@ -0,0 +1,30 @@ +package com.portingdeadmods.nautec.capabilities.bacteria; + +import com.portingdeadmods.nautec.bacteria.Bacteria; +import com.portingdeadmods.nautec.data.components.ComponentBacteriaStorage; +import net.minecraft.core.component.DataComponentType; +import net.minecraft.world.item.ItemStack; + +import java.util.function.Supplier; + +public record ItemBacteriaWrapper(Supplier> componentType, ItemStack itemStack) implements IBacteriaStorage { + @Override + public void setBacteria(Bacteria bacteria) { + itemStack.set(componentType, new ComponentBacteriaStorage(bacteria, getBacteriaAmount())); + } + + @Override + public Bacteria getBacteria() { + return itemStack.get(componentType).bacteria(); + } + + @Override + public void setBacteriaAmount(long bacteriaAmount) { + itemStack.set(componentType, new ComponentBacteriaStorage(getBacteria(), bacteriaAmount)); + } + + @Override + public long getBacteriaAmount() { + return itemStack.get(componentType).bacteriaAmount(); + } +} diff --git a/src/main/java/com/portingdeadmods/nautec/content/blockentities/multiblock/controller/AugmentationStationBlockEntity.java b/src/main/java/com/portingdeadmods/nautec/content/blockentities/multiblock/controller/AugmentationStationBlockEntity.java index a6203c77..3bd02c52 100644 --- a/src/main/java/com/portingdeadmods/nautec/content/blockentities/multiblock/controller/AugmentationStationBlockEntity.java +++ b/src/main/java/com/portingdeadmods/nautec/content/blockentities/multiblock/controller/AugmentationStationBlockEntity.java @@ -29,6 +29,7 @@ import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.RecipeHolder; +import net.minecraft.world.item.enchantment.Enchantments; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.phys.AABB; diff --git a/src/main/java/com/portingdeadmods/nautec/content/items/PetriDishItem.java b/src/main/java/com/portingdeadmods/nautec/content/items/PetriDishItem.java new file mode 100644 index 00000000..62aff5be --- /dev/null +++ b/src/main/java/com/portingdeadmods/nautec/content/items/PetriDishItem.java @@ -0,0 +1,11 @@ +package com.portingdeadmods.nautec.content.items; + +import com.portingdeadmods.nautec.api.items.IBacteriaItem; +import com.portingdeadmods.nautec.capabilities.bacteria.IBacteriaStorage; +import net.minecraft.world.item.Item; + +public class PetriDishItem extends Item implements IBacteriaItem { + public PetriDishItem(Properties properties) { + super(properties); + } +} diff --git a/src/main/java/com/portingdeadmods/nautec/data/NTDataComponents.java b/src/main/java/com/portingdeadmods/nautec/data/NTDataComponents.java index 9f70685c..51a61d8c 100644 --- a/src/main/java/com/portingdeadmods/nautec/data/NTDataComponents.java +++ b/src/main/java/com/portingdeadmods/nautec/data/NTDataComponents.java @@ -2,6 +2,7 @@ import com.mojang.serialization.Codec; import com.portingdeadmods.nautec.Nautec; +import com.portingdeadmods.nautec.data.components.ComponentBacteriaStorage; import com.portingdeadmods.nautec.data.components.ComponentPowerStorage; import net.minecraft.core.component.DataComponentType; import net.minecraft.network.codec.ByteBufCodecs; @@ -18,6 +19,8 @@ public final class NTDataComponents { () -> builder -> builder.persistent(ComponentPowerStorage.CODEC).networkSynchronized(ComponentPowerStorage.STREAM_CODEC)); public static final Supplier> FLUID = registerDataComponentType("fluid", () -> builder -> builder.persistent(SimpleFluidContent.CODEC).networkSynchronized(SimpleFluidContent.STREAM_CODEC)); + public static final Supplier> BACTERIA = registerDataComponentType("bacteria", + () -> builder -> builder.persistent(ComponentBacteriaStorage.CODEC).networkSynchronized(ComponentBacteriaStorage.STREAM_CODEC)); public static final Supplier> OPEN = registerDataComponentType("open", () -> builder -> builder.persistent(Codec.BOOL).networkSynchronized(ByteBufCodecs.BOOL)); diff --git a/src/main/java/com/portingdeadmods/nautec/data/components/ComponentBacteriaStorage.java b/src/main/java/com/portingdeadmods/nautec/data/components/ComponentBacteriaStorage.java new file mode 100644 index 00000000..adb7f3d2 --- /dev/null +++ b/src/main/java/com/portingdeadmods/nautec/data/components/ComponentBacteriaStorage.java @@ -0,0 +1,63 @@ +package com.portingdeadmods.nautec.data.components; + +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import com.portingdeadmods.nautec.bacteria.Bacteria; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.Items; + +import java.util.Objects; + +public record ComponentBacteriaStorage(Bacteria bacteria, long bacteriaAmount) { + public static final ComponentBacteriaStorage EMPTY = new ComponentBacteriaStorage(new Bacteria() { + @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; + } + }, 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) + ).apply(instance, ComponentBacteriaStorage::new)); + public static final StreamCodec STREAM_CODEC = StreamCodec.composite( + Bacteria.STREAM_CODEC, + ComponentBacteriaStorage::bacteria, + ByteBufCodecs.VAR_LONG, + ComponentBacteriaStorage::bacteriaAmount, + ComponentBacteriaStorage::new + ); + + @Override + public boolean equals(Object o) { + if (!(o instanceof ComponentBacteriaStorage(Bacteria bacteria1, long amount))) return false; + return bacteriaAmount == amount && Objects.equals(bacteria, bacteria1); + } + + @Override + public int hashCode() { + return Objects.hash(bacteria, bacteriaAmount); + } +} diff --git a/src/main/java/com/portingdeadmods/nautec/datagen/ItemModelProvider.java b/src/main/java/com/portingdeadmods/nautec/datagen/ItemModelProvider.java index 2b59a301..ae5e3d76 100644 --- a/src/main/java/com/portingdeadmods/nautec/datagen/ItemModelProvider.java +++ b/src/main/java/com/portingdeadmods/nautec/datagen/ItemModelProvider.java @@ -53,6 +53,8 @@ protected void registerModels() { basicItem(NTItems.GLASS_VIAL.get()); basicItem(NTItems.ELECTROLYTE_ALGAE_SERUM_VIAL.get()); + basicItem(NTItems.PETRI_DISH.get()); + basicItem(NTItems.PRISM_MONOCLE.get()); basicItem(NTItems.DIVING_HELMET.get()); diff --git a/src/main/java/com/portingdeadmods/nautec/registries/NTItems.java b/src/main/java/com/portingdeadmods/nautec/registries/NTItems.java index 5b704d18..a85908c2 100644 --- a/src/main/java/com/portingdeadmods/nautec/registries/NTItems.java +++ b/src/main/java/com/portingdeadmods/nautec/registries/NTItems.java @@ -6,6 +6,7 @@ import com.portingdeadmods.nautec.content.items.tiers.NTArmorMaterials; import com.portingdeadmods.nautec.content.items.tools.*; import com.portingdeadmods.nautec.data.NTDataComponents; +import com.portingdeadmods.nautec.data.components.ComponentBacteriaStorage; import net.minecraft.core.component.DataComponents; import net.minecraft.world.item.*; import net.minecraft.world.level.ItemLike; @@ -83,6 +84,11 @@ public final class NTItems { public static final DeferredItem GLASS_VIAL = registerItem("glass_vial", Item::new, new Item.Properties()); public static final DeferredItem ELECTROLYTE_ALGAE_SERUM_VIAL = registerItem("eas_vial", Item::new, new Item.Properties()); + // BACTERIA + public static final DeferredItem PETRI_DISH = registerItem("petri_dish", PetriDishItem::new, () -> new Item.Properties() + .stacksTo(1) + .component(NTDataComponents.BACTERIA, ComponentBacteriaStorage.EMPTY)); + // ARMOR // CURIO ITEMS public static final DeferredItem PRISMATIC_BATTERY = registerItem("prismatic_battery", @@ -130,6 +136,9 @@ public final class NTItems { public static DeferredItem registerItem(String name, Function itemConstructor, Item.Properties properties) { return registerItem(name, itemConstructor, properties, true); } + public static DeferredItem registerItem(String name, Function itemConstructor, Supplier properties) { + return registerItem(name, itemConstructor, properties, true); + } private static DeferredItem registerItemBucket(String name, Supplier item) { return ITEMS.register(name, item); @@ -149,6 +158,14 @@ public static DeferredItem registerItem(String name, Functio return toReturn; } + public static DeferredItem registerItem(String name, Function itemConstructor, Supplier properties, boolean addToTab) { + DeferredItem toReturn = ITEMS.register(name, () -> itemConstructor.apply(properties.get())); + if (addToTab) { + CREATIVE_TAB_ITEMS.add(toReturn); + } + return toReturn; + } + static { if (ModList.get().isLoaded("modonomicon")) { NAUTEC_GUIDE = ModonomiconCompat.registerItem();