Skip to content

Commit

Permalink
fixing recipes
Browse files Browse the repository at this point in the history
cooking
  • Loading branch information
Faithcaio committed Nov 22, 2023
1 parent 7a6aa0b commit d5827bc
Show file tree
Hide file tree
Showing 25 changed files with 140 additions and 78 deletions.
2 changes: 1 addition & 1 deletion SpongeAPI
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.common.item.recipe.cooking;
package org.spongepowered.common.item.recipe;

import org.checkerframework.checker.nullness.qual.Nullable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
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;
Expand All @@ -62,7 +61,7 @@ public abstract class SpongeRecipeRegistration<R extends Recipe<Container>> impl
protected final DataPack<RecipeRegistration> pack;

public SpongeRecipeRegistration(final ResourceLocation key,
final Item resultItem, final String group, final DataPack<RecipeRegistration> pack, final RecipeCategory recipeCategory,
final String group, final DataPack<RecipeRegistration> pack, final RecipeCategory recipeCategory,
final RecipeSerializer<? extends R> serializer) {
this.key = key;
this.serializer = serializer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.common.item.recipe.cooking;
package org.spongepowered.common.item.recipe;

import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
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.cooking.SpongeBlastingRecipe;
import org.spongepowered.common.item.recipe.cooking.SpongeCampfireCookingRecipe;
import org.spongepowered.common.item.recipe.cooking.SpongeCookingRecipeSerializer;
import org.spongepowered.common.item.recipe.cooking.SpongeFurnaceRecipe;
import org.spongepowered.common.item.recipe.cooking.SpongeSmokingRecipe;
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.minecraft.world.item.crafting.BlastingRecipe;
import net.minecraft.world.item.crafting.CookingBookCategory;
import net.minecraft.world.item.crafting.Ingredient;
import org.spongepowered.common.item.recipe.ResultFunctionRecipe;
import org.spongepowered.common.item.recipe.ingredient.IngredientResultUtil;

public class SpongeBlastingRecipe extends BlastingRecipe implements ResultFunctionRecipe {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.minecraft.world.item.crafting.CampfireCookingRecipe;
import net.minecraft.world.item.crafting.CookingBookCategory;
import net.minecraft.world.item.crafting.Ingredient;
import org.spongepowered.common.item.recipe.ResultFunctionRecipe;
import org.spongepowered.common.item.recipe.ingredient.IngredientResultUtil;

public class SpongeCampfireCookingRecipe extends CampfireCookingRecipe implements ResultFunctionRecipe {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import net.minecraft.world.Container;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CookingBookCategory;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.RecipeSerializer;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.datapack.DataPack;
import org.spongepowered.api.datapack.DataPacks;
Expand All @@ -42,19 +42,16 @@
import org.spongepowered.api.item.recipe.cooking.CookingRecipe;
import org.spongepowered.api.util.Ticks;
import org.spongepowered.common.inventory.util.InventoryUtil;
import org.spongepowered.common.item.recipe.SpongeRecipeRegistration;
import org.spongepowered.common.item.util.ItemStackUtil;
import org.spongepowered.common.util.AbstractResourceKeyedBuilder;

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

public final class SpongeCookingRecipeBuilder extends AbstractResourceKeyedBuilder<RecipeRegistration, CookingRecipe.Builder>
implements CookingRecipe.Builder.ResultStep, CookingRecipe.Builder.IngredientStep, CookingRecipe.Builder.EndStep {

private net.minecraft.world.item.crafting.RecipeType type;
private net.minecraft.world.item.crafting.RecipeType<?> type;
private Ingredient ingredient;
private ItemStack result;
private Function<Container, net.minecraft.world.item.ItemStack> resultFunction;
Expand All @@ -65,6 +62,7 @@ public final class SpongeCookingRecipeBuilder extends AbstractResourceKeyedBuild
private DataPack<RecipeRegistration> pack = DataPacks.RECIPE;

private RecipeCategory recipeCategory = RecipeCategory.MISC; // TODO support category
private CookingBookCategory cookingCategory = CookingBookCategory.MISC; // TODO support category

@Override
public ResultStep ingredient(final org.spongepowered.api.item.recipe.crafting.Ingredient ingredient) {
Expand Down Expand Up @@ -129,7 +127,7 @@ public EndStep cookingTime(final Ticks ticks) {

@Override
public IngredientStep type(final RecipeType<CookingRecipe> type) {
this.type = (net.minecraft.world.item.crafting.RecipeType) type;
this.type = (net.minecraft.world.item.crafting.RecipeType<?>) type;
return this;
}

Expand All @@ -151,39 +149,17 @@ protected RecipeRegistration build0() {
Objects.requireNonNull(this.ingredient, "ingredient");
Objects.requireNonNull(this.result, "result");

if (this.experience == null) {
this.experience = 0f;
}

final List<Ingredient> ingredientList = Collections.singletonList(this.ingredient);

final RecipeSerializer<?> serializer;
if (this.type == net.minecraft.world.item.crafting.RecipeType.BLASTING) {
if (this.cookingTime == null) {
this.cookingTime = Ticks.of(100);
}
serializer = SpongeRecipeRegistration.determineSerializer(this.result, this.resultFunction, null, ingredientList, RecipeSerializer.BLASTING_RECIPE, SpongeRecipeSerializers.SPONGE_BLASTING);
} else if (this.type == net.minecraft.world.item.crafting.RecipeType.CAMPFIRE_COOKING) {
if (this.cookingTime == null) {
this.cookingTime = Ticks.of(600);
}
serializer = SpongeRecipeRegistration.determineSerializer(this.result, this.resultFunction, null, ingredientList, RecipeSerializer.CAMPFIRE_COOKING_RECIPE, SpongeRecipeSerializers.SPONGE_CAMPFIRE_COOKING);
} else if (this.type == net.minecraft.world.item.crafting.RecipeType.SMOKING) {
if (this.cookingTime == null) {
this.cookingTime = Ticks.of(100);
}
serializer = SpongeRecipeRegistration.determineSerializer(this.result, this.resultFunction, null, ingredientList, RecipeSerializer.SMOKING_RECIPE, SpongeRecipeSerializers.SPONGE_SMOKING);
} else if (this.type == net.minecraft.world.item.crafting.RecipeType.SMELTING) {
if (this.cookingTime == null) {
this.cookingTime = Ticks.of(200);
}
serializer = SpongeRecipeRegistration.determineSerializer(this.result, this.resultFunction, null, ingredientList, RecipeSerializer.SMELTING_RECIPE, SpongeRecipeSerializers.SPONGE_SMELTING);
} else {
throw new IllegalArgumentException("Unknown RecipeType " + this.type);
}

return new SpongeCookingRecipeRegistration((ResourceLocation) (Object) this.key, serializer, this.group,
this.ingredient, this.experience, this.cookingTime, this.result, this.resultFunction, this.pack, this.recipeCategory);
return SpongeCookingRecipeRegistration.of((ResourceLocation) (Object) this.key,
this.type,
this.group,
this.ingredient,
this.experience,
this.cookingTime,
this.result,
this.resultFunction,
this.pack,
this.recipeCategory,
this.cookingCategory);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,127 @@
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.BlastingRecipe;
import net.minecraft.world.item.crafting.CampfireCookingRecipe;
import net.minecraft.world.item.crafting.CookingBookCategory;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.item.crafting.SmeltingRecipe;
import net.minecraft.world.item.crafting.SmokingRecipe;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.datapack.DataPack;
import org.spongepowered.api.item.recipe.Recipe;
import org.spongepowered.api.item.recipe.RecipeRegistration;
import org.spongepowered.api.item.recipe.cooking.CookingRecipe;
import org.spongepowered.api.util.Ticks;
import org.spongepowered.common.item.recipe.SpongeRecipeRegistration;
import org.spongepowered.common.item.recipe.SpongeRecipeSerializers;

import java.util.Collections;
import java.util.List;
import java.util.function.Function;

public class SpongeCookingRecipeRegistration extends SpongeRecipeRegistration {
public class SpongeCookingRecipeRegistration extends SpongeRecipeRegistration implements SpongeRecipeRegistration.ResultFunctionRegistration<Container>{

// Vanilla Recipe
private final Ingredient ingredient;
private final Item result;
private final float experience;
private final Ticks cookingTime;

// Sponge Recipe
private final ItemStack spongeResult;
private final Function<Container, ItemStack> resultFunction;
private final RecipeType<?> type;
private final CookingBookCategory cookingCategory;

public SpongeCookingRecipeRegistration(final ResourceLocation key, final RecipeSerializer<?> serializer,
public SpongeCookingRecipeRegistration(final ResourceLocation key, final RecipeType<?> type, final RecipeSerializer<?> serializer,
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, spongeResult.getItem(), group, pack, category, serializer);
final DataPack<RecipeRegistration> pack, final RecipeCategory category, final CookingBookCategory cookingCategory) {
super(key, group, pack, category, serializer);
this.type = type;
this.ingredient = ingredient;
this.result = spongeResult.getItem();
this.experience = experience;
this.cookingTime = cookingTime;
this.spongeResult = spongeResult.hasTag() ? spongeResult : null;
this.spongeResult = spongeResult;
this.resultFunction = resultFunction;
this.cookingCategory = cookingCategory;
}

public static SpongeCookingRecipeRegistration of(final ResourceLocation key, final RecipeType<?> type, final @Nullable String group,
final Ingredient ingredient, final Float experience, final @Nullable Ticks cookingTime, final ItemStack result,
final Function<Container, ItemStack> resultFunction, final DataPack<RecipeRegistration> pack, final RecipeCategory recipeCategory, final CookingBookCategory cookingCategory)
{
final List<Ingredient> ingredientList = Collections.singletonList(ingredient);

var finalCookingTime = cookingTime;

final RecipeSerializer<?> serializer;
if (type == RecipeType.BLASTING) {
if (finalCookingTime == null) {
finalCookingTime = Ticks.of(100);
}
serializer = SpongeRecipeRegistration.determineSerializer(result, resultFunction, null, ingredientList, RecipeSerializer.BLASTING_RECIPE, SpongeRecipeSerializers.SPONGE_BLASTING);
} else if (type == RecipeType.CAMPFIRE_COOKING) {
if (finalCookingTime == null) {
finalCookingTime = Ticks.of(600);
}
serializer = SpongeRecipeRegistration.determineSerializer(result, resultFunction, null, ingredientList, RecipeSerializer.CAMPFIRE_COOKING_RECIPE, SpongeRecipeSerializers.SPONGE_CAMPFIRE_COOKING);
} else if (type == RecipeType.SMOKING) {
if (finalCookingTime == null) {
finalCookingTime = Ticks.of(100);
}
serializer = SpongeRecipeRegistration.determineSerializer(result, resultFunction, null, ingredientList, RecipeSerializer.SMOKING_RECIPE, SpongeRecipeSerializers.SPONGE_SMOKING);
} else if (type == RecipeType.SMELTING) {
if (finalCookingTime == null) {
finalCookingTime = Ticks.of(200);
}
serializer = SpongeRecipeRegistration.determineSerializer(result, resultFunction, null, ingredientList, RecipeSerializer.SMELTING_RECIPE, SpongeRecipeSerializers.SPONGE_SMELTING);
} else {
throw new IllegalArgumentException("Unknown RecipeType " + type);
}

return new SpongeCookingRecipeRegistration(key, type, serializer, group,
ingredient, experience == null ? 0 : experience, finalCookingTime, result, resultFunction, pack, recipeCategory, cookingCategory);
}

@Override
public Recipe recipe() {
this.ensureCached();
final int ticksCookingTime = (int) this.cookingTime.ticks();
if (type == RecipeType.BLASTING) {
if (this.serializer instanceof SpongeCookingRecipeSerializer<?>) {
return (CookingRecipe) new SpongeBlastingRecipe(this.group, this.cookingCategory, this.ingredient, this.spongeResult, this.experience, ticksCookingTime, this.key.toString());
}
return (CookingRecipe) new BlastingRecipe(this.group, this.cookingCategory, this.ingredient, this.spongeResult, this.experience, ticksCookingTime);
}
if (type == RecipeType.CAMPFIRE_COOKING) {
if (this.serializer instanceof SpongeCookingRecipeSerializer<?>) {
return (CookingRecipe) new SpongeCampfireCookingRecipe(this.group, this.cookingCategory, this.ingredient, this.spongeResult, this.experience, ticksCookingTime, this.key.toString());
}
return (CookingRecipe) new CampfireCookingRecipe(this.group, this.cookingCategory, this.ingredient, this.spongeResult, this.experience, ticksCookingTime);
}
if (type == RecipeType.SMOKING) {
if (this.serializer instanceof SpongeCookingRecipeSerializer<?>) {
return (CookingRecipe) new SpongeSmokingRecipe(this.group, this.cookingCategory, this.ingredient, this.spongeResult, this.experience, ticksCookingTime, this.key.toString());
}
return (CookingRecipe) new SmokingRecipe(this.group, this.cookingCategory, this.ingredient, this.spongeResult, this.experience, ticksCookingTime);
}
if (type == RecipeType.SMELTING) {
if (this.serializer instanceof SpongeCookingRecipeSerializer<?>) {
return (CookingRecipe) new SpongeFurnaceRecipe(this.group, this.cookingCategory, this.ingredient, this.spongeResult, this.experience, ticksCookingTime, this.key.toString());
}
return (CookingRecipe) new SmeltingRecipe(this.group, this.cookingCategory, this.ingredient, this.spongeResult, this.experience, ticksCookingTime);
}
throw new IllegalArgumentException("Unknown RecipeType " + type);


}

@Override
public Function<Container, ItemStack> resultFunction() {
return this.resultFunction;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.minecraft.world.item.crafting.CookingBookCategory;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.RecipeSerializer;
import org.spongepowered.common.item.recipe.ResultFunctionRecipe;
import org.spongepowered.common.util.Constants;

// Custom Serializer with support for:
Expand Down Expand Up @@ -79,7 +80,7 @@ public void toNetwork(final FriendlyByteBuf buffer, final R recipe) {
throw new UnsupportedOperationException("custom serializer needs client side support");
}

interface CookingRecipeFactory<T extends AbstractCookingRecipe> {
public interface CookingRecipeFactory<T extends AbstractCookingRecipe> {
default T create(String group, CookingBookCategory category, Ingredient ingredient, ItemStack result, float experience, int cookingTime,
ItemStack spongeResult, final String resultFunctionId) {
return this.create(group, category, ingredient, spongeResult.isEmpty() ? result : spongeResult, experience, cookingTime, resultFunctionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.minecraft.world.item.crafting.CookingBookCategory;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.SmeltingRecipe;
import org.spongepowered.common.item.recipe.ResultFunctionRecipe;
import org.spongepowered.common.item.recipe.ingredient.IngredientResultUtil;

public class SpongeFurnaceRecipe extends SmeltingRecipe implements ResultFunctionRecipe {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.minecraft.world.item.crafting.CookingBookCategory;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.SmokingRecipe;
import org.spongepowered.common.item.recipe.ResultFunctionRecipe;
import org.spongepowered.common.item.recipe.ingredient.IngredientResultUtil;

public class SpongeSmokingRecipe extends SmokingRecipe implements ResultFunctionRecipe {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import net.minecraft.data.recipes.RecipeCategory;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.CraftingBookCategory;
import net.minecraft.world.item.crafting.RecipeSerializer;
import org.spongepowered.api.datapack.DataPack;
Expand All @@ -35,7 +34,7 @@
import org.spongepowered.api.item.recipe.RecipeRegistration;
import org.spongepowered.api.world.server.ServerWorld;
import org.spongepowered.common.item.recipe.SpongeRecipeRegistration;
import org.spongepowered.common.item.recipe.cooking.SpongeRecipeSerializers;
import org.spongepowered.common.item.recipe.SpongeRecipeSerializers;

import java.util.HashMap;
import java.util.List;
Expand All @@ -59,7 +58,7 @@ public SpongeSpecialCraftingRecipeRegistration(ResourceLocation key,
Function<CraftingGridInventory, List<ItemStack>> remainingItemsFunction,
Function<CraftingGridInventory, ItemStack> resultFunction,
DataPack<RecipeRegistration> pack, final RecipeCategory recipeCategory) {
super(key, Items.AIR, "", pack, recipeCategory, null);
super(key, "", pack, recipeCategory, null);

this.biPredicate = biPredicate;
this.remainingItemsFunction = remainingItemsFunction;
Expand Down
Loading

0 comments on commit d5827bc

Please sign in to comment.