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); } } }
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. ^_^
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.