Skip to content

Commit

Permalink
may or may not be working
Browse files Browse the repository at this point in the history
  • Loading branch information
Thepigcat76 committed Dec 15, 2024
1 parent eb068a6 commit b1dce77
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 128 deletions.
12 changes: 3 additions & 9 deletions src/main/java/com/portingdeadmods/nautec/NTRegistries.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.portingdeadmods.nautec.api.augments.AugmentSlot;
import com.portingdeadmods.nautec.api.augments.AugmentType;
import com.portingdeadmods.nautec.api.bacteria.BacteriaSerializer;
import com.portingdeadmods.nautec.api.multiblocks.Multiblock;
import com.portingdeadmods.nautec.api.bacteria.Bacteria;
import net.minecraft.core.Registry;
Expand All @@ -15,18 +16,11 @@ public final class NTRegistries {
private static final ResourceKey<Registry<AugmentType<?>>> AUGMENT_TYPE_KEY = ResourceKey.createRegistryKey(ResourceLocation.fromNamespaceAndPath(Nautec.MODID, "augment_type"));
public static final ResourceKey<Registry<AugmentSlot>> AUGMENT_SLOT_KEY = ResourceKey.createRegistryKey(ResourceLocation.fromNamespaceAndPath(Nautec.MODID, "augment_slot"));
private static final ResourceKey<Registry<Multiblock>> MULTIBLOCK_KEY = 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<Bacteria>> BACTERIA_KEY = ResourceKey.createRegistryKey(ResourceLocation.fromNamespaceAndPath(Nautec.MODID, "bacteria"));

public static final Registry<AugmentType<?>> AUGMENT_TYPE = new RegistryBuilder<>(AUGMENT_TYPE_KEY).create();
public static final Registry<AugmentSlot> AUGMENT_SLOT = new RegistryBuilder<>(AUGMENT_SLOT_KEY).create();
public static final Registry<Multiblock> MULTIBLOCK = new RegistryBuilder<>(MULTIBLOCK_KEY).create();

@SubscribeEvent
public static void registerDatapackRegistries(DataPackRegistryEvent.NewRegistry event) {
event.dataPackRegistry(
BACTERIA_KEY,
Bacteria.CODEC,
Bacteria.CODEC
);
}
public static final Registry<BacteriaSerializer<?>> BACTERIA_SERIALIZER = new RegistryBuilder<>(BACTERIA_SERIALIZER_KEY).create();
}
88 changes: 39 additions & 49 deletions src/main/java/com/portingdeadmods/nautec/api/bacteria/Bacteria.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.portingdeadmods.nautec.api.bacteria;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.portingdeadmods.nautec.Nautec;
import com.portingdeadmods.nautec.utils.codec.CodecUtils;
import com.portingdeadmods.nautec.NTRegistries;
import io.netty.buffer.ByteBuf;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;

public interface Bacteria {
Codec<Bacteria> CODEC = NTRegistries.BACTERIA_SERIALIZER.byNameCodec().dispatch(Bacteria::getSerializer, BacteriaSerializer::mapCodec);
StreamCodec<RegistryFriendlyByteBuf, Bacteria> STREAM_CODEC = ByteBufCodecs.registry(NTRegistries.BACTERIA_SERIALIZER_KEY).dispatch(Bacteria::getSerializer, BacteriaSerializer::streamCodec);

/*
Bacteria! We begin with only one!
Bacteria! Two's what we then become!
Expand All @@ -23,60 +24,49 @@ public interface Bacteria {
Bacteria! We will take over the world!
*/
ResourceLocation id();
Item type();
float growthRate();
float mutationResistance();
float productionRate();
int lifespan();
int color();

default byte getAlpha() {
return (byte) ((color() >> 24) & 0xFF);
}
BacteriaStats stats();

default byte getRed() {
return (byte) ((color() >> 16) & 0xFF);
}
BacteriaSerializer<?> getSerializer();

default byte getGreen() {
return (byte) ((color() >> 8) & 0xFF);
static Builder of() {
return new Builder();
}

default byte getBlue() {
return (byte) (color() & 0xFF);
}
class Builder {
private float growthRate;
private float mutationResistance;
private float productionRate;
private int lifespan;
private int color;

public Builder growthRate(float growthRate) {
this.growthRate = growthRate;
return this;
}

Codec<Bacteria> CODEC = RecordCodecBuilder.create(
instance -> instance.group(
ResourceLocation.CODEC.fieldOf("id").forGetter(Bacteria::id),
CodecUtils.ITEM_CODEC.fieldOf("type").forGetter(Bacteria::type),
Codec.FLOAT.fieldOf("growth_rate").forGetter(Bacteria::growthRate),
Codec.FLOAT.fieldOf("mutation_resistance").forGetter(Bacteria::mutationResistance),
Codec.FLOAT.fieldOf("production_rate").forGetter(Bacteria::productionRate),
Codec.INT.fieldOf("lifespan").forGetter(Bacteria::lifespan),
Codec.INT.fieldOf("color").forGetter(Bacteria::color)
).apply(instance, BacteriaImpl::new)
);
public Builder mutationResistance(float mutationResistance) {
this.mutationResistance = mutationResistance;
return this;
}

StreamCodec<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,
ByteBufCodecs.INT,
Bacteria::color,
(a, b, c, d, e, f) -> new BacteriaImpl(Nautec.rl("empty"), a, b, c, d, e, f)
);
public Builder productionRate(float productionRate) {
this.productionRate = productionRate;
return this;
}

public Builder lifespan(int lifespan) {
this.lifespan = lifespan;
return this;
}

public Builder color(int color) {
this.color = color;
return this;
}

class Builder {
public Bacteria build(ResourceLocation location) {
return new BacteriaImpl(location, Items.AIR, 0, 0, 0, 0, 0);
return new BacteriaImpl(location, new BacteriaStats(Items.AIR, growthRate, mutationResistance, productionRate, lifespan, color));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,9 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;

public record BacteriaImpl(
ResourceLocation id,
Item type,
float growthRate,
float mutationResistance,
float productionRate,
int lifespan,
int color) implements Bacteria {

@Override
public byte getAlpha() {
return (byte) ((color >> 24) & 0xFF);
}

@Override
public byte getRed() {
return (byte) ((color >> 16) & 0xFF);
}

@Override
public byte getGreen() {
return (byte) ((color >> 8) & 0xFF);
}

public record BacteriaImpl(ResourceLocation id, BacteriaStats stats) implements Bacteria {
@Override
public byte getBlue() {
return (byte) (color & 0xFF);
public BacteriaSerializer getSerializer() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.portingdeadmods.nautec.api.bacteria;

import com.mojang.serialization.MapCodec;
import io.netty.buffer.ByteBuf;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;

public interface BacteriaSerializer<T extends Bacteria> {
MapCodec<T> mapCodec();

StreamCodec<RegistryFriendlyByteBuf, T> streamCodec();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.portingdeadmods.nautec.api.bacteria;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.portingdeadmods.nautec.Nautec;
import com.portingdeadmods.nautec.utils.codec.CodecUtils;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;

public record BacteriaStats(Item type,
float growthRate,
float mutationResistance,
float productionRate,
int lifespan,
int color) {
public static final Codec<BacteriaStats> CODEC = RecordCodecBuilder.create(
instance -> instance.group(
CodecUtils.ITEM_CODEC.fieldOf("type").forGetter(BacteriaStats::type),
Codec.FLOAT.fieldOf("growth_rate").forGetter(BacteriaStats::growthRate),
Codec.FLOAT.fieldOf("mutation_resistance").forGetter(BacteriaStats::mutationResistance),
Codec.FLOAT.fieldOf("production_rate").forGetter(BacteriaStats::productionRate),
Codec.INT.fieldOf("lifespan").forGetter(BacteriaStats::lifespan),
Codec.INT.fieldOf("color").forGetter(BacteriaStats::color)
).apply(instance, BacteriaStats::new)
);

public static final StreamCodec<RegistryFriendlyByteBuf, BacteriaStats> STREAM_CODEC = StreamCodec.composite(
CodecUtils.ITEM_STREAM_CODEC,
BacteriaStats::type,
ByteBufCodecs.FLOAT,
BacteriaStats::growthRate,
ByteBufCodecs.FLOAT,
BacteriaStats::mutationResistance,
ByteBufCodecs.FLOAT,
BacteriaStats::productionRate,
ByteBufCodecs.INT,
BacteriaStats::lifespan,
ByteBufCodecs.INT,
BacteriaStats::color,
BacteriaStats::new
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,21 @@

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.portingdeadmods.nautec.Nautec;
import com.portingdeadmods.nautec.NTRegistries;
import com.portingdeadmods.nautec.api.bacteria.Bacteria;
import com.portingdeadmods.nautec.registries.NTBacterias;
import com.portingdeadmods.nautec.utils.BacteriaHelper;
import net.minecraft.core.HolderLookup;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;

import java.util.Objects;
import java.util.function.Function;

public record ComponentBacteriaStorage(Bacteria bacteria, long bacteriaAmount) {
public static final ComponentBacteriaStorage EMPTY = new ComponentBacteriaStorage(new Bacteria() {
@Override
public ResourceLocation id() {
return Nautec.rl("empty");
}

@Override
public Item type() {
return Items.AIR;
}

@Override
public float growthRate() {
return 0;
}

@Override
public float mutationResistance() {
return 0;
}

@Override
public float productionRate() {
return 0;
}

@Override
public int lifespan() {
return 0;
}

@Override
public int color() {
return 0;
}
}, 0);
public static final Function<HolderLookup.Provider, ComponentBacteriaStorage> EMPTY =
lookup -> new ComponentBacteriaStorage(BacteriaHelper.getBacteria(lookup, NTBacterias.EMPTY), 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.portingdeadmods.nautec.NTRegistries;
import com.portingdeadmods.nautec.Nautec;
import com.portingdeadmods.nautec.registries.NTBacteria;
import com.portingdeadmods.nautec.registries.NTBacterias;
import com.portingdeadmods.nautec.registries.NTBlocks;
import net.minecraft.core.HolderGetter;
import net.minecraft.core.HolderLookup;
Expand Down Expand Up @@ -50,7 +50,7 @@ public DatapackRegistryProvider(PackOutput output, CompletableFuture<HolderLooku

public static final RegistrySetBuilder BUILDER = new RegistrySetBuilder()
// -- BACTERIA --
.add(NTRegistries.BACTERIA_KEY, NTBacteria::bootstrap)
.add(NTRegistries.BACTERIA_KEY, NTBacterias::bootstrap)
// -- WORLDGEN --
// Beach - Higher Spawn Chance - 1 to 4 Ores in a Vein
.add(Registries.CONFIGURED_FEATURE, context -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import net.minecraft.data.worldgen.BootstrapContext;
import net.minecraft.resources.ResourceKey;

public class NTBacteria {
public class NTBacterias {
public static final ResourceKey<Bacteria> EMPTY = key("empty");

public static void bootstrap(BootstrapContext<Bacteria> context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class NTCreativeTabs {

if (item.asItem() instanceof IBacteriaItem) {
ItemStack stack = new ItemStack(item);
stack.set(NTDataComponents.BACTERIA, Optional.of(ComponentBacteriaStorage.EMPTY));
stack.set(NTDataComponents.BACTERIA, Optional.of(ComponentBacteriaStorage.EMPTY.apply(params.holders())));
output.accept(item);
output.accept(stack);
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/portingdeadmods/nautec/utils/BacteriaHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.portingdeadmods.nautec.utils;

import com.portingdeadmods.nautec.NTRegistries;
import com.portingdeadmods.nautec.api.bacteria.Bacteria;
import net.minecraft.core.HolderLookup;
import net.minecraft.resources.ResourceKey;

public final class BacteriaHelper {
public static Bacteria getBacteria(HolderLookup.Provider lookup, ResourceKey<Bacteria> bacteriaType) {
return lookup.asGetterLookup().lookupOrThrow(NTRegistries.BACTERIA_KEY).getOrThrow(bacteriaType).value();
}
}

0 comments on commit b1dce77

Please sign in to comment.