Skip to content

Commit

Permalink
Parse item recipes better
Browse files Browse the repository at this point in the history
  • Loading branch information
Morphan1 committed Aug 5, 2015
1 parent 7e16731 commit c51f998
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 60 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, List<dItem>> specialrecipesMap = new HashMap<dItem, List<dItem>>();
public static Map<dItem, List<dItem>> shapelessRecipesMap = new HashMap<dItem, List<dItem>>();
public static Map<ItemScriptContainer, List<dItem>> specialrecipesMap = new HashMap<ItemScriptContainer, List<dItem>>();
public static Map<ItemScriptContainer, List<dItem>> shapelessRecipesMap = new HashMap<ItemScriptContainer, List<dItem>>();

dNPC npc = null;
dPlayer player = null;
Expand All @@ -118,55 +118,13 @@ public ItemScriptContainer(YamlConfiguration configurationSection, String script
// Get recipe list from item script
List<String> recipeList = getStringList("RECIPE");

// Process all tags in list
for (int n = 0; n < recipeList.size(); n++) {
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 List
List<dItem> ingredients = new ArrayList<dItem>();

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

for (String element : elements) {
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
if (shouldRegister) {
specialrecipesMap.put(getItemFrom(), ingredients);
}
// Process later so that any item script ingredients can be fulfilled
ItemScriptHelper.recipes_to_register.put(this, recipeList);

}

if (contains("SHAPELESS_RECIPE")) {
String list = TagManager.tag(getString("SHAPELESS_RECIPE"), new BukkitTagContext(player, npc, false, null, dB.shouldDebug(this), new dScript(this)));
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);
}
ItemScriptHelper.shapeless_to_register.put(this, getString("SHAPELESS_RECIPE"));
}

if (contains("FURNACE_RECIPE")) {
Expand Down
@@ -1,14 +1,19 @@
package net.aufdemrand.denizen.scripts.containers.core;

import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.events.bukkit.ScriptReloadEvent;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dInventory;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.tags.BukkitTagContext;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.events.OldEventManager;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.objects.dScript;
import net.aufdemrand.denizencore.tags.TagManager;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
Expand All @@ -25,17 +30,15 @@

import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class ItemScriptHelper implements Listener {

public static Map<String, ItemScriptContainer> item_scripts = new ConcurrentHashMap<String, ItemScriptContainer>(8, 0.9f, 1);

public static Map<String, ItemScriptContainer> item_scripts_by_hash_id = new HashMap<String, ItemScriptContainer>();
public static Map<ItemScriptContainer, List<String>> recipes_to_register = new HashMap<ItemScriptContainer, List<String>>();
public static Map<ItemScriptContainer, String> shapeless_to_register = new HashMap<ItemScriptContainer, String>();

public ItemScriptHelper() {
DenizenAPI.getCurrentInstance().getServer().getPluginManager()
Expand All @@ -48,6 +51,73 @@ public static void removeDenizenRecipes() {
ItemScriptContainer.shapelessRecipesMap.clear();
}

@EventHandler
public void scriptReload(ScriptReloadEvent event) {

for (Map.Entry<ItemScriptContainer, List<String>> entry : recipes_to_register.entrySet()) {

ItemScriptContainer container = entry.getKey();
List<String> recipeList = entry.getValue();

// Process all tags in list
for (int n = 0; n < recipeList.size(); n++) {
recipeList.set(n, TagManager.tag(recipeList.get(n), new BukkitTagContext(container.player, container.npc,
false, null, dB.shouldDebug(container), new dScript(container))));
}

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

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

for (String element : elements) {
dItem ingredient = dItem.valueOf(element.replaceAll("[iImM]@", ""));
if (ingredient == null) {
dB.echoError("Invalid dItem ingredient, recipe will not be registered for item script '"
+ container.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
if (shouldRegister) {
ItemScriptContainer.specialrecipesMap.put(container, ingredients);
}
}

for (Map.Entry<ItemScriptContainer, String> entry : shapeless_to_register.entrySet()) {

ItemScriptContainer container = entry.getKey();
String string = entry.getValue();

String list = TagManager.tag(string, new BukkitTagContext(container.player, container.npc,
false, null, dB.shouldDebug(container), new dScript(container)));

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 '"
+ container.getName() + "': " + element);
shouldRegister = false;
break;
}
ingredients.add(ingredient);
}
if (shouldRegister) {
ItemScriptContainer.shapelessRecipesMap.put(container, ingredients);
}
}
}

public static boolean isBound(ItemStack item) {
return (isItemscript(item) && getItemScriptContainer(item).bound);
}
Expand Down Expand Up @@ -200,7 +270,7 @@ public boolean processSpecialRecipes(final CraftingInventory inventory, final Pl

// Get the result of the special recipe that this matrix matches,
// if any
dItem result1 = getSpecialRecipeResult(matrix1);
dItem result1 = getSpecialRecipeResult(matrix1, player);

boolean returnme = result1 != null;

Expand All @@ -215,7 +285,7 @@ public void run() {

// Get the result of the special recipe that this matrix matches,
// if any
dItem result = getSpecialRecipeResult(matrix);
dItem result = getSpecialRecipeResult(matrix, player);

// Proceed only if the result was not null
if (result != null) {
Expand Down Expand Up @@ -263,11 +333,11 @@ else if (dItem.matches(determination)) {

// Check if a CraftingInventory's crafting matrix matches a special
// recipe and return that recipe's dItem result if it does
public dItem getSpecialRecipeResult(ItemStack[] matrix) {
public dItem getSpecialRecipeResult(ItemStack[] matrix, Player player) {

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

// Check if the two sets of items match each other
Expand All @@ -292,18 +362,18 @@ else if (!valueN.getMaterial().matchesMaterialData(matrixN.getMaterial().getMate
}

// If all the items match, return the special recipe's dItem key
return entry.getKey();
return entry.getKey().getItemFrom(dPlayer.mirrorBukkitPlayer(player), null);
}

primary:
for (Map.Entry<dItem, List<dItem>> entry :
for (Map.Entry<ItemScriptContainer, List<dItem>> entry :
ItemScriptContainer.shapelessRecipesMap.entrySet()) {
for (int i = 0; i < entry.getValue().size(); i++) {
if (!containsAny(entry.getValue().get(i), matrix)) {
continue primary;
}
}
return entry.getKey();
return entry.getKey().getItemFrom(dPlayer.mirrorBukkitPlayer(player), null);
}

return null;
Expand All @@ -329,7 +399,7 @@ public boolean emulateSpecialRecipeResultShiftClick(CraftingInventory inventory,

// Get the result of the special recipe that this matrix matches,
// if any
dItem result = getSpecialRecipeResult(matrix);
dItem result = getSpecialRecipeResult(matrix, player);

// Proceed only if the result was not null
if (result != null) {
Expand Down

0 comments on commit c51f998

Please sign in to comment.