Catch the Explosion of world.createexplosion by using events

Discussion in 'Plugin Development' started by magicced01, May 3, 2015.

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

    magicced01

    Hello Guys,

    I am writing a PvP Plugin and I am currently working on an Egg Grenade.
    Code:
         public void onEggThrow(ProjectileHitEvent e)
         {
            if(e.getEntity() instanceof Egg)
            {
                 Player p = (Player) e.getEntity().getShooter();
                if(MyClasses.PlayerClassCache.get(p.getName()) == "demoman")
                {
                    Location l = e.getEntity().getLocation();
                    Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
                     {
    
                        @Override
                        public void run()
                        {
                            e.getEntity().getWorld().createExplosion(l.getX(),l.getY(),l.getZ(),12,false,false);
    
    
                           
                        }
                       
                    }
                   
                    , 20*3);
    
    
                }
    
            }
         }
    The Code itself is working but im looking for a possibility to catch the Explosion by using events.
    So far i tried the EntityExplodeEvent and the BlockExplodeEvent but both of them seem not to be working.
    Do you have an Idea what I can use to Catch the Event?
     
  2. Offline

    Garnetty

    You could try spawning a primed tnt with a fuse tick of 1, put the TNT's hashcode in a List, and catch the explosion with an event that way. Something like this:

    Code:
     ArrayList<Integer> tnts = new ArrayList<>();
    
        @EventHandler
        public void onEggHit(ProjectileHitEvent e)
        {
            if (e.getEntity() instanceof Egg)
            {
                Egg egg = (Egg) e.getEntity();
            
                // whatever you use to find out if it's an "explosive egg" goes here
            
                final Location loc = egg.getLocation();
            
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable()
                {
                    public void run()
                    {
                        TNTPrimed tnt = loc.getWorld().spawn(loc, TNTPrimed.class);
                        tnt.setFuseTicks(1);
                    
                        // Set the tnt yield to whatever you want (this is the size of the explosion)
                        tnt.setYield(4.00F);
                    
                        tnts.add(tnt.hashCode());
                    }
                }, 20 * 3);
            }
        }
    
        @EventHandler
        public void onExplode(EntityExplodeEvent e)
        {
            if (e.getEntity() instanceof TNTPrimed)
            {
                TNTPrimed tnt = (TNTPrimed) e.getEntity();
            
                if (tnts.contains(tnt.hashCode()))
                {
                    // Make it so no blocks explode
                    e.blockList().clear();
                
                }
            }
        }
    I just took a wild guess that you wanted to make no blocks explode

    Also, if you want, you can multiply the damage of the explosion. If you listen for an EntityDamageByEntity event, check if the damager was tnt, and check if the tnt was in that list, you can set the damage to whatever it was multiplied by x
     
  3. Offline

    mine-care

    Dont abuse static (generally speaking) and dont compare objects with ==
    As far as i know there is no event being called when explosions are called this way although I'm unsure since I haven't see the source.

    @Garnetty try not to spoonfeed people with the answer directly since most of them will be copy pasting the ready made answer without getting anything out of it.
     
  4. Offline

    magicced01

    What you quoted is a Hashmap. Thats all Working Fine
    And ty @Ganetty thats what i was looking for
     
Thread Status:
Not open for further replies.

Share This Page