Solved Get the player who threw a snowball?

Discussion in 'Plugin Development' started by bmckalip, Dec 11, 2014.

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

    bmckalip

    Hey guys. I was coding a simple grenade plugin for practice, and ran into an issue I can't seem to solve. the detonateGrenade class works fine, does exactly what I want it to, which is to explode snowballs on hit in a certain world. However, I'm now trying to add a cost to it, so I'm adding a second event where I remove x amount of tnt from the players inventory when a snowball is thrown in a certain world.

    Here's the issue: The ProjectileLaunchEvent event doesn't have a getPlayer() method (or similar) to my understanding. What seems to be the issue. Thanks for the help.

    Code:
        @EventHandler
        public void detonateGrenade(ProjectileHitEvent event){
            Entity entity = event.getEntity();
            World world = entity.getWorld();
            String playersWorld = world.getName();
        
            if(playersWorld.equalsIgnoreCase(worldName) && entity instanceof Snowball){
                world.createExplosion(entity.getLocation(), numOfTnt + 4);
            }
        }
    
        @EventHandler
        public void throwGrenade(ProjectileLaunchEvent event){
            Entity entity = event.getEntity();
            World world = entity.getWorld();
            String playersWorld = world.getName();
           //this is the problem here. getPlayer() is the functionality I want, but seemingly doesn't exist for this specific event.
            Player player = event.getPlayer();
            Inventory inv = player.getInventory();
            ItemStack tnt = new ItemStack(Material.TNT, numOfTnt);
       
         if(playersWorld.equalsIgnoreCase(worldName) && entity instanceof Snowball && inv.contains(tnt)){
           inv.removeItem(tnt);
           player.updateInventory();
         }
        }
     
    Last edited: Dec 11, 2014
  2. Offline

    TGRHavoc

    You can use "event.getEntity().getShooter()" in both events to get the ProjectilSource. You can then check if the ProjectileSource is an instance of a player.
     
  3. Offline

    bmckalip

    thanks, that worked great.
     
  4. Offline

    TGRHavoc

  5. Offline

    ChipDev

    To find all methods of a class/constructor, do the class name, for this example 'Projectile' then press the period, and it will show all of the methods for it; or use the java docs !
     
Thread Status:
Not open for further replies.

Share This Page