Skip to content

Commit

Permalink
finish bacteria capability
Browse files Browse the repository at this point in the history
  • Loading branch information
Thepigcat76 committed Dec 14, 2024
1 parent 84ed1fc commit 9bded9c
Show file tree
Hide file tree
Showing 14 changed files with 241 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/portingdeadmods/nautec/Nautec.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,8 +101,6 @@ private void onRegisterAugments(RegisterEvent event) {
}
}



private void registerCapabilities(RegisterCapabilitiesEvent event) {
registerItemCaps(event);
registerBECaps(event);
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/portingdeadmods/nautec/bacteria/Bacteria.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -32,4 +35,18 @@ public interface Bacteria {
Codec.INT.fieldOf("lifespan").forGetter(Bacteria::lifespan)
).apply(instance, BacteriaImpl::new)
);

StreamCodec<RegistryFriendlyByteBuf, Bacteria> 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
);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,6 +17,12 @@ public static final class PowerStorage {
public static final EntityCapability<IPowerStorage, @Nullable Direction> ENTITY = EntityCapability.createSided(create("power"), IPowerStorage.class);
}

public static final class BacteriaStorage {
public static final BlockCapability<IBacteriaStorage, @Nullable Direction> BLOCK = BlockCapability.createSided(create("bacteria"), IBacteriaStorage.class);
public static final ItemCapability<IBacteriaStorage, @Nullable Void> ITEM = ItemCapability.createVoid(create("bacteria"), IBacteriaStorage.class);
public static final EntityCapability<IBacteriaStorage, @Nullable Direction> ENTITY = EntityCapability.createSided(create("bacteria"), IBacteriaStorage.class);
}

private static ResourceLocation create(String path) {
return ResourceLocation.fromNamespaceAndPath(Nautec.MODID, path);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CompoundTag> {
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<Tag> 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<Pair<Bacteria, Tag>> 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());
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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<DataComponentType<ComponentBacteriaStorage>> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +19,8 @@ public final class NTDataComponents {
() -> builder -> builder.persistent(ComponentPowerStorage.CODEC).networkSynchronized(ComponentPowerStorage.STREAM_CODEC));
public static final Supplier<DataComponentType<SimpleFluidContent>> FLUID = registerDataComponentType("fluid",
() -> builder -> builder.persistent(SimpleFluidContent.CODEC).networkSynchronized(SimpleFluidContent.STREAM_CODEC));
public static final Supplier<DataComponentType<ComponentBacteriaStorage>> BACTERIA = registerDataComponentType("bacteria",
() -> builder -> builder.persistent(ComponentBacteriaStorage.CODEC).networkSynchronized(ComponentBacteriaStorage.STREAM_CODEC));
public static final Supplier<DataComponentType<Boolean>> OPEN = registerDataComponentType("open",
() -> builder -> builder.persistent(Codec.BOOL).networkSynchronized(ByteBufCodecs.BOOL));

Expand Down
Original file line number Diff line number Diff line change
@@ -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<ComponentBacteriaStorage> 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<RegistryFriendlyByteBuf, ComponentBacteriaStorage> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/portingdeadmods/nautec/registries/NTItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,6 +84,11 @@ public final class NTItems {
public static final DeferredItem<Item> GLASS_VIAL = registerItem("glass_vial", Item::new, new Item.Properties());
public static final DeferredItem<Item> ELECTROLYTE_ALGAE_SERUM_VIAL = registerItem("eas_vial", Item::new, new Item.Properties());

// BACTERIA
public static final DeferredItem<PetriDishItem> PETRI_DISH = registerItem("petri_dish", PetriDishItem::new, () -> new Item.Properties()
.stacksTo(1)
.component(NTDataComponents.BACTERIA, ComponentBacteriaStorage.EMPTY));

// ARMOR
// CURIO ITEMS
public static final DeferredItem<BatteryItem> PRISMATIC_BATTERY = registerItem("prismatic_battery",
Expand Down Expand Up @@ -130,6 +136,9 @@ public final class NTItems {
public static <T extends Item> DeferredItem<T> registerItem(String name, Function<Item.Properties, T> itemConstructor, Item.Properties properties) {
return registerItem(name, itemConstructor, properties, true);
}
public static <T extends Item> DeferredItem<T> registerItem(String name, Function<Item.Properties, T> itemConstructor, Supplier<Item.Properties> properties) {
return registerItem(name, itemConstructor, properties, true);
}

private static <T extends Item> DeferredItem<T> registerItemBucket(String name, Supplier<T> item) {
return ITEMS.register(name, item);
Expand All @@ -149,6 +158,14 @@ public static <T extends Item> DeferredItem<T> registerItem(String name, Functio
return toReturn;
}

public static <T extends Item> DeferredItem<T> registerItem(String name, Function<Item.Properties, T> itemConstructor, Supplier<Item.Properties> properties, boolean addToTab) {
DeferredItem<T> 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();
Expand Down

0 comments on commit 9bded9c

Please sign in to comment.