Skip to content

Commit

Permalink
add dynamic material matching to item script recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 5, 2021
1 parent 8b090bf commit 6dab60f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
Expand Up @@ -111,6 +111,7 @@ public class ItemScriptContainer extends ScriptContainer {
// # For an empty slot, use "air".
// # By default, items require an exact match. For a material-based match, use the format "material:MaterialNameHere" like "material:stick".
// # To make multiple different items match for any slot, just separate them with slashes, like "stick/stone". To match multiple materials, use "material:a/b/c".
// # You can also make a dynamic material matcher using '*', like "material:*_log" to match any log block.
// # Note that to require multiple of an item as an input, the only option is to use multiple slots. A single slot cannot require a quantity of items, as that is not part of the minecraft recipe system.
// # | All recipes must include this key!
// input:
Expand Down
Expand Up @@ -6,6 +6,7 @@
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizencore.events.ScriptEvent;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.core.ScriptTag;
Expand Down Expand Up @@ -79,21 +80,38 @@ else if (c == '/' && !brackets) {
return output;
}

public ItemStack[] textToItemArray(ItemScriptContainer container, String text) {
public ItemStack[] textToItemArray(ItemScriptContainer container, String text, boolean exact) {
if (CoreUtilities.toLowerCase(text).equals("air")) {
return new ItemStack[0];
}
List<String> ingredientText = splitByNonBracketedSlashes(text);
ItemStack[] outputItems = new ItemStack[ingredientText.size()];
for (int i = 0; i < outputItems.length; i++) {
ItemTag ingredient = ItemTag.valueOf(ingredientText.get(i), container);
if (ingredient == null) {
Debug.echoError("Invalid ItemTag ingredient, recipe will not be registered for item script '" + container.getName() + "': " + text);
return null;
List<ItemStack> outputItems = new ArrayList<>(ingredientText.size());
for (int i = 0; i < ingredientText.size(); i++) {
String entry = ingredientText.get(i);
if (!exact && ScriptEvent.isAdvancedMatchable(entry)) {
boolean any = false;
ScriptEvent.MatchHelper matcher = ScriptEvent.createMatcher(entry);
for (Material material : Material.values()) {
if (matcher.doesMatch(CoreUtilities.toLowerCase(material.name()))) {
outputItems.add(new ItemStack(material, 1));
any = true;
}
}
if (!any) {
Debug.echoError("Invalid ItemTag ingredient (empty advanced matcher), recipe will not be registered for item script '" + container.getName() + "': " + entry);
return null;
}
}
else {
ItemTag ingredient = ItemTag.valueOf(entry, container);
if (ingredient == null) {
Debug.echoError("Invalid ItemTag ingredient, recipe will not be registered for item script '" + container.getName() + "': " + entry);
return null;
}
outputItems.add(ingredient.getItemStack().clone());
}
outputItems[i] = ingredient.getItemStack().clone();
}
return outputItems;
return outputItems.toArray(new ItemStack[0]);
}

public void registerShapedRecipe(ItemScriptContainer container, ItemStack item, List<String> recipeList, String internalId, String group) {
Expand All @@ -114,14 +132,12 @@ public void registerShapedRecipe(ItemScriptContainer container, ItemStack item,

for (String element : elements) {
String itemText = element;
if (itemText.startsWith("material:")) {
exacts.add(false);
boolean isExact = !itemText.startsWith("material:");
if (!isExact) {
itemText = itemText.substring("material:".length());
}
else {
exacts.add(true);
}
ItemStack[] items = textToItemArray(container, itemText);
exacts.add(isExact);
ItemStack[] items = textToItemArray(container, itemText, isExact);
if (items == null) {
return;
}
Expand Down Expand Up @@ -159,14 +175,12 @@ public void registerShapelessRecipe(ItemScriptContainer container, ItemStack ite
List<Boolean> exacts = new ArrayList<>();
for (String element : ListTag.valueOf(list, context)) {
String itemText = element;
if (itemText.startsWith("material:")) {
exacts.add(false);
boolean isExact = !itemText.startsWith("material:");
if (!isExact) {
itemText = itemText.substring("material:".length());
}
else {
exacts.add(true);
}
ItemStack[] items = textToItemArray(container, itemText);
exacts.add(isExact);
ItemStack[] items = textToItemArray(container, itemText, isExact);
if (items == null) {
return;
}
Expand All @@ -185,7 +199,7 @@ public void registerFurnaceRecipe(ItemScriptContainer container, ItemStack item,
exact = false;
furnaceItemString = furnaceItemString.substring("material:".length());
}
ItemStack[] items = textToItemArray(container, furnaceItemString);
ItemStack[] items = textToItemArray(container, furnaceItemString, exact);
if (items == null) {
return;
}
Expand All @@ -198,7 +212,7 @@ public void registerStonecuttingRecipe(ItemScriptContainer container, ItemStack
exact = false;
inputItemString = inputItemString.substring("material:".length());
}
ItemStack[] items = textToItemArray(container, inputItemString);
ItemStack[] items = textToItemArray(container, inputItemString, exact);
if (items == null) {
return;
}
Expand Down

0 comments on commit 6dab60f

Please sign in to comment.