From d6e626d94695866131e594f289ae45ea616664c3 Mon Sep 17 00:00:00 2001 From: Brianna O'Keefe Date: Tue, 16 Oct 2018 18:30:19 -0400 Subject: [PATCH] Fixed issue with autocrafting --- .../levels/modules/ModuleAutoCrafting.java | 67 ++++++++++++------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/EpicHoppers-Plugin/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoCrafting.java b/EpicHoppers-Plugin/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoCrafting.java index 4c1c902..3d6e923 100644 --- a/EpicHoppers-Plugin/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoCrafting.java +++ b/EpicHoppers-Plugin/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoCrafting.java @@ -8,12 +8,30 @@ import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.inventory.*; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class ModuleAutoCrafting implements Module { private final Map cachedRecipes = new HashMap<>(); + public static List compressItemStack(List target) { + HashMap sortingList = new HashMap<>(); + for (ItemStack item : target) { + if (sortingList.containsKey(item.getType())) { + ItemStack existing = sortingList.get(item.getType()); + existing.setAmount(existing.getAmount() + item.getAmount()); + sortingList.put(existing.getType(), existing); + } else { + sortingList.put(item.getType(), item); + } + } + List list = new ArrayList<>(sortingList.values()); + return list; + } + @Override public String getName() { return "AutoCrafting"; @@ -24,23 +42,34 @@ public class ModuleAutoCrafting implements Module { org.bukkit.block.Hopper hopperBlock = hopper.getHopper(); main: for (Recipe recipe : Bukkit.getServer().getRecipesFor(new ItemStack(hopper.getAutoCrafting()))) { - if (!(recipe instanceof ShapedRecipe)&&!(recipe instanceof ShapelessRecipe)) continue; + if (!(recipe instanceof ShapedRecipe) && !(recipe instanceof ShapelessRecipe)) continue; List ingredientMap = null; - if(recipe instanceof ShapelessRecipe)ingredientMap = ((ShapelessRecipe) recipe).getIngredientList(); - if(recipe instanceof ShapedRecipe)ingredientMap = new ArrayList<>(((ShapedRecipe) recipe).getIngredientMap().values()); + if (recipe instanceof ShapelessRecipe) ingredientMap = ((ShapelessRecipe) recipe).getIngredientList(); + if (recipe instanceof ShapedRecipe) + ingredientMap = new ArrayList<>(((ShapedRecipe) recipe).getIngredientMap().values()); if (hopperBlock.getInventory().getSize() == 0) continue; - for (ItemStack item : ingredientMap) { - if(!hopperBlock.getInventory().contains(item)){ - continue main; - } - } - for (ItemStack item : ingredientMap) { - hopperBlock.getInventory().removeItem(item); + Map items = new HashMap<>(); + for (ItemStack item : ingredientMap) { + if (!items.containsKey(item.getType())) { + items.put(item.getType(), item.getAmount()); + } else { + items.put(item.getType(), items.get(item.getType()) + 1); } - hopperBlock.getInventory().addItem(recipe.getResult()); } + + for (Material material : items.keySet()) { + ItemStack item = new ItemStack(material, items.get(material)); + if (!hopperBlock.getInventory().contains(item)) { + continue main; + } + } + for (Material material : items.keySet()) { + ItemStack item = new ItemStack(material, items.get(material)); + hopperBlock.getInventory().removeItem(item); + } + hopperBlock.getInventory().addItem(recipe.getResult()); } } } @@ -102,18 +131,4 @@ public class ModuleAutoCrafting implements Module { } return false; } - public static List compressItemStack(List target){ - HashMap sortingList = new HashMap<>(); - for (ItemStack item:target){ - if (sortingList.containsKey(item.getType())){ - ItemStack existing = sortingList.get(item.getType()); - existing.setAmount(existing.getAmount()+item.getAmount()); - sortingList.put(existing.getType(),existing); - }else { - sortingList.put(item.getType(),item); - } - } - List list = new ArrayList<>(sortingList.values()); - return list; - } }