Jump to content

Ground items to list?


Recommended Posts

Posted

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!

Posted
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 :)

Posted

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?

Posted

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
Posted (edited)

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
Posted (edited)

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
Posted (edited)

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
Posted

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

 

@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

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...