# Best way to compare items in a container?

## Recommended Posts

Hey.

The title says it all, I need to compare 8 slots in my custom container and see if they have the same items in them. (empty slots are fine) The only way I can think to do it is with a ton of if statements, is there a better way to do this?

Thanks,

TheRealMcrafter

##### Share on other sites

1. Get a list of items for the two containers that you want to compare.

2. Sort the lists of items with a well-defined ordering (lexigraphically by name, etc).

3. Compare the lists (using a loop of some sort).

There are more efficient ways, but this way is straightforward and easy to code.

[spoiler=An alternative]

1. Make a copy of container B.

2. For every item in container A, remove it from the copy of B.

3. If you try to remove an item and it's not there, then A and B aren't equal.

4. At the end, if there are still items remaining in the copy of B, then A and B aren't equal.

##### Share on other sites

How would I account for empty slots, though?

##### Share on other sites

You wouldn't have to. The only information you need in order to determine if two containers have the same items is

1. Do they contain the same items?

2. If they both contain an item, do they contain the same amount of that item?

The algorithms I gave effectively check for both of these. You don't actually need to know anything about empty slots. (Unless you specifically want to compare them, in which case I misunderstood the question.)

##### Share on other sites

Basically, I have 2 types of fuel rods (items) that go into the slots (reactor). If there is only "Uranium" in the container (reactor), then return true. If there is only "Plutonium" in the container (reactor), then return true. If there is a mix of "Uranium" and "Plutonium" in the container (reactor), then return false.

##### Share on other sites

Oh, I thought you had two containers and you wanted to see if they had the same items.

It turns out that you have one container, and you want to see if it contains one type of item or not.

[spoiler=If you only need to consider uranium and plutonium]

1. Create two booleans,

`hasUranium`

and

`hasPlutonium`

.

2. Loop through the inventory contents, if you see uranium, set

`hasUranium`

to true. Do the same for plutonium.

3. At the end, return

`hasUranium != hasPlutonium`

. (You can also use "^," the xor symbol in Java, which does the same thing.)

Note that this will return true if the container is empty. You can easily modify it so that it returns false in this case.

[spoiler=If you need to consider many item types]

1. Get the first item in your container, store it in a variable,

`firstItem`

.

2. Loop through the inventory contents, see if every other item has the same item type as first item.

3. If any result from 2 yields false, then there are multiple types of items in your container.

Again, it's pretty easy to add in a case for an empty container.

Sorry for the confusion.

##### Share on other sites

Ah! Thats exactly what I was looking for. I actually did this before in my mod but when I changed some stuff i accidentally deleted the code for it, thanks so much!

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

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

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

• ### Posts

• Wow this looks a lot simpler than I thought it would be. Especially compared to the setup I had to do for capabilities. I'll look at all this and update if I have any issues ty
• Me and a group of friends were trying to get a modpack to work so we can play together. It works for everyone else, except me. At first when I tried to import the modpack, it said "invalid file" but it didn't with anyone else's. The owner of the modpack resent it several times and I still kept getting the same issue, so I opened the modlist and downloaded every single mod by hand. I thought that it would resolve the issue, but instead, it started saying "Forge Modloader Installation Failed" after I created a new installation, so I tried to use a pre-existing modpack, made sure it was the same version "1.20.1, forge version 47.3.0" and swap the mods there, but it still had the same issue, and now none of my modpacks are working at all. They all keep saying that the modloader installation failed. None of us have any idea what to do as we aren't the techiest people. Any help is appreciated. Thank you!
• I don't think there's any documentation on them yet, I mainly just looked at the Vanilla code when implementing mine. You can see a fairly simple example here: Registration: https://github.com/Choonster-Minecraft-Mods/TestMod3/blob/f3bea10d5f455fa0904056caafd8c844b6fd13ae/src/main/java/choonster/testmod3/init/ModDataComponents.java#L50-L58 Item Registration: https://github.com/Choonster-Minecraft-Mods/TestMod3/blob/f3bea10d5f455fa0904056caafd8c844b6fd13ae/src/main/java/choonster/testmod3/init/ModItems.java#L264-L266 Usage: https://github.com/Choonster-Minecraft-Mods/TestMod3/blob/f3bea10d5f455fa0904056caafd8c844b6fd13ae/src/main/java/choonster/testmod3/world/item/EntityCheckerItem.java
• Okay, I'm glad I made a backup then. I'm confused as to where there is any information on how to work with Data Components. I don't see anything about it on the Forge docs or any tutorials on this in general unless I'm missing it somehow. I can see the DataComponents decompiled class file but I'm not sure how to work with it. Maybe it's beyond my technical expertise?
• The situation is the same in 1.20.6. Data Components were introduced in 1.20.5/1.20.6 and Item Capabilities haven't been re-implemented since then.

• ### Who's Online (See full list)

×

• #### Activity

×
• Create New...