Play a sound and explode when I hit someone with a SmallFireball

Discussion in 'Plugin Development' started by TRIPL3_CATS, Feb 20, 2015.

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

    TRIPL3_CATS

    I want to play a sound when I hit someone with my magic wand ( a gun that shoots a small fireball ) I want it to play a sound, I just need to know how to, then I will lookup the sounds on the internet, then explode without doing any damage to the terrain. Only hurting entities.
     
  2. Offline

    Qwertyness_

    Use player.playSound(location, sound, volume, pitch) to play the sound.

    As for the explosion, use World.createExplosion(location, power) and just use a really low power. If the power is below 1, most blocks won't break. Just play around with the power to see what won't break anything. If you want to do more damage, you could set up a listener that would listen for explosion damage and multiply it. As an alternative, you could use a full power explosion and listen for block breaking and cancel it if the cause was an explosion.
     
  3. Offline

    TRIPL3_CATS

    So, how would I add that if I had this:
    @EventHandler

    public void onClick(PlayerInteractEvent e) {

    Player p = e.getPlayer();

    Action a = e.getAction();

    ItemStack i = p.getItemInHand();

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

    if (i.getType() == Material.BONE) {

    if (i.getItemMeta().hasDisplayName()) {

    if (i.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.BLUE + "" + ChatColor.BOLD + "Magical Wand")) {

    p.launchProjectile(SmallFireball.class).setVelocity(p.getLocation().getDirection().multiply(0.5)); {

    I cannot figure out how to make World.createExplosion(location, power) working.. I got the Sound though..
    Full Class:
    Code:
    package me.tripl3_cats.magicwand;
    
    import java.util.Arrays;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.SmallFireball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class MagicWand extends JavaPlugin implements Listener {
    
        public void onEnable() {
            wandrecipe();
            this.getServer().getPluginManager().registerEvents(this, this);
        }
       
        private void wandrecipe() {
            ItemStack wand = new ItemStack(Material.BONE, 1);
            wand.addUnsafeEnchantment(Enchantment.LOOT_BONUS_BLOCKS, 10);
            ItemMeta meta = wand.getItemMeta();
            meta.setDisplayName(ChatColor.BLUE + "" + ChatColor.BOLD + "Magical Wand");
            meta.setLore(Arrays.asList("Powered by redstone!"));
            wand.setItemMeta(meta);
           
            ShapedRecipe wandrecipe = new ShapedRecipe(wand);
            wandrecipe.shape(
                    "@#$",
                    "%!%",
                    "$#@");
            wandrecipe.setIngredient('!', Material.BONE);
            wandrecipe.setIngredient('@', Material.MAGMA_CREAM);
            wandrecipe.setIngredient('#', Material.DIAMOND);
            wandrecipe.setIngredient('$', Material.EMERALD_BLOCK);
            wandrecipe.setIngredient('%', Material.GLOWSTONE_DUST);
            Bukkit.getServer().addRecipe(wandrecipe);
        }
       
        @EventHandler
        public void onClick(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        Action a = e.getAction();
        ItemStack i = p.getItemInHand();
        if (a == Action.RIGHT_CLICK_AIR || a == Action.RIGHT_CLICK_BLOCK) {
        if (i.getType() == Material.BONE) {
        if (i.getItemMeta().hasDisplayName()) {
        if (i.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.BLUE + "" + ChatColor.BOLD + "Magical Wand")) {
        p.playSound(p.getLocation(), Sound.FIZZ, 5F, 3F);
        World.createExplosion((p.getLocation), 1);
        p.launchProjectile(SmallFireball.class).setVelocity(p.getLocation().getDirection().multiply(0.5)); {
           
                            }
                        }
                    }
                }
            }
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  4. Offline

    Qwertyness_

    Try using an actual instance of world rather than the static method.
     
  5. Offline

    TRIPL3_CATS

    I have this now:
    p.getWorld().createExplosion(p.getLocation(), 1);
    But, it explodes the player not where the fireball lands
     
  6. Offline

    Konato_K

    @TRIPL3_CATS Listen to EntityDamageByEntityEvent and ProjectileHitEvent (I think EntityExplodeEvent works too)
     
  7. Offline

    TRIPL3_CATS

    I am so confused!!!!! I am really TIRED and I want to goto bed but I need to finish this before I goto bed, this makes no sense how do I use it!? D: D: D:

    I have this:
    Code:
        @EventHandler
        public void onClick(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        Action a = e.getAction();
        ItemStack i = p.getItemInHand();
        if (a == Action.RIGHT_CLICK_AIR || a == Action.RIGHT_CLICK_BLOCK) {
        if (i.getType() == Material.BONE) {
        if (i.getItemMeta().hasDisplayName()) {
        if (i.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.BLUE + "" + ChatColor.BOLD + "Magical Wand")) {
        p.playSound(p.getLocation(), Sound.FIZZ, 5F, 3F);
        p.getWorld().createExplosion(p.getLocation(), 1.0F);
        p.launchProjectile(SmallFireball.class).setVelocity(p.getLocation().getDirection().multiply(0.5)); {
    Where would I put them?!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  8. Offline

    Qwertyness_

    Oh, I thought you already had the fireball location. Before I can answer effectively, I must ask a noob question. Do fireballs do damage when they hit an entity?
     
  9. Offline

    TheEntropy

    @Qwertyness_ I believe they create an explosion that hurts an entity :p
     
  10. Offline

    Qwertyness_

    In that case, listen on a EntityDamageByEntity event an check if the entity is a Player instance and the damager is a Fireball instance. If so, you can spawn the explosion at the damager entity and increase the damage in the event of you would like as well.

    You can find some information on listeners here: http://wiki.bukkit.org/Plugin_Tutorial
     
    Last edited: Feb 20, 2015
  11. Offline

    xTrollxDudex

    I believe it should be just EntityDamageEvent, as the damage is caused by the explosion, not the fireball striking the player.
     
    tomudding likes this.
  12. Offline

    Qwertyness_

    And then you would check DamageCause?
     
  13. Offline

    Konato_K

    Just to clear out, if the fireball hits the entity directly, it will cause projectile damage, otherwise it would be explosion damage.
     
    xTrollxDudex likes this.
Thread Status:
Not open for further replies.

Share This Page