Thread: [Elvarg] Simple Mystery Box

Results 1 to 5 of 5
  1. #1 [Elvarg] Simple Mystery Box 
    OWNER @ BOFA (Now Hiring)
    Mr Bill's Avatar
    Join Date
    May 2010
    Posts
    512
    Thanks given
    14
    Thanks received
    26
    Rep Power
    19
    If you do use this, please rep or thank! Appreciate it.

    First of all, of course it’s nothing special. I just figured this is a good base to release and to give a little back to the community for all of the help I've been given over time. Do what you want with it! I made this very quickly, so there's probably a lot of things I could have done differently.



    New class, called MysteryBox.java
    Code:
    package com.elvarg.game.content;
    
    import java.util.Random;
    
    import com.elvarg.game.entity.impl.player.Player;
    
    public class MysteryBox {
    
    	/**
    	 * The item id of mystery box. Used for donators receiving incentive.
    	 */
    	public static final int DONATOR_MYSTERY_BOX = 6199;
    
    	/**
    	 * If the roll is between 0 and 300
    	 */
    	public static int[] LOW_QUALITY_ITEMS = new int[] { 995 };
    
    	/**
    	 * If the roll is between 300 and 400
    	 */
    	public static int[] MED_QUALITY_ITEMS = new int[] { 6585 };
    
    	/**
    	 * If the roll is between 400 and 425
    	 */
    	public static int[] HIGH_QUALITY_ITEMS = new int[] { 7462 };
    
    	/**
    	 * If the roll is between 425 and 430
    	 */
    	public static int[] VERY_GOOD_QUALITY_ITEMS = new int[] { 20997, 20014, 12817, 12825, 20011, 1038, 1040, 1042, 1044, 1046, 1048 };
    
    	/**
    	 * Generate a random number
    	 */
    	public static int generateRandomNumber() {
    		return new Random().nextInt(430 - 0) + 0;
    	}
    	
    	/**
    	 * Get a random item from the array of items
    	 */
    	public static int getRandomItem(int[] array) {
    	        return array[new Random().nextInt(array.length)];
            }
    
    	/**
    	 * Opens a mystery box and gives a player a random item
    	 * 
    	 * @param player
    	 */
    	public static void openMysteryBox(Player player) {
    		int randomNumber = generateRandomNumber();
    		int randomItem = 0;
    
    		// Stop actions..
    		player.getMovementQueue().reset();
    
    		player.getInventory().delete(DONATOR_MYSTERY_BOX, 1);
    		if (randomNumber > 0 && randomNumber < 300) {
    			// Select low quality item to give player
    			randomItem = getRandomItem(LOW_QUALITY_ITEMS);
    		}
    		if (randomNumber > 299 && randomNumber < 401) {
    			// Select med quality item to give player
    			randomItem = getRandomItem(MED_QUALITY_ITEMS);
    		}
    		if (randomNumber > 401 && randomNumber < 451) {
    			// Select high quality item to give player
    			randomItem = getRandomItem(HIGH_QUALITY_ITEMS);
    		}
    		if (randomNumber > 424 && randomNumber < 431) {
    			// Select extremely rare quality item to give player
    			randomItem = getRandomItem(VERY_GOOD_QUALITY_ITEMS);
    		}
    
    		player.getInventory().add(randomItem, 1);
    		player.getPacketSender().sendMessage("You rolled a " + randomNumber + " and receive a random item. Thank you for being a loyal player.");
    	}
    }
    ItemActionPacketListener.java
    Code:
    		case MysteryBox.DONATOR_MYSTERY_BOX:
    			MysteryBox.openMysteryBox(player);
    			break;
    Last edited by Mr Bill; 08-01-2019 at 06:13 PM.
    Reply With Quote  
     

  2. #2  
    Arix Developer

    Tutus Frutus's Avatar
    Join Date
    Feb 2018
    Posts
    297
    Thanks given
    30
    Thanks received
    124
    Rep Power
    403
    Thanks for the contribution
    Reply With Quote  
     

  3. #3  
    OWNER @ BOFA (Now Hiring)
    Mr Bill's Avatar
    Join Date
    May 2010
    Posts
    512
    Thanks given
    14
    Thanks received
    26
    Rep Power
    19
    Quote Originally Posted by Tutus Frutus View Post
    Thanks for the contribution
    Sure! Enjoy.
    Reply With Quote  
     

  4. #4  
    Registered Member

    Join Date
    Nov 2014
    Posts
    243
    Thanks given
    38
    Thanks received
    139
    Rep Power
    193
    Hey this is nice.

    Just a few pointers:

    1) There is no need for your arrays to be public. By making your arrays public you give everyone the ability to modify them.

    2) Your mystery box class ideally should not be doing work on your player (just do one thing, give us the random item, for example)

    Code:
     public static int rollItem()  { ... };
    You should have a system of handling item click interactions that doesn't require to stuff a bunch of case statements inside of a single class. I.e.

    Code:
     MysteryBoxItemHandler extends ItemHandler { 
    @Inject Player player ... // could add inside constructor or have a way to inejct it on obj creation
    
     super(MysterBox.DONATOR_BOX) ...
    
     @Override public void click() { 
    player.getInventory.remove(item);
    player.getInventory.add(MysteryBox.rollItem())
    }
    This is just a sample, you should consider making a framework like this as you can then tightly package your features without worrying about editting some random file each time.

    Do the work on the player here, not in the MysterBox class.


    3. This is a good place to use enums

    Code:
    public final class MysteryBox {
    
    private static class MysterBoxRewards {
    LOW(0, 300, { ... }) 
    
    
    }
    
    private static MysterBoxReward forRoll(int roll) {
    //dont reccomend you use values() directly
    return values().stream().filter(val => val.minRoll >= roll && val.maxRoll <= roll).findFirst();
    
    }
    And your rolling item could be condensed immensely and more scalable without having these if blocks.
    Reply With Quote  
     

  5. #5  
    OWNER @ BOFA (Now Hiring)
    Mr Bill's Avatar
    Join Date
    May 2010
    Posts
    512
    Thanks given
    14
    Thanks received
    26
    Rep Power
    19
    Quote Originally Posted by Intrice Joe View Post
    Hey this is nice.

    Just a few pointers:

    1) There is no need for your arrays to be public. By making your arrays public you give everyone the ability to modify them.

    2) Your mystery box class ideally should not be doing work on your player (just do one thing, give us the random item, for example)

    Code:
     public static int rollItem()  { ... };
    You should have a system of handling item click interactions that doesn't require to stuff a bunch of case statements inside of a single class. I.e.

    Code:
     MysteryBoxItemHandler extends ItemHandler { 
    @Inject Player player ... // could add inside constructor or have a way to inejct it on obj creation
    
     super(MysterBox.DONATOR_BOX) ...
    
     @Override public void click() { 
    player.getInventory.remove(item);
    player.getInventory.add(MysteryBox.rollItem())
    }
    This is just a sample, you should consider making a framework like this as you can then tightly package your features without worrying about editting some random file each time.

    Do the work on the player here, not in the MysterBox class.


    3. This is a good place to use enums

    Code:
    public final class MysteryBox {
    
    private static class MysterBoxRewards {
    LOW(0, 300, { ... }) 
    
    
    }
    
    private static MysterBoxReward forRoll(int roll) {
    //dont reccomend you use values() directly
    return values().stream().filter(val => val.minRoll >= roll && val.maxRoll <= roll).findFirst();
    
    }
    And your rolling item could be condensed immensely and more scalable without having these if blocks.
    Great pointers. I'll probably get back to cleaning it up some this weekend. I like the idea you have of handling the clicks inside of MysteryBox.java rather than in ItemActionPackerListener.java
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 07-17-2019, 09:44 PM
  2. Replies: 0
    Last Post: 10-14-2018, 11:20 PM
  3. Replies: 20
    Last Post: 11-01-2015, 01:56 AM
  4. Simple Mystery box and Crystal key combining
    By Wulfite in forum Snippets
    Replies: 27
    Last Post: 01-12-2014, 05:47 PM
  5. ..::[508] Simple Bank Box [508]::..
    By Mimz in forum Snippets
    Replies: 2
    Last Post: 12-06-2009, 09:59 PM
Tags for this Thread

View Tag Cloud

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •