Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.broken.cheststorage.util.ItemUtily;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.inventory.ItemStack;
- import java.util.*;
- public class CraftingUtility {
- private static final Map<Location, Map<ItemStack, ItemStack>> craftedItems = new HashMap<>();
- private final HashMap<ItemStack, ItemStack> itemStacksList = new HashMap<>();
- private Inventory inventory;
- private Location location;
- private List<ItemStack> listOfItems;
- private boolean ifChestFull = false;
- public CraftingUtility(Inventory inventory, Location location, List<ItemStack> listOfItemsToCraft) {
- this.inventory = inventory;
- this.location = location;
- this.listOfItems = listOfItemsToCraft;
- }
- public HashMap<Integer, ItemStack> checkCraftingRecipeExist() {
- HashMap<Integer, ItemStack> outputItemsIfNoSpaceInChest = new HashMap<>();
- int itemAmont = 0;
- for (Crafting craftingRecipe : Crafting.values()) {
- String recipeValues = craftingRecipe.toString();
- if (Minecraftversion(recipeValues)) continue;
- String matrialNameFirst = Crafting.valueOf(recipeValues).getMaterialName1();
- String matrialNameSecond = Crafting.valueOf(recipeValues).getMaterialName2();
- String matrialNameThird = Crafting.valueOf(recipeValues).getMaterialName3();
- String matrialNameFourth = Crafting.valueOf(recipeValues).getMaterialName4();
- String matrialNameFifth = Crafting.valueOf(recipeValues).getMaterialName5();
- if (Minecraftversion(matrialNameFirst, matrialNameSecond, matrialNameThird, matrialNameFourth, matrialNameFifth))
- continue;
- for (ItemStack itemListForCrafting : listOfItems) {
- Map<Material, Integer> amountOfItemsInventory = getItemsAmount(craftingRecipe, itemListForCrafting, this.inventory);
- if (!amountOfItemsInventory.isEmpty()) {
- Map<Material, ItemStack> itemStacks = setItemstacksAmount(amountOfItemsInventory, craftingRecipe);
- if (ifItAreColor(craftingRecipe, itemListForCrafting, itemStacks) || (matrialNameFirst != null && matrialNameSecond == null && matrialNameThird == null && matrialNameFourth == null && matrialNameFifth == null)) {
- if (isItemsMatch(recipeValues, itemListForCrafting) &&
- !itemStacks.isEmpty() && checkAmountForRecipeFirst(itemStacks, recipeValues, returnItemToRecipe(matrialNameFirst, amountOfItemsInventory))) {
- Material material = returnItemToRecipe(matrialNameFirst, amountOfItemsInventory);
- Material getmatrialName = Material.valueOf(Crafting.valueOf(recipeValues).name());
- outputItemsIfNoSpaceInChest = inventory.addItem(new ItemStack(getmatrialName, checkIfItTallFlowers(material) ? Crafting.valueOf(recipeValues).getNumberOutput() + 1 : Crafting.valueOf(recipeValues).getNumberOutput()));
- if (outputItemsIfNoSpaceInChest.isEmpty()) {
- if (craftedItems.containsKey(this.location) && craftedItems.get(this.location).containsKey(ItemUtily.createItemStackAsOne(new ItemStack(material)))) {
- itemAmont = craftedItems.get(this.location).get(ItemUtily.createItemStackAsOne(new ItemStack(material))).getAmount();
- ItemStack items = craftedItems.get(this.location).get(ItemUtily.createItemStackAsOne(new ItemStack(material)));
- items.clone().setAmount(itemAmont);
- this.itemStacksList.put(ItemUtily.createItemStackAsOne(new ItemStack(material)), items);
- }
- this.itemStacksList.put(ItemUtily.createItemStackAsOne(new ItemStack(material)), ItemUtily.createItemStackWhitAmount(getmatrialName, (checkIfItTallFlowers(material) ? Crafting.valueOf(recipeValues).getNumberOutput() + 1 + (craftedItems.isEmpty() ? 0 : itemAmont) : Crafting.valueOf(recipeValues).getNumberOutput() + (craftedItems.isEmpty() ? 0 : itemAmont))));
- craftedItems.put(this.location, this.itemStacksList);
- takeItemsRecipeIngrediens(inventory, craftingRecipe, itemStacks, material);
- }
- }
- } else if (ifItAreColorMatchTwoItems(craftingRecipe, itemListForCrafting, itemStacks) || (!ifItAreColor(craftingRecipe, itemListForCrafting, itemStacks) && matrialNameFirst != null && matrialNameSecond != null && matrialNameThird == null && matrialNameFourth == null && matrialNameFifth == null)) {
- if (isItemsMatch(recipeValues, itemListForCrafting) &&
- !itemStacks.isEmpty() && checkAmountForRecipeFirst(itemStacks, recipeValues, returnItemToRecipe(matrialNameSecond, amountOfItemsInventory)) &&
- checkAmountForRecipeSecond(itemStacks, recipeValues)) {
- Material material = returnItemToRecipe(matrialNameFirst, amountOfItemsInventory);
- Material materialSecond = returnItemToRecipe(matrialNameSecond, amountOfItemsInventory);
- Material recipeNameOutput = Material.valueOf(Crafting.valueOf(recipeValues).name());
- outputItemsIfNoSpaceInChest = inventory.addItem(ItemUtily.createItemStackWhitAmount(recipeNameOutput, isAColor(recipeNameOutput) ? (Crafting.valueOf(recipeValues).getNumberOutput() * 2) + addExtraIfSpecialcolor(materialSecond, recipeNameOutput) : Crafting.valueOf(recipeValues).getNumberOutput()));
- if (outputItemsIfNoSpaceInChest.isEmpty()) {
- if (craftedItems.containsKey(this.location) && craftedItems.get(this.location).containsKey(ItemUtily.createItemStackAsOne(new ItemStack(material)))) {
- itemAmont = craftedItems.get(this.location).get(ItemUtily.createItemStackAsOne(new ItemStack(material))).getAmount();
- ItemStack items = craftedItems.get(this.location).get(ItemUtily.createItemStackAsOne(new ItemStack(material)));
- //items.clone().setAmount(Math.min(items.getMaxStackSize(), items.getAmount() + (isAColor(recipeNameOutput) ? (itemAmont * 2) : itemAmont)));
- this.itemStacksList.put(ItemUtily.createItemStackAsOne(new ItemStack(material)), items);
- }
- this.itemStacksList.put(ItemUtily.createItemStackAsOne(new ItemStack(material)), ItemUtily.createItemStackWhitAmount(recipeNameOutput, isAColor(recipeNameOutput) ? ((Crafting.valueOf(recipeValues).getNumberOutput() * 2) + (craftedItems.isEmpty() ? 0 : itemAmont) + addExtraIfSpecialcolor(materialSecond, recipeNameOutput)) : (Crafting.valueOf(recipeValues).getNumberOutput() + (craftedItems.isEmpty() ? 0 : itemAmont))));
- craftedItems.put(this.location, this.itemStacksList);
- takeItemsRecipeIngrediens(inventory, craftingRecipe, itemStacks, materialSecond);
- }
- }
- } else if ((!ifItAreColor(craftingRecipe, itemListForCrafting, itemStacks) && matrialNameFirst != null && matrialNameSecond != null && matrialNameThird != null && matrialNameFourth == null && matrialNameFifth == null)) {
- if (isItemsMatch(recipeValues, itemListForCrafting) &&
- !itemStacks.isEmpty() && checkAmountForRecipeFirst(itemStacks, recipeValues, returnItemToRecipe(matrialNameFirst, amountOfItemsInventory)) &&
- checkAmountForRecipeSecond(itemStacks, recipeValues) && checkAmountForRecipeThird(itemStacks, recipeValues, returnItemToRecipe(matrialNameSecond, amountOfItemsInventory))) {
- Material material = returnItemToRecipe(matrialNameFirst, amountOfItemsInventory);
- Material materialSecond = returnItemToRecipe(matrialNameSecond, amountOfItemsInventory);
- Material matrialThird = returnItemToRecipe(matrialNameThird, amountOfItemsInventory);
- Material recipeNameOutput = Material.valueOf(Crafting.valueOf(recipeValues).name());
- outputItemsIfNoSpaceInChest = inventory.addItem(new ItemStack(recipeNameOutput, isAColor(recipeNameOutput) ? (Crafting.valueOf(recipeValues).getNumberOutput() + 2) + addExtraIfSpecialcolor(materialSecond, recipeNameOutput) : Crafting.valueOf(recipeValues).getNumberOutput()));
- if (outputItemsIfNoSpaceInChest.isEmpty()) {
- if (craftedItems.containsKey(this.location) && craftedItems.get(this.location).containsKey(ItemUtily.createItemStackAsOne(new ItemStack(material)))) {
- itemAmont = craftedItems.get(this.location).get(ItemUtily.createItemStackAsOne(new ItemStack(material))).getAmount();
- ItemStack items = craftedItems.get(this.location).get(ItemUtily.createItemStackAsOne(new ItemStack(material)));
- items.clone().setAmount(itemAmont);
- this.itemStacksList.put(ItemUtily.createItemStackAsOne(new ItemStack(material)), items);
- }
- this.itemStacksList.put(ItemUtily.createItemStackAsOne(new ItemStack(material)), ItemUtily.createItemStackWhitAmount(recipeNameOutput, isAColor(recipeNameOutput) ? (Crafting.valueOf(recipeValues).getNumberOutput() + 2) + addExtraIfSpecialcolor(materialSecond, recipeNameOutput) : (Crafting.valueOf(recipeValues).getNumberOutput() + (craftedItems.isEmpty() ? 0 : itemAmont))));
- craftedItems.put(this.location, this.itemStacksList);
- takeItemsRecipeIngrediens(inventory, craftingRecipe, itemStacks, materialSecond);
- }
- }
- } else if ((!ifItAreColor(craftingRecipe, itemListForCrafting, itemStacks) && matrialNameFirst != null && matrialNameSecond != null && matrialNameThird != null && matrialNameFourth != null && matrialNameFifth == null)) {
- if (isItemsMatch(recipeValues, itemListForCrafting) &&
- !itemStacks.isEmpty() && checkAmountForRecipeFirst(itemStacks, recipeValues, returnItemToRecipe(matrialNameFirst, amountOfItemsInventory)) &&
- checkAmountForRecipeSecond(itemStacks, recipeValues) && checkAmountForRecipeThird(itemStacks, recipeValues) &&
- checkAmountForRecipeFourth(itemStacks, recipeValues)) {
- Material material = returnItemToRecipe(matrialNameFirst, amountOfItemsInventory);
- Material materialSecond = returnItemToRecipe(matrialNameSecond, amountOfItemsInventory);
- Material recipeNameOutput = Material.valueOf(Crafting.valueOf(recipeValues).name());
- outputItemsIfNoSpaceInChest = inventory.addItem(new ItemStack(recipeNameOutput, isAColor(recipeNameOutput) ? (Crafting.valueOf(recipeValues).getNumberOutput() + 2) + addExtraIfSpecialcolor(materialSecond, recipeNameOutput) : Crafting.valueOf(recipeValues).getNumberOutput()));
- if (outputItemsIfNoSpaceInChest.isEmpty()) {
- if (craftedItems.containsKey(this.location) && craftedItems.get(this.location).containsKey(ItemUtily.createItemStackAsOne(new ItemStack(material)))) {
- itemAmont = craftedItems.get(this.location).get(ItemUtily.createItemStackAsOne(new ItemStack(material))).getAmount();
- ItemStack items = craftedItems.get(this.location).get(ItemUtily.createItemStackAsOne(new ItemStack(material)));
- items.clone().setAmount(itemAmont);
- this.itemStacksList.put(ItemUtily.createItemStackAsOne(new ItemStack(material)), items);
- }
- this.itemStacksList.put(ItemUtily.createItemStackAsOne(new ItemStack(material)), ItemUtily.createItemStackWhitAmount(recipeNameOutput, isAColor(recipeNameOutput) ? (Crafting.valueOf(recipeValues).getNumberOutput() + 2) + addExtraIfSpecialcolor(materialSecond, recipeNameOutput) : (Crafting.valueOf(recipeValues).getNumberOutput() + (craftedItems.isEmpty() ? 0 : itemAmont))));
- craftedItems.put(this.location, this.itemStacksList);
- takeItemsRecipeIngrediens(inventory, craftingRecipe, itemStacks, null);
- }
- }
- } else if ((!ifItAreColor(craftingRecipe, itemListForCrafting, itemStacks) && matrialNameFirst != null && matrialNameSecond != null && matrialNameThird != null && matrialNameFourth != null && matrialNameFifth != null)) {
- if (isItemsMatch(recipeValues, itemListForCrafting) &&
- !itemStacks.isEmpty() && checkAmountForRecipeFirst(itemStacks, recipeValues, returnItemToRecipe(matrialNameFirst, amountOfItemsInventory)) &&
- checkAmountForRecipeSecond(itemStacks, recipeValues) && checkAmountForRecipeThird(itemStacks, recipeValues) &&
- checkAmountForRecipeFourth(itemStacks, recipeValues) && checkAmountForRecipeFifth(itemStacks, recipeValues)) {
- Material material = returnItemToRecipe(matrialNameFirst, amountOfItemsInventory);
- Material materialSecond = returnItemToRecipe(matrialNameSecond, amountOfItemsInventory);
- Material recipeNameOutput = Material.valueOf(Crafting.valueOf(recipeValues).name());
- outputItemsIfNoSpaceInChest = inventory.addItem(new ItemStack(recipeNameOutput, isAColor(recipeNameOutput) ? (Crafting.valueOf(recipeValues).getNumberOutput() + 2) + addExtraIfSpecialcolor(materialSecond, recipeNameOutput) : Crafting.valueOf(recipeValues).getNumberOutput()));
- if (outputItemsIfNoSpaceInChest.isEmpty()) {
- if (craftedItems.containsKey(this.location) && craftedItems.get(this.location).containsKey(ItemUtily.createItemStackAsOne(new ItemStack(material)))) {
- itemAmont = craftedItems.get(this.location).get(ItemUtily.createItemStackAsOne(new ItemStack(material))).getAmount();
- ItemStack items = craftedItems.get(this.location).get(ItemUtily.createItemStackAsOne(new ItemStack(material)));
- items.clone().setAmount(itemAmont);
- this.itemStacksList.put(ItemUtily.createItemStackAsOne(new ItemStack(material)), items);
- }
- this.itemStacksList.put(ItemUtily.createItemStackAsOne(new ItemStack(material)), ItemUtily.createItemStackWhitAmount(recipeNameOutput, isAColor(recipeNameOutput) ? (Crafting.valueOf(recipeValues).getNumberOutput() + 2) + addExtraIfSpecialcolor(materialSecond, recipeNameOutput) : (Crafting.valueOf(recipeValues).getNumberOutput() + (craftedItems.isEmpty() ? 0 : itemAmont))));
- craftedItems.put(this.location, this.itemStacksList);
- takeItemsRecipeIngrediens(inventory, craftingRecipe, itemStacks, null);
- }
- }
- }
- }
- }
- }
- return outputItemsIfNoSpaceInChest;
- }
- public boolean Minecraftversion(String recipe) {
- if (Bukkit.getServer().getBukkitVersion().equals("1.16.5-R0.1-SNAPSHOT"))
- if (recipe.equals("COBBLED_DEEPSLATE") || recipe.equals("TINTED_GLASS") || recipe.equals("POLISHED_DEEPSLATE") || recipe.equals("POLISHED_DEEPSLATE_SLAB") || recipe.equals("POLISHED_DEEPSLATE_STAIRS") || recipe.equals("POLISHED_DEEPSLATE_WALL") || recipe.equals("COBBLED_DEEPSLATE_SLAB") || recipe.equals("COBBLED_DEEPSLATE_STAIRS") || recipe.equals("COBBLED_DEEPSLATE_WALL")) {
- return true;
- }
- return false;
- }
- public boolean Minecraftversion(String... recipeIngredients) {
- if (Bukkit.getServer().getBukkitVersion().equals("1.16.5-R0.1-SNAPSHOT"))
- for (String ingredient : recipeIngredients)
- if (ingredient != null)
- for (String ingredien : ingredient.split(",")) {
- if (ingredien.equals("COBBLED_DEEPSLATE")) {
- return true;
- }
- }
- return false;
- }
- /**
- * Check if it a color recipe, is for bypass the check if the recipe has more an one matral to use and the
- * items you has added only demand 1 for the recipe, it will dynamicly chose right type after amount items
- * for the recipe.
- * <p>
- * So for example light_gray_dye has two diffrent type of recipe one ony need one item and second need two diffrent item.
- * It will shose that type some fith the cuurent added items.
- *
- * @param craftingRecipe the recipe you whant to check aginst.
- * @param itemListForCrafting the list of items you want to craft too.
- * @param inventoryItems the items inside the inventory
- * @return if the item inside the inventory match the items inside the recipe
- */
- public boolean ifItAreColor(Crafting craftingRecipe, ItemStack itemListForCrafting, Map<Material, ItemStack> inventoryItems) {
- if (craftingRecipe.toString().equals("LIGHT_BLUE_DYE") && itemListForCrafting.getType().toString().equals("LIGHT_BLUE_DYE"))
- for (String recipe : craftingRecipe.getMaterialName1().split(",")) {
- for (ItemStack items : inventoryItems.values()) {
- if (items.getType().toString().equals("BLUE_ORCHID"))
- if (recipe.equals(items.getType().toString())) {
- return true;
- }
- }
- }
- else if (craftingRecipe.toString().equals("LIGHT_GRAY_DYE") && itemListForCrafting.getType().toString().equals("LIGHT_GRAY_DYE"))
- for (String recipe : craftingRecipe.getMaterialName1().split(",")) {
- for (ItemStack items : inventoryItems.values()) {
- if (items.getType().toString().equals("WHITE_TULIP") || items.getType().toString().equals("OXEYE_DAISY") || items.getType().toString().equals("AZURE_BLUET"))
- if (recipe.equals(items.getType().toString())) {
- return true;
- }
- }
- }
- else if (craftingRecipe.toString().equals("MAGENTA_DYE") && itemListForCrafting.getType().toString().equals("MAGENTA_DYE"))
- for (String recipe : craftingRecipe.getMaterialName1().split(",")) {
- for (ItemStack items : inventoryItems.values()) {
- if (items.getType().toString().equals("ALLIUM") || items.getType().toString().equals("LILAC"))
- if (recipe.equals(items.getType().toString())) {
- return true;
- }
- }
- }
- else if (craftingRecipe.toString().equals("PINK_DYE") && itemListForCrafting.getType().toString().equals("PINK_DYE"))
- for (String recipe : craftingRecipe.getMaterialName1().split(",")) {
- for (ItemStack items : inventoryItems.values()) {
- if (items.getType().toString().equals("PINK_TULIP") || items.getType().toString().equals("PEONY"))
- if (recipe.equals(items.getType().toString())) {
- return true;
- }
- }
- }
- else if (craftingRecipe.toString().equals("RED_DYE") && itemListForCrafting.getType().toString().equals("RED_DYE"))
- for (String recipe : craftingRecipe.getMaterialName1().split(",")) {
- for (ItemStack items : inventoryItems.values()) {
- if (items.getType().toString().equals("RED_TULIP") || items.getType().toString().equals("BEETROOT") || items.getType().toString().equals("POPPY") || items.getType().toString().equals("ROSE_BUSH"))
- if (recipe.equals(items.getType().toString())) {
- System.out.println("items " + items);
- return true;
- }
- }
- }
- return false;
- }
- /**
- * Check if it a tall flower
- *
- * @param itemUsecrafting the item you added inside the inventory to check
- * @return true if the item are a tall flower.
- */
- public boolean checkIfItTallFlowers(Material itemUsecrafting) {
- if (itemUsecrafting != null)
- return itemUsecrafting == Material.LILAC || itemUsecrafting == Material.PEONY || itemUsecrafting == Material.ROSE_BUSH;
- return false;
- }
- /**
- * check if colors match if the recipe has more an 2 diffrent recipe ingredens and bypass the check.
- * Is for recipe some has 1,2 and 3 diffreent items, but only added 2 matched items inside the inventory.
- *
- * @param craftingRecipe the recipe you want to check against
- * @param itemListForCrafting the list of items you has added you want to craft.
- * @param inventoryItems the contents of the inventory
- * @return true if the items match the recipe demand.
- */
- public boolean ifItAreColorMatchTwoItems(Crafting craftingRecipe, ItemStack itemListForCrafting, Map<Material, ItemStack> inventoryItems) {
- if (craftingRecipe.toString().equals("MAGENTA_DYE") && itemListForCrafting.getType().toString().equals("MAGENTA_DYE"))
- for (String recipe : craftingRecipe.getMaterialName1().split(",")) {
- for (ItemStack items : inventoryItems.values()) {
- if (items.getType().toString().equals("PINK_DYE") || items.getType().toString().equals("PURPLE_DYE"))
- if (recipe.equals(items.getType().toString())) {
- System.out.println("items are " + items);
- return true;
- }
- }
- }
- return false;
- }
- /**
- * Simpel check if it a color.
- *
- * @param outputItem the item you want to make.
- * @return true if it a color recipe.
- */
- public boolean isAColor(Material outputItem) {
- switch (outputItem.toString()) {
- case "BLACK_DYE":
- case "CYAN_DYE":
- case "LIGHT_BLUE_DYE":
- case "LIGHT_GRAY_DYE":
- case "MAGENTA_DYE":
- case "ORANGE_DYE":
- case "PINK_DYE":
- case "RED_DYE":
- return true;
- default:
- return false;
- }
- }
- /**
- * Check if it a color and add extra item if it is a special color some has more items as output.
- *
- * @param itemToTake the item you has added to the inventory
- * @param outputItem the recipe output.
- * @return a number if it a match or 0 if not.
- */
- public int addExtraIfSpecialcolor(Material itemToTake, Material outputItem) {
- switch (outputItem.toString()) {
- case "LIGHT_GRAY_DYE":
- if (itemToTake == Material.BLACK_DYE)
- return 1;
- case "BLACK_DYE":
- case "CYAN_DYE":
- case "LIGHT_BLUE_DYE":
- case "MAGENTA_DYE":
- if (itemToTake == Material.WHITE_DYE)
- return 1;
- case "ORANGE_DYE":
- case "PINK_DYE":
- case "RED_DYE":
- default:
- return 0;
- }
- }
- public boolean checkIfChestFull() {
- if (checkCraftingRecipeExist().isEmpty()) {
- ifChestFull = !checkCraftingRecipeExist().isEmpty();
- }
- return ifChestFull;
- }
- /**
- * Will set amount of items inside the inventory after the matched recipe ingredens.
- *
- * @param ingrediensMap the items inside the inventory.
- * @param ingrediens the ingrediens you need to make the recipe.
- * @return a map with the items and the amount is set inside the stack
- */
- public Map<Material, ItemStack> setItemstacksAmount(Map<Material, Integer> ingrediensMap, Crafting ingrediens) {
- Map<Material, ItemStack> map = new HashMap<>();
- ItemStack recipeMatrial;
- ItemStack recipeMatrialSecond;
- ItemStack recipeMatrialThird;
- ItemStack recipeMatrialFourth;
- ItemStack recipeMatrialFifth;
- if (returnItemToRecipe(ingrediens.getMaterialName1(), ingrediensMap) != null)
- if (ingrediens.getMaterialName1() != null && isMapContainsRecipeIngrediens(ingrediens.getMaterialName1(), ingrediensMap)) {
- for (Map.Entry<Material, Integer> item : ingrediensMap.entrySet())
- if (isItemsMatch(ingrediens.getMaterialName1(), ItemUtily.createItemStack(item.getKey()))) {
- Material matrialNameFirst = item.getKey();
- int itemAmountSecond = ingrediensMap.get(matrialNameFirst);
- recipeMatrial = new ItemStack(matrialNameFirst, itemAmountSecond);
- map.put(matrialNameFirst, recipeMatrial);
- }
- }
- if (returnItemToRecipe(ingrediens.getMaterialName2(), ingrediensMap) != null)
- if (ingrediens.getMaterialName2() != null && isMapContainsRecipeIngrediens(ingrediens.getMaterialName2(), ingrediensMap)) {
- for (Map.Entry<Material, Integer> item : ingrediensMap.entrySet())
- if (isItemsMatch(ingrediens.getMaterialName2(), ItemUtily.createItemStack(item.getKey()))) {
- Material matrialNameSecond = item.getKey();
- int itemAmountSecond = ingrediensMap.get(matrialNameSecond);
- recipeMatrialSecond = new ItemStack(matrialNameSecond, itemAmountSecond);
- map.put(matrialNameSecond, recipeMatrialSecond);
- }
- }
- if (returnItemToRecipe(ingrediens.getMaterialName3(), ingrediensMap) != null)
- if (ingrediens.getMaterialName3() != null && isMapContainsRecipeIngrediens(ingrediens.getMaterialName3(), ingrediensMap)) {
- Material matrialNameThird = returnItemToRecipe(ingrediens.getMaterialName3(), ingrediensMap);
- int itemAmountThird = ingrediensMap.get(matrialNameThird);
- recipeMatrialThird = new ItemStack(matrialNameThird, itemAmountThird);
- map.put(matrialNameThird, recipeMatrialThird);
- }
- if (returnItemToRecipe(ingrediens.getMaterialName4(), ingrediensMap) != null)
- if (ingrediens.getMaterialName4() != null && isMapContainsRecipeIngrediens(ingrediens.getMaterialName4(), ingrediensMap)) {
- Material matrialNameFourth = returnItemToRecipe(ingrediens.getMaterialName4(), ingrediensMap);
- int itemAmountFourth = ingrediensMap.get(matrialNameFourth);
- recipeMatrialFourth = new ItemStack(matrialNameFourth, itemAmountFourth);
- map.put(matrialNameFourth, recipeMatrialFourth);
- }
- if (returnItemToRecipe(ingrediens.getMaterialName5(), ingrediensMap) != null)
- if (ingrediens.getMaterialName5() != null && isMapContainsRecipeIngrediens(ingrediens.getMaterialName5(), ingrediensMap)) {
- Material matrialNameFifth = returnItemToRecipe(ingrediens.getMaterialName5(), ingrediensMap);
- int itemAmountFifth = ingrediensMap.get(matrialNameFifth);
- recipeMatrialFifth = new ItemStack(matrialNameFifth, itemAmountFifth);
- map.put(matrialNameFifth, recipeMatrialFifth);
- }
- return map;
- }
- /**
- * Count items inside the inventory, some match the recipe ingredens.
- * It will compere the recipe you want to makw with the list of recipe
- * some exist in Crafting.
- *
- * @param recipes you want to match against.
- * @param theRecipeYouWhants the recipe you want to match.
- * @param inventory the inventory you match the items from.
- * @return a map with the Matrial and amount of this type inside the inventory.
- */
- public Map<Material, Integer> getItemsAmount(Crafting recipes, ItemStack theRecipeYouWhants, Inventory inventory) {
- Map<Material, Integer> map = new HashMap<>();
- int amountFirst = 0;
- int amountSecond = 0;
- int amountThird = 0;
- int amountFourt = 0;
- int amountFifth = 0;
- for (ItemStack itemStacks : inventory) {
- if (theRecipeYouWhants.getType().toString().equals(recipes.toString()) && itemStacks != null) {
- if (isItemsMatch(recipes.getMaterialName1(), itemStacks)) {
- amountFirst += itemStacks.getAmount();
- map.put(itemStacks.getType(), ItemUtily.countItemStacks(itemStacks, inventory));
- } else if (isItemsMatch(recipes.getMaterialName2(), itemStacks)) {
- amountSecond += itemStacks.getAmount();
- map.put(itemStacks.getType(), ItemUtily.countItemStacks(itemStacks, inventory));
- } else if (isItemsMatch(recipes.getMaterialName3(), itemStacks)) {
- amountThird += itemStacks.getAmount();
- map.put(itemStacks.getType(), ItemUtily.countItemStacks(itemStacks, inventory));
- } else if (isItemsMatch(recipes.getMaterialName4(), itemStacks)) {
- amountFourt += itemStacks.getAmount();
- map.put(itemStacks.getType(), ItemUtily.countItemStacks(itemStacks, inventory));
- } else if (isItemsMatch(recipes.getMaterialName5(), itemStacks)) {
- amountFifth += itemStacks.getAmount();
- map.put(itemStacks.getType(), ItemUtily.countItemStacks(itemStacks, inventory));
- }
- }
- }
- return map;
- }
- /**
- * Remove the items inside the inventory if it exist inbside the inventory
- *
- * @param inventory the inventory the items shall be removed from.
- * @param itemStack the item you want to remove.
- */
- public void removeItems(Inventory inventory, ItemStack itemStack) {
- for (int i = 0; i < inventory.getSize(); i++) {
- ItemStack itemStacks = inventory.getContents()[i];
- if (itemStacks != null && itemStacks.isSimilar(itemStack)) {
- inventory.setItem(i, new ItemStack(Material.AIR));
- }
- }
- }
- /**
- * Check to remove right item from the inventory and amount it shall be removed.
- * It will add new amount inside the inventory.
- *
- * @param inventory some you remove and add items too.
- * @param recipes the craftingrecipes it vill check items to remove against.
- * @param itemStacks the itemstacks some the inventory contains and amount.
- * @param itemToTake the item you want to check from the inventory match some conditons
- * (in this case if it a color, soo it to remove right amount).
- */
- public void takeItemsRecipeIngrediens(Inventory inventory, Crafting recipes, Map<Material, ItemStack> itemStacks, Material itemToTake) {
- Set<ItemStack> ifItHasRemovedSameItem = new HashSet<>();
- for (Map.Entry<Material, ItemStack> item : itemStacks.entrySet()) {
- ItemStack itemStack = item.getValue();
- if (itemStack != null && isItemsMatch(recipes.getMaterialName1(), itemStack)) {
- if (itemStack.getAmount() >= recipes.getAmontNeededCrafting2() && !ifItHasRemovedSameItem.isEmpty()) {
- if (ifItHasRemovedSameItem.size() == ItemUtily.countItemStacks(inventory, itemStack))
- ifItHasRemovedSameItem.clear();
- ifItHasRemovedSameItem.add(itemStack);
- continue;
- }
- removeItems(inventory, itemStack);
- itemStack.setAmount(checkAdvanceColorRecipes(recipes.toString(), itemToTake) ? itemStack.getAmount() - (recipes.getAmontNeededCrafting1() + 1) : itemStack.getAmount() - recipes.getAmontNeededCrafting1());
- inventory.addItem(itemStack);
- ifItHasRemovedSameItem.add(itemStack);
- } else if (itemStack != null && isItemsMatch(recipes.getMaterialName2(), itemStack)) {
- if (checkAdvanceColorRecipes(recipes.toString(), itemToTake) && itemStack.getType() == Material.PINK_DYE)
- continue;
- if (!checkAdvanceColorRecipes(recipes.toString(), itemToTake) && itemStack.getType() == Material.WHITE_DYE)
- continue;
- removeItems(inventory, itemStack);
- itemStack.setAmount(itemStack.getAmount() - recipes.getAmontNeededCrafting2());
- inventory.addItem(itemStack);
- } else if (itemStack != null && isItemsMatch(recipes.getMaterialName3(), itemStack)) {
- removeItems(inventory, itemStack);
- itemStack.setAmount(checkAdvanceColorRecipes(recipes.toString(), itemToTake) ? itemStack.getAmount() - (recipes.getAmontNeededCrafting3() + 1) : itemStack.getAmount() - recipes.getAmontNeededCrafting3());
- inventory.addItem(itemStack);
- } else if (itemStack != null && isItemsMatch(recipes.getMaterialName4(), itemStack)) {
- removeItems(inventory, itemStack);
- itemStack.setAmount(itemStack.getAmount() - recipes.getAmontNeededCrafting4());
- inventory.addItem(itemStack);
- } else if (itemStack != null && isItemsMatch(recipes.getMaterialName5(), itemStack)) {
- removeItems(inventory, itemStack);
- itemStack.setAmount(itemStack.getAmount() - recipes.getAmontNeededCrafting5());
- inventory.addItem(itemStack);
- }
- }
- }
- /**
- * check if it a special color and return true if it is.
- *
- * @param recipeOutput the recipe it curently try to make
- * @param itemUsedInrecipe the item you want to check if the recipe contains the item.
- * @return true if it match a special color.
- */
- private boolean checkAdvanceColorRecipes(String recipeOutput, Material itemUsedInrecipe) {
- if (itemUsedInrecipe != null)
- if (recipeOutput.equals("LIGHT_GRAY_DYE") && itemUsedInrecipe.toString().equals("BLACK_DYE"))
- return true;
- else if (recipeOutput.equals("MAGENTA_DYE") && (itemUsedInrecipe.toString().equals("WHITE_DYE") || itemUsedInrecipe.toString().equals("BONE_MEAL")))
- return true;
- return false;
- }
- /**
- * Check if a item match the item demanded for the recipe.
- *
- * @param ingridients from the recipe
- * @param itemYoucraft the items you want to check if it match the curent recipe
- * @return true if the items match the curent recipe.
- */
- public boolean isItemsMatch(String ingridients, ItemStack itemYoucraft) {
- if (ingridients != null && itemYoucraft != null && ingridients.contains(","))
- for (String recipe : ingridients.split(",")) {
- if (recipe.equals(itemYoucraft.getType().toString())) {
- return true;
- }
- }
- else if (ingridients != null && itemYoucraft != null && !ingridients.contains(",")) {
- return ingridients.equals(itemYoucraft.getType().toString());
- }
- return false;
- }
- /**
- * Check recipe ingrediens to the items inside the inventory.
- *
- * @param ingrediens the recipe ingridens.
- * @param map the contents of your inventory.
- * @return a item if it match or null if it not find any match.
- */
- public Material returnItemToRecipe(String ingrediens, Map<Material, Integer> map) {
- for (Map.Entry<Material, Integer> itemStack : map.entrySet())
- if (ingrediens != null && itemStack != null && ingrediens.contains(","))
- for (String recipe : ingrediens.split(",")) {
- if (recipe.equals(itemStack.getKey().toString())) {
- return itemStack.getKey();
- }
- }
- else if (ingrediens != null && !ingrediens.contains(","))
- return Material.valueOf(ingrediens);
- return null;
- }
- /**
- * Check if the map contains the needed recipe ingrediens.
- *
- * @param ingrediens the curent recipe ingredins to check.
- * @param map the inventory contents
- * @return true if the item match recipe ingredien.
- */
- public boolean isMapContainsRecipeIngrediens(String ingrediens, Map<Material, Integer> map) {
- for (Map.Entry<Material, Integer> itemStack : map.entrySet()) {
- if (ingrediens != null && itemStack != null && ingrediens.contains(","))
- for (String recipe : ingrediens.split(",")) {
- if (recipe.equals(itemStack.getKey().toString())) {
- return true;
- }
- }
- else if (ingrediens != null && itemStack != null && !ingrediens.contains(",") && ingrediens.equals(itemStack.getKey().toString()))
- return true;
- }
- return false;
- }
- /**
- * Some recipe has several diffrent items you can choose from, this will split up the recipe ingrediens
- * and check every one aginst the item inside the inventory and return a item if it match.
- *
- * @param itemFromInventory the item inside the inventory
- * @param ingrediensFromRecipe the recipe ingridens some it deamand
- * @return the matching item if it exist or return null if it not a match.
- */
- public Material splitIngrediens(Material itemFromInventory, String ingrediensFromRecipe) {
- if (itemFromInventory != null && ingrediensFromRecipe != null && ingrediensFromRecipe.contains(","))
- for (String ingredien : ingrediensFromRecipe.split(","))
- if (Material.valueOf(ingredien) == itemFromInventory) {
- return itemFromInventory;
- }
- if (ingrediensFromRecipe != null && !ingrediensFromRecipe.contains(","))
- return Material.valueOf(ingrediensFromRecipe);
- return null;
- }
- /**
- * Check the amount of item one type or if recipe has several diffrent matrial it can use
- * it include even this from the inventory.
- * <p>
- * Like for example planks has several wood types, but you can use several diffrent type´s to craft
- * an item.
- *
- * @param itemsInsideInventory the items you have inside the inventory
- * @param craftingRecipes the recipe ingridient you want to match aginst.
- * @param itemUsedToCrafting the item some you want to check (in this case I use it fot colors some has diffrent recipe)
- * @return true if it equal or more items inside the inventory.
- */
- public boolean checkAmountForRecipeFirst(Map<Material, ItemStack> itemsInsideInventory, String craftingRecipes, Material... itemUsedToCrafting) {
- int itemAmount = 0;
- boolean matchedItem = false;
- if (itemUsedToCrafting != null)
- for (Material items : itemUsedToCrafting)
- if (items == Material.BLACK_DYE) {
- matchedItem = true;
- break;
- }
- if (itemsInsideInventory != null && craftingRecipes != null)
- for (Map.Entry<Material, ItemStack> item : itemsInsideInventory.entrySet()) {
- if (item.getValue().getType() == splitIngrediens(item.getValue().getType(), Crafting.valueOf(craftingRecipes).getMaterialName1())) {
- itemAmount += item.getValue().getAmount();
- if (itemAmount >= ((craftingRecipes.equals("LIGHT_GRAY_DYE") && matchedItem) ?
- Crafting.valueOf(craftingRecipes).getAmontNeededCrafting1() + 1 : Crafting.valueOf(craftingRecipes).getAmontNeededCrafting1())) {
- return true;
- }
- }
- }
- return false;
- }
- /**
- * Check the amount of item one type or if recipe has several diffrent matrial it can use
- * it include even this from the inventory.
- * <p>
- * Like for example planks has several wood types, but you can use several diffrent type´s to craft
- * an item.
- *
- * @param itemsInsideInventory the items you have inside the inventory
- * @param craftingRecipes the recipe ingridient you want to match aginst.
- * @param itemUsedToCrafting the item some you want to check (in this case I use it fot colors some has diffrent recipe)
- * @return true if it equal or more items inside the inventory.
- */
- public boolean checkAmountForRecipeSecond(Map<Material, ItemStack> itemsInsideInventory, String craftingRecipes, Material... itemUsedToCrafting) {
- int itemSecondAmount = 0;
- boolean matchedItem = false;
- if (itemUsedToCrafting != null)
- for (Material items : itemUsedToCrafting)
- if (items == Material.MAGENTA_DYE) {
- matchedItem = true;
- break;
- }
- if (itemsInsideInventory != null && craftingRecipes != null)
- for (Map.Entry<Material, ItemStack> item : itemsInsideInventory.entrySet()) {
- if (item.getValue().getType() == splitIngrediens(item.getValue().getType(), Crafting.valueOf(craftingRecipes).getMaterialName2())) {
- itemSecondAmount += item.getValue().getAmount();
- if (itemSecondAmount >= Crafting.valueOf(craftingRecipes).getAmontNeededCrafting2())
- return true;
- }
- }
- return false;
- }
- /**
- * Check the amount of item one type or if recipe has several diffrent matrial it can use
- * it include even this from the inventory.
- * <p>
- * Like for example planks has several wood types, but you can use several diffrent type´s to craft
- * an item.
- *
- * @param itemsInsideInventory the items you have inside the inventory
- * @param craftingRecipes the recipe ingridient you want to match aginst.
- * @param itemUsedToCrafting the item some you want to check (in this case I use it fot colors some has diffrent recipe)
- * @return true if it equal or more items inside the inventory.
- */
- public boolean checkAmountForRecipeThird(Map<Material, ItemStack> itemsInsideInventory, String craftingRecipes, Material... itemUsedToCrafting) {
- int itemAmount = 0;
- boolean matchedItem = false;
- if (itemUsedToCrafting != null)
- for (Material items : itemUsedToCrafting)
- if (items == Material.WHITE_DYE || items == Material.BONE_MEAL) {
- matchedItem = true;
- break;
- }
- if (itemsInsideInventory != null && craftingRecipes != null)
- for (Map.Entry<Material, ItemStack> item : itemsInsideInventory.entrySet()) {
- if (item.getValue().getType() == splitIngrediens(item.getValue().getType(), Crafting.valueOf(craftingRecipes).getMaterialName3())) {
- itemAmount += item.getValue().getAmount();
- if (itemAmount >= ((craftingRecipes.equals("MAGENTA_DYE") && matchedItem) ?
- Crafting.valueOf(craftingRecipes).getAmontNeededCrafting1() + 1 : Crafting.valueOf(craftingRecipes).getAmontNeededCrafting3()))
- return true;
- }
- }
- return false;
- }
- /**
- * Check the amount of item one type or if recipe has several diffrent matrial it can use
- * it include even this from the inventory.
- * <p>
- * Like for example planks has several wood types, but you can use several diffrent type´s to craft
- * an item.
- *
- * @param itemsInsideInventory the items you have inside the inventory
- * @param craftingRecipes the recipe ingridient you want to match aginst.
- * @param itemUsedToCrafting the item some you want to check (in this case I use it fot colors some has diffrent recipe)
- * @return true if it equal or more items inside the inventory.
- */
- public boolean checkAmountForRecipeFourth(Map<Material, ItemStack> itemsInsideInventory, String craftingRecipes, Material... itemUsedToCrafting) {
- int itemAmount = 0;
- boolean matchedItem = false;
- if (itemUsedToCrafting != null)
- for (Material items : itemUsedToCrafting)
- if (items == Material.WHITE_DYE || items == Material.BONE_MEAL) {
- matchedItem = true;
- break;
- }
- if (itemsInsideInventory != null && craftingRecipes != null)
- for (Map.Entry<Material, ItemStack> item : itemsInsideInventory.entrySet()) {
- if (item.getValue().getType() == splitIngrediens(item.getValue().getType(), Crafting.valueOf(craftingRecipes).getMaterialName4())) {
- itemAmount += item.getValue().getAmount();
- if (itemAmount >= Crafting.valueOf(craftingRecipes).getAmontNeededCrafting4())
- return true;
- }
- }
- return false;
- }
- /**
- * Check the amount of item one type or if recipe has several diffrent matrial it can use
- * it include even this from the inventory.
- * <p>
- * Like for example planks has several wood types, but you can use several diffrent type´s to craft
- * an item.
- *
- * @param itemsInsideInventory the items you have inside the inventory
- * @param craftingRecipes the recipe ingridient you want to match aginst.
- * @param itemUsedToCrafting the item some you want to check (in this case I use it fot colors some has diffrent recipe)
- * @return true if it equal or more items inside the inventory.
- */
- public boolean checkAmountForRecipeFifth(Map<Material, ItemStack> itemsInsideInventory, String craftingRecipes, Material... itemUsedToCrafting) {
- int itemAmount = 0;
- boolean matchedItem = false;
- if (itemUsedToCrafting != null)
- for (Material items : itemUsedToCrafting)
- if (items == Material.WHITE_DYE || items == Material.BONE_MEAL) {
- matchedItem = true;
- break;
- }
- if (itemsInsideInventory != null && craftingRecipes != null)
- for (Map.Entry<Material, ItemStack> item : itemsInsideInventory.entrySet()) {
- if (item.getValue().getType() == splitIngrediens(item.getValue().getType(), Crafting.valueOf(craftingRecipes).getMaterialName5())) {
- itemAmount += item.getValue().getAmount();
- if (itemAmount >= Crafting.valueOf(craftingRecipes).getAmontNeededCrafting5())
- return true;
- }
- }
- return false;
- }
- /**
- * Get the contents of a specific location and see what was crafted.
- *
- * @param location of the container some crafted the items.
- * @return Itemstack of the crafted items.
- */
- public Map<ItemStack, ItemStack> getCraftedItems(Location location) {
- return craftedItems.get(location);
- }
- /**
- * Get all the contents crafted on all locations.
- *
- * @return The map contents.
- */
- public Map<Location, Map<ItemStack, ItemStack>> getCraftedItems() {
- return craftedItems;
- }
- /**
- * Remove crafted items on one location.
- *
- * @param location of the container some crafted the items.
- */
- public void removeCraftedItems(Location location) {
- craftedItems.remove(location);
- }
- }
Add Comment
Please, Sign In to add comment