Fixed issue with autocrafting

This commit is contained in:
Brianna O'Keefe 2018-10-16 18:30:19 -04:00
parent 4aef1d858d
commit d6e626d946

View File

@ -8,12 +8,30 @@ import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.*; 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 { public class ModuleAutoCrafting implements Module {
private final Map<Material, Recipe> cachedRecipes = new HashMap<>(); private final Map<Material, Recipe> cachedRecipes = new HashMap<>();
public static List<ItemStack> compressItemStack(List<ItemStack> target) {
HashMap<Material, ItemStack> 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<ItemStack> list = new ArrayList<>(sortingList.values());
return list;
}
@Override @Override
public String getName() { public String getName() {
return "AutoCrafting"; return "AutoCrafting";
@ -27,23 +45,34 @@ public class ModuleAutoCrafting implements Module {
if (!(recipe instanceof ShapedRecipe) && !(recipe instanceof ShapelessRecipe)) continue; if (!(recipe instanceof ShapedRecipe) && !(recipe instanceof ShapelessRecipe)) continue;
List<ItemStack> ingredientMap = null; List<ItemStack> ingredientMap = null;
if (recipe instanceof ShapelessRecipe) ingredientMap = ((ShapelessRecipe) recipe).getIngredientList(); if (recipe instanceof ShapelessRecipe) ingredientMap = ((ShapelessRecipe) recipe).getIngredientList();
if(recipe instanceof ShapedRecipe)ingredientMap = new ArrayList<>(((ShapedRecipe) recipe).getIngredientMap().values()); if (recipe instanceof ShapedRecipe)
ingredientMap = new ArrayList<>(((ShapedRecipe) recipe).getIngredientMap().values());
if (hopperBlock.getInventory().getSize() == 0) continue; if (hopperBlock.getInventory().getSize() == 0) continue;
Map<Material, Integer> items = new HashMap<>();
for (ItemStack item : ingredientMap) { 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);
}
}
for (Material material : items.keySet()) {
ItemStack item = new ItemStack(material, items.get(material));
if (!hopperBlock.getInventory().contains(item)) { if (!hopperBlock.getInventory().contains(item)) {
continue main; continue main;
} }
} }
for (ItemStack item : ingredientMap) { for (Material material : items.keySet()) {
ItemStack item = new ItemStack(material, items.get(material));
hopperBlock.getInventory().removeItem(item); hopperBlock.getInventory().removeItem(item);
} }
hopperBlock.getInventory().addItem(recipe.getResult()); hopperBlock.getInventory().addItem(recipe.getResult());
} }
} }
} }
}
public List<Material> getBlockedItems(Hopper hopper) { public List<Material> getBlockedItems(Hopper hopper) {
List<Material> materials = new ArrayList<>(); List<Material> materials = new ArrayList<>();
@ -102,18 +131,4 @@ public class ModuleAutoCrafting implements Module {
} }
return false; return false;
} }
public static List<ItemStack> compressItemStack(List<ItemStack> target){
HashMap<Material,ItemStack> 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<ItemStack> list = new ArrayList<>(sortingList.values());
return list;
}
} }