Jump to content

[1.7.10] Having a GUI enable buttons depending on items in a players inventory


Recommended Posts

Posted

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.

Posted

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

Posted

 

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();



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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.