Skip to content

Commit

Permalink
allow multiple recipes per script, for #1851
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Sep 15, 2019
1 parent 2d2b386 commit cfa81e4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 21 deletions.
Expand Up @@ -70,21 +70,30 @@ public class ItemScriptContainer extends ScriptContainer {
// - ...
//
// # You can specify the items required to craft your item. For an empty slot, use air.
// recipe:
// - ItemTag|ItemTag|ItemTag
// - ItemTag|ItemTag|ItemTag
// - ItemTag|ItemTag|ItemTag
//
// # You can specify a material that can be smelted into your item.
// # Note: This can overwrite existing furnace recipes.
// # If no_id is specified, only the material/data pair will be validated.
// # This might misbehave with some smelting systems, as the Minecraft smelting logic may refuse
// # To continue smelting items in some cases when the script validator gets in the way.
// furnace_recipe: ItemTag
// recipes:
// 1:
// - ItemTag|ItemTag|ItemTag
// - ItemTag|ItemTag|ItemTag
// - ItemTag|ItemTag|ItemTag
// # Add more recipes if you want.
// 2:
// - ...
//
// # You can specify a list of materials that make up a shapeless recipe.
// # Note: This can overwrite existing shapeless recipes.
// shapeless_recipe: ItemTag|...
// shapeless_recipes:
// 1: ItemTag|...
// # Add more shapeless recipes if you want.
// 2: ...
//
// # You can specify a material that can be smelted into your item.
// # Note: This can overwrite existing furnace recipes.
// furnace_recipes:
// 1:
// # The input item
// input: ItemTag
// # Add more furnace recipes if you want.
// 2: ...
//
// # Set to true to not store the scriptID on the item, treating it as an item dropped by any other plugin.
// # NOTE: THIS IS NOT RECOMMENDED UNLESS YOU HAVE A SPECIFIC REASON TO USE IT.
Expand Down
Expand Up @@ -9,10 +9,13 @@
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.core.ScriptTag;
import com.denizenscript.denizencore.tags.TagManager;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.YamlConfiguration;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import org.bukkit.*;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
Expand Down Expand Up @@ -50,7 +53,7 @@ public static void removeDenizenRecipes() {
}
}

public void registerShapedRecipe(ItemScriptContainer container, List<String> recipeList) {
public void registerShapedRecipe(ItemScriptContainer container, List<String> recipeList, int id) {
for (int n = 0; n < recipeList.size(); n++) {
recipeList.set(n, TagManager.tag(recipeList.get(n), new BukkitTagContext(container.player, container.npc, new ScriptTag(container))));
}
Expand All @@ -69,7 +72,7 @@ public void registerShapedRecipe(ItemScriptContainer container, List<String> rec
}
}
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13)) {
NamespacedKey key = new NamespacedKey("denizen", "item_" + CoreUtilities.toLowerCase(container.getName()) + "_shaped_recipe");
NamespacedKey key = new NamespacedKey("denizen", "item_" + CoreUtilities.toLowerCase(container.getName()) + "_shaped_recipe_" + id);
ShapedRecipe recipe = new ShapedRecipe(key, container.getCleanReference().getItemStack()).shape("ABC", "DEF", "GHI");
for (int i = 0; i < ingredients.size(); i++) {
recipe.setIngredient("ABCDEFGHI".charAt(i), new RecipeChoice.ExactChoice(ingredients.get(i).getItemStack().clone()));
Expand All @@ -81,7 +84,7 @@ public void registerShapedRecipe(ItemScriptContainer container, List<String> rec
}
}

public void registerShapelessRecipe(ItemScriptContainer container, String shapelessString) {
public void registerShapelessRecipe(ItemScriptContainer container, String shapelessString, int id) {
String list = TagManager.tag(shapelessString, new BukkitTagContext(container.player, container.npc, new ScriptTag(container)));
List<ItemTag> ingredients = new ArrayList<>();
for (String element : ListTag.valueOf(list)) {
Expand All @@ -99,14 +102,14 @@ public void registerShapelessRecipe(ItemScriptContainer container, String shapel
for (int i = 0; i < input.length; i++) {
input[i] = ingredients.get(i).getItemStack().clone();
}
NMSHandler.getItemHelper().registerShapelessRecipe(CoreUtilities.toLowerCase(container.getName()), result, input);
NMSHandler.getItemHelper().registerShapelessRecipe(CoreUtilities.toLowerCase(container.getName()) + "_" + id, result, input);
}
else {
shapelessRecipesMap.put(container, ingredients);
}
}

public void registerFurnaceRecipe(ItemScriptContainer container, String furnaceItemString, float exp, int time) {
public void registerFurnaceRecipe(ItemScriptContainer container, String furnaceItemString, float exp, int time, int id) {
ItemTag furnace_item = ItemTag.valueOf(furnaceItemString, container);
if (furnace_item == null) {
Debug.echoError("Invalid item '" + furnaceItemString + "', furnace recipe will not be registered for item script '" + container.getName() + "'.");
Expand All @@ -115,7 +118,7 @@ public void registerFurnaceRecipe(ItemScriptContainer container, String furnaceI
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13)) {
ItemStack result = container.getCleanReference().getItemStack().clone();
ItemStack input = furnace_item.getItemStack().clone();
NMSHandler.getItemHelper().registerFurnaceRecipe(CoreUtilities.toLowerCase(container.getName()), result, input, exp, time);
NMSHandler.getItemHelper().registerFurnaceRecipe(CoreUtilities.toLowerCase(container.getName()) + "_" + id, result, input, exp, time);
}
else {
FurnaceRecipe recipe = new FurnaceRecipe(container.getCleanReference().getItemStack(), furnace_item.getMaterial().getMaterial(), furnace_item.getItemStack().getDurability());
Expand All @@ -129,14 +132,42 @@ public void scriptReload(ScriptReloadEvent event) {

currentFurnaceRecipes.clear();
for (ItemScriptContainer container : item_scripts.values()) {
if (container.contains("recipes")) {
YamlConfiguration section = container.getConfigurationSection("recipes");
int id = 1;
for (StringHolder key : section.getKeys(false)) {
registerShapedRecipe(container, section.getStringList(key.str), id);
id++;
}
}
if (container.contains("shapeless_recipes")) {
YamlConfiguration section = container.getConfigurationSection("shapeless_recipes");
int id = 1;
for (StringHolder key : section.getKeys(false)) {
registerShapelessRecipe(container, section.getString(key.str), id);
id++;
}
}
if (container.contains("furnace_recipes")) {
YamlConfiguration section = container.getConfigurationSection("furnace_recipes");
int id = 1;
for (StringHolder key : section.getKeys(false)) {
YamlConfiguration subSection = section.getConfigurationSection(key.str);
float exp = 0;
int cookTime = 40;
registerFurnaceRecipe(container, subSection.getString("input"), exp, cookTime, id);
id++;
}
}
// Old script style
if (container.contains("RECIPE")) {
registerShapedRecipe(container, container.getStringList("RECIPE"));
registerShapedRecipe(container, container.getStringList("RECIPE"), 0);
}
if (container.contains("SHAPELESS_RECIPE")) {
registerShapelessRecipe(container, container.getString("SHAPELESS_RECIPE"));
registerShapelessRecipe(container, container.getString("SHAPELESS_RECIPE"), 0);
}
if (container.contains("FURNACE_RECIPE")) {
registerFurnaceRecipe(container, container.getString("FURNACE_RECIPE"), 0, 40);
registerFurnaceRecipe(container, container.getString("FURNACE_RECIPE"), 0, 40, 0);
}
}
}
Expand Down

0 comments on commit cfa81e4

Please sign in to comment.