Fixed autocrafting acting weird

This commit is contained in:
The_Creeper_Cow 2018-10-15 12:05:36 +02:00
parent 1ad3c3faf2
commit afa1978f3e

View File

@ -8,10 +8,7 @@ import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
public class ModuleAutoCrafting implements Module {
@ -27,19 +24,28 @@ 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;
List<ItemStack> ingredientMap = null;
if(recipe instanceof ShapelessRecipe)ingredientMap = ((ShapelessRecipe) recipe).getIngredientList();
if(recipe instanceof ShapedRecipe)ingredientMap = (List<ItemStack>) ((ShapedRecipe) recipe).getIngredientMap().values();
if (hopperBlock.getInventory().getSize() == 0) continue;
//TODO DEBUG
if (recipe instanceof ShapedRecipe || recipe instanceof ShapelessRecipe) {
List<ItemStack> ingredientMap = null;
if (recipe instanceof ShapelessRecipe) {
ingredientMap = ((ShapelessRecipe) recipe).getIngredientList();
} else if (recipe instanceof ShapedRecipe) {
ingredientMap = new ArrayList<>(((ShapedRecipe) recipe).getIngredientMap().values());
}
ingredientMap = compressItemStack(ingredientMap);
if (hopperBlock.getInventory().getSize() == 0) continue;
for (ItemStack item : ingredientMap) {
if (!hopperBlock.getInventory().contains(item.getType(), item.getAmount())) continue main;
for (ItemStack item : ingredientMap) {
if(!hopperBlock.getInventory().contains(item)){
continue main;
}
}
for (ItemStack item : ingredientMap) {
hopperBlock.getInventory().removeItem(item);
}
hopperBlock.getInventory().addItem(recipe.getResult());
}
for (ItemStack item : ingredientMap) {
hopperBlock.getInventory().removeItem(item);
}
hopperBlock.getInventory().addItem(new ItemStack(hopper.getAutoCrafting()));
}
}
}
@ -101,4 +107,18 @@ 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;
}
}