Package com.sk89q.craftbook.mechanics.crafting.RecipeManager

Examples of com.sk89q.craftbook.mechanics.crafting.RecipeManager.RecipeType


        if (!(sender instanceof Player)) return;
        LocalPlayer player = plugin.wrapPlayer((Player) sender);

        String name = context.getString(0);
        RecipeType type = RecipeType.getTypeFromName(context.getString(1));
        HashMap<String, Object> advancedData = new HashMap<String, Object>();

        if (context.hasFlag('p')) {
            advancedData.put("permission-node", context.getFlag('p'));
        }

        ItemStack[] slots = new ItemStack[]{((Player) sender).getInventory().getItem(9),((Player) sender).getInventory().getItem(10),
                ((Player) sender).getInventory().getItem(11),((Player) sender).getInventory().getItem(18),((Player) sender).getInventory().getItem(19),
                ((Player) sender).getInventory().getItem(20),((Player) sender).getInventory().getItem(27),((Player) sender).getInventory().getItem(28),
                ((Player) sender).getInventory().getItem(29)};

        if(type == RecipeType.SHAPED) {

            LinkedHashMap<CraftingItemStack, Character> items = new LinkedHashMap<CraftingItemStack, Character>();

            int furtherestX = -1;
            int furtherestY = -1;

            for (int slot = 0; slot < 3; slot++) {
                ItemStack stack = slots[slot];
                if(ItemUtil.isStackValid(stack)) {
                    furtherestY = 0;
                    if(furtherestX < slot)
                        furtherestX = slot;
                }
            }
            for (int slot = 3; slot < 6; slot++) {
                ItemStack stack = slots[slot];
                if(ItemUtil.isStackValid(stack)) {
                    furtherestY = 1;
                    if(furtherestX < slot-3)
                        furtherestX = slot-3;
                }
            }
            for (int slot = 6; slot < 9; slot++) {
                ItemStack stack = slots[slot];
                if(ItemUtil.isStackValid(stack)) {
                    furtherestY = 2;
                    if(furtherestX < slot-6)
                        furtherestX = slot-6;
                }
            }

            if(furtherestX > 2)
                furtherestX = 2;

            String[] shape = new String[furtherestY+1];
            Character[] characters = new Character[]{'a','b','c','d','e','f','g','h','i'};
            int curChar = 0;

            for(int y = 0; y < furtherestY+1; y++) {
                for(int x = 0; x < furtherestX+1; x++) {

                    String c = " ";
                    CraftingItemStack stack = new CraftingItemStack(slots[x+y*3]);
                    if(ItemUtil.isStackValid(stack.getItemStack())) {

                        boolean found = false;
                        for(Entry<CraftingItemStack, Character> st : items.entrySet()) {
                            if(st.getKey().isSameType(stack)) {
                                c = st.getValue().toString();
                                found = true;
                                break;
                            }
                        }
                        if(!found) {
                            items.put(stack, characters[curChar]);
                            c = characters[curChar].toString();
                            curChar++;
                        }
                    }

                    if(x == 0)
                        shape[y] = c;
                    else
                        shape[y] = shape[y] + c;
                }
            }

            List<CraftingItemStack> results = getResults(((Player) sender).getInventory());
            if(results.size() > 1)
                advancedData.put("extra-results", results.subList(1, results.size()));
            else if (results.isEmpty()) {
                player.printError("Results are required to create a recipe!");
                return;
            }

            try {
                RecipeManager.Recipe recipe = RecipeManager.INSTANCE.new Recipe(name, type, items, Arrays.asList(shape), results.get(0), advancedData);
                RecipeManager.INSTANCE.addRecipe(recipe);
                if (CustomCrafting.INSTANCE == null) {
                    player.printError("You do not have CustomCrafting enabled, or Java has bugged and unloaded it (Did you use /reload?)!");
                    return;
                }
                CustomCrafting.INSTANCE.addRecipe(recipe);
                player.print("Successfully added a new " + type.name() + " recipe!");
            } catch (Exception e) {
                player.printError("Error adding recipe! See console for more details!");
                BukkitUtil.printStacktrace(e);
            }

        } else if (type == RecipeType.SHAPELESS || type == RecipeType.FURNACE) {

            ArrayList<CraftingItemStack> ingredients = new ArrayList<CraftingItemStack>();

            for(ItemStack slot : slots) {

                if(!ItemUtil.isStackValid(slot))
                    continue;

                CraftingItemStack stack = new CraftingItemStack(slot.clone());

                boolean used = false;
                for(CraftingItemStack compare : ingredients) {

                    if(compare.isSameType(stack)) {
                        ingredients.set(ingredients.indexOf(compare), compare.add(stack));
                        used = true;
                        break;
                    }
                }

                if(!used)
                    ingredients.add(stack);
            }

            List<CraftingItemStack> results = getResults(((Player) sender).getInventory());
            if(results.size() > 1)
                advancedData.put("extra-results", results.subList(1, results.size()));
            else if (results.isEmpty()) {
                player.printError("Results are required to create a recipe!");
                return;
            }

            try {
                RecipeManager.Recipe recipe = RecipeManager.INSTANCE.new Recipe(name, type, ingredients, results.get(0), advancedData);
                RecipeManager.INSTANCE.addRecipe(recipe);
                CustomCrafting.INSTANCE.addRecipe(recipe);
                player.print("Successfully added a new " + type.name() + " recipe!");
            } catch (Exception e) {
                player.printError("Error adding recipe! See console for more details!");
                BukkitUtil.printStacktrace(e);
            }
        }
View Full Code Here

TOP

Related Classes of com.sk89q.craftbook.mechanics.crafting.RecipeManager.RecipeType

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.