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
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?
xCyanide Use egg.setShooter(); if you shoot the egg and then to all above Stevenpcc LinearLogic xCyanide
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.
LinearLogic Can you give me an example of how I would store and retrieve it? What would I use?(HashMap or something)
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.
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
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).
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 public void shootSnowball(Double speed, Player player) { Location shootLocation = player.getEyeLocation(); Vector directionVector = shootLocation.getDirection().normalize(); double startShift = 2; Vector shootShiftVector = new Vector(directionVector.getX() * startShift, directionVector.getY() * startShift, directionVector.getZ() * startShift); shootLocation = shootLocation.add(shootShiftVector.getX(), shootShiftVector.getY(), shootShiftVector.getZ()); Snowball snowball = shootLocation.getWorld().spawn(shootLocation, Snowball.class); snowball.setVelocity(directionVector.multiply(speed)); proj.put(snowball.getUniqueId(), player); if(snowball instanceof Snowball){ ((Snowball) snowball).setShooter(player); } }