I've been designing a game server, where a player has inventory, bank, and other features with items.
There are some definitions of what item can be in my game:
- Item is either stackable or not stackable.
- If item is stackable, item's amount can be up to 100 per stack.
So since each item can have an amount if it's stackable, my regular idea was having two arrays, int[] itemIds
and int[] amount
, where itemIds
holds the IDs of the items, and amount holds the amount for item in that index.
I must keep the indexes the same, because when the client selects an item, it sends over the slot id he clicked on.
But then I thought, I could just do an Item object, with the tools in that class for that item, and have a private property isStackable
and amount
.
But then my mate said the following thing:
If you want to change some value of an Item object, you'll need to call a new construct and then get the old value then add the new value and then pass it to the construct. with primitives you can do that instant, which gives also performance positives
But then I thought of an idea, have a class named ItemStorage
which will contain two arrays, items and amount like I described above:
public class ItemStorage {
private int[] items;
private int[] amount;
public void setItems(int[] a, int[] b) {
this.items = a;
this.amount = b;
}
public int[] getItems() {
return this.items;
}
public int[] getAmount() {
return this.amount;
}
}
I have a class called Inventory, it's my domain object for handling the inventory, it extends the interface Storage<ItemStorage>
.
public interface Storage<T> {
public T getStorage();
}
Now, since it implements it, the class must have the method getStorage()
which will return ItemStorage
, so to modify items, you do inventory.getStorage().getItems()[0] = 50;
But I am not sure about it, I am sure that List<Item>
or Item[]
will be much friendly, but I am not really sure why my mate dislikes it.
What do you think about my idea, and his opinion? Do you have another idea I can use for this?
How the items are handled in the game
The game contains over 25,000 items, each item is unique. The game uses each item as an id to represent it's uniqueness. The first item out of that item list, will have an id of 1.
Lets look at item 1445, It's a sword.
When the client wants to wear the item, he has to click on it in his inventory. This click will send a packet to the server, with the slot id he clicked in the inventory.
There's an example, similar to my inventory:
Let's base on that picture.
You see 16 slots in that picture, starting from 0 to 15. Player clicks on slot 4, and the client will pass the slot 4 to the server. The server will check through the inventory, what item is sitting on slot 4 in the array, it will see the amulet with id 555 for example (like in the picture).
Therefore I need slots to be supported in my design.