Skip to content

Commit

Permalink
Finish Recipe Serializers (for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
Noaaan committed Jan 4, 2025
1 parent d5f3d13 commit 52f477d
Show file tree
Hide file tree
Showing 20 changed files with 297 additions and 292 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.*;
import de.dafuqs.spectrum.*;
import io.wispforest.endec.*;
import net.minecraft.server.*;
import net.minecraft.server.command.*;
import net.minecraft.server.world.*;
Expand All @@ -16,6 +17,8 @@
public interface FusionShrineRecipeWorldEffect {

Map<String, FusionShrineRecipeWorldEffect> TYPES = new HashMap<>();

Endec<FusionShrineRecipeWorldEffect> ENDEC = Endec.STRING.xmap(FusionShrineRecipeWorldEffect::fromString, Objects::toString);

FusionShrineRecipeWorldEffect NOTHING = register("nothing", new FusionShrineRecipeWorldEffect.SingleTimeRecipeWorldEffect() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import net.minecraft.util.*;
import org.jetbrains.annotations.*;

public class GatedSpectrumRecipe<C extends RecipeInput> implements GatedRecipe<C> {
public abstract class GatedSpectrumRecipe<C extends RecipeInput> implements GatedRecipe<C> {

public final String group;
public final boolean secret;
Expand Down Expand Up @@ -43,8 +43,10 @@ public Identifier getRequiredAdvancementIdentifier() {
}

@Override
public abstract Identifier getRecipeTypeUnlockIdentifier();

public Identifier getRecipeTypeUnlockIdentifier() {
return null;
}

//TODO: Should this be false when the recipe is unlocked?
@Override
public boolean isIgnoredInRecipeBook() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,14 @@
import io.wispforest.endec.impl.*;
import io.wispforest.owo.serialization.*;
import io.wispforest.owo.serialization.endec.*;
import io.wispforest.endec.*;
import io.wispforest.endec.impl.*;
import io.wispforest.owo.serialization.*;
import io.wispforest.owo.serialization.endec.*;
import net.minecraft.enchantment.*;
import net.minecraft.item.*;
import net.minecraft.registry.*;

import java.util.*;

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

// public static final StructEndec<EnchantmentUpgradeRecipe> RECIPE_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),
// MinecraftEndecs.IDENTIFIER.fieldOf("enchantment", recipe -> recipe.enchantmentIdentifier),
// Endec.INT.fieldOf("enchantment_destination_level", recipe -> recipe.enchantmentDestinationLevel),
// /// unwrap into these specific fields
// Endec.INT.fieldOf("required_experience", recipe -> recipe.requiredExperience),
// MinecraftEndecs.ofRegistry(Registries.ITEM).fieldOf("required_item",recipe -> recipe.requiredItem),
// Endec.INT.fieldOf("required_item_count", recipe -> recipe.requiredItemCount),
// ///
// EnchantmentUpgradeRecipe::new
// );

// 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,24 @@ public class FusionShrineRecipe extends GatedStackSpectrumRecipe<FusionShrineBlo
// copy all nbt data from the first stack in the ingredients to the output stack
protected final boolean copyNbt;

public FusionShrineRecipe(String group, boolean secret, Identifier requiredAdvancementIdentifier,
List<IngredientStack> craftingInputs, FluidIngredient fluid, ItemStack output, float experience, int craftingTime, boolean yieldUpgradesDisabled, boolean playCraftingFinishedEffects, boolean copyNbt,
List<SpectrumLocationPredicate<?, ?>> spectrumLocationPredicates, @NotNull FusionShrineRecipeWorldEffect startWorldEffect, @NotNull List<FusionShrineRecipeWorldEffect> duringWorldEffects, @NotNull FusionShrineRecipeWorldEffect finishWorldEffect, @Nullable Text description) {
public FusionShrineRecipe(
String group,
boolean secret,
Identifier requiredAdvancementIdentifier,
List<IngredientStack> craftingInputs,
FluidIngredient fluid,
ItemStack output,
float experience,
int craftingTime,
boolean yieldUpgradesDisabled,
boolean playCraftingFinishedEffects,
boolean copyNbt,
List<SpectrumLocationPredicate<?, ?>> spectrumLocationPredicates,
@NotNull FusionShrineRecipeWorldEffect startWorldEffect,
@NotNull List<FusionShrineRecipeWorldEffect> duringWorldEffects,
@NotNull FusionShrineRecipeWorldEffect finishWorldEffect,
@Nullable Text description
) {
super(group, secret, requiredAdvancementIdentifier);

this.craftingInputs = craftingInputs;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package de.dafuqs.spectrum.recipe.fusion_shrine;

import com.google.gson.*;
import de.dafuqs.spectrum.*;
import de.dafuqs.spectrum.api.predicate.*;
import com.mojang.serialization.*;
import de.dafuqs.spectrum.api.predicate.location.*;
import de.dafuqs.spectrum.api.recipe.*;
import de.dafuqs.spectrum.recipe.*;
import io.wispforest.endec.*;
import io.wispforest.endec.impl.*;
import io.wispforest.owo.serialization.*;
import io.wispforest.owo.serialization.endec.*;
import net.minecraft.item.*;
import net.minecraft.network.*;
import net.minecraft.network.codec.*;
import net.minecraft.text.*;
import net.minecraft.util.*;

import java.util.*;

public class FusionShrineRecipeSerializer implements GatedRecipeSerializer<FusionShrineRecipe> {

public static final StructEndec<FusionShrineRecipe> ENDEC = StructEndecBuilder.<FusionShrineRecipe>of(
public static final StructEndec<FusionShrineRecipe> 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),
Expand All @@ -29,132 +28,143 @@ public class FusionShrineRecipeSerializer implements GatedRecipeSerializer<Fusio
Endec.BOOLEAN.optionalFieldOf("disable_yield_upgrades", recipe -> recipe.yieldUpgradesDisabled, false),
Endec.BOOLEAN.optionalFieldOf("play_crafting_finished_effects", recipe -> recipe.playCraftingFinishedEffects, true),
Endec.BOOLEAN.optionalFieldOf("copy_components", recipe -> recipe.copyNbt, false),
// TODO - List of WorldConditionPredicates
// TODO - FusionShrineRecipeWorldEffect when starting
// TODO - List of FusionShrineRecipeWorldEffect during the craft
// TODO - FusionShrineRecipeWorldEffect when craft is finished
MinecraftEndecs.TEXT.optionalFieldOf("description", recipe -> recipe.description, Text.EMPTY)
CodecUtils.toEndec(SpectrumLocationPredicate.CODEC).listOf().optionalFieldOf("world_conditions",recipe -> recipe.spectrumLocationPredicates, List.of()),
FusionShrineRecipeWorldEffect.ENDEC.fieldOf("start_crafting_effect", recipe -> recipe.startWorldEffect),
FusionShrineRecipeWorldEffect.ENDEC.listOf().fieldOf("during_crafting_effects", recipe -> recipe.duringWorldEffects),
FusionShrineRecipeWorldEffect.ENDEC.fieldOf("finish_crafting_effect", recipe -> recipe.finishWorldEffect),
MinecraftEndecs.TEXT.optionalFieldOf("description", recipe -> recipe.description, Text.empty()),
FusionShrineRecipe::new
);

@Override
public FusionShrineRecipe read(Identifier identifier, JsonObject jsonObject) {
String group = readGroup(jsonObject);
boolean secret = readSecret(jsonObject);
Identifier requiredAdvancementIdentifier = readRequiredAdvancementIdentifier(jsonObject);

JsonArray ingredientArray = JsonHelper.getArray(jsonObject, "ingredients");
List<IngredientStack> craftingInputs = RecipeParser.ingredientStacksFromJson(ingredientArray, ingredientArray.size());
if (craftingInputs.size() > 7) {
throw new JsonParseException("Recipe cannot have more than 7 ingredients. Has " + craftingInputs.size());
}

FluidIngredient fluid = FluidIngredient.EMPTY;
if (JsonHelper.hasJsonObject(jsonObject, "fluid")) {
JsonObject fluidObject = JsonHelper.getObject(jsonObject, "fluid");
FluidIngredient.JsonParseResult result = FluidIngredient.fromJson(fluidObject);
fluid = result.result();
if (result.malformed()) {
// Currently handling malformed input leniently. May throw an error in the future.
SpectrumCommon.logError("Fusion Recipe " + identifier + "contains a malformed fluid input tag! This recipe will not be craftable.");
} else if (result.result() == FluidIngredient.EMPTY && !result.isTag()) { // tags get populated after recipes are
SpectrumCommon.logError("Fusion Recipe " + identifier + " specifies fluid " + result.id() + " that does not exist! This recipe will not be craftable.");
}
}

ItemStack output;
if (JsonHelper.hasJsonObject(jsonObject, "result")) {
output = RecipeUtils.itemStackWithNbtFromJson(JsonHelper.getObject(jsonObject, "result"));
} else {
output = ItemStack.EMPTY;
}
float experience = JsonHelper.getFloat(jsonObject, "experience", 0);
int craftingTime = JsonHelper.getInt(jsonObject, "time", 200);
boolean yieldUpgradesDisabled = JsonHelper.getBoolean(jsonObject, "disable_yield_upgrades", false);
boolean playCraftingFinishedEffects = JsonHelper.getBoolean(jsonObject, "play_crafting_finished_effects", true);

List<SpectrumLocationPredicateType<?>> worldConditions = new ArrayList<>();
if (JsonHelper.hasArray(jsonObject, "world_conditions")) {
for (JsonElement element : JsonHelper.getArray(jsonObject, "world_conditions")) {
worldConditions.add(SpectrumLocationPredicateType.fromJson(element));
}
}

FusionShrineRecipeWorldEffect startWorldEffect = FusionShrineRecipeWorldEffect.fromString(JsonHelper.getString(jsonObject, "start_crafting_effect", null));
List<FusionShrineRecipeWorldEffect> duringWorldEffects = new ArrayList<>();
if (JsonHelper.hasArray(jsonObject, "during_crafting_effects")) {
JsonArray worldEffectsArray = JsonHelper.getArray(jsonObject, "during_crafting_effects");
for (int i = 0; i < worldEffectsArray.size(); i++) {
duringWorldEffects.add(FusionShrineRecipeWorldEffect.fromString(worldEffectsArray.get(i).getAsString()));
}
}
FusionShrineRecipeWorldEffect finishWorldEffect = FusionShrineRecipeWorldEffect.fromString(JsonHelper.getString(jsonObject, "finish_crafting_effect", null));

Text description;
if (JsonHelper.hasString(jsonObject, "description")) {
description = Text.translatable(JsonHelper.getString(jsonObject, "description"));
} else {
description = null;
}
boolean copyNbt = JsonHelper.getBoolean(jsonObject, "copy_nbt", false);
if (copyNbt && output.isEmpty()) {
throw new JsonParseException("Recipe does have copy_nbt set, but has no output!");
}

return this.recipeFactory.create(identifier, group, secret, requiredAdvancementIdentifier,
craftingInputs, fluid, output, experience, craftingTime, yieldUpgradesDisabled, playCraftingFinishedEffects, copyNbt,
worldConditions, startWorldEffect, duringWorldEffects, finishWorldEffect, description);
public MapCodec<FusionShrineRecipe> codec() {
return CodecUtils.toMapCodec(ENDEC);
}


@Override
public void write(PacketByteBuf packetByteBuf, FusionShrineRecipe recipe) {
packetByteBuf.writeString(recipe.group);
packetByteBuf.writeBoolean(recipe.secret);
writeNullableIdentifier(packetByteBuf, recipe.requiredAdvancementIdentifier);

packetByteBuf.writeShort(recipe.craftingInputs.size());
for (IngredientStack ingredientStack : recipe.craftingInputs) {
ingredientStack.write(packetByteBuf);
}

writeFluidIngredient(packetByteBuf, recipe.fluid);
packetByteBuf.writeItemStack(recipe.output);
packetByteBuf.writeFloat(recipe.experience);
packetByteBuf.writeInt(recipe.craftingTime);
packetByteBuf.writeBoolean(recipe.yieldUpgradesDisabled);
packetByteBuf.writeBoolean(recipe.playCraftingFinishedEffects);

if (recipe.getDescription().isEmpty()) {
packetByteBuf.writeText(Text.literal(""));
} else {
packetByteBuf.writeText(recipe.getDescription().get());
}
packetByteBuf.writeBoolean(recipe.copyNbt);
public PacketCodec<RegistryByteBuf, FusionShrineRecipe> packetCodec() {
return CodecUtils.toPacketCodec(ENDEC);
}


@Override
public FusionShrineRecipe read(Identifier identifier, PacketByteBuf packetByteBuf) {
String group = packetByteBuf.readString();
boolean secret = packetByteBuf.readBoolean();
Identifier requiredAdvancementIdentifier = readNullableIdentifier(packetByteBuf);

short craftingInputCount = packetByteBuf.readShort();
List<IngredientStack> ingredients = IngredientStack.decodeByteBuf(packetByteBuf, craftingInputCount);

FluidIngredient fluid = readFluidIngredient(packetByteBuf);
ItemStack output = packetByteBuf.readItemStack();
float experience = packetByteBuf.readFloat();
int craftingTime = packetByteBuf.readInt();
boolean yieldUpgradesDisabled = packetByteBuf.readBoolean();
boolean playCraftingFinishedEffects = packetByteBuf.readBoolean();

Text description = packetByteBuf.readText();
boolean copyNbt = packetByteBuf.readBoolean();

return this.recipeFactory.create(identifier, group, secret, requiredAdvancementIdentifier,
ingredients, fluid, output, experience, craftingTime, yieldUpgradesDisabled, playCraftingFinishedEffects, copyNbt,
List.of(), FusionShrineRecipeWorldEffect.NOTHING, List.of(), FusionShrineRecipeWorldEffect.NOTHING, description);
}
// @Override
// public FusionShrineRecipe read(Identifier identifier, JsonObject jsonObject) {
// String group = readGroup(jsonObject);
// boolean secret = readSecret(jsonObject);
// Identifier requiredAdvancementIdentifier = readRequiredAdvancementIdentifier(jsonObject);
//
// JsonArray ingredientArray = JsonHelper.getArray(jsonObject, "ingredients");
// List<IngredientStack> craftingInputs = RecipeParser.ingredientStacksFromJson(ingredientArray, ingredientArray.size());
// if (craftingInputs.size() > 7) {
// throw new JsonParseException("Recipe cannot have more than 7 ingredients. Has " + craftingInputs.size());
// }
//
// FluidIngredient fluid = FluidIngredient.EMPTY;
// if (JsonHelper.hasJsonObject(jsonObject, "fluid")) {
// JsonObject fluidObject = JsonHelper.getObject(jsonObject, "fluid");
// FluidIngredient.JsonParseResult result = FluidIngredient.fromJson(fluidObject);
// fluid = result.result();
// if (result.malformed()) {
// // Currently handling malformed input leniently. May throw an error in the future.
// SpectrumCommon.logError("Fusion Recipe " + identifier + "contains a malformed fluid input tag! This recipe will not be craftable.");
// } else if (result.result() == FluidIngredient.EMPTY && !result.isTag()) { // tags get populated after recipes are
// SpectrumCommon.logError("Fusion Recipe " + identifier + " specifies fluid " + result.id() + " that does not exist! This recipe will not be craftable.");
// }
// }
//
// ItemStack output;
// if (JsonHelper.hasJsonObject(jsonObject, "result")) {
// output = RecipeUtils.itemStackWithNbtFromJson(JsonHelper.getObject(jsonObject, "result"));
// } else {
// output = ItemStack.EMPTY;
// }
// float experience = JsonHelper.getFloat(jsonObject, "experience", 0);
// int craftingTime = JsonHelper.getInt(jsonObject, "time", 200);
// boolean yieldUpgradesDisabled = JsonHelper.getBoolean(jsonObject, "disable_yield_upgrades", false);
// boolean playCraftingFinishedEffects = JsonHelper.getBoolean(jsonObject, "play_crafting_finished_effects", true);
//
// List<SpectrumLocationPredicateType<?>> worldConditions = new ArrayList<>();
// if (JsonHelper.hasArray(jsonObject, "world_conditions")) {
// for (JsonElement element : JsonHelper.getArray(jsonObject, "world_conditions")) {
// worldConditions.add(SpectrumLocationPredicateType.fromJson(element));
// }
// }
//
// FusionShrineRecipeWorldEffect startWorldEffect = FusionShrineRecipeWorldEffect.fromString(JsonHelper.getString(jsonObject, "start_crafting_effect", null));
// List<FusionShrineRecipeWorldEffect> duringWorldEffects = new ArrayList<>();
// if (JsonHelper.hasArray(jsonObject, "during_crafting_effects")) {
// JsonArray worldEffectsArray = JsonHelper.getArray(jsonObject, "during_crafting_effects");
// for (int i = 0; i < worldEffectsArray.size(); i++) {
// duringWorldEffects.add(FusionShrineRecipeWorldEffect.fromString(worldEffectsArray.get(i).getAsString()));
// }
// }
// FusionShrineRecipeWorldEffect finishWorldEffect = FusionShrineRecipeWorldEffect.fromString(JsonHelper.getString(jsonObject, "finish_crafting_effect", null));
//
// Text description;
// if (JsonHelper.hasString(jsonObject, "description")) {
// description = Text.translatable(JsonHelper.getString(jsonObject, "description"));
// } else {
// description = null;
// }
// boolean copyNbt = JsonHelper.getBoolean(jsonObject, "copy_nbt", false);
// if (copyNbt && output.isEmpty()) {
// throw new JsonParseException("Recipe does have copy_nbt set, but has no output!");
// }
//
// return this.recipeFactory.create(identifier, group, secret, requiredAdvancementIdentifier,
// craftingInputs, fluid, output, experience, craftingTime, yieldUpgradesDisabled, playCraftingFinishedEffects, copyNbt,
// worldConditions, startWorldEffect, duringWorldEffects, finishWorldEffect, description);
// }
//
//
// @Override
// public void write(PacketByteBuf packetByteBuf, FusionShrineRecipe recipe) {
// packetByteBuf.writeString(recipe.group);
// packetByteBuf.writeBoolean(recipe.secret);
// writeNullableIdentifier(packetByteBuf, recipe.requiredAdvancementIdentifier);
//
// packetByteBuf.writeShort(recipe.craftingInputs.size());
// for (IngredientStack ingredientStack : recipe.craftingInputs) {
// ingredientStack.write(packetByteBuf);
// }
//
// writeFluidIngredient(packetByteBuf, recipe.fluid);
// packetByteBuf.writeItemStack(recipe.output);
// packetByteBuf.writeFloat(recipe.experience);
// packetByteBuf.writeInt(recipe.craftingTime);
// packetByteBuf.writeBoolean(recipe.yieldUpgradesDisabled);
// packetByteBuf.writeBoolean(recipe.playCraftingFinishedEffects);
//
// if (recipe.getDescription().isEmpty()) {
// packetByteBuf.writeText(Text.literal(""));
// } else {
// packetByteBuf.writeText(recipe.getDescription().get());
// }
// packetByteBuf.writeBoolean(recipe.copyNbt);
// }
//
//
// @Override
// public FusionShrineRecipe read(Identifier identifier, PacketByteBuf packetByteBuf) {
// String group = packetByteBuf.readString();
// boolean secret = packetByteBuf.readBoolean();
// Identifier requiredAdvancementIdentifier = readNullableIdentifier(packetByteBuf);
//
// short craftingInputCount = packetByteBuf.readShort();
// List<IngredientStack> ingredients = IngredientStack.decodeByteBuf(packetByteBuf, craftingInputCount);
//
// FluidIngredient fluid = readFluidIngredient(packetByteBuf);
// ItemStack output = packetByteBuf.readItemStack();
// float experience = packetByteBuf.readFloat();
// int craftingTime = packetByteBuf.readInt();
// boolean yieldUpgradesDisabled = packetByteBuf.readBoolean();
// boolean playCraftingFinishedEffects = packetByteBuf.readBoolean();
//
// Text description = packetByteBuf.readText();
// boolean copyNbt = packetByteBuf.readBoolean();
//
// return this.recipeFactory.create(identifier, group, secret, requiredAdvancementIdentifier,
// ingredients, fluid, output, experience, craftingTime, yieldUpgradesDisabled, playCraftingFinishedEffects, copyNbt,
// List.of(), FusionShrineRecipeWorldEffect.NOTHING, List.of(), FusionShrineRecipeWorldEffect.NOTHING, description);
// }

}
Loading

0 comments on commit 52f477d

Please sign in to comment.