Skip to content

Commit

Permalink
Parse dItem recipes on script load
Browse files Browse the repository at this point in the history
  • Loading branch information
Morphan1 committed Aug 3, 2015
1 parent ffbdf66 commit c82b27c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
Expand Up @@ -98,8 +98,8 @@ public class ItemScriptContainer extends ScriptContainer {
// -->

// A map storing special recipes that use itemscripts as ingredients
public static Map<dItem, dList> specialrecipesMap = new HashMap<dItem, dList>();
public static Map<dItem, dList> shapelessRecipesMap = new HashMap<dItem, dList>();
public static Map<dItem, List<dItem>> specialrecipesMap = new HashMap<dItem, List<dItem>>();
public static Map<dItem, List<dItem>> shapelessRecipesMap = new HashMap<dItem, List<dItem>>();

dNPC npc = null;
dPlayer player = null;
Expand All @@ -123,27 +123,50 @@ public ItemScriptContainer(YamlConfiguration configurationSection, String script
recipeList.set(n, TagManager.tag(recipeList.get(n), new BukkitTagContext(player, npc, false, null, dB.shouldDebug(this), new dScript(this))));
}

// Store every ingredient in a dList
dList ingredients = new dList();
// Store every ingredient in a List
List<dItem> ingredients = new ArrayList<dItem>();

for (String recipeRow : recipeList) {
boolean shouldRegister = true;
recipeLoop: for (String recipeRow : recipeList) {
String[] elements = recipeRow.split("\\|", 3);

for (String element : elements) {
ingredients.add(element.replaceAll("[iImM]@", ""));
dItem ingredient = dItem.valueOf(element.replaceAll("[iImM]@", ""));
if (ingredient == null) {
dB.echoError("Invalid dItem ingredient, recipe will not be registered for item script '"
+ getName() + "': " + element);
shouldRegister = false;
break recipeLoop;
}
ingredients.add(ingredient);
}
}

// Add the recipe to Denizen's item script recipe list so it
// will be checked manually inside ItemScriptHelper
specialrecipesMap.put(getItemFrom(), ingredients);
if (shouldRegister) {
specialrecipesMap.put(getItemFrom(), ingredients);
}

}

if (contains("SHAPELESS_RECIPE")) {
String list = TagManager.tag(getString("SHAPELESS_RECIPE"), new BukkitTagContext(player, npc, false, null, dB.shouldDebug(this), new dScript(this)));
dList actual_list = dList.valueOf(list);
shapelessRecipesMap.put(getItemFrom(), actual_list);
List<dItem> ingredients = new ArrayList<dItem>();
boolean shouldRegister = true;
for (String element : dList.valueOf(list)) {
dItem ingredient = dItem.valueOf(element.replaceAll("[iImM]@", ""));
if (ingredient == null) {
dB.echoError("Invalid dItem ingredient, shapeless recipe will not be registered for item script '"
+ getName() + "': " + element);
shouldRegister = false;
break;
}
ingredients.add(ingredient);
}
if (shouldRegister) {
shapelessRecipesMap.put(getItemFrom(), ingredients);
}
}

if (contains("FURNACE_RECIPE")) {
Expand Down
Expand Up @@ -267,15 +267,15 @@ public dItem getSpecialRecipeResult(ItemStack[] matrix) {

// Iterate through all the special recipes
master:
for (Map.Entry<dItem, dList> entry :
for (Map.Entry<dItem, List<dItem>> entry :
ItemScriptContainer.specialrecipesMap.entrySet()) {

// Check if the two sets of items match each other
for (int n = 0; n < 9; n++) {

// Use dItem.valueOf on the entry values to ensure
// correct comparison
dItem valueN = dItem.valueOf(entry.getValue().get(n));
dItem valueN = entry.getValue().get(n);
dItem matrixN = matrix.length <= n || matrix[n] == null ? new dItem(Material.AIR) : new dItem(matrix[n].clone());

// If one's an item script and the other's not, it's a fail
Expand All @@ -296,10 +296,10 @@ else if (!valueN.getMaterial().matchesMaterialData(matrixN.getMaterial().getMate
}

primary:
for (Map.Entry<dItem, dList> entry :
for (Map.Entry<dItem, List<dItem>> entry :
ItemScriptContainer.shapelessRecipesMap.entrySet()) {
for (int i = 0; i < entry.getValue().size(); i++) {
if (!containsAny(dItem.valueOf(entry.getValue().get(i)), matrix)) {
if (!containsAny(entry.getValue().get(i), matrix)) {
continue primary;
}
}
Expand Down

0 comments on commit c82b27c

Please sign in to comment.