Skip to content

Commit

Permalink
Add public getters for shaped recipe max width and height (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
TelepathicGrunt authored Jan 1, 2024
1 parent bd3a222 commit 5daf727
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
--- a/net/minecraft/world/item/crafting/ShapedRecipePattern.java
+++ b/net/minecraft/world/item/crafting/ShapedRecipePattern.java
@@ -19,7 +_,21 @@
@@ -19,7 +_,30 @@
import net.minecraft.world.inventory.CraftingContainer;

public record ShapedRecipePattern(int width, int height, NonNullList<Ingredient> ingredients, Optional<ShapedRecipePattern.Data> data) {
+ /** @deprecated Neo: use {@link #MAX_WIDTH} and {@link #MAX_HEIGHT} */ @Deprecated
+ /** @deprecated Neo: use {@link #getMaxWidth} and {@link #getMaxHeight} */ @Deprecated
private static final int MAX_SIZE = 3;
+ static int MAX_WIDTH = 3;
+ static int MAX_HEIGHT = 3;
+ static int maxWidth = 3;
+ static int maxHeight = 3;
+
+ public static int getMaxWidth() {
+ return maxWidth;
+ }
+
+ public static int getMaxHeight() {
+ return maxHeight;
+ }
+
+ /**
+ * Expand the max width and height allowed in the deserializer.
+ * This should be called by modders who add custom crafting tables that are larger than the vanilla 3x3.
+ * @param width your max recipe width
+ * @param height your max recipe height
+ */
+ public static void setCraftingSize(int width, int height) {
+ if (MAX_WIDTH < width) MAX_WIDTH = width;
+ if (MAX_HEIGHT < height) MAX_HEIGHT = height;
+ if (maxWidth < width) maxWidth = width;
+ if (maxHeight < height) maxHeight = height;
+ }
+
public static final MapCodec<ShapedRecipePattern> MAP_CODEC = ShapedRecipePattern.Data.MAP_CODEC
Expand All @@ -28,8 +37,8 @@
private static final Codec<List<String>> PATTERN_CODEC = Codec.STRING.listOf().comapFlatMap(p_312085_ -> {
- if (p_312085_.size() > 3) {
- return DataResult.error(() -> "Invalid pattern: too many rows, 3 is maximum");
+ if (p_312085_.size() > MAX_HEIGHT) {
+ return DataResult.error(() -> "Invalid pattern: too many rows, %s is maximum".formatted(MAX_HEIGHT));
+ if (p_312085_.size() > maxHeight) {
+ return DataResult.error(() -> "Invalid pattern: too many rows, %s is maximum".formatted(maxHeight));
} else if (p_312085_.isEmpty()) {
return DataResult.error(() -> "Invalid pattern: empty pattern not allowed");
} else {
Expand All @@ -38,8 +47,8 @@
for(String s : p_312085_) {
- if (s.length() > 3) {
- return DataResult.error(() -> "Invalid pattern: too many columns, 3 is maximum");
+ if (s.length() > MAX_WIDTH) {
+ return DataResult.error(() -> "Invalid pattern: too many columns, %s is maximum".formatted(MAX_WIDTH));
+ if (s.length() > maxWidth) {
+ return DataResult.error(() -> "Invalid pattern: too many columns, %s is maximum".formatted(maxWidth));
}

if (i != s.length()) {
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
} else {
- return aingredient.length > 9
- ? DataResult.error(() -> "Too many ingredients for shapeless recipe")
+ return aingredient.length > ShapedRecipePattern.MAX_HEIGHT * ShapedRecipePattern.MAX_WIDTH
+ ? DataResult.error(() -> "Too many ingredients for shapeless recipe. The maximum is: %s".formatted(ShapedRecipePattern.MAX_HEIGHT * ShapedRecipePattern.MAX_WIDTH))
+ return aingredient.length > ShapedRecipePattern.maxHeight * ShapedRecipePattern.maxWidth
+ ? DataResult.error(() -> "Too many ingredients for shapeless recipe. The maximum is: %s".formatted(ShapedRecipePattern.maxHeight * ShapedRecipePattern.maxWidth))
: DataResult.success(NonNullList.of(Ingredient.EMPTY, aingredient));
}
},

0 comments on commit 5daf727

Please sign in to comment.