[Tutorial] Creating a throwable Grenade

Discussion in 'Spigot Plugin Development' started by HardTimeStudio, May 3, 2015.

  1. Hey guys,
    today I'll be showing you how to create a throwable grenade. So enough talking let's start.

    Create a throwable Grenade

    We only need an EventHandler for a grenade, which Needs to be the PlayerInteractEvent.
    Code (Text):
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
    Then we Need to define a Player.
    Code (Text):
    Player p = e.getPlayer();
    Lets start checking some things which have to be true to throw a grenade.
    Code (Text):
    if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
        if(e.getMaterial() == Material.STONE_PLATE) { //Note this can be anything you want. I just used STONE_PLATE for my exaple.
        }
    }
    After this we come to the part where we remove one Grenade from the players Inventory.
    Code (Text):
    p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
    Now we come to the part where the Grenade get throwed.
    Code (Text):
    final Item grenade = p.getWorld().dropItem(p.getEyeLocation(), new ItemStack(Material.STONE_PLATE));

    grenade.setVelocity(p.getLocation().getDirection().multiply(0.8D));
    And now the last part is creating the Explosion.
    Code (Text):
    Bukkit.getScheduler().scheduleSyncDelayedTask(<yourMainClass>, new Runnable() {
        @Override
        public void run() {
            grenade.getWorld().createExplosion(grenade.getLocation().getX, grenade.getLocation().getY, grenade.getLocation().getZ, 3, false, false);
        }
    }, 20*3);
    So the complete EventHandler will look like this
    Code (Text):
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
            if(e.getMaterial() == Material.STONE_PLATE) {
                p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
           
               final Item grenade = p.getWorld().dropItem(p.getEyeLocation(), new ItemStack(Material.STONE_PLATE));
               grenade.setVelocity(p.getLocation().getDirection().multiply(0.8D));
           
                Bukkit.getScheduler().scheduleSyncDelayedTask(<yourMainClass>, new Runnable() {
                    @Override
                    public void run() {
                        grenade.getWorld().createExplosion(grenade.getLocation().getX, grenade.getLocation().getY, grenade.getLocation().getZ, 3, false, false);
                    }
                }, 20*3);
            }
        }
    }
     
    • Like Like x 3
    • Informative Informative x 2
    • Useful Useful x 2
  2. jflory7

    jflory7 Retired Moderator
    Retired Benefactor

    Thread moved => Spigot Plugin Development
     
  3. More of a snippet than a tutorial, to be honest.
     
    • Agree Agree x 1
  4. Code itself can be a tutorial document.
     
    • Agree Agree x 1
  5. Not sure if this should be a tutorial as it doesn't have much of a practical application.
     
  6. let those who find this useful decide.
     
  7. It seems like to be really simple tutorial but I appreciate the good job.
     
  8. It's perfect to have some code snippets references that you can easily find on a search. That qualifies as that. Sure a step forward for the newbies eager to learn something. It gives enough info to let a person know how to handle things, yet not enough to be qualified as spoon feeding.

    Keep it up. ^_^
     
    • Agree Agree x 1
  9. thank you! i were looking for this! seems really easy to make
     
  10. Alfie

    Patron

    Won't change functionality at all but instead of
    Code (Text):

        if (condition) {
            if (othercondition) {
                 // do something
             }
       }
     
    I would personally do something like:
    Code (Text):

       if (!(condition && othercondition)) {
          return;
       }

       // do something
     
    Makes it much cleaner in my opinion.
     
    • Agree Agree x 1
  11. his post is kinda a few time ago and its not worky
     
  12. Alfie

    Patron

    There isn't a deadline on good practice.
     
    • Agree Agree x 1