Tutorial How to use Randoms to get random locations

Discussion in 'Resources' started by mine2012craft, Dec 18, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    mine2012craft

    Hello again Coders,

    I'm going to teach you on how to use Randoms! You can use this to generate random locations, items, monsters, and more. For this case, we will be using random locations to create mini-cloud explosions around the player.

    Start by creating a class. I am going to be naming my class "CloudRandomness"

    Next, make the class a Listener. We will be listening to check if A. the Player is holding a Nether Star, and B. Whether the player is shifting or not.

    Now, we need to make a constructor to create a variable that gives us an instance of our core or main.

    In the beginning, your class should look like this:

    Code:
    public class CloudRandomness implements Listener{
        private Core plugin;
        public CloudRandomness (Core core){
            plugin = core;
        }
    
    }
    Now lets create our event. We will be using the "PlayerToggleSneakEvent" as we want to check if the player is shifting or not. Make sure to add the @EventHandler

    Code:
            @EventHandler
            public void cloud (PlayerToggleSneakEvent event){
           
           
            }
    Now, lets check if the player is holding a nether star and is shifting. We will also add a null checker to make sure the player isn't holding nothing. Also, we want to make sure that the player is currently shifting, as the event alone checks if the player Presses or releases shift. the final modifier next to the player variable is needed later.

    Code:
        @EventHandler
            public void cloud (PlayerToggleSneakEvent event){
           
                final Player player = event.getPlayer();
                if (!player.isSneaking()){
                if (player.getInventory().getItemInMainHand() != null){
                    if (player.getInventory().getItemInMainHand().getType() == Material.NETHER_STAR){
    
                    }
                }
                }
            }
    Now, lets create our random variable and our location. This will be used later, but its better to do it now than later. The final modifiers next to the 2 variables are needed for the runnables later on.

    Code:
               
        final Random rand = new Random();
        final Location loc = player.getLocation();
    
    
    Now, lets create a runnable that will generate poofs every 5 ticks.

    Create a runnable that looks similar to this. This is where the plugin variable we created earlier is used. Don't forget to add your unimplemented methods.

    Code:
                        new BukkitRunnable(){
    
                            @Override
                            public void run() {
    
                           
                            }
                       
                        }.runTaskTimer(plugin, 0, 5);
    Now, lets create our random x, y, and z variable. This is where we will be using our Random variable. We will also create a new location as well to add to our other location.

    Create the variables as so in your BukkitRunnable:

    Code:
                      new BukkitRunnable(){
    
    int minimum = -5; // Easier changing the minimum
                            int maximum = 5; // Easier changing the maximum
    
                            @Override
                            public void run() {
                                int x = rand.nextInt(maximum - minimum) + minimum;
                                int y = rand.nextInt(maximum - 1) + 1;
                                int z = rand.nextInt(maximum - minimum) + minimum;
                                Location l = new Location(loc.getWorld(), x, y, z);
    Now, we can add and subtract the location to our other location inside our run:

    Code:
    public void run() {
                           
    int x = rand.nextInt(maximum - minimum) + minimum;
                                int y = rand.nextInt(maximum - 1) + 1;
                                int z = rand.nextInt(maximum - minimum) + minimum;
                                Location l = new Location(loc.getWorld(), x, y, z);
    
                                loc.add(l);
                           
                           
                           
                                loc.subtract(l);
                           
                            }
    Now, let us check if the player is no longer sneaking. If not, then cancel the runnable.

    Code:
                    loc.add(l);
                           
                                if (!player.isSneaking()){
                                    this.cancel();
                                }
                           
                                loc.subtract(l);
    Finally, lets create our poofs of particles! I will be using a particle library that can be found here to generate the particles: https://bukkit.org/threads/1-8-particleeffect-v1-7.154406/

    Create your poof particles within the add and subtract lines. We will also add a little sound too to know its a poof:

    Code:
                                ParticleEffect.CLOUD.display(0, 0, 0, 0.5F, 100, loc, 40);
                                 player.getLocation().getWorld().playSound(loc, Sound.ENTITY_ITEM_PICKUP, 1, 1);
    In the end, your public void should look like this:

    Code:
    @EventHandler
            public void cloud (PlayerToggleSneakEvent event){
           
                final Player player = event.getPlayer();
                if (!player.isSneaking()){
                    if (player.getInventory().getItemInMainHand() != null){
                    if (player.getInventory().getItemInMainHand().getType() == Material.NETHER_STAR){
    
                        final Random rand = new Random();
                        final Location loc = player.getLocation();
                   
                        new BukkitRunnable(){
                       
                            int minimum = -5; // Easier changing the minimum
                            int maximum = 5; // Easier changing the maximum
    
                            @Override
                            public void run() {
                                int x = rand.nextInt(maximum - minimum) + minimum;
                                int y = rand.nextInt(maximum - 1) + 1;
                                int z = rand.nextInt(maximum - minimum) + minimum;
                                Location l = new Location(loc.getWorld(), x, y, z);
                                loc.add(l);
                           
                                ParticleEffect.CLOUD.display(0, 0, 0, 0.5F, 100, loc, 40);
                                 player.getLocation().getWorld().playSound(loc, Sound.ENTITY_ITEM_PICKUP, 1, 1);
                                if (!player.isSneaking()){
                                    this.cancel();
                                }
                           
                                loc.subtract(l);
                           
                            }
                       
                        }.runTaskTimer(plugin, 0, 5);
                   
                    }
                }
                }
            }
    Your poof should also be generating poofs above the surface in random locations too if you did it correctly!

    That's it for Randoms! I will assume you know how to add it to your core or main class and make it functional. With a bit of tweaking and learning you can learn how to spawn mobs, drop items, and even rain down Arrows or even icebolts (I did the icebolt thing it was awesome).

    If you have any comments, suggestions or find any bugs with my code let me know.

    Thank you,
    WarlordWeaponry
     
    Last edited: Dec 21, 2016
  2. The plugin global variable should be private, no reason for it to be package or publically available.

    Make sure to indent your code so it is easier to follow.
    Also make sure to check if the players hand is null! This can throw an NPE

    Making a new Random everytime someone sneaks is not very efficient, make it a global variable instead.
     
    0ct0berBkkitPlgins likes this.
  3. Offline

    mine2012craft

    @bwfcwalshy Thanks for the corrections. I'll fix them when I have the time (On my phone)

    Also now that you mention it I probably should use null checkers more often... Haven't used them much.
     
  4. Offline

    mine2012craft

    @AlvinB I prefer Randoms. Its sorta what I've been rollin with for awhile.
     
Thread Status:
Not open for further replies.

Share This Page