Jump to content

Fetch - Inventory Items List [ NOOB HELP ]


Recommended Posts

Posted (edited)

                                 [Fixed]

Hello guys, I were coding an ( Inventory Items List Fetcher ) system to have a list of my inventory items in the log.

It shows the items list  but when i tried adding a quantity next to it, it just type x1 next to every item name as it check the slot quantity, So for example if i have 4 logs it will just say -> logs x1 4 times instead of x4

 

 

2h5txjn.png

here is my code: 

	public void onStart() throws InterruptedException, NullPointerException {

		Item[] k = getInventory().getItems();
		int i = 0;
		while (i < 28) {
			if (k[i] != null) {
				log(k[i].getName() + " x" + k[i].getAmount());

			}
			i++;
		}

	}

I would really appreciate is someone helped me with this as i couldnt figure it out, Spent more then 3 hours with no luck and couldnt fund an tutorial of it even i have checked the api system with no result

 

All comments, suggestions and ideas are allowed and is much appreciated.

 

Best regards, Mr Pro Pop.

Thanks in advance.

Edited by Mr Pro Pop
Posted (edited)

If you want to fix the code as you have it, change the logger line to:

log(k[i].getName() + " x" + inventory.getAmount(k[i]);

Because right now you're creating a list of items independent of the inventory items - all of the items are going to have a default amount of 1.

 

Also, just a general suggestion - it would be much simpler to do this as a 'for each' loop than a while loop. While loops and for loops have different designed purposes, and I'd recommend that you read up on it if you want to start creating scripts.

for(Item i: inventory.getItems()) {
     if(i != null) {
         log(i.getName() + " x" + inventory.getAmount(i));
     }
}

Note that this will also repeat items that are inside of your inventory, though. If you want to prevent that, your original method (combined with inventory.getAmount()) of solvency might be better in terms of seeing if it was already stated, possibly by checking the first named index of the item.

Edited by Imateamcape
Posted (edited)

This is how you would want to do it. The reason for it is because getInventory().getItems() returns a list of each individual item.

This means that doing getAmount on each Item will print only 1 or whatever the count of that SPECIFIC item is. Not only that, but looping through through getItems() will go through each instance of an item rather than each different type of item.

 

So you store what items you have already done in an Array then when looping through getItems, check if you have already done that item, if you haven't then print it and add it to the Array of items you have done.

 

 Here is working code:

List<String> itemsAlreadyLogged = new ArrayList<String>(); // Array that contains all the items already logged
    	for(Item i : getInventory().getItems())
    	{
    		if(itemsAlreadyLogged != null && i != null)
    			if(!itemsAlreadyLogged.contains(i.getName())) // If the item is not in the array of items already logged 
        		{
        			log("Item: " + i.getName() + " Amount: " + getInventory().getAmount(i.getName()));
        			itemsAlreadyLogged.add(i.getName());
        		}
    	}
Edited by venetox
  • Like 1
Posted

 

This is how you would want to do it. The reason for it is because getInventory().getItems() returns a list of each individual item.

This means that doing getAmount on each Item will print only 1 or whatever the count of that SPECIFIC item is. Not only that, but looping through through getItems() will go through each instance of an item rather than each different type of item.

 

So you store what items you have already done in an Array then when looping through getItems, check if you have already done that item, if you haven't then print it and add it to the Array of items you have done.

 

 Here is working code:

List<String> itemsAlreadyLogged = new ArrayList<String>(); // Array that contains all the items already logged
    	for(Item i : getInventory().getItems())
    	{
    		if(itemsAlreadyLogged != null && i != null)
    			if(!itemsAlreadyLogged.contains(i.getName())) // If the item is not in the array of items already logged 
        		{
        			log("Item: " + i.getName() + " Amount: " + getInventory().getAmount(i.getName()));
        			itemsAlreadyLogged.add(i.getName());
        		}
    	}

Thanks bro, it works fine with me with no errors, I really appreciate.

Even tanks for the deep explanation of how it works and what to do plus the notes you have added, You really helped me.

Best regards, Mr Pro Pop!

Posted

Thanks bro, it works fine with me with no errors, I really appreciate.

Even tanks for the deep explanation of how it works and what to do plus the notes you have added, You really helped me.

Best regards, Mr Pro Pop!

 

No problem! I made sure to explain the logic behind it rather giving you just the code because otherwise it can be hard to understand it when new to programming!

Good luck with all your future scripts and stuff!

Posted

 

This is how you would want to do it. The reason for it is because getInventory().getItems() returns a list of each individual item.

This means that doing getAmount on each Item will print only 1 or whatever the count of that SPECIFIC item is. Not only that, but looping through through getItems() will go through each instance of an item rather than each different type of item.

 

So you store what items you have already done in an Array then when looping through getItems, check if you have already done that item, if you haven't then print it and add it to the Array of items you have done.

 

 Here is working code:

List<String> itemsAlreadyLogged = new ArrayList<String>(); // Array that contains all the items already logged
    	for(Item i : getInventory().getItems())
    	{
    		if(itemsAlreadyLogged != null && i != null)
    			if(!itemsAlreadyLogged.contains(i.getName())) // If the item is not in the array of items already logged 
        		{
        			log("Item: " + i.getName() + " Amount: " + getInventory().getAmount(i.getName()));
        			itemsAlreadyLogged.add(i.getName());
        		}
    	}

 

 

Thanks bro, it works fine with me with no errors, I really appreciate.

Even tanks for the deep explanation of how it works and what to do plus the notes you have added, You really helped me.

Best regards, Mr Pro Pop!

 

 

Or you could do something like:

Arrays.stream(getInventory().getItems())
      .filter(Objects::nonNull)
      .distinct()
      .map(item -> String.format("Item: %s Amount: %d",
                                 item.getName(),
                                 getInventory().getAmount(item.getName())))
      .forEach(this::log);
Posted

 

Or you could do something like:

Arrays.stream(getInventory().getItems())
      .filter(Objects::nonNull)
      .distinct()
      .map(item -> String.format("Item: %s Amount: %d",
                                 item.getName(),
                                 getInventory().getAmount(item.getName())))
      .forEach(this::log);

I'm not particularly experienced with the stuff Java has so I didn't even know that was possible although I can see what itpr was doing. A nooby probably wouldnt have understood it too which is why I tried to stick to something basic also.

I really like that way though. I guess that just shows how much knowing the languages specific functions etc can make your code much better. I have been programmign in C# pretty much exclusively although I learnt programming in Java so I normally would go through things like that with Lambda expressions in c# but still don't really know how they work in Java.

Looks like I learnt something knew! Cheers.

Posted

Aha thanks explv, The code you provided is little bit hard for me to understand but i much appreciate for helping bro!

Arrays.stream(getInventory().getItems())
      .filter(Objects::nonNull)
      .distinct()
      .map(item -> String.format("Item: %s Amount: %d",
                                 item.getName(),
                                 getInventory().getAmount(item.getName())))
      .forEach(this::log);

The above is equivalent to:

// Get all inventory items as an Item[]
Item[] inventoryItems = getInventory().getItems();
        
// Create a Stream of Items from the Item[]
Stream<Item> itemStream = Stream.of(inventoryItems);

// Remove any Item from the Stream that is null
Stream<Item> itemStreamNoNull = itemStream.filter(item -> item != null);

// Get only the unique Items from the Stream
Stream<Item> uniqueItems = itemStreamNoNull.distinct();

// Convert each Item to a String containing the item name & amount
Stream<String> itemTexts = uniqueItems.map(item -> "Item: " + item.getName() + " Amount: " + getInventory().getAmount(item.getName()));

// For each String, log it on the console
itemTexts.forEach(itemText -> log(itemText));

Hope that helps

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...