Plugin Help Why won't this work?

Discussion in 'Plugin Help/Development/Requests' started by awesomehenderson, Aug 29, 2015.

Thread Status:
Not open for further replies.
  1. Okay so i'm coding this Bombs plugin and It doesn't work?

    My Code:

    package me.awesomehenderson.bombsplus;

    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.EnderPearl;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Projectile;
    import org.bukkit.entity.Snowball;
    import org.bukkit.entity.ThrownExpBottle;
    import org.bukkit.entity.ThrownPotion;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class Bombs extends JavaPlugin implements Listener {

    @EventHandler
    public void onProjectileHit(ProjectileHitEvent e) {
    Projectile p = e.getEntity();
    if (!(p instanceof Snowball)) return;
    Snowball s = (Snowball) p;
    s.getWorld().createExplosion(s.getLocation(), 5.0F);
    for (Entity en : s.getNearbyEntities(10, 10, 10)) {
    if (en instanceof Player) {
    Player pl = (Player) en;
    if (!(pl == e.getEntity().getShooter())) pl.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 200, 0));
    if (!(pl == e.getEntity().getShooter())) pl.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 200, 0));
    pl.sendMessage("The Bomb temporary knocked you out!");
    }
    }
    }
    @EventHandler
    public void onProjectileHit1(ProjectileHitEvent e) {
    Projectile p = e.getEntity();
    if (!(p instanceof EnderPearl)) return;
    EnderPearl ep = (EnderPearl) p;
    ep.getWorld().createExplosion(ep.getLocation(), 10.0F);
    for (Entity en : ep.getNearbyEntities(10, 10, 10)) {
    if (en instanceof Player) {
    Player pl = (Player) en;
    if (!(pl == e.getEntity().getShooter())) pl.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 200, 0));
    if (!(pl == e.getEntity().getShooter())) pl.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 200, 0));
    pl.sendMessage("The Bomb temporary knocked you out!");
    }
    }
    }
    @EventHandler
    public void onProjectileHit2(ProjectileHitEvent e) {
    Projectile p = e.getEntity();
    if (!(p instanceof ThrownExpBottle)) return;
    ThrownExpBottle txp = (ThrownExpBottle) p;
    txp.getWorld().createExplosion(txp.getLocation(), 25.0F);
    for (Entity en : txp.getNearbyEntities(10, 10, 10)) {
    if (en instanceof Player) {
    Player pl = (Player) en;
    if (!(pl == e.getEntity().getShooter())) pl.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 200, 0));
    if (!(pl == e.getEntity().getShooter())) pl.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 200, 0));
    pl.sendMessage("The Bomb temporary knocked you out!");
    }
    }
    }
    @EventHandler
    public void onProjectileHit3(ProjectileHitEvent e) {
    Projectile p = e.getEntity();
    if (!(p instanceof Arrow)) return;
    Arrow a = (Arrow) p;
    a.getWorld().createExplosion(a.getLocation(), 50.0F);
    for (Entity en : a.getNearbyEntities(10, 10, 10)) {
    if (en instanceof Player) {
    Player pl = (Player) en;
    if (!(pl == e.getEntity().getShooter())) pl.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 200, 0));
    if (!(pl == e.getEntity().getShooter())) pl.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 200, 0));
    pl.sendMessage("The Bomb temporary knocked you out!");
    }
    }
    }
    @EventHandler
    public void onProjectileHit4(ProjectileHitEvent e) {
    Projectile p = e.getEntity();
    if (!(p instanceof ThrownPotion)) return;
    ThrownPotion pt = (ThrownPotion) p;
    pt.getWorld().createExplosion(pt.getLocation(), 100.0F);
    for (Entity en : pt.getNearbyEntities(10, 10, 10)) {
    if (en instanceof Player) {
    Player pl = (Player) en;
    if (!(pl == e.getEntity().getShooter())) pl.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 200, 0));
    if (!(pl == e.getEntity().getShooter())) pl.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 200, 0));
    pl.sendMessage("The Bomb temporary knocked you out!");
    }
    }
    }
    }
     
  2. Offline

    Xerox262

    You didn't register your events, you need to add this
    Code:
    public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
        }
    
    Also your code could be greatly shortened to this
    Code:
        @EventHandler
        public void projectile(ProjectileHitEvent e) {
            Projectile projectile = e.getEntity();
            if (projectile.getShooter() instanceof Player) {
                Player shooter = (Player) projectile.getShooter();
                float strength;
                if (projectile instanceof Snowball) {
                    strength = 5;
                } else if (projectile instanceof EnderPearl) {
                    strength = 10;
                } else if (projectile instanceof ThrownExpBottle) {
                    strength = 25;
                } else if (projectile instanceof Arrow) {
                    strength = 50;
                } else if (projectile instanceof ThrownPotion) {
                    strength = 100;
                } else {
                    return;
                }
                createExplosion(shooter, projectile, strength);
            }
        }
    
        private void createExplosion(Player shooter, Projectile projectile,
                float strength) {
            projectile.getWorld().createExplosion(projectile.getLocation(),
                    strength);
            for (Entity entity : projectile.getNearbyEntities(10, 10, 10)) {
                if (entity instanceof Player) {
                    Player player = (Player) entity;
                    if (player != shooter) {
                        player.addPotionEffect(new PotionEffect(
                                PotionEffectType.POISON, 200, 200));
                        player.addPotionEffect(new PotionEffect(
                                PotionEffectType.BLINDNESS, 200, 200));
                        player.sendMessage("The bomb has temporarily knocked you out!");
                    }
                }
            }
        }
    
    also makes it much easier to edit, for example if you wanted to make the bomb message red then you only have to do it once instead of having to do it 5 times
     
    Last edited: Aug 29, 2015
Thread Status:
Not open for further replies.

Share This Page