Switching Snowballs Help?

Discussion in 'Plugin Development' started by vtg_the_kid, Sep 26, 2013.

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

    vtg_the_kid

    I'm trying to make it so when a guy throws a snowball at another guy they switch positions, why doesn't this work

    Code:java
    1. public void switchPlaces(Player p1, Player p2) {
    2. Location l1 = p1.getLocation();
    3. p1.teleport(p2.getLocation());
    4. p2.teleport(l1);
    5. }
    6.  
    7. @EventHandler(priority = EventPriority.HIGHEST)
    8. public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    9. Player entity = (Player) event.getDamager();
    10. Player entity2 = (Player) event.getEntity();
    11. if (entity instanceof Snowball) {
    12. switchPlaces(entity, entity2);
    13. }
    14. }

    please ignore my bad usage of variable names
     
  2. Offline

    CrazyGuy3000

    Didn't have time to test this but I think this works...
    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST)
    2. public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    3. Player entity = (Player) event.getDamager();
    4. Player entity2 = (Player) event.getEntity();
    5. Location entityloc = entity.getLocation();
    6. Location entity2loc = entity2.getLocation();
    7. entity.teleport(entity2loc);
    8. entity2.teleport(entityloc);
    9. }


    EDIT: Sorry, cleaned the code and fixed a typo
     
  3. Offline

    blablubbabc

    vtg_the_kid Make sure, that the EntityDamageByEntity is actually called on your server. It isn't for example called, if the hit player is in creative mode, or the server has pvp turned off in the server.properties or the world.
     
  4. the damager is a snowballs and not a player when a snowball hits another players, so
    Player entity = (Player) event.getDamager();
    will fail
     
  5. Offline

    CakeProgrammer

    try this :
    Code:java
    1. @EventHandler
    2. public void EntityDamageByEntity(EntityDamageByEntityEvent event){
    3. Player player = (Player) event.getEntity();
    4. if(event.getDamager() instanceof Snowball){
    5. Snowball snowball = (Snowball) event.getDamager();
    6. Location locationShooter = snowball.getShooter().getLocation();
    7. Location playerLocation = player.getLocation();
    8. snowball.getShooter().teleport(playerLocation);
    9. player.teleport(locationShooter);
    10. }
    11. }
     
Thread Status:
Not open for further replies.

Share This Page