Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

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.

 

 

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

  • Author

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.

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.

  • Author

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.

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.