Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public ItemStack getResultingItem(Material[] mats) {
- List<ItemStack> convert = new ArrayList<ItemStack>();
- for (Material m : mats)
- convert.add(new ItemStack(m));
- return getResultingItem(convert);
- }
- private Map<List<ItemStack>, ItemStack> cache = new HashMap<List<ItemStack>, ItemStack>();
- private List<Recipe> recipes;
- public ItemStack getResultingItem(List<ItemStack> items) {
- if (cache.containsKey(items))
- return cache.get(items);
- if (recipes == null) {
- recipes = new ArrayList<Recipe>();
- Iterator<Recipe> it = Bukkit.recipeIterator();
- while (it.hasNext()) {
- Recipe rec = it.next();
- recipes.add(rec);
- }
- }
- for (Recipe r : recipes) {
- if (r instanceof ShapedRecipe) {
- if (matchesShaped((ShapedRecipe) r, items))
- return r.getResult();
- } else if (r instanceof ShapelessRecipe) {
- ShapelessRecipe less = (ShapelessRecipe) r;
- if (matchesShapeless(less.getChoiceList(), items))
- return r.getResult();
- }
- }
- return null;
- }
- private boolean matchesShaped(ShapedRecipe shape, List<ItemStack> items) {
- String[] map = shape.getShape();
- Map<Character, RecipeChoice> choices = shape.getChoiceMap();
- for (Entry<Character, RecipeChoice> entry : choices.entrySet()) {
- if (entry.getValue() == null)
- continue;
- }
- int index = 0;
- boolean test = true;
- for (String s : map) {
- if (!test)
- break;
- for (Character c : s.toCharArray()) {
- RecipeChoice currentChoice = choices.get(c);
- if (currentChoice == null) {
- if (index < items.size() && items.get(index) != null
- && items.get(index).getType() != Material.AIR) {
- test = false;
- break;
- }
- index++;
- continue;
- }
- if (index >= items.size()) {
- test = false;
- break;
- }
- if (!currentChoice.test(items.get(index))) {
- test = false;
- break;
- }
- index++;
- }
- }
- return test;
- }
- private boolean matchesShapeless(List<RecipeChoice> choice, List<ItemStack> items) {
- items = new ArrayList<ItemStack>(items);
- for (RecipeChoice c : choice) {
- boolean match = false;
- for (int i = 0; i < items.size(); i++) {
- ItemStack item = items.get(i);
- if (item == null || item.getType() == Material.AIR)
- continue;
- if (c.test(item)) {
- match = true;
- items.remove(i);
- break;
- }
- }
- if (!match)
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement