Download

Combining Items, where should the Code go?

Hi there, this is more of a “Where should I”-question rather than “How do I”-Question.

I have an Inventory, which consits of the Items (Array of ItemStack) and has Methods to Add/Remove Items or ItemStacks.
I want some Items to be Cominable (but not every Item should be).
The Question is: Where should I put the Logic (check if combinable etc.) for combining?
First of all:
Item A + Item B = Item B + Item A = Item C (Order doesn’t matter)

First, I thought of creating an “Combinable” Interface that Items should implement:


IsCombineableWith(Item i);
Item GetItemResult(Item toCombineWith); //You may combine one item with different items

However, that would mean that Item A and Item B need to implement this interface the exact same way (just the Item is replaced)

Another thought would be implementing a “Combinable Map” in the Inventory (Combine Logic would be in Inventory instead of Item).
Here, I would use a Set of two Items as a Key and the Resulting Item as the Value. I just have to check for the Set (Do Sets always return the same Value when hashed? How is it treated when I put Item B first in a Set and then A. Sets usually don’t care about order)

Any suggestions?