Added support for multiple crafting recipes in Autocrafting.

This commit is contained in:
Brianna 2019-04-25 13:55:02 -04:00
parent 6d33ad9e4a
commit e4a6f8c0fd

View File

@ -15,7 +15,7 @@ import java.util.Map;
public class ModuleAutoCrafting implements Module { public class ModuleAutoCrafting implements Module {
private final Map<Material, Recipe> cachedRecipes = new HashMap<>(); private final Map<Material, Recipes> cachedRecipes = new HashMap<>();
private final Map<Hopper, Material> lastMaterial = new HashMap<>(); private final Map<Hopper, Material> lastMaterial = new HashMap<>();
public static List<ItemStack> compressItemStack(List<ItemStack> target) { public static List<ItemStack> compressItemStack(List<ItemStack> target) {
@ -43,56 +43,59 @@ public class ModuleAutoCrafting implements Module {
if (hopper.getAutoCrafting() != null && canMove(hopperInventory, new ItemStack(hopper.getAutoCrafting()))) { if (hopper.getAutoCrafting() != null && canMove(hopperInventory, new ItemStack(hopper.getAutoCrafting()))) {
Recipe recipe = cachedRecipes.get(hopper.getAutoCrafting()); if (cachedRecipes.get(hopper.getAutoCrafting()) == null) return;
if (!(recipe instanceof ShapedRecipe) && !(recipe instanceof ShapelessRecipe)) return;
List<ItemStack> ingredientMap = null;
if (recipe instanceof ShapelessRecipe) ingredientMap = ((ShapelessRecipe) recipe).getIngredientList();
if (recipe instanceof ShapedRecipe)
ingredientMap = new ArrayList<>(((ShapedRecipe) recipe).getIngredientMap().values());
if (hopperInventory.getSize() == 0) return;
Map<Material, Integer> items = new HashMap<>(); top:
for (ItemStack item : ingredientMap) { for (Recipe recipe : cachedRecipes.get(hopper.getAutoCrafting()).getRecipes()) {
if (item == null) continue; if (!(recipe instanceof ShapedRecipe) && !(recipe instanceof ShapelessRecipe)) continue;
if (!items.containsKey(item.getType())) { List<ItemStack> ingredientMap = null;
items.put(item.getType(), item.getAmount()); if (recipe instanceof ShapelessRecipe) ingredientMap = ((ShapelessRecipe) recipe).getIngredientList();
} else { if (recipe instanceof ShapedRecipe)
items.put(item.getType(), items.get(item.getType()) + 1); ingredientMap = new ArrayList<>(((ShapedRecipe) recipe).getIngredientMap().values());
} if (hopperInventory.getSize() == 0) return;
}
for (Material material : items.keySet()) { Map<Material, Integer> items = new HashMap<>();
int amt = 0; for (ItemStack item : ingredientMap) {
ItemStack item = new ItemStack(material, items.get(material)); if (item == null) continue;
for (ItemStack i : hopperInventory.getContents()) { if (!items.containsKey(item.getType())) {
if (i == null) continue; items.put(item.getType(), item.getAmount());
if (i.getType() != material) continue;
amt += i.getAmount();
}
if (amt < item.getAmount()) {
return;
}
}
main2:
for (Material material : items.keySet()) {
int amtRemoved = 0;
ItemStack toRemove = new ItemStack(material, items.get(material));
for (ItemStack i : hopperInventory.getContents()) {
if (i == null || i.getType() != material) continue;
if (toRemove.getAmount() - amtRemoved <= i.getAmount()) {
toRemove.setAmount(toRemove.getAmount() - amtRemoved);
hopperInventory.removeItem(toRemove);
continue main2;
} else { } else {
amtRemoved += i.getAmount(); items.put(item.getType(), items.get(item.getType()) + 1);
hopperInventory.removeItem(i);
} }
} }
}
hopperInventory.addItem(recipe.getResult());
}
for (Material material : items.keySet()) {
int amt = 0;
ItemStack item = new ItemStack(material, items.get(material));
for (ItemStack i : hopperInventory.getContents()) {
if (i == null) continue;
if (i.getType() != material) continue;
amt += i.getAmount();
}
if (amt < item.getAmount()) {
continue top;
}
}
main2:
for (Material material : items.keySet()) {
int amtRemoved = 0;
ItemStack toRemove = new ItemStack(material, items.get(material));
for (ItemStack i : hopperInventory.getContents()) {
if (i == null || i.getType() != material) continue;
if (toRemove.getAmount() - amtRemoved <= i.getAmount()) {
toRemove.setAmount(toRemove.getAmount() - amtRemoved);
hopperInventory.removeItem(toRemove);
continue main2;
} else {
amtRemoved += i.getAmount();
hopperInventory.removeItem(i);
}
}
}
hopperInventory.addItem(recipe.getResult());
}
}
} }
public List<Material> getBlockedItems(Hopper hopper) { public List<Material> getBlockedItems(Hopper hopper) {
@ -109,20 +112,24 @@ public class ModuleAutoCrafting implements Module {
} }
if (!cachedRecipes.containsKey(material)) { if (!cachedRecipes.containsKey(material)) {
Recipes recipes = new Recipes();
for (Recipe recipe : Bukkit.getServer().getRecipesFor(new ItemStack(material))) { for (Recipe recipe : Bukkit.getServer().getRecipesFor(new ItemStack(material))) {
cachedRecipes.put(material, recipe); recipes.addRecipe(recipe);
} }
cachedRecipes.put(material, recipes);
} else { } else {
Recipe recipe = cachedRecipes.get(material); Recipes recipes = cachedRecipes.get(material);
if (recipe instanceof ShapedRecipe) { for (Recipe recipe : recipes.getRecipes()) {
for (ItemStack itemStack : ((ShapedRecipe) recipe).getIngredientMap().values()) { if (recipe instanceof ShapedRecipe) {
if (itemStack == null) continue; for (ItemStack itemStack : ((ShapedRecipe) recipe).getIngredientMap().values()) {
materials.add(itemStack.getType()); if (itemStack == null) continue;
} materials.add(itemStack.getType());
} else if (recipe instanceof ShapelessRecipe) { }
for (ItemStack itemStack : ((ShapelessRecipe) recipe).getIngredientList()) { } else if (recipe instanceof ShapelessRecipe) {
if (itemStack == null) continue; for (ItemStack itemStack : ((ShapelessRecipe) recipe).getIngredientList()) {
materials.add(itemStack.getType()); if (itemStack == null) continue;
materials.add(itemStack.getType());
}
} }
} }
} }
@ -149,4 +156,21 @@ public class ModuleAutoCrafting implements Module {
} }
return false; return false;
} }
class Recipes {
private List<Recipe> recipes = new ArrayList<>();
public List<Recipe> getRecipes() {
return new ArrayList<>(recipes);
}
public void addRecipe(Recipe recipe) {
this.recipes.add(recipe);
}
public void clearRecipes() {
recipes.clear();
}
}
} }