Skip to content

Commit

Permalink
bacteria stats rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
Thepigcat76 committed Jan 5, 2025
1 parent 622c741 commit f483db8
Show file tree
Hide file tree
Showing 22 changed files with 505 additions and 112 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/portingdeadmods/nautec/NTRegistries.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public final class NTRegistries {
ResourceKey.createRegistryKey(ResourceLocation.fromNamespaceAndPath(Nautec.MODID, "multiblock"));
public static final ResourceKey<Registry<BacteriaSerializer<?>>> BACTERIA_SERIALIZER_KEY =
ResourceKey.createRegistryKey(ResourceLocation.fromNamespaceAndPath(Nautec.MODID, "bacteria_serializer"));
public static final ResourceKey<Registry<BacteriaStatsSerializer<?>>> BACTERIA_STATS_SERIALIZER_KEY =
public static final ResourceKey<Registry<BacteriaStatsSerializer<?, ?>>> BACTERIA_STATS_SERIALIZER_KEY =
ResourceKey.createRegistryKey(ResourceLocation.fromNamespaceAndPath(Nautec.MODID, "bacteria_stats_serializer"));
public static final ResourceKey<Registry<Bacteria>> BACTERIA_KEY =
ResourceKey.createRegistryKey(ResourceLocation.fromNamespaceAndPath(Nautec.MODID, "bacteria"));
Expand All @@ -29,5 +29,5 @@ public final class NTRegistries {
public static final Registry<AugmentSlot> AUGMENT_SLOT = new RegistryBuilder<>(AUGMENT_SLOT_KEY).create();
public static final Registry<Multiblock> MULTIBLOCK = new RegistryBuilder<>(MULTIBLOCK_KEY).create();
public static final Registry<BacteriaSerializer<?>> BACTERIA_SERIALIZER = new RegistryBuilder<>(BACTERIA_SERIALIZER_KEY).create();
public static final Registry<BacteriaStatsSerializer<?>> BACTERIA_STATS_SERIALIZER = new RegistryBuilder<>(BACTERIA_STATS_SERIALIZER_KEY).sync(true).create();
public static final Registry<BacteriaStatsSerializer<?, ?>> BACTERIA_STATS_SERIALIZER = new RegistryBuilder<>(BACTERIA_STATS_SERIALIZER_KEY).sync(true).create();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.portingdeadmods.nautec.content.bacteria.CollapsedStats;
import com.portingdeadmods.nautec.content.bacteria.SimpleCollapsedStats;
import com.portingdeadmods.nautec.content.bacteria.SimpleBacteriaStats;
import com.portingdeadmods.nautec.registries.NTBacterias;
import com.portingdeadmods.nautec.utils.BacteriaHelper;
Expand All @@ -17,37 +17,37 @@ public final class BacteriaInstance {
public static final Codec<BacteriaInstance> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Bacteria.BACTERIA_TYPE_CODEC.fieldOf("bacteria").forGetter(BacteriaInstance::getBacteria),
Codec.LONG.fieldOf("amount").forGetter(BacteriaInstance::getAmount),
CollapsedStats.CODEC.fieldOf("stats").forGetter(BacteriaInstance::getStats)
CollapsedBacteriaStats.CODEC.fieldOf("stats").forGetter(BacteriaInstance::getStats)
).apply(instance, BacteriaInstance::new));
public static final StreamCodec<RegistryFriendlyByteBuf, BacteriaInstance> STREAM_CODEC = StreamCodec.composite(
Bacteria.BACTERIA_TYPE_STREAM_CODEC,
BacteriaInstance::getBacteria,
ByteBufCodecs.VAR_LONG,
BacteriaInstance::getAmount,
CollapsedStats.STREAM_CODEC,
CollapsedBacteriaStats.STREAM_CODEC,
BacteriaInstance::getStats,
BacteriaInstance::new
);

private final ResourceKey<Bacteria> bacteria;
private long amount;
private CollapsedStats stats;
private CollapsedBacteriaStats stats;

public BacteriaInstance(ResourceKey<Bacteria> bacteria, HolderLookup.Provider lookup) {
this(bacteria, 1, BacteriaHelper.getBacteria(lookup, bacteria).initialStats());
}

public BacteriaInstance(ResourceKey<Bacteria> bacteria, BacteriaStats stats) {
public BacteriaInstance(ResourceKey<Bacteria> bacteria, BacteriaStats<?> stats) {
this(bacteria, 1, stats);
}

public BacteriaInstance(ResourceKey<Bacteria> bacteria, long amount, BacteriaStats stats) {
public BacteriaInstance(ResourceKey<Bacteria> bacteria, long amount, BacteriaStats<?> stats) {
this.bacteria = bacteria;
this.amount = amount;
this.stats = stats.collapse();
}

public BacteriaInstance(ResourceKey<Bacteria> bacteria, long amount, CollapsedStats stats) {
public BacteriaInstance(ResourceKey<Bacteria> bacteria, long amount, CollapsedBacteriaStats stats) {
this.bacteria = bacteria;
this.amount = amount;
this.stats = stats;
Expand All @@ -65,11 +65,11 @@ public ResourceKey<Bacteria> getBacteria() {
return bacteria;
}

public void setStats(CollapsedStats stats) {
public void setStats(CollapsedBacteriaStats stats) {
this.stats = stats;
}

public CollapsedStats getStats() {
public CollapsedBacteriaStats getStats() {
return this.stats;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,31 @@

import com.mojang.serialization.Codec;
import com.portingdeadmods.nautec.NTRegistries;
import com.portingdeadmods.nautec.content.bacteria.CollapsedStats;
import com.portingdeadmods.nautec.utils.ranges.FloatRange;
import com.portingdeadmods.nautec.utils.ranges.IntRange;
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<BacteriaStats> CODEC =
public interface BacteriaStats<C extends CollapsedBacteriaStats> {
Codec<BacteriaStats<?>> CODEC =
NTRegistries.BACTERIA_STATS_SERIALIZER.byNameCodec().dispatch(BacteriaStats::getSerializer, BacteriaStatsSerializer::mapCodec);
StreamCodec<RegistryFriendlyByteBuf, BacteriaStats> STREAM_CODEC =
StreamCodec<RegistryFriendlyByteBuf, BacteriaStats<?>> STREAM_CODEC =
ByteBufCodecs.registry(NTRegistries.BACTERIA_STATS_SERIALIZER_KEY).dispatch(BacteriaStats::getSerializer, BacteriaStatsSerializer::streamCodec);

FloatRange growthRate();

List<Float> growthRate();
FloatRange mutationResistance();

List<Float> mutationResistance();
FloatRange productionRate();

List<Float> productionRate();
IntRange lifespan();

List<Integer> lifespan();

CollapsedStats collapse();
C collapse();

int color();

BacteriaStatsSerializer<?> getSerializer();
BacteriaStatsSerializer<?, ?> getSerializer();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;

public interface BacteriaStatsSerializer<T extends BacteriaStats> {
public interface BacteriaStatsSerializer<C extends CollapsedBacteriaStats, T extends BacteriaStats<C>> {
MapCodec<T> mapCodec();

StreamCodec<RegistryFriendlyByteBuf, T> streamCodec();

MapCodec<C> collapsedMapCodec();

StreamCodec<RegistryFriendlyByteBuf, C> collapsedStreamCodec();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;

public interface CollapsedBacteriaStats {
Codec<CollapsedBacteriaStats> CODEC =
NTRegistries.BACTERIA_STATS_SERIALIZER.byNameCodec().dispatch(CollapsedBacteriaStats::getSerializer, BacteriaStatsSerializer::collapsedMapCodec);
StreamCodec<RegistryFriendlyByteBuf, CollapsedBacteriaStats> STREAM_CODEC =
ByteBufCodecs.registry(NTRegistries.BACTERIA_STATS_SERIALIZER_KEY).dispatch(CollapsedBacteriaStats::getSerializer, BacteriaStatsSerializer::collapsedStreamCodec);

BacteriaStatsSerializer<?, ?> getSerializer();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.portingdeadmods.nautec.api.client.screen;

import com.portingdeadmods.nautec.api.blockentities.ContainerBlockEntity;
import com.portingdeadmods.nautec.api.menu.NTMachineMenu;
import com.portingdeadmods.nautec.api.menu.slots.SlotBacteriaStorage;
import com.portingdeadmods.nautec.api.menu.slots.SlotFluidHandler;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.neoforged.neoforge.fluids.FluidStack;
import org.jetbrains.annotations.NotNull;

import java.text.NumberFormat;
import java.util.List;

public abstract class NTMachineScreen<T extends ContainerBlockEntity> extends AbstractContainerScreen<NTMachineMenu<T>> {
private SlotFluidHandler hoveredFluidHandlerSlot;
private SlotBacteriaStorage hoveredBacteriaStorageSlot;

private final NumberFormat nf = NumberFormat.getIntegerInstance();

public NTMachineScreen(NTMachineMenu<T> menu, Inventory playerInventory, Component title) {
super(menu, playerInventory, title);

this.titleLabelY = 4;
this.imageHeight = 174;
}

@Override
public void render(GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
renderBackground(pGuiGraphics, pMouseX, pMouseX, pPartialTick);
super.render(pGuiGraphics, pMouseX, pMouseY, pPartialTick);
renderTooltip(pGuiGraphics, pMouseX, pMouseY);

hoverFluidSlot(pMouseX, pMouseY);

hoverBacteriaSlot(pMouseX, pMouseY);

Font font = minecraft.font;
if (this.hoveredBacteriaStorageSlot != null) {
SimpleCollapsedStats
pGuiGraphics.renderComponentTooltip(font, List.of(Component.literal("Bacteria Storage")), pMouseX, pMouseY);
}

if (this.hoveredFluidHandlerSlot != null) {
FluidStack fluid = this.hoveredFluidHandlerSlot.getFluidStack();
pGuiGraphics.renderComponentTooltip(font, List.of(
fluid.getHoverName(),
Component.literal("%s / %s mb".formatted(
nf.format(fluid.getAmount()),
nf.format(this.hoveredFluidHandlerSlot.getFluidCapacity()))
).withStyle(ChatFormatting.GRAY)
), pMouseX, pMouseY);
}
}

private void hoverFluidSlot(int pMouseX, int pMouseY) {
for (SlotFluidHandler fSlot : menu.getFluidTankSlots()) {
if (isHovering(fSlot.getX(), fSlot.getY(), fSlot.getWidth(), fSlot.getHeight(), pMouseX, pMouseY)) {
this.hoveredFluidHandlerSlot = fSlot;
return;
}
}
this.hoveredFluidHandlerSlot = null;
}

private void hoverBacteriaSlot(int pMouseX, int pMouseY) {
for (SlotBacteriaStorage bSlot : menu.getBacteriaStorageSlots()) {
if (isHovering(bSlot.getX(), bSlot.getY(), bSlot.getWidth(), bSlot.getHeight(), pMouseX, pMouseY)) {
this.hoveredBacteriaStorageSlot = bSlot;
return;
}
}
this.hoveredBacteriaStorageSlot = null;
}

@Override
protected void renderBg(GuiGraphics guiGraphics, float delta, int mouseX, int mouseY) {
guiGraphics.blit(getBackgroundTexture(), leftPos, topPos, 0, 0, imageWidth, imageHeight);
}

public abstract @NotNull ResourceLocation getBackgroundTexture();

public SlotFluidHandler getHoveredFluidHandlerSlot() {
return hoveredFluidHandlerSlot;
}

public SlotBacteriaStorage getHoveredBacteriaStorageSlot() {
return hoveredBacteriaStorageSlot;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.portingdeadmods.nautec.api.menu;

import com.portingdeadmods.nautec.api.blockentities.ContainerBlockEntity;
import com.portingdeadmods.nautec.api.menu.slots.SlotBacteriaStorage;
import com.portingdeadmods.nautec.api.menu.slots.SlotFluidHandler;
import net.minecraft.core.NonNullList;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.MenuType;
import org.jetbrains.annotations.NotNull;

public abstract class NTMachineMenu<T extends ContainerBlockEntity> extends NTAbstractContainerMenu<T> {
private final NonNullList<SlotFluidHandler> fluidTankSlots;
private final NonNullList<SlotBacteriaStorage> bacteriaStorageSlots;

public NTMachineMenu(MenuType<?> menuType, int containerId, @NotNull Inventory inv, @NotNull T blockEntity) {
super(menuType, containerId, inv, blockEntity);

addPlayerInventory(inv, 92);
addPlayerHotbar(inv, 150);

this.fluidTankSlots = NonNullList.create();
this.bacteriaStorageSlots = NonNullList.create();
}

public void addFluidHandlerSlot(SlotFluidHandler slot) {
this.fluidTankSlots.add(slot);
}

public void addBacteriaStorageSlot(SlotBacteriaStorage slot) {
this.bacteriaStorageSlots.add(slot);
}

public NonNullList<SlotFluidHandler> getFluidTankSlots() {
return fluidTankSlots;
}

public NonNullList<SlotBacteriaStorage> getBacteriaStorageSlots() {
return bacteriaStorageSlots;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.portingdeadmods.nautec.api.menu.slots;

import net.neoforged.neoforge.fluids.capability.IFluidHandler;

public abstract class AbstractSlot {
protected final int slot;
public int index;
protected final int x;
protected final int y;

public AbstractSlot(int index, int x, int y) {
this.slot = index;
this.x = x;
this.y = y;
}

public int getX() {
return x;
}

public int getY() {
return y;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.portingdeadmods.nautec.api.menu.slots;

import com.portingdeadmods.nautec.capabilities.bacteria.IBacteriaStorage;

public class SlotBacteriaStorage extends AbstractSlot {
private static final int WIDTH = 18;
private static final int HEIGHT = 18;
private final IBacteriaStorage bacteriaStorage;

public SlotBacteriaStorage(IBacteriaStorage bacteriaStorage, int index, int x, int y) {
super(index, x, y);
this.bacteriaStorage = bacteriaStorage;
}

public IBacteriaStorage getBacteriaStorage() {
return bacteriaStorage;
}

public int getWidth() {
return WIDTH;
}

public int getHeight() {
return HEIGHT;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.portingdeadmods.nautec.api.menu.slots;

import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.capability.IFluidHandler;

public class SlotFluidHandler extends AbstractSlot {
private final IFluidHandler fluidHandler;
private final int width;
private final int height;

public SlotFluidHandler(IFluidHandler fluidHandler, int index, int x, int y, int width, int height) {
super(index, x, y);
this.fluidHandler = fluidHandler;
this.width = width;
this.height = height;
}

public FluidStack getFluidStack() {
return fluidHandler.getFluidInTank(slot);
}

public int getFluidCapacity() {
return fluidHandler.getTankCapacity(slot);
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}

public IFluidHandler getFluidHandler() {
return fluidHandler;
}
}
Loading

0 comments on commit f483db8

Please sign in to comment.