Elrol_Arrowsend Posted February 1, 2016 Posted February 1, 2016 Hello, I am making a block that once you open its GUI it checks the players inventory for multiple itemstacks, and if the player has the item stacks and the minimum quantity of the items the GUI's button for a specific item is enabled to allow the player to click the button taking these items and returning the item to the players inventory. I have managed to get this to work with only one item stack, and when I was adding 2 item stacks I had to add another for loop to check all of the players itemStacks that are not null to see if they had both of the stacks. now I am trying to make a single method to check for if the player has all the items in a recipe which can have more then just 2 or 3 different items, is there a good way to check a players inventory for items and quantity, I know that the player.inventory.hasItem(item); will only return true if the player has the item, not checking for if the player has a minimum requirement of that item, even the player.inventory.hasItemStack(itemStack) which I thought would be what I wanted only checks for the items. I am using packets to check for the players inventory. if this is difficult to understand then think about it like I was trying to make the forge from Skyrim in minecraft. it has a similar feature, if the player has all the required items it will be lit up and allow you to craft it. Quote
coolAlias Posted February 1, 2016 Posted February 1, 2016 No getting around a loop - easiest way to make sure it handles quantity is to write your own #hasItemStack method. Quote http://i.imgur.com/NdrFdld.png[/img]
Elrol_Arrowsend Posted February 1, 2016 Author Posted February 1, 2016 that is what I had done yet my attempts to make one have failed time and time again, I have tried this: ItemStack[] recipe = new ItemStack[]{new ItemStack(Items.apple, 2), new ItemStack(Items.leather, 3)}; for(int slots=0; slots < player.inventory.mainInventory.length; slots++){ for(int ingredients = 0; < recipe.length; recipe++){ if(player.inventory.mainInventory[slots] != null && player.inventory.mainInventory[slots].getItem().equals(recipe[ingredients].getItem()) && player.inventory.mainInventory[slots].stacksize >= recipe[ingredients].stacksize){ activateButton(): } } but it returns per item I cant get it to check if the player has x y and z before returning true, I have been working on this bit of code for like 2 days now, today I got the button to activate if the player had one item and the correct amount of that item, and if the recipes contained the same amount of items then I would just make multiple for loops one per item nestled inside of each other but the number of items per recipe change so that would be rather unreliable Quote
Failender Posted February 1, 2016 Posted February 1, 2016 int[] itemCounts = new int[recipe.length] for(i<recipe.length) { for(j<inventoryCount) { if(recipe[i].getItem()==inventory.get(j)) itemCounts[i]+=inventory[j].stacksize; } } bool flag=true for(i<recipce.lenght) { if(!(itemCounts[i]>=recipe[i].stacksize)) { flag = false; break; } } if(flag) doYourStuff(); Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.