Skip to content

Commit

Permalink
Some recipe and serializer fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Electro593 committed Jan 25, 2025
1 parent 8f0be7c commit 3bc5291
Showing 42 changed files with 211 additions and 320 deletions.
Original file line number Diff line number Diff line change
@@ -5,8 +5,6 @@
import net.minecraft.util.*;
import org.jetbrains.annotations.*;

import java.util.*;

public interface GatedRecipeSerializer<T extends Recipe<?>> extends RecipeSerializer<T> {

static void writeNullableIdentifier(PacketByteBuf buf, @Nullable Identifier identifier) {
@@ -26,16 +24,4 @@ static void writeNullableIdentifier(PacketByteBuf buf, @Nullable Identifier iden
return null;
}

static @NotNull FluidIngredient readFluidIngredient(PacketByteBuf buf) {
boolean isTag = buf.readBoolean();
Identifier id = readNullableIdentifier(buf);
return FluidIngredient.fromIdentifier(id, isTag);
}

static void writeFluidIngredient(PacketByteBuf buf, @NotNull FluidIngredient ingredient) {
Objects.requireNonNull(ingredient);
buf.writeBoolean(ingredient.isTag());
writeNullableIdentifier(buf, ingredient.id());
}

}
Original file line number Diff line number Diff line change
@@ -209,7 +209,7 @@ public void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLooku

Inventories.readNbt(nbt, this.inventory, registryLookup);

CodecHelper.fromNbt(InkStorageComponent.CODEC, nbt.get("InkStorage"), storage ->
CodecHelper.fromNbt(InkStorageComponent.CODEC, nbt.get("InkStorage")).ifPresent(storage ->
this.inkStorage = new IndividualCappedInkStorage(storage.maxEnergyTotal(), storage.storedEnergy()));
this.craftingTime = nbt.getShort("CraftingTime");
this.craftingTimeTotal = nbt.getShort("CraftingTimeTotal");
Original file line number Diff line number Diff line change
@@ -173,7 +173,7 @@ public void inventoryChanged() {
public void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
super.readNbt(nbt, registryLookup);

CodecHelper.fromNbt(InkStorageComponent.CODEC, nbt.get("InkStorage"),storage ->
CodecHelper.fromNbt(InkStorageComponent.CODEC, nbt.get("InkStorage")).ifPresent(storage ->
this.inkStorage = new IndividualCappedInkStorage(storage.maxEnergyTotal(), storage.storedEnergy()));
if (nbt.contains("Looper", NbtElement.COMPOUND_TYPE)) {
this.tickLooper = TickLooper.readNbt(nbt.getCompound("Looper"));
Original file line number Diff line number Diff line change
@@ -115,7 +115,7 @@ public void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLooku
if (!this.readLootTable(nbt)) {
Inventories.readNbt(nbt, this.inventory, registryLookup);
}
CodecHelper.fromNbt(InkStorageComponent.CODEC, nbt.get("InkStorage"), storage ->
CodecHelper.fromNbt(InkStorageComponent.CODEC, nbt.get("InkStorage")).ifPresent(storage ->
this.inkStorage = new TotalCappedInkStorage(storage.maxEnergyTotal(), storage.storedEnergy()));
this.ownerUUID = PlayerOwned.readOwnerUUID(nbt);
if (nbt.contains("SelectedColor", NbtElement.STRING_TYPE)) {
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package de.dafuqs.spectrum.blocks.pastel_network.network;

import de.dafuqs.spectrum.blocks.pastel_network.nodes.*;
import de.dafuqs.spectrum.helpers.CodecHelper;
import de.dafuqs.spectrum.helpers.*;
import net.minecraft.nbt.*;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.registry.*;
import net.minecraft.server.world.*;
import net.minecraft.world.*;
import org.jetbrains.annotations.*;
@@ -48,9 +48,8 @@ public NbtCompound writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup regis

public static ServerPastelNetworkManager fromNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
ServerPastelNetworkManager manager = new ServerPastelNetworkManager();
for (NbtElement element : nbt.getList("Networks", NbtElement.COMPOUND_TYPE)) {
CodecHelper.fromNbt(ServerPastelNetwork.CODEC, element, manager.networks::add);
}
for (NbtElement element : nbt.getList("Networks", NbtElement.COMPOUND_TYPE))
CodecHelper.fromNbt(ServerPastelNetwork.CODEC, element).ifPresent(manager.networks::add);
return manager;
}

Original file line number Diff line number Diff line change
@@ -58,17 +58,18 @@ public class PedestalBlockEntity extends LockableContainerBlockEntity implements
protected UUID ownerUUID;
protected PedestalVariant pedestalVariant;
protected DefaultedList<ItemStack> inventory;
protected CraftingRecipeInput recipeInput;
protected boolean shouldCraft;
protected float storedXP;
protected int craftingTime;
protected int craftingTimeTotal;
public @Nullable RecipeEntry<?> currentRecipe;
protected PedestalRecipeTier cachedMaxPedestalTier;
protected long cachedMaxPedestalTierTick;
protected UpgradeHolder upgrades;
protected boolean inventoryChanged;

public CraftingRecipeInput recipeInput;
public @Nullable RecipeEntry<?> currentRecipe;

protected final PropertyDelegate propertyDelegate = new BlockPosDelegate(pos) {
@Override
public int get(int index) {
31 changes: 28 additions & 3 deletions src/main/java/de/dafuqs/spectrum/helpers/CodecHelper.java
Original file line number Diff line number Diff line change
@@ -2,20 +2,45 @@

import com.mojang.datafixers.util.*;
import com.mojang.serialization.*;
import de.dafuqs.spectrum.mixin.accessors.*;
import net.minecraft.nbt.*;
import net.minecraft.registry.*;

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class CodecHelper {

public static MapCodec<RegistryWrapper.WrapperLookup> LOOKUP = new MapCodec<>() {
@Override
public <T> Stream<T> keys(DynamicOps<T> dynamicOps) {
return Stream.empty();
}

@Override
public <T> DataResult<RegistryWrapper.WrapperLookup> decode(DynamicOps<T> dynamicOps, MapLike<T> mapLike) {
if (dynamicOps instanceof RegistryOps<T> registryOps) {
var infoGetter = ((RegistryOpsAccessor) registryOps).getRegistryInfoGetter();
var lookup = ((CachedRegistryInfoGetterAccessor) infoGetter).getRegistriesLookup();
return DataResult.success(lookup);
}
return DataResult.error(() -> "Error: The LOOKUP codec requires RegistryOps.");
}

@Override
public <T> RecordBuilder<T> encode(RegistryWrapper.WrapperLookup wrapperLookup, DynamicOps<T> dynamicOps, RecordBuilder<T> recordBuilder) {
return recordBuilder;
}
};

public static <T> Codec<List<T>> singleOrList(Codec<T> codec) {
return Codec.withAlternative(codec.listOf(), codec, List::of);
}

public static <T> void fromNbt(Codec<T> codec, NbtElement nbt, Consumer<? super T> ifValid) {
if (nbt != null)
codec.decode(NbtOps.INSTANCE, nbt).result().map(Pair::getFirst).ifPresent(ifValid);
public static <T> Optional<T> fromNbt(Codec<T> codec, NbtElement nbt) {
if (nbt == null) return Optional.empty();
return codec.decode(NbtOps.INSTANCE, nbt).result().map(Pair::getFirst);
}

public static <T> void toNbt(Codec<T> codec, T value, Consumer<? super NbtElement> ifValid) {
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

import com.mojang.datafixers.util.*;
import io.netty.buffer.*;
import net.minecraft.network.*;
import net.minecraft.network.codec.*;
import net.minecraft.network.encoding.*;
import net.minecraft.registry.*;
@@ -14,6 +15,7 @@ public class PacketCodecHelper {

public static final PacketCodec<ByteBuf, Vec3i> VEC3I = PacketCodec.tuple(PacketCodecs.VAR_INT, Vec3i::getX, PacketCodecs.VAR_INT, Vec3i::getY, PacketCodecs.VAR_INT, Vec3i::getZ, Vec3i::new);
public static final PacketCodec<ByteBuf, Vec3d> VEC3D = PacketCodec.tuple(PacketCodecs.DOUBLE, Vec3d::getX, PacketCodecs.DOUBLE, Vec3d::getY, PacketCodecs.DOUBLE, Vec3d::getZ, Vec3d::new);
public static final PacketCodec<RegistryByteBuf, RegistryWrapper.WrapperLookup> LOOKUP = PacketCodec.ofStatic((buf, value) -> {}, RegistryByteBuf::getRegistryManager);

/**
* Use this if you can't use PacketCodecs.registryValue, such as when it isn't a RegistryByteBuf,
Original file line number Diff line number Diff line change
@@ -12,6 +12,8 @@

public class SuspiciousBrewItem extends BeverageItem {

//TODO should this use the SuspiciousStewContents component instead?

public SuspiciousBrewItem(Settings settings) {
super(settings.component(DataComponentTypes.POTION_CONTENTS, PotionContentsComponent.DEFAULT));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.dafuqs.spectrum.mixin.accessors;

import net.minecraft.registry.*;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.gen.*;

@Mixin(RegistryOps.CachedRegistryInfoGetter.class)
public interface CachedRegistryInfoGetterAccessor {

@Accessor
RegistryWrapper.WrapperLookup getRegistriesLookup();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.dafuqs.spectrum.mixin.accessors;

import net.minecraft.registry.*;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.gen.*;

@Mixin(RegistryOps.class)
public interface RegistryOpsAccessor {

@Accessor
RegistryOps.RegistryInfoGetter getRegistryInfoGetter();

}
14 changes: 13 additions & 1 deletion src/main/java/de/dafuqs/spectrum/recipe/IngredientStack.java
Original file line number Diff line number Diff line change
@@ -29,6 +29,10 @@ public IngredientStack(Ingredient ingredient, ComponentPredicate componentPredic
this.count = count;
}

public IngredientStack(Ingredient ingredient) {
this(ingredient, null, 1);
}

public int getCount() {
return count;
}
@@ -37,14 +41,22 @@ public Ingredient getIngredient() {
return ingredient;
}

public static IngredientStack of(Ingredient ingredient) {
return new IngredientStack(ingredient);
}

public static IngredientStack ofItems(Item item) {
return new IngredientStack(Ingredient.ofItems(item), null, 1);
return new IngredientStack(Ingredient.ofItems(item));
}

public static IngredientStack ofItems(int count, Item item) {
return new IngredientStack(Ingredient.ofItems(item), null, count);
}

public static IngredientStack ofTag(TagKey<Item> tag) {
return new IngredientStack(Ingredient.fromTag(tag));
}

public static IngredientStack ofTag(TagKey<Item> tag, int count) {
return new IngredientStack(Ingredient.fromTag(tag), null, count);
}
Original file line number Diff line number Diff line change
@@ -91,7 +91,7 @@ public boolean fits(int width, int height) {
@Override
public ItemStack getResult(RegistryWrapper.WrapperLookup registryLookup) {
List<BlockState> states = getGrowthStages();
return states.get(states.size() - 1).getBlock().asItem().getDefaultStack();
return states.getLast().getBlock().asItem().getDefaultStack();
}

@Override
Original file line number Diff line number Diff line change
@@ -118,7 +118,7 @@ public ItemStack createIcon() {

@Override
public RecipeSerializer<?> getSerializer() {
return SpectrumRecipeTypes.ENCHANTMENT_UPGRADE_RECIPE_SERIALIZER;
return SpectrumRecipeSerializers.ENCHANTMENT_UPGRADE_RECIPE_SERIALIZER;
}

@Override
Original file line number Diff line number Diff line change
@@ -9,28 +9,14 @@
import net.minecraft.item.*;
import net.minecraft.registry.*;

import java.util.*;

public class EnchantmentUpgradeRecipeSerializer extends EndecRecipeSerializer<EnchantmentUpgradeRecipe> implements GatedRecipeSerializer<EnchantmentUpgradeRecipe> {

// FIXME - Experimental. Will likely break as I don't believe the recipes are properly being registered
// Maybe the recipe injection code (KubeJS compat) is easier?
public static final StructEndec<EnchantmentUpgradeRecipe> ENDEC = StructEndecBuilder.of(
Endec.STRING.optionalFieldOf("group", recipe -> recipe.group, ""),
Endec.BOOLEAN.optionalFieldOf("secret", recipe -> recipe.secret, false),
MinecraftEndecs.IDENTIFIER.fieldOf("required_advancement", recipe -> recipe.requiredAdvancementIdentifier),
CodecUtils.toEndec(Enchantment.ENTRY_CODEC).fieldOf("enchantment", recipe -> recipe.enchantmentEntry),
EnchantUpgradeLevelEntry.ENDEC.listOf().xmap(enchantUpgradeLevelEntries -> {
List<EnchantUpgradeLevelEntry> entries = new ArrayList<>();
for (EnchantUpgradeLevelEntry enchantmentUpgradeRecipe : enchantUpgradeLevelEntries) {
entries.add(new EnchantUpgradeLevelEntry(
enchantmentUpgradeRecipe.experience(),
enchantmentUpgradeRecipe.requiredItem(),
enchantmentUpgradeRecipe.count())
);
}
return entries;
}, o -> o).fieldOf("levels", EnchantmentUpgradeRecipe::getDefaultLevelEntry),
EnchantUpgradeLevelEntry.ENDEC.listOf().fieldOf("levels", EnchantmentUpgradeRecipe::getDefaultLevelEntry),
EnchantmentUpgradeRecipe::createRecipes
);

@@ -39,11 +25,14 @@ public EnchantmentUpgradeRecipeSerializer() {
}

public record EnchantUpgradeLevelEntry(int experience, Item requiredItem, int count) {

public static final Endec<EnchantUpgradeLevelEntry> ENDEC = StructEndecBuilder.of(
Endec.INT.fieldOf("experience", EnchantUpgradeLevelEntry::experience),
MinecraftEndecs.ofRegistry(Registries.ITEM).fieldOf("required_item", EnchantUpgradeLevelEntry::requiredItem),
Endec.INT.fieldOf("count", EnchantUpgradeLevelEntry::count),
EnchantUpgradeLevelEntry::new
);

}

}
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
import java.util.*;

public class DragonrotConvertingRecipe extends FluidConvertingRecipe {

public static final Identifier UNLOCK_IDENTIFIER = SpectrumCommon.locate("hidden/interact_with_dragonrot");
private static final Set<Item> outputItems = new HashSet<>();

@@ -30,7 +30,7 @@ public ItemStack createIcon() {

@Override
public RecipeSerializer<?> getSerializer() {
return SpectrumRecipeTypes.DRAGONROT_CONVERTING_SERIALIZER;
return SpectrumRecipeSerializers.DRAGONROT_CONVERTING_SERIALIZER;
}

@Override
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
import net.minecraft.world.*;
import org.jetbrains.annotations.*;

public class FluidConvertingRecipe extends GatedSpectrumRecipe<RecipeInput> {
public abstract class FluidConvertingRecipe extends GatedSpectrumRecipe<RecipeInput> {

protected final Ingredient input;
protected final ItemStack output;
Original file line number Diff line number Diff line change
@@ -5,55 +5,21 @@
import io.wispforest.endec.impl.*;
import io.wispforest.owo.serialization.*;
import io.wispforest.owo.serialization.endec.*;
import net.minecraft.item.*;
import net.minecraft.recipe.*;
import net.minecraft.util.*;

public class FluidConvertingRecipeSerializer extends EndecRecipeSerializer<FluidConvertingRecipe> implements GatedRecipeSerializer<FluidConvertingRecipe> {
public class FluidConvertingRecipeSerializer<T extends FluidConvertingRecipe> extends EndecRecipeSerializer<T> implements GatedRecipeSerializer<T> {

public static final StructEndec<FluidConvertingRecipe> ENDEC = StructEndecBuilder.of(
Endec.STRING.optionalFieldOf("group", recipe -> recipe.group, ""),
Endec.BOOLEAN.optionalFieldOf("secret", recipe -> recipe.secret, false),
MinecraftEndecs.IDENTIFIER.fieldOf("required_advancement", recipe -> recipe.requiredAdvancementIdentifier),
CodecUtils.toEndec(Ingredient.DISALLOW_EMPTY_CODEC).fieldOf("ingredient", recipe -> recipe.input),
MinecraftEndecs.ITEM_STACK.fieldOf("result", recipe -> recipe.output),
FluidConvertingRecipe::new
);

public FluidConvertingRecipeSerializer() {
super(ENDEC);
public FluidConvertingRecipeSerializer(StructEndecBuilder.Function5<String, Boolean, Identifier, Ingredient, ItemStack, T> factory) {
super(StructEndecBuilder.of(
Endec.STRING.optionalFieldOf("group", recipe -> recipe.group, ""),
Endec.BOOLEAN.optionalFieldOf("secret", recipe -> recipe.secret, false),
MinecraftEndecs.IDENTIFIER.fieldOf("required_advancement", recipe -> recipe.requiredAdvancementIdentifier),
CodecUtils.toEndec(Ingredient.DISALLOW_EMPTY_CODEC).fieldOf("ingredient", recipe -> recipe.input),
MinecraftEndecs.ITEM_STACK.fieldOf("result", recipe -> recipe.output),
factory
));
}

// @Override
// public R read(Identifier identifier, JsonObject jsonObject) {
// String group = readGroup(jsonObject);
// boolean secret = readSecret(jsonObject);
// Identifier requiredAdvancementIdentifier = readRequiredAdvancementIdentifier(jsonObject);
//
// JsonElement jsonElement = JsonHelper.getObject(jsonObject, "ingredient");
// Ingredient ingredient = Ingredient.fromJson(jsonElement);
// ItemStack outputItemStack = RecipeUtils.itemStackWithNbtFromJson(JsonHelper.getObject(jsonObject, "result"));
//
// return this.recipeFactory.create(identifier, group, secret, requiredAdvancementIdentifier, ingredient, outputItemStack);
// }
//
// @Override
// public void write(PacketByteBuf packetByteBuf, R recipe) {
// packetByteBuf.writeString(recipe.group);
// packetByteBuf.writeBoolean(recipe.secret);
// writeNullableIdentifier(packetByteBuf, recipe.requiredAdvancementIdentifier);
//
// recipe.input.write(packetByteBuf);
// packetByteBuf.writeItemStack(recipe.output);
// }
//
// @Override
// public R read(Identifier identifier, PacketByteBuf packetByteBuf) {
// String group = packetByteBuf.readString();
// boolean secret = packetByteBuf.readBoolean();
// Identifier requiredAdvancementIdentifier = readNullableIdentifier(packetByteBuf);
//
// Ingredient ingredient = Ingredient.fromPacket(packetByteBuf);
// ItemStack outputItemStack = packetByteBuf.readItemStack();
// return this.recipeFactory.create(identifier, group, secret, requiredAdvancementIdentifier, ingredient, outputItemStack);
// }

}
Loading

0 comments on commit 3bc5291

Please sign in to comment.