Getting craftable items based on a player's inventory

Discussion in 'Plugin Development' started by TechTeller96, Apr 10, 2014.

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

    TechTeller96

    Hello,
    I've been trying to figure out for the past few days, how to get a List of items that can be crafted by a player based on his inventory, i.e if he has the correct amount and resources for that.

    My problem is not that though, my problem arises when I try to CHECK if the player has enough items. I get the itemstacks in his inventory, reduce them to size 1, and check if he has said items. Now this works fine for most items except stuff like buttons (wood and stone) that need you to have 2 of the same item, but since Im reducing his inventory's contents to 1, buttons dont pop up in the canCraft List.

    Is there anyway to compare itemstacks while ignoring the amount of each of them and checking only the material?
     
  2. Offline

    NonameSL

    Code:java
    1. ItemStack is;
    2. ItemStack otherIs;
    3. is.isSimilar(otherIs);

    That will compare the itemstacks, ignoring the amount.
     
  3. Offline

    TechTeller96

    Alright, let me ask a different question.

    How would I get all the blocks (into a List<ItemStack>) that can be crafted by a player if he has all the required items in his inventory? For example:

    Player's inventory: 2 wooden logs

    canCraft List: Sign, Sticks, Wooden buttons, wooden tools, wooden planks, chests, (anything that requires wood or items made by wood BUT not any other items)
     
  4. TechTeller96 I guess you could loop over all the recipes and then check if they can make it and, if they can, add it to the list... But that definitely sounds like a bad way to do it, but might be the only way.
     
  5. Offline

    TechTeller96

    I figured that out, the actual "checking if they can make it" is what's got me stumped.

    Code:java
    1. public void populateInventory()
    2. {
    3. ItemStack[] a = p.getInventory().getContents();
    4.  
    5. Iterator<Recipe> iterator = Bukkit.recipeIterator();
    6. while(iterator.hasNext())
    7. {
    8. Recipe r = iterator.next();
    9. if(!(r instanceof FurnaceRecipe) && containsAll(p.getInventory(), r))
    10. {
    11. canCraft.add(r.getResult());
    12. }
    13. }
    14. Bukkit.broadcastMessage(canCraft.toString());
    15.  
    16. for(int cPage=0;cPage<10;cPage++)
    17. {
    18. addItems(cPage, canCraft);
    19. }
    20. }


    Method: containsAll():
    Code:java
    1. private boolean containsAll(Inventory inv, Recipe r)
    2. {
    3. ItemStack[] items = inv.getContents();
    4.  
    5. if(Arrays.asList(items).containsAll(getItemsFromRecipe(r)))
    6. return true;
    7. return false;
    8. }
     
  6. TechTeller96 Oh I see. Then I'd recommend something like this:

    - Make a HashMap of <Material, Integer>
    - For each of the ingredients, then check if the HashMap contains that Material. If it does, increment the value by one. If not, add it to the Map with value of 1.
    - After you've done that for all ingredients, use the containsAtLeast() method in Inventory to check they have everything in the Map with the right amount.

    That way you'd end up doing something like

    Code:
    inv.containsAtLeast(new ItemStack(Material.WOOD_PLANK), 2);
    For a button.
     
  7. Offline

    TechTeller96

    Right, that sounds good. What if it were a bit more complicated though?

    Say, I wanted to check for a wooden pick. That needs 3 planks and 2 sticks. 4 sticks are made from 2 planks, so thats 5 planks, which is 2 wooden logs rounded up.
     
  8. TechTeller96 I guess some sort of logic that just looks up the recipes for the ingredients (if they don't have the ingredients themselves) until it breaks down to raw materials. Again, this does seem like quite a task for something that sounds relatively simple...

    Come to think of it, I believe I've seen something similar to this, although I couldn't tell you where.
     
  9. Offline

    TechTeller96

    http://tekkitwiki.com/wiki/Crafting_Table_II
    ;)
     
  10. Offline

    TechTeller96

    Bump - any other suggestions?
     
Thread Status:
Not open for further replies.

Share This Page