My own crafting table: Checking for correct recipe!

Discussion in 'Plugin Development' started by Blockhead7360, Aug 11, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    Blockhead7360

    Hello! I am having some trouble.

    I am making my own crafting table (obviously it isn't an actual block, because you can't add custom blocks in regular Minecraft without a mod). This crafting table would open when I right click on a certain block, but that's not the point.

    Here is the code, and below this is my explanation:

    Code:
    List<ItemStack[]> shape = getInDiff(inv);
    
            boolean extra = false;
            boolean correct = false;
            for (CraftingRecipe cr : recipes){
                int line = 0;
                for (String s1 : cr.getCreator().getShape()){
                    int letter = 0;
                    for (int s2 = 0; s2 < s1.length(); s2++){
                        if (s1.charAt(s2) == ' ' && shape.get(line)[letter].getType() != Material.AIR){
                            extra = true;
                            break;
                        }
                        if (cr.getCreator().getIngredients().get(s1.charAt(s2)).equals(shape.get(line)[letter])){
                            extra = true;
                            break;
                        }
                        letter++;
                      
                    }
                    if (extra){
                        extra = false;
                        break;
                    }
                    line++;
                }
              
              
              
            }
    I am trying to check to see if the recipe provided ("shape" variable) is equal to the correct recipe ("cr.getCreator()").

    I have a custom class called CraftingRecipe, with one of the methods #getCreator().getShape(). This returns a String[] array with the crafting recipe stored inside. This recipe is the correct recipe. Basically, the array has 3 values, each with 3 letters of a string. If you know how a shaped recipe works, then you know what I did here. Each value of the array is a line, and each letter of each line represents an item. I get the ItemStack out of this by doing #getCreator().getIngredients().get(CHAR). This returns an ItemStack. As you can see in the loop, it loops through each recipe that exists, then loops through each line of the recipe, and then loops through each letter to get the CHAR. The character is then used in the getIngredients HashMap<Character, ItemStack> to get the ItemStack that is supposed to be in that place.

    The List<ItemStack[]> is basically the same thing, but the reason it is formatted this way is because I didn't want to set it to a String and then back to an ItemStack, but I could if you wanted me to. Basically, each ItemStack[] in the ArrayList is a line, and each ItemStack in the ItemStack array is a specific ItemStack, basically formatted the same way as the #getCreator(). This is why I used line++ and letter++.

    Anyways, here is the problem:
    I've done all this, but what do I do from here? How do I get it to check to see if the recipe was correct?

    I know my code might be kind of messy, and I'm open to all suggestions that go towards me making more neater code (as this may help me now and in the future). But mainly, I need to know check if the recipe was a match.

    Thanks! Please help as soon as you can!

    - Blockhead7360

    EDIT: Also, you probably see that I have that "correct" boolean that is not being used. I just put that there in case I could use that in anything.
     
  2. Offline

    Zombie_Striker

    @Blockhead7360
    If you have an ordered system (0 = top left, 9 = bottom right), then you compare if the item with index N for one array is the same as the item with index N at the other array. Since you want to be able to stack items (amounts do not matter), you will have to manually check the type, name, lore, ect to see if the itemstacks are the same. If you want code already written for you, I have made this Util you can use/ base your code off of.
    https://bukkit.org/threads/creating-recipes-with-specific-ingredients-itemstacks.427709/
     
  3. that would be 10 slots. Should be 0-8 ;)
     
    Zombie_Striker likes this.
  4. Offline

    Blockhead7360

    @Zombie_Striker ahh I see what you mean. I have one loop to transform the shape and get ingredient into a list of itemstacks and then I can simply loop over that to check if it's equal! Thanks, I'll mark this as solved in a few mins.

    Also, I think itemstack.equals does the same thing doesn't it? I looked into the source code a while back and it just returns true after looping through the type, durability, and such.


    Sent from my iPhone using Tapatalk
     
  5. Offline

    Zombie_Striker

    @Blockhead7360
    Itemstack#equals compares if every variable is the same. If you want the ability to put in any amount, or have any other exceptions, then you will need to compare the individual values.
     
  6. Offline

    Blockhead7360

    @Zombie_Striker

    I think if you do ItemStack#isSimilar, it doesn't compare item amounts. I think I may use that as I want everything else to be the same, such as durability, meta, and more


    Sent from my iPhone using Tapatalk
     
  7. Offline

    Zombie_Striker

    @Blockhead7360
    isSimular does not check item meta though. You will have to check the display names/lore separately.
     
Thread Status:
Not open for further replies.

Share This Page