Skip to content

Commit

Permalink
start fixing recipes
Browse files Browse the repository at this point in the history
stonecutting and smithing done
  • Loading branch information
Faithcaio committed Nov 22, 2023
1 parent f374fc0 commit 7a6aa0b
Show file tree
Hide file tree
Showing 16 changed files with 130 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,45 @@
package org.spongepowered.common.item.recipe;

import com.google.gson.JsonObject;
import com.mojang.serialization.JsonOps;
import net.minecraft.advancements.Advancement;
import net.minecraft.advancements.AdvancementHolder;
import net.minecraft.advancements.AdvancementRewards;
import net.minecraft.advancements.critereon.RecipeUnlockedTrigger;
import net.minecraft.core.NonNullList;
import net.minecraft.core.RegistryAccess;
import net.minecraft.data.recipes.FinishedRecipe;
import net.minecraft.data.recipes.RecipeCategory;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.Container;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeSerializer;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.ResourceKey;
import org.spongepowered.api.data.persistence.DataContainer;
import org.spongepowered.api.data.persistence.DataFormats;
import org.spongepowered.api.datapack.DataPack;
import org.spongepowered.api.item.recipe.RecipeRegistration;
import org.spongepowered.common.item.recipe.ingredient.IngredientResultUtil;
import org.spongepowered.common.item.recipe.ingredient.SpongeIngredient;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.function.Function;

public abstract class SpongeRecipeRegistration implements RecipeRegistration, FinishedRecipe {
public abstract class SpongeRecipeRegistration<R extends Recipe<Container>> implements RecipeRegistration {

protected final ResourceLocation key;
protected final RecipeSerializer<?> serializer;
protected final RecipeSerializer<? extends R> serializer;
protected final AdvancementHolder advancement;
protected final String group;
protected final DataPack<RecipeRegistration> pack;

public SpongeRecipeRegistration(final ResourceLocation key, final RecipeSerializer<?> serializer, final Item resultItem, final String group, final DataPack<RecipeRegistration> pack, final RecipeCategory recipeCategory) {
public SpongeRecipeRegistration(final ResourceLocation key,
final Item resultItem, final String group, final DataPack<RecipeRegistration> pack, final RecipeCategory recipeCategory,
final RecipeSerializer<? extends R> serializer) {
this.key = key;
this.serializer = serializer;
this.pack = pack;
Expand All @@ -71,17 +74,17 @@ public SpongeRecipeRegistration(final ResourceLocation key, final RecipeSerializ
this.group = group == null ? "" : group;
}

public static <C extends Container> RecipeSerializer<?> determineSerializer(final ItemStack resultStack,
final Function<C, ItemStack> resultFunction,
final Function<net.minecraft.world.inventory.CraftingContainer, NonNullList<ItemStack>> remainingItemsFunction,
final Map<Character, Ingredient> ingredients, final RecipeSerializer<?> vanilla, final RecipeSerializer<?> sponge) {
public static <R extends Recipe<C>, C extends Container> RecipeSerializer<? extends R> determineSerializer(final ItemStack resultStack,
final Function<C, ItemStack> resultFunction,
final Function<net.minecraft.world.inventory.CraftingContainer, NonNullList<ItemStack>> remainingItemsFunction,
final Map<Character, Ingredient> ingredients, final RecipeSerializer<R> vanilla, final RecipeSerializer<? extends R> sponge) {
return SpongeRecipeRegistration.determineSerializer(resultStack, resultFunction, remainingItemsFunction, ingredients.values(), vanilla, sponge);
}

public static <C extends Container> RecipeSerializer<?> determineSerializer(final ItemStack resultStack,
final Function<C, ItemStack> resultFunction,
final Function<net.minecraft.world.inventory.CraftingContainer, NonNullList<ItemStack>> remainingItemsFunction,
final Collection<Ingredient> ingredients, final RecipeSerializer<?> vanilla, final RecipeSerializer<?> sponge) {
public static <R extends Recipe<C>, C extends Container> RecipeSerializer<? extends R> determineSerializer(final ItemStack resultStack,
final Function<C, ItemStack> resultFunction,
final Function<net.minecraft.world.inventory.CraftingContainer, NonNullList<ItemStack>> remainingItemsFunction,
final Collection<Ingredient> ingredients, final RecipeSerializer<R> vanilla, final RecipeSerializer<? extends R> sponge) {
if (resultStack.hasTag() || resultFunction != null || remainingItemsFunction != null) {
return sponge;
}
Expand All @@ -93,50 +96,36 @@ public static <C extends Container> RecipeSerializer<?> determineSerializer(fina
return vanilla;
}

@Override
public ResourceLocation id() {
return this.key;
public static <C extends Container> boolean isVanillaSerializer(final ItemStack resultStack,
final Function<C, ItemStack> resultFunction,
final Function<net.minecraft.world.inventory.CraftingContainer, NonNullList<ItemStack>> remainingItemsFunction,
final Collection<Ingredient> ingredients) {
if (resultStack.hasTag() || resultFunction != null || remainingItemsFunction != null) {
return false;
}
for (final Ingredient value : ingredients) {
if (value instanceof SpongeIngredient) {
return false;
}
}
return true;
}

@Override
public ResourceKey key() {
return (ResourceKey) (Object) this.key;
}

@Override
public RecipeSerializer<?> type() {
return this.serializer;
}

@Override
public void serializeRecipeData(final JsonObject json) {
if (!this.group.isEmpty()) {
json.addProperty("group", this.group);
}
this.serializeShape(json);
this.serializeResult(json);
this.serializeAdditional(json);
}

public abstract void serializeShape(JsonObject json);
public abstract void serializeResult(JsonObject json);
public void serializeAdditional(final JsonObject json) {
}

@Nullable @Override
public AdvancementHolder advancement() {
return this.advancement;
}

@Override
public int contentVersion() {
return 1;
}

@Override
public DataContainer toContainer() {
var json = Recipe.CODEC.encodeStart(JsonOps.INSTANCE, (Recipe<?>) this.recipe()).result().get().getAsJsonObject();
try {
return DataFormats.JSON.get().read(this.serializeRecipe().toString());
return DataFormats.JSON.get().read(json); // TODO serialize or get DataContainer without serializing
} catch (final IOException e) {
throw new IllegalStateException(e);
}
Expand All @@ -147,11 +136,34 @@ public DataPack<RecipeRegistration> pack() {
return this.pack;
}

@SuppressWarnings("unchecked")
protected void ensureCached() {
if (this instanceof SpongeRecipeRegistration.ResultFunctionRegistration<?> rfr) {
IngredientResultUtil.cacheResultFunction(this.key, (Function) rfr.resultFunction());
}
if (this instanceof SpongeRecipeRegistration.RemainingItemsFunctionRegistration<?> rifr) {
IngredientResultUtil.cacheRemainingItemsFunction(this.key, (Function) rifr.remainingItems());
}
}

public static JsonObject encode(RecipeRegistration template, RegistryAccess access) {
try {
return ((FinishedRecipe) template).serializeRecipe();
if (template instanceof SpongeRecipeRegistration<?> srr) {
srr.ensureCached();
}
return Recipe.CODEC.encodeStart(JsonOps.INSTANCE, (Recipe<?>) template.recipe()).result().get().getAsJsonObject();
} catch (Exception e) {
throw new RuntimeException("Could not encode recipe" + template.key(), e);
}
}

public interface ResultFunctionRegistration<C> {

Function<C, net.minecraft.world.item.ItemStack> resultFunction();
}

public interface RemainingItemsFunctionRegistration<C> {

Function<C, NonNullList<net.minecraft.world.item.ItemStack>> remainingItems();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
*/
package org.spongepowered.common.item.recipe.cooking;

import com.google.gson.JsonObject;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.data.recipes.RecipeCategory;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.Container;
Expand All @@ -38,8 +35,6 @@
import org.spongepowered.api.item.recipe.RecipeRegistration;
import org.spongepowered.api.util.Ticks;
import org.spongepowered.common.item.recipe.SpongeRecipeRegistration;
import org.spongepowered.common.item.recipe.ingredient.IngredientResultUtil;
import org.spongepowered.common.util.Constants;

import java.util.function.Function;

Expand All @@ -59,7 +54,7 @@ public SpongeCookingRecipeRegistration(final ResourceLocation key, final RecipeS
final String group, final Ingredient ingredient, final float experience, final Ticks cookingTime,
final ItemStack spongeResult, final Function<Container, ItemStack> resultFunction,
final DataPack<RecipeRegistration> pack, final RecipeCategory category) {
super(key, serializer, spongeResult.getItem(), group, pack, category);
super(key, spongeResult.getItem(), group, pack, category, serializer);
this.ingredient = ingredient;
this.result = spongeResult.getItem();
this.experience = experience;
Expand All @@ -68,28 +63,4 @@ public SpongeCookingRecipeRegistration(final ResourceLocation key, final RecipeS
this.resultFunction = resultFunction;
}

@Override
public void serializeShape(final JsonObject json) {
json.add(Constants.Recipe.COOKING_INGREDIENT, this.ingredient.toJson(false));
}

@Override
public void serializeResult(final JsonObject json) {
final Registry<Item> itemRegistry = BuiltInRegistries.ITEM;
json.addProperty(Constants.Recipe.RESULT, itemRegistry.getKey(this.result).toString());
// Sponge Recipe
if (this.spongeResult != null) {
this.spongeResult.setCount(1);
json.add(Constants.Recipe.SPONGE_RESULT, IngredientResultUtil.serializeItemStack(this.spongeResult));
}
if (this.resultFunction != null) {
json.addProperty(Constants.Recipe.SPONGE_RESULTFUNCTION, IngredientResultUtil.cacheResultFunction(this.id(), this.resultFunction));
}
}

@Override
public void serializeAdditional(final JsonObject json) {
json.addProperty(Constants.Recipe.COOKING_EXP, this.experience);
json.addProperty(Constants.Recipe.COOKING_TIME, this.cookingTime.ticks());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public SpongeCookingRecipeSerializer(final CookingRecipeFactory<R> factory, fina
$$2 -> $$2.group(
ExtraCodecs.strictOptionalField(Codec.STRING, "group", "").forGetter(AbstractCookingRecipe::getGroup),
CookingBookCategory.CODEC.fieldOf("category").orElse(CookingBookCategory.MISC).forGetter(AbstractCookingRecipe::category),
Ingredient.CODEC_NONEMPTY.fieldOf("ingredient").forGetter($$0x -> $$0x.getIngredients().get(0)),
BuiltInRegistries.ITEM.byNameCodec().xmap(ItemStack::new, ItemStack::getItem).fieldOf("result")
Ingredient.CODEC_NONEMPTY.fieldOf(Constants.Recipe.COOKING_INGREDIENT).forGetter($$0x -> $$0x.getIngredients().get(0)),
BuiltInRegistries.ITEM.byNameCodec().xmap(ItemStack::new, ItemStack::getItem).fieldOf(Constants.Recipe.RESULT)
.forGetter($$0x -> $$0x.getResultItem(null)),
Codec.FLOAT.fieldOf("experience").orElse(0.0F).forGetter(AbstractCookingRecipe::getExperience),
Codec.INT.fieldOf("cookingtime").orElse(defaultCookingTime).forGetter(AbstractCookingRecipe::getCookingTime),
Codec.FLOAT.fieldOf(Constants.Recipe.COOKING_EXP).orElse(0.0F).forGetter(AbstractCookingRecipe::getExperience),
Codec.INT.fieldOf(Constants.Recipe.COOKING_TIME).orElse(defaultCookingTime).forGetter(AbstractCookingRecipe::getCookingTime),
ItemStack.CODEC.optionalFieldOf(Constants.Recipe.SPONGE_RESULT, ItemStack.EMPTY).forGetter(raw -> raw.getResultItem(null).hasTag() ? raw.getResultItem(null) : ItemStack.EMPTY),
Codec.STRING.optionalFieldOf(Constants.Recipe.SPONGE_RESULTFUNCTION, null).forGetter(ResultFunctionRecipe::resultFunctionId)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.StonecutterRecipe;
import org.spongepowered.common.item.recipe.crafting.custom.SpongeSpecialCraftingRecipeRegistration;
import org.spongepowered.common.item.recipe.crafting.custom.SpongeSpecialCraftingRecipeSerializer;
import org.spongepowered.common.item.recipe.crafting.custom.SpongeSpecialRecipe;
import org.spongepowered.common.item.recipe.crafting.shaped.SpongeShapedCraftingRecipeSerializer;
import org.spongepowered.common.item.recipe.crafting.shaped.SpongeShapedRecipe;
import org.spongepowered.common.item.recipe.crafting.shapeless.SpongeShapelessCraftingRecipeSerializer;
import org.spongepowered.common.item.recipe.crafting.shapeless.SpongeShapelessRecipe;
import org.spongepowered.common.item.recipe.smithing.SpongeSmithingRecipe;
import org.spongepowered.common.item.recipe.smithing.SpongeSmithingRecipeSerializer;
import org.spongepowered.common.item.recipe.stonecutting.SpongeStonecuttingRecipe;
Expand All @@ -45,9 +48,9 @@ public interface SpongeRecipeSerializers {
SpongeCookingRecipeSerializer<SpongeCampfireCookingRecipe> SPONGE_CAMPFIRE_COOKING = register("campfire_cooking", new SpongeCookingRecipeSerializer<>(SpongeCampfireCookingRecipe::new, 100));
SpongeCookingRecipeSerializer<SpongeFurnaceRecipe> SPONGE_SMELTING = register("smelting", new SpongeCookingRecipeSerializer<>(SpongeFurnaceRecipe::new, 200));
RecipeSerializer<?> SPONGE_CRAFTING_SHAPED = register("crafting_shaped", new SpongeShapedCraftingRecipeSerializer());
RecipeSerializer<?> SPONGE_CRAFTING_SHAPELESS = register("crafting_shapeless", new SpongeShapelessCraftingRecipeSerializer());
RecipeSerializer<?> SPONGE_SMITHING = register("smithing", new SpongeSmithingRecipeSerializer<>(SpongeSmithingRecipe::new));
RecipeSerializer<?> SPONGE_STONECUTTING = register("stonecutting", new SpongeStonecuttingRecipeSerializer<>(SpongeStonecuttingRecipe::new));
RecipeSerializer<SpongeShapelessRecipe> SPONGE_CRAFTING_SHAPELESS = register("crafting_shapeless", new SpongeShapelessCraftingRecipeSerializer());
RecipeSerializer<SpongeSmithingRecipe> SPONGE_SMITHING = register("smithing", new SpongeSmithingRecipeSerializer<>(SpongeSmithingRecipe::new));
RecipeSerializer<SpongeStonecuttingRecipe> SPONGE_STONECUTTING = register("stonecutting", new SpongeStonecuttingRecipeSerializer<>(SpongeStonecuttingRecipe::new));
SpongeCookingRecipeSerializer<SpongeSmokingRecipe> SPONGE_SMOKING = register("smoking", new SpongeCookingRecipeSerializer<>(SpongeSmokingRecipe::new, 100));

SpongeSpecialCraftingRecipeSerializer<SpongeSpecialRecipe> SPONGE_SPECIAL = register("special", new SpongeSpecialCraftingRecipeSerializer<>(SpongeSpecialCraftingRecipeRegistration::get));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*/
package org.spongepowered.common.item.recipe.crafting.custom;

import com.google.gson.JsonObject;
import net.minecraft.data.recipes.RecipeCategory;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Items;
Expand Down Expand Up @@ -60,7 +59,7 @@ public SpongeSpecialCraftingRecipeRegistration(ResourceLocation key,
Function<CraftingGridInventory, List<ItemStack>> remainingItemsFunction,
Function<CraftingGridInventory, ItemStack> resultFunction,
DataPack<RecipeRegistration> pack, final RecipeCategory recipeCategory) {
super(key, null, Items.AIR, "", pack, recipeCategory);
super(key, Items.AIR, "", pack, recipeCategory, null);

this.biPredicate = biPredicate;
this.remainingItemsFunction = remainingItemsFunction;
Expand All @@ -79,11 +78,4 @@ public RecipeSerializer<?> type() {
return SpongeRecipeSerializers.SPONGE_SPECIAL;
}

@Override
public void serializeShape(final JsonObject json) {
}

@Override
public void serializeResult(final JsonObject json) {
}
}
Loading

0 comments on commit 7a6aa0b

Please sign in to comment.