I need some help

Discussion in 'Plugin Development' started by xCyanide, Aug 7, 2013.

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

    xCyanide

    Ok, so I have a playerinteractevent and when a player right clicks an item it will shoot an egg. If the egg kills a player I want to get the player that threw the egg(AKA the killer). Does anyone know how I can do that? Also, when a player throws and egg it would create an explosion, how would I make it so that explosion wouldn't hurt the player that threw it
     
  2. Offline

    LinearLogic

    Does the player actually throw the egg (if so, listen for an EntityDamageByEntity event, cast event.getDamager() to a projectile, and cast projectile.getShooter() to a Player), or is it shot from something?
     
  3. Offline

    Stevenpcc

    If it works the same as arrows, it should be something like this

    Code:java
    1. egg.getShooter();
     
  4. Offline

    xCyanide

  5. Offline

    Bammerbom

  6. Offline

    xCyanide

    Hmm, ok I will try that.
    Offtopic: Does anyone know what event is used when you till grass/dirt?
     
  7. Offline

    LinearLogic

    If the player isn't actually throwing the egg, you won't be able to use projectile.getShooter(). You'll need to track the player instead: when the player clicks the block and the egg is fired, store the player with the egg projectile. When the projectile hits, retrieve the player object paired with it.

    There's no specific event for soil tilling. Just listen for a PlayerInteractEvent and check whether the player is right-clicking soil with a hoe.
     
  8. Offline

    xCyanide

    LinearLogic
    Can you give me an example of how I would store and retrieve it? What would I use?(HashMap or something)
     
  9. Offline

    LinearLogic

    Use a HashMap<UUID, Player>. When you launch the egg (a projectile entity), store its UUID along with the player who "fired" it. Then in your EntityDamageByEntityEvent handler, check if there is a player mapped to the UUID of event.getDamager(). If so, said player is your shooter.
     
  10. Offline

    xCyanide

    LinearLogic
    How would I check if there is a player mapped to the UUID of event.getDamager()? I probably know it, but it is the way you worded it, so I am thinking am I doing to correctly :p
     
  11. Offline

    LinearLogic

    Check if the HashMap has an entry with the UUID of event.getDamager() as the key (use map.containsKey(uuid) or check if map.get(uuid) != null).
     
  12. Offline

    xCyanide

    LinearLogic
    hmm, so I did know what to do, but the problem is I don't know how to get the uuid since this is how I put the uuid and player into the hashmap.
    Code:java
    1. public void shootSnowball(Double speed, Player player) {
    2. Location shootLocation = player.getEyeLocation();
    3. Vector directionVector = shootLocation.getDirection().normalize();
    4.  
    5. double startShift = 2;
    6. Vector shootShiftVector = new Vector(directionVector.getX() * startShift, directionVector.getY() * startShift, directionVector.getZ() * startShift);
    7. shootLocation = shootLocation.add(shootShiftVector.getX(), shootShiftVector.getY(), shootShiftVector.getZ());
    8.  
    9. Snowball snowball = shootLocation.getWorld().spawn(shootLocation, Snowball.class);
    10. snowball.setVelocity(directionVector.multiply(speed));
    11.  
    12. proj.put(snowball.getUniqueId(), player);
    13.  
    14. if(snowball instanceof Snowball){
    15. ((Snowball) snowball).setShooter(player);
    16. }
    17. }
     
  13. Offline

    LinearLogic

    Well if you're setting the projectile's shooter, you don't need to do all that I mentioned...
     
  14. Offline

    xCyanide

    LinearLogic
    I didn't even notice it until you pointed that out :p
     
Thread Status:
Not open for further replies.

Share This Page