Skip to content

Commit cb11d6d

Browse files
committed
Support exact items in shapeless recipes on Paper
1 parent 99e2f95 commit cb11d6d

File tree

4 files changed

+71
-16
lines changed

4 files changed

+71
-16
lines changed

src/main/java/com/laytonsmith/abstraction/MCShapelessRecipe.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ public interface MCShapelessRecipe extends MCRecipe {
88
void addIngredient(MCItemStack ingredient);
99
void addIngredient(MCMaterial ingredient);
1010
void addIngredient(MCMaterial... ingredients);
11+
void addIngredient(MCItemStack... ingredients);
1112
List<MCMaterial[]> getIngredients();
1213
}

src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCShapelessRecipe.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,13 @@ public void addIngredient(MCMaterial... ingredients) {
8585
}
8686
recipe.addIngredient(new RecipeChoice.MaterialChoice(concrete));
8787
}
88+
89+
@Override
90+
public void addIngredient(MCItemStack... ingredients) {
91+
ItemStack[] concrete = new ItemStack[ingredients.length];
92+
for(int i = 0; i < ingredients.length; i++) {
93+
concrete[i] = (ItemStack) ingredients[i].getHandle();
94+
}
95+
recipe.addIngredient(new RecipeChoice.ExactChoice(concrete));
96+
}
8897
}

src/main/java/com/laytonsmith/core/ObjectGenerator.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,11 +2363,44 @@ public MCRecipe recipe(Mixed c, Target t) {
23632363
for(Mixed ingredient : ingredients.asList()) {
23642364
if(ingredient.isInstanceOf(CArray.TYPE)) {
23652365
if(((CArray) ingredient).isAssociative()) {
2366-
((MCShapelessRecipe) ret).addIngredient(recipeItem(ingredient, t));
2366+
// Single ingredient item array
2367+
if(((CArray) ingredient).containsKey("meta")) {
2368+
// Single exact ingredient item choice
2369+
((MCShapelessRecipe) ret).addIngredient(new MCItemStack[]{recipeItem(ingredient, t)});
2370+
} else {
2371+
// Single ingredient material with optional qty
2372+
((MCShapelessRecipe) ret).addIngredient(recipeItem(ingredient, t));
2373+
}
23672374
} else {
2368-
((MCShapelessRecipe) ret).addIngredient(recipeMaterialChoice((CArray) ingredient, t));
2375+
// Multiple ingredient choices
2376+
CArray list = (CArray) ingredient;
2377+
MCMaterial[] mats = new MCMaterial[(int) list.size()];
2378+
MCItemStack[] items = new MCItemStack[(int) list.size()];
2379+
boolean exactItemMatch = false;
2380+
for(int index = 0; index < list.size(); index++) {
2381+
Mixed choice = list.get(index, t);
2382+
if(choice.isInstanceOf(CArray.TYPE)) {
2383+
exactItemMatch = true;
2384+
items[index] = recipeItem(choice, t);
2385+
} else {
2386+
mats[index] = recipeMaterial(choice, t);
2387+
}
2388+
}
2389+
if(exactItemMatch) {
2390+
// Multiple exact item ingredient choices
2391+
for(int index = 0; index < items.length; index++) {
2392+
if(items[index] == null) {
2393+
items[index] = StaticLayer.GetItemStack(mats[index], 1);
2394+
}
2395+
}
2396+
((MCShapelessRecipe) ret).addIngredient(items);
2397+
} else {
2398+
// Multiple material ingredient choices
2399+
((MCShapelessRecipe) ret).addIngredient(mats);
2400+
}
23692401
}
23702402
} else {
2403+
// single ingredient material
23712404
((MCShapelessRecipe) ret).addIngredient(recipeMaterial(ingredient, t));
23722405
}
23732406
}

src/main/java/com/laytonsmith/core/functions/Recipes.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,17 @@ public Integer[] numArgs() {
7979
public String docs() {
8080
return "boolean {recipeArray} Adds a recipe to the server and returns whether it was added or not. ----"
8181
+ " The recipeArray can contain the following keys:"
82-
+ " <pre>"
83-
+ " type: (required) The type of recipe. Expected to be 'BLASTING', 'CAMPFIRE', 'FURNACE', 'SHAPED',"
84-
+ " 'SHAPELESS', 'SMOKING', or 'STONECUTTING'.\n"
85-
+ " key: (required) A unique string id for this recipe.\n"
86-
+ " result: (required) The result item array of the recipe.\n"
87-
+ " group: (optional) A group id for grouping recipes in the recipe book.\n"
88-
+ " shape: (shaped) The shape of the recipe. Represented as a 3 index normal array.\n"
89-
+ " ingredients: (shaped and shapeless recipes) Ingredients array of the recipe. See examples.\n"
90-
+ " input: (cooking and stonecutting) The input item type or array of types for this recipe.\n"
91-
+ " cookingtime: (cooking) The amount of time in ticks for the input item to cook.\n"
92-
+ " experience: (cooking) The amount of experience generated by this recipe."
93-
+ " </pre>"
82+
+ "'''type''' : (required) The type of recipe. Expected to be 'BLASTING', 'CAMPFIRE', 'FURNACE',"
83+
+ " 'SHAPED', 'SHAPELESS', 'SMOKING', or 'STONECUTTING'.\n"
84+
+ "'''key''' : (required) A unique string id for this recipe.\n"
85+
+ "'''result''' : (required) The result item array of the recipe.\n"
86+
+ "'''group''' : (optional) A group id for grouping recipes in the recipe book.\n"
87+
+ "'''shape''' : (shaped) The shape of the recipe. Represented as a 3 index normal array.\n"
88+
+ "'''ingredients''' : (shaped and shapeless recipes) Ingredients array of the recipe. See examples.\n"
89+
+ "'''input''' : (cooking and stonecutting) The input item type or array of types for this recipe.\n"
90+
+ "'''cookingtime''' : (cooking) The amount of time in ticks for the input item to cook.\n"
91+
+ "'''experience''' : (cooking) The amount of experience generated by this recipe.\n"
92+
+ "\n"
9493
+ " Shaped Recipe Data. Turns 9 stone into obsidian."
9594
+ " <pre>{\n"
9695
+ " key: 'stone_to_obsidian',\n"
@@ -158,14 +157,14 @@ public ExampleScript[] examples() throws ConfigCompileException {
158157
"add_recipe(array(\n"
159158
+ "\t'key': 'regen_extended',\n"
160159
+ "\t'type': 'SHAPED',\n"
161-
+ "\t'result': array('name': 'POTION', 'meta': array('base': array('type': 'REGEN', 'extended': true))),\n"
160+
+ "\t'result': array('name': 'POTION', 'meta': array('potiontype': 'LONG_REGENERATION')),\n"
162161
+ "\t'shape': array(\n"
163162
+ "\t\t'RRR',\n"
164163
+ "\t\t'RPR',\n"
165164
+ "\t\t'RRR'),\n"
166165
+ "\t'ingredients': array(\n"
167166
+ "\t\t'R': 'REDSTONE',\n"
168-
+ "\t\t'P': array('name': 'POTION', 'meta': array('base': array('type': 'REGEN'))))));",
167+
+ "\t\t'P': array('name': 'POTION', 'meta': array('potiontype': 'REGENERATION')))));",
169168
"Turns a normal regen potion surrounded by redstone into an extended regen potion."),
170169

171170
new ExampleScript("Demonstrates a shaped recipe with multiple ingredient choices."
@@ -178,6 +177,19 @@ public ExampleScript[] examples() throws ConfigCompileException {
178177
+ "\t'shape': array('X', 'X', 'X'),\n"
179178
+ "\t'ingredients': array('X': array('RED_MUSHROOM', 'BROWN_MUSHROOM'))));",
180179
"Crafts a mushroom stem block."),
180+
181+
new ExampleScript("Demonstrates a shapeless recipe with exact ingredients."
182+
+ " This is only possible on Paper servers. On Spigot, only the item material will be considered."
183+
+ " Each element in the ingredients array may be a single item/material or an array of item/material"
184+
+ " choices (like how different types of planks can create a stick).",
185+
"add_recipe(array(\n"
186+
+ "\t'key': 'debug_stick',\n"
187+
+ "\t'type': 'SHAPELESS',\n"
188+
+ "\t'result': array('name': 'DEBUG_STICK'),\n"
189+
+ "\t'ingredients': array(\n"
190+
+ "\t\tarray('name': 'NETHER_STAR'),\n"
191+
+ "\t\tarray('name': 'STICK', 'meta': array('repair': 1, 'enchants': array('silk_touch': 1))))));",
192+
"Turns a nether star and a stick enchanted with silk touch into a debug stick."),
181193
};
182194
}
183195
}

0 commit comments

Comments
 (0)