Flub 1,288 Posted October 6, 2021 Report Share Posted October 6, 2021 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! Link to comment Share on other sites More sharing options...
Mystery 338 Posted October 6, 2021 Report Share Posted October 6, 2021 nice1 nice1 Link to comment Share on other sites More sharing options...
Flub 1,288 Posted October 6, 2021 Author Report Share Posted October 6, 2021 Thanks Anything to clean up some of the messes I see Link to comment Share on other sites More sharing options...
0117be 66 Posted June 2, 2022 Report Share Posted June 2, 2022 Good sht bro Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now