Jump to content

Ground items to list?


Aviri

Recommended Posts

How would I collect a list of the all ground items that I can edit?
Been trying to do this for about 3 hours now with no luck...

 

Basically need to get all the ground items, and then out of all out those remove any items I don't want, and then give me how many items are left within the list.

 

Been trying with List and Collections, but no luck.

Any tips would be great, thanks!

Link to comment
Share on other sites

List<String> forbiddenNames = Arrays.asList(new String[] { "Bones", "Coins" });
getGroundItems().getAll().stream().filter((gi) -> (gi != null && gi.exists() && !forbiddenNames.contains(gi.getName()))).collect(Collectors.toList());

This is pretty beast.

Have no idea how it works but I'm going to figure it out.

 

Thank you! :)

 

Thanks apa, too :)

Link to comment
Share on other sites

For completeness, the good old filter 

List<GroundItem> list = groundItems.filter(i -> <conditions>)

groundItems.filter(i -> i.getName().equals("name"));

Still prefer this one over java 8 streams cuz it's unreadable.

Would I still be able to get the size (how many) of result items using streams as he shows?

Link to comment
Share on other sites

This is pretty beast.

Have no idea how it works but I'm going to figure it out.

 

Thank you! smile.png

 

Thanks apa, too smile.png

 

My streams tutorial can somewhat explain what's happening.

 

We filter our GroundItems list to the following conditions:

  • They aren't null (!= null)
  • They exist (.exists())
  • Their names are not in the forbidden list (!forbiddenNames.contains(gi.getName()))

This will return a list with any ground item that isn't Bones or Coins.

  • Like 1
Link to comment
Share on other sites

My streams tutorial can somewhat explain what's happening.

 

We filter our GroundItems list to the following conditions:

  • They aren't null (!= null)
  • They exist (.exists())
  • Their names are not in the forbidden list (!forbiddenNames.contains(gi.getName()))

This will return a list with any ground item that isn't Bones or Coins.

Thank you so much smile.png

 

EDIT: 

Okay, that part works just fine now. (Thank you Bobrocket, you taught me streams today smile.png)

Now I need to get a list of my inventory items but only make sure the list size (or amount of slots I need to clear up) only reaches the size of wanted ground items. 

Note: to put this in context, I only need to check this when my inv is full and my wanted items are on the ground. (simple conditions I don't need help with)

 

Basically, my inventory is full, I have 3 items on the floor that I want, and need to make 3 spaces in my inventory, so I need to remove 3 of the shitty items I have in my inventory.

 

In a simpler form, how would I set a condition to limit the size of the list? (I know how to set the shitty item condition mentioned above).

Or can I get the list, and then trim it to the size of wanted items?

 

Thanks in advance, sorry if I don't explain all this too well.

 

 

Edited by Aviri
Link to comment
Share on other sites

Thank you so much smile.png

 

EDIT: 

Okay, that part works just fine now. (Thank you Bobrocket, you taught me streams today smile.png)

Now I need to get a list of my inventory items but only make sure the list size (or amount of slots I need to clear up) only reaches the size of wanted ground items. 

Note: to put this in context, I only need to check this when my inv is full and my wanted items are on the ground. (simple conditions I don't need help with)

 

Basically, my inventory is full, I have 3 items on the floor that I want, and need to make 3 spaces in my inventory, so I need to remove 3 of the shitty items I have in my inventory.

 

In a simpler form, how would I set a condition to limit the size of the list? (I know how to set the shitty item condition mentioned above).

Or can I get the list, and then trim it to the size of wanted items?

 

Thanks in advance, sorry if I don't explain all this too well.

 

List<Item> junk = getInventory().filter(itm -> itm.getName().equals("Junk"));

List<Item> trimmed = (junk.size() > 3) ? junk.subList(0, 3) : junk;

 

Edited by FrostBug
  • Like 1
Link to comment
Share on other sites

I'm not a professional but I made a version of this that wrote the items in a list on the paint a while back, here's the snippet just because. Even though the problem was already solved.

		List<GroundItem> itemsog = getGroundItems().getAll();
		int count = 0;
		for (int i = 0; i < itemsog.toArray().length; i++) {
			for (int i2 = 0; i2 < itemsog.toArray().length; i2++) {
				if (itemsog.get(i).getName().equalsIgnoreCase(itemsog.get(i2).getName()) && count == 1) {
					itemsog.remove(i2);
					count = 0;
				}
				if (itemsog.get(i).getName().equalsIgnoreCase(itemsog.get(i2).getName()) && count == 0) {
					count = 1;
				}
			}
		}
                g.setFont(new Font("Trebuchet MS", Font.PLAIN, 10);
                g.setColor(Color.WHITE);
		for (int i = 0; i < itemsog.toArray().length; i++) {
			g.drawString(itemsog.get(i).getName(), 8, 30 + (i * 11));
		}

I know it's lazy as hell and doesn't properly use the API but it's something I improvised due to lack of knowledge there was an easier way xD

Even though it's dirty code it works fine and weeds out all duplicate item names and displays them in the top left corner. of the screen in a nice screen space saving list.

Edited by Paradox68
Link to comment
Share on other sites

I'm not a professional but I made a version of this that wrote the items in a list on the paint a while back, here's the snippet just because. Even though the problem was already solved.

		List<GroundItem> itemsog = getGroundItems().getAll();
		int count = 0;
		for (int i = 0; i < itemsog.toArray().length; i++) {
			for (int i2 = 0; i2 < itemsog.toArray().length; i2++) {
				if (itemsog.get(i).getName().equalsIgnoreCase(itemsog.get(i2).getName()) && count == 1) {
					itemsog.remove(i2);
					count = 0;
				}
				if (itemsog.get(i).getName().equalsIgnoreCase(itemsog.get(i2).getName()) && count == 0) {
					count = 1;
				}
			}
		}
                g.setFont(new Font("Trebuchet MS", Font.PLAIN, 10);
                g.setColor(Color.WHITE);
		for (int i = 0; i < itemsog.toArray().length; i++) {
			g.drawString(itemsog.get(i).getName(), 8, 30 + (i * 11));
		}

I know it's lazy as hell and doesn't properly use the API but it's something I improvised due to lack of knowledge there was an easier way xD

Even though it's dirty code it works fine and weeds out all duplicate item names and displays them in the top left corner. of the screen in a nice screen space saving list.

 

@OP, if you want a cleaner version of Paradox's code (pretty inefficient, hard to read etc) here you go:

List<GroundItem> groundItems = getGroundItems().getAll().stream().distinct().collect(Collectors.toList());
int j = 0;
groundItems.forEach((gi) -> (g.drawString(gi.getName(), 8, 30 + ((j++) * 11))));
Link to comment
Share on other sites

 

@OP, if you want a cleaner version of Paradox's code (pretty inefficient, hard to read etc) here you go:

List<GroundItem> groundItems = getGroundItems().getAll().stream().distinct().collect(Collectors.toList());
int j = 0;
groundItems.forEach((gi) -> (g.drawString(gi.getName(), 8, 30 + ((j++) * 11))));

 

 

I knew there had to be a way to do this in like one line. I just hadn't heard of stream().distinct() method before. Thanks for the info i'll definitely be implementing this method in future scripts.

Edited by Paradox68
Link to comment
Share on other sites

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
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

  • Recently Browsing   0 members

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