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.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<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
public String getName() {
return "AutoCrafting";
@ -24,26 +42,37 @@ 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<ItemStack> 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;
Map<Material, Integer> items = new HashMap<>();
for (ItemStack item : ingredientMap) {
if(!hopperBlock.getInventory().contains(item)){
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)) {
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().addItem(recipe.getResult());
}
}
}
}
public List<Material> getBlockedItems(Hopper hopper) {
List<Material> materials = new ArrayList<>();
@ -102,18 +131,4 @@ public class ModuleAutoCrafting implements Module {
}
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;
}
}