Skip to content

Commit

Permalink
Wrap shaped recipes in a way that is lazier and doesn't require ATs
Browse files Browse the repository at this point in the history
  • Loading branch information
pupnewfster committed Jul 12, 2024
1 parent 6820c02 commit b163ad5
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingBookCategory;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.ShapedRecipe;
Expand Down Expand Up @@ -102,16 +101,16 @@ protected void validate(ResourceLocation id) {

@Override
protected Recipe<?> asRecipe() {
return wrapRecipe(
return wrapRecipe(new ShapedRecipe(
Objects.requireNonNullElse(this.group, ""),
RecipeBuilder.determineBookCategory(this.category),
ShapedRecipePattern.of(this.key, this.pattern),
new ItemStack(this.result, this.count),
this.showNotification
);
));
}

protected Recipe<?> wrapRecipe(String group, CraftingBookCategory category, ShapedRecipePattern pattern, ItemStack result, boolean showNotification) {
return new ShapedRecipe(group, category, pattern, result, showNotification);
protected Recipe<?> wrapRecipe(ShapedRecipe recipe) {
return recipe;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import mekanism.api.annotations.NothingNullByDefault;
import mekanism.common.recipe.upgrade.MekanismShapedRecipe;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingBookCategory;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.ShapedRecipePattern;
import net.minecraft.world.item.crafting.ShapedRecipe;
import net.minecraft.world.level.ItemLike;

@NothingNullByDefault
Expand All @@ -24,7 +22,7 @@ public static MekDataShapedRecipeBuilder shapedRecipe(ItemLike result, int count
}

@Override
protected Recipe<?> wrapRecipe(String group, CraftingBookCategory category, ShapedRecipePattern pattern, ItemStack result, boolean showNotification) {
return new MekanismShapedRecipe(group, category, pattern, result, showNotification);
protected Recipe<?> wrapRecipe(ShapedRecipe recipe) {
return new MekanismShapedRecipe(recipe);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import mekanism.api.annotations.NothingNullByDefault;
import mekanism.common.recipe.builder.ExtendedShapedRecipeBuilder;
import net.minecraft.data.recipes.RecipeCategory;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingBookCategory;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.ShapedRecipePattern;
import net.minecraft.world.item.crafting.ShapedRecipe;
import net.minecraft.world.level.ItemLike;

@NothingNullByDefault
Expand All @@ -26,7 +24,7 @@ public static PaxelShapedRecipeBuilder shapedRecipe(ItemLike result, int count)
}

@Override
protected Recipe<?> wrapRecipe(String group, CraftingBookCategory category, ShapedRecipePattern pattern, ItemStack result, boolean showNotification) {
return new PaxelRecipe(group, category, pattern, result, showNotification);
protected Recipe<?> wrapRecipe(ShapedRecipe recipe) {
return new PaxelRecipe(recipe);
}
}
64 changes: 59 additions & 5 deletions src/main/java/mekanism/common/recipe/WrappedShapedRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@

import mekanism.api.annotations.NothingNullByDefault;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingBookCategory;
import net.minecraft.world.item.crafting.CraftingInput;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.ShapedRecipe;
import net.minecraft.world.item.crafting.ShapedRecipePattern;
import net.minecraft.world.level.Level;

@NothingNullByDefault
public abstract class WrappedShapedRecipe extends ShapedRecipe {

protected WrappedShapedRecipe(String group, CraftingBookCategory category, ShapedRecipePattern pattern, ItemStack result, boolean showNotification) {
super(group, category, pattern, result, showNotification);
private final ShapedRecipe internal;

protected WrappedShapedRecipe(ShapedRecipe internal) {
//Note: We override all uses and calls to pattern and result, so we can just pass null and empty to them
// Because pattern is AT'd to public however, we make use of it to pass it to super, in case another mod is querying the value
super(internal.getGroup(), internal.category(), internal.pattern, ItemStack.EMPTY, internal.showNotification());
this.internal = internal;
}

public ShapedRecipe getInternal() {
return internal;
}

@Override
Expand All @@ -23,6 +32,51 @@ protected WrappedShapedRecipe(String group, CraftingBookCategory category, Shape
public boolean matches(CraftingInput input, Level world) {
//Note: We do not override the matches method if it matches ignoring NBT,
// to ensure that we return the proper value for if there is a match that gives a proper output
return super.matches(input, world) && !assemble(input, world.registryAccess()).isEmpty();
return internal.matches(input, world) && !assemble(input, world.registryAccess()).isEmpty();
}

@Override
public boolean canCraftInDimensions(int width, int height) {
return internal.canCraftInDimensions(width, height);
}

@Override
public ItemStack getResultItem(HolderLookup.Provider provider) {
return internal.getResultItem(provider);
}

@Override
public NonNullList<ItemStack> getRemainingItems(CraftingInput input) {
return internal.getRemainingItems(input);
}

@Override
public NonNullList<Ingredient> getIngredients() {
return internal.getIngredients();
}

@Override
public boolean isSpecial() {
return internal.isSpecial();
}

@Override
public ItemStack getToastSymbol() {
return internal.getToastSymbol();
}

@Override
public int getWidth() {
return internal.getWidth();
}

@Override
public int getHeight() {
return internal.getHeight();
}

@Override
public boolean isIncomplete() {
return internal.isIncomplete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.mojang.datafixers.util.Function3;
import com.mojang.datafixers.util.Function4;
import com.mojang.datafixers.util.Function5;
import com.mojang.datafixers.util.Function7;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
Expand Down Expand Up @@ -54,11 +53,9 @@
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingBookCategory;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.ShapedRecipe;
import net.minecraft.world.item.crafting.ShapedRecipePattern;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs;

Expand All @@ -67,10 +64,10 @@ public record MekanismRecipeSerializer<RECIPE extends Recipe<?>>(MapCodec<RECIPE

private static final Codec<FloatingLong> FLOAT_LONG_AT_LEAST_ONE = FloatingLong.CODEC.validate(fl -> fl.smallerThan(FloatingLong.ONE) ? DataResult.error(() -> "Expected energyMultiplier to be at least one.") : DataResult.success(fl));

public static <RECIPE extends WrappedShapedRecipe> MekanismRecipeSerializer<RECIPE> wrapped(Function5<String, CraftingBookCategory, ShapedRecipePattern, ItemStack, Boolean, RECIPE> wrapper) {
public static <RECIPE extends WrappedShapedRecipe> MekanismRecipeSerializer<RECIPE> wrapped(Function<ShapedRecipe, RECIPE> wrapper) {
return new MekanismRecipeSerializer<>(
RecipeSerializer.SHAPED_RECIPE.codec().xmap(shaped -> wrapper.apply(shaped.getGroup(), shaped.category(), shaped.pattern, shaped.result, shaped.showNotification()), Function.identity()),
RecipeSerializer.SHAPED_RECIPE.streamCodec().map(shaped -> wrapper.apply(shaped.getGroup(), shaped.category(), shaped.pattern, shaped.result, shaped.showNotification()), Function.identity())
RecipeSerializer.SHAPED_RECIPE.codec().xmap(wrapper, WrappedShapedRecipe::getInternal),
RecipeSerializer.SHAPED_RECIPE.streamCodec().map(wrapper, WrappedShapedRecipe::getInternal)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@
import mekanism.common.registries.MekanismRecipeSerializersInternal;
import net.minecraft.core.HolderLookup;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingBookCategory;
import net.minecraft.world.item.crafting.CraftingInput;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.ShapedRecipe;
import net.minecraft.world.item.crafting.ShapedRecipePattern;

@NothingNullByDefault
public class MekanismShapedRecipe extends WrappedShapedRecipe {

public MekanismShapedRecipe(String group, CraftingBookCategory category, ShapedRecipePattern pattern, ItemStack result, boolean showNotification) {
super(group, category, pattern, result, showNotification);
public MekanismShapedRecipe(ShapedRecipe internal) {
super(internal);
}

@Override
Expand Down
4 changes: 1 addition & 3 deletions src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,4 @@ public net.minecraft.world.level.levelgen.placement.PlacedFeature placeWithConte
public net.minecraft.world.level.material.MapColor MATERIAL_COLORS
public net.minecraft.world.level.storage.loot.functions.CopyNbtFunction$Path

public net.minecraft.world.phys.AABB getDirection(Lnet/minecraft/world/phys/AABB;Lnet/minecraft/world/phys/Vec3;[DLnet/minecraft/core/Direction;DDD)Lnet/minecraft/core/Direction;

public net.minecraft.world.item.crafting.ShapedRecipe result
public net.minecraft.world.phys.AABB getDirection(Lnet/minecraft/world/phys/AABB;Lnet/minecraft/world/phys/Vec3;[DLnet/minecraft/core/Direction;DDD)Lnet/minecraft/core/Direction;
6 changes: 2 additions & 4 deletions src/tools/java/mekanism/tools/common/recipe/PaxelRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
import mekanism.tools.common.registries.ToolsRecipeSerializers;
import net.minecraft.core.HolderLookup;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingBookCategory;
import net.minecraft.world.item.crafting.CraftingInput;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.ShapedRecipe;
import net.minecraft.world.item.crafting.ShapedRecipePattern;

@NothingNullByDefault
public class PaxelRecipe extends WrappedShapedRecipe {

public PaxelRecipe(String group, CraftingBookCategory category, ShapedRecipePattern pattern, ItemStack result, boolean showNotification) {
super(group, category, pattern, result, showNotification);
public PaxelRecipe(ShapedRecipe internal) {
super(internal);
}

@Override
Expand Down

0 comments on commit b163ad5

Please sign in to comment.