when a projectile hits a block event

Discussion in 'Plugin Development' started by ilethz, Aug 24, 2013.

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

    ilethz

    Hello! what is the code/event when a projectile hits a block?
    It's a simple question!


    Thank you :)
     
  2. Offline

    Assist

    ilethz
    ProjectileHitEvent
     
  3. Offline

    ilethz

    thank you, but how i get projectile Target block, e.g: when a snowball hits the wall(Block) spawn some particles!
    Assist
     
  4. Offline

    Assist

    ilethz
    There's no target block in ProjectileHitEvent, but you could do something like this
    Code:
    @EventHandler
    public void onProjectileHit(ProjectileHitEvent event) {
        if (event.getEntity() instanceof Snowball)
            event.getEntity().getWorld().playEffect(event.getEntity().getLocation(), Effect.MOBSPAWNER_FLAMES, 1);
    }
    This should spawn mobspawner flames wherever the snowball hits.
     
  5. Offline

    ilethz

    Thank you :)

    Assist
    It's not working! when i shoot a snowball nothing happens i add a sound but nothing happens

    Assist

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

    Assist

    ilethz
    No need to bump your thread every 1 hour. Have you registered your events? Also show me your current code.
     
  7. Offline

    ilethz

    Yes i'm already register my event
    here is;
    Code:
    package ilethz.guns.main;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Effect;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Fireball;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityDamageByBlockEvent;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Guns extends JavaPlugin implements Listener {
     
            public void onEnable() {
                    Bukkit.getServer().getPluginManager().registerEvents(this, this);
            }
         
            @EventHandler
            public void onPlayerInteract(PlayerInteractEvent e) {
                    if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
                 
                    if (!(e.getItem().getType() == Material.IRON_HOE)) return;
                 
                    @SuppressWarnings("unused")
                    Snowball s = e.getPlayer().launchProjectile(Snowball.class);
                    e.getPlayer().getInventory().getItemInHand().addUnsafeEnchantment(Enchantment.DURABILITY, 10);
                    e.getPlayer().playSound(e.getPlayer().getLocation(), Sound.EXPLODE, 2.3F, 0F);
     
            }   
            @EventHandler
            public void onEntityDamage(EntityDamageByEntityEvent e) {
                    if (e.getDamager() instanceof Fireball) {
                            Snowball s = (Snowball) e.getDamager();
                            if (s.getShooter() instanceof Player) {
                                    Player shooter = (Player) s.getShooter();
                                   
                                    if (shooter.getItemInHand().getType() == Material.IRON_HOE) {
                                            e.setDamage(10.0);
                                              shooter.playSound(shooter.getLocation(), Sound.WITHER_IDLE, 20F, 0.1F);
                                    }
                            }
                    }
            }
            @EventHandler
            public void onProjectileHit(ProjectileHitEvent event) {
                if (event.getEntity().getShooter() instanceof Player) {
                if (event.getEntity() instanceof Snowball)
                    event.getEntity().getWorld().playSound(event.getEntity().getLocation(), Sound.ANVIL_LAND, 2F, 2.9F);
                    event.getEntity().getWorld().playEffect(event.getEntity().getLocation(), Effect.MOBSPAWNER_FLAMES, 1);
            }
            }
            }
     
Thread Status:
Not open for further replies.

Share This Page