[Ruse] Improved Random Item[] Selection

Hey everyone.

I bet all of you have come across the need to select a random item from a group before.

You’ll see this in many areas of your server. Most prominently in your mystery / donator box files, raid rewards and crystal chest too!

An example of an item group is:

Item[] items = {new Item(113, 1) , newI Item(333,4)};

Now, we have a group that includes two items. The most common way of selecting a random reward (You’ll see this EVERYWHERE) is;

Item reward = (items[Misc.getRandom(items.length - 1)], 1);

This will work. However, it’s ugly! There are always ways to improve.

Here is a new method for you… I’ve added mine into Misc.java.

public static Item randomItem(final Item[] collection) {
		return collection[random(collection.length - 1)];
	}

Now, lets select a random item from our group using this new method!

(If added in Misc.java)

Item reward = Misc.randomItem(Items);

How much cleaner is that!

You can then use the item however you want.

E.g.

player.getInventory().add(item);

Thank you!

Wrap Fin GIF by GIPHY Studios Originals