Get player who got hit by a snowball?

Discussion in 'Plugin Development' started by rwah_whool, Mar 3, 2015.

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

    rwah_whool

    So I'm trying to make a bukkit plugin, and it's supposed to prevent players from getting damage when hit by snowballs. It's also supposed to spawn a snowball into a player's inventory when they get hit. Right now, all it does is prevent the player from getting damage, but doesn't spawn a snowball into the player's inventory. Anyone know what I did wrong? This is my code so far:

    @EventHandler
    public void onSnowballHit(EntityDamageByEntityEvent event) {
    Entity thrownEntity = event.getEntity();
    if (thrownEntity.getType().equals(Material.SNOW_BALL)) {
    if (event.getDamager() instanceof Player) {
    Player thrower = (Player) event.getDamager();
    Player hitPlayer = (Player) event.getEntity();
    event.setCancelled(true);
    hitPlayer.getInventory().addItem(
    new ItemStack(Material.SNOW_BALL, 1));
    hitPlayer.sendMessage(AQUA
    + "A snowball was added to your inventory by"
    + thrower.getDisplayName());

    }
    }
    }
     
  2. Try to use EntityDamageEvent instead of EntityDamageByEntityEvent.

    Like this
    Code:
    @EventHandler
        public void onSnowballHit(EntityDamageEvent event) {
            DamageCause thrownEntity = event.getCause().PROJECTILE;
            if (thrownEntity.equals(Material.SNOW_BALL)) {
                if (event.getEntity() instanceof Player) {
                    EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) event;
                    Player thrower = (Player) e.getDamager();
                    Player hitPlayer = (Player) e.getEntity();
                    event.setCancelled(true);
                    hitPlayer.getInventory().addItem(
                            new ItemStack(Material.SNOW_BALL, 1));
                    hitPlayer.sendMessage(AQUA
                            + "A snowball was added to your inventory by"
                            + thrower.getDisplayName());
    
                }
            }
        }
     
    Last edited: Mar 3, 2015
  3. Offline

    Konato_K

    @Xfqlo23 So, you're comparing a DamageCause with a Material?


    @rwah_whool Your logic is kinda broken, if a player gets hit by a Snowball then getEntity will return the player being damaged, not the snowball, and getDamager will return the snowball, not the shooter.
     
  4. Offline

    rwah_whool

    @Konato_K Hmm, well, do you have any suggestions on how to fix this? I'm not very skilled with bukkit coding.

    @Xfqlo23 I tried this, and the player doesn't receive any damage when they're hit by a snowball. What I need now is for the player to receive a snowball in their inventory when they are hit. I tried this on my code posted above, but it does not work. How many I fix this? Anyone?
     
  5. Offline

    Konato_K

    @rwah_whool Players never get damage by snowballs, they only get pushed.

    Fix what I said, getEntity returns the player, getDamager returns the Snowball
     
  6. Offline

    rwah_whool

    @Konato_K Don't I already have that? As I said before, I'm not exactly skilled at coding bukkit and plugins, and I'm not sure what you are saying. Sorry if I sound like an idiot.... Could you possibly point out exactly what needs to be changed? Or even modify it and post the new code for me? I'm completely lost ... :oops:
     
  7. Offline

    Konato_K

    @rwah_whool I won't spoonfeed you, just read what I wrote until it makes sense and check your logic again.
     
  8. Offline

    rwah_whool

    @Konato_K I changed it according to your advice, yet it still fails to work. This is my code:

    Code (open)

    package com.Embarqmail.Practice;

    import java.util.logging.Logger;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Projectile;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Tortuga extends JavaPlugin implements Listener
    {
    Logger myPluginLogger = Bukkit.getLogger();

    @Override
    public void onEnable()
    {
    myPluginLogger.info("My plugin is being started!");
    }

    @Override
    public void onDisable()
    {
    myPluginLogger.info("My plugin is being disabled!");
    }

    @EventHandler
    public void onSnowballHit(ProjectileHitEvent event) {
    Projectile thrownEntity = event.getEntity();
    Entity shooter = thrownEntity.getShooter();
    if (thrownEntity.getType().equals(Material.SNOW_BALL)) {
    if (shooter instanceof Player) {
    Player hitPlayer = (Player) event.getEntity();
    hitPlayer.getInventory().addItem(
    new ItemStack(Material.SNOW_BALL, 1));
    hitPlayer.sendMessage(ChatColor.AQUA
    + "A snowball was added to " + hitPlayer + "'s inventory by"
    + shooter.getCustomName());
    }
    }
    }

    }
     
  9. Offline

    Konato_K

    @rwah_whool

    - Remove that logger, use getLogger instead
    - Remove your enable and disable messages, Bukkit does this for you
    - You didn't register your events
    - You didn't change the code like I said, you're blindy casting the Entity (which is not going to be the snowball) to a Projectile
     
    Shortninja66 likes this.
Thread Status:
Not open for further replies.

Share This Page