help with abilities

Discussion in 'Spigot Plugin Development' started by OfirTIM, May 15, 2015.

  1. hello guys i am back and i did'nt give up from development :D soo... i chossed to change to the start... something like abilities... soo i know the main code for giving Items....

    Code:

    ItemStack attacker = new ItemStack(ItemManagement.createItem(Material.WOOD_SWORD, 1, (short)0, "§0Enderman blade"));
    attacker.addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
    attacker.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 1);
    attacker.addUnsafeEnchantment(Enchantment.KNOCKBACK, 1);
    p.getInventory().setItem(0, attacker);

    but i wanna change it... that if the player is right click with the sword he will shoot fireball! could someone give me the simplest code? thanks!

    Regards, OfirTIM
     
  2. Use the PlayerInteractEvent and spawn a Fireball with a Velocity.
     
    • Like Like x 1
  3. Yeah, this is probably the best way to go about it. For setting the velocity you can invoke the getDirection method of the player's location and multiply by whatever you want.
     
  4. @EventHandler
    public void onInteract(PlayerInteractEvent e) {
    if(e.getAction()==Action.RIGHT_CLICK_AIR || e.getAction()==Action.RIGHT_CLICK_BLOCK) {
    if(e.getPlayer().getItemInHand().getItemMeta().getDisplayName()=="§0Enderman blade") {
    // code for the fireball here
    }
    }
    }
     
  5. - Use ChatColor class, instead of using he special ChatColor symbols, for future compatibility. The special character can also corrupt some files on certain operating systems, therefore the ChatColor class is better.

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {

    Player player = event.getPlayer();
    Action action = event.getAction();

    if(action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) {

    Fireball fireball = player.launchProjectile(Fireball.class);
    fireball.setVelocity(player.getLocation().getDirection().multiply(1.6));

    }
    }

    Please understand this code without copy pasting.
     
  6. It's not good to spoon feed... They won't try to understand it and will just c & p.
     
  7. That is why I wrote at the end of the message to please understand the code without copy and pasting, because I know for a fact it is bad, and nobody will learn from it. And I usually don't spoon-feed, but in reality, it's not much code. I am just trying to help, and they just came back to the API, soo.
     
    • Like Like x 1
  8. Everyone, please format your code properly... like, put it between CODE and /CODE.
     
  9. *cough* eight month old thread *cough*
     
    • Funny Funny x 1
  10. Whoops... sorry for that.