Switcher Kit Coding

Discussion in 'Plugin Development' started by TCO_007, Jun 18, 2014.

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

    TCO_007

    Hello! I am trying to code the kit switcher which most are familiar with. What I want to ask though is why will it not teleport the players here in this code? It says its wrong but I really dont see that as the case. Can anyone help? Thanks!
    Code:
    public void onReverser(EntityDamageByEntityEvent e){
            Snowball snow = (Snowball) e.getDamager();
            final Player shooter = (Player) snow.getShooter();
            final Player hit =(Player) e.getEntity();
            if (e.getEntity() instanceof Player && e.getDamager() instanceof Snowball){
                Location ShooterLocation = shooter.getLocation();
                Location DamagedLocation = hit.getLocation();
                shooter.teleport(DamagedLocation);
                hit.teleport(ShooterLocation);
            }
        } 
     
  2. Offline

    fireblast709

    TCO_007 aside the unchecked casting, do you have the EventHandler annotation on the method and have you registered your Listener
     
  3. Offline

    TCO_007

    Oh fail lol
    This is what I have now but it says there is an error with the teleport.
    Code:java
    1. @EventHandler
    2. public void onReverser(EntityDamageByEntityEvent e){
    3. Snowball snow = (Snowball) e.getDamager();
    4. final Player shooter = (Player) snow.getShooter();
    5. final Player hit =(Player) e.getEntity();
    6. if (e.getEntity() instanceof Player && e.getDamager() instanceof Snowball){
    7. Location ShooterLocation = (Location)shooter.getLocation();
    8. Location DamagedLocation = (Location)hit.getLocation();
    9. shooter.teleport(DamagedLocation);
    10. hit.teleport(ShooterLocation);
    11. }
    12. }


    fireblast709

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  4. Offline

    Gater12

    TCO_007
    You still have unchecked casting.

    What is the error? You can also pass Entity to the teleport parameter.
     
  5. Offline

    TCO_007

    I am new to coding so let me ask this, what is unchecking casting? I thought I fixed it by casting Location but you guys say I didnt so I should ask what havent I casted? How can I "pass" Entity to the teleport? Do you mean as a cast?
    Gater12

    Do you mean like this?
    Code:java
    1. @EventHandler
    2. public void onReverser(EntityDamageByEntityEvent e){
    3. Snowball snow = (Snowball) e.getDamager();
    4. final Player shooter = (Player) snow.getShooter();
    5. final Player hit =(Player) e.getEntity();
    6. if (e.getEntity() instanceof Player && e.getDamager() instanceof Snowball){
    7. Location ShooterLocation = (Location)shooter.getLocation();
    8. Location DamagedLocation = (Location)hit.getLocation();
    9. shooter.teleport((Entity)DamagedLocation);
    10. hit.teleport((Entity)ShooterLocation);
    11. }
    12. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  6. Offline

    Gater12

    TCO_007
    You cast Snowball to damaged, Player to shooter, Player to entity without checking first.
    teleport can accept entity instance as a parameter.
     
  7. Offline

    TCO_007

    So should it look like this Gater12
    Code:java
    1. @EventHandler
    2. public void onReverser(EntityDamageByEntityEvent e){
    3. Snowball snow = (Snowball) e.getDamager();
    4. final Player shooter = (Player) snow.getShooter();
    5. final Player hit =(Player) e.getEntity();
    6. if (e.getEntity() instanceof Player && e.getDamager() instanceof Snowball){
    7. Location ShooterLocation = (Location)shooter.getLocation();
    8. Location DamagedLocation = (Location)hit.getLocation();
    9. shooter.teleport((Snowball)DamagedLocation);
    10. hit.teleport((Player)ShooterLocation);
    11. }
    12. }
    13.  
     
  8. Offline

    LordVakar

    TCO_007
    final Player shooter =(Player) snow.getShooter();
    final Player hit =(Player) e.getEntity();
    This should go inside the if statement
     
  9. Offline

    TCO_007

    ok Thank you! Ill test it now to see if it works. Like this correct?
    Code:java
    1. @EventHandler
    2. public void onReverser(EntityDamageByEntityEvent e){
    3. Snowball snow = (Snowball) e.getDamager();
    4. if (e.getEntity() instanceof Player && e.getDamager() instanceof Snowball){
    5. final Player shooter =(Player) snow.getShooter();
    6. final Player hit =(Player) e.getEntity();
    7. Location ShooterLocation = (Location)shooter.getLocation();
    8. Location DamagedLocation = (Location)hit.getLocation();
    9. shooter.teleport((Snowball)DamagedLocation);
    10. hit.teleport((Player)ShooterLocation);
    11. }
    12. }
     
  10. Offline

    Necrodoom

    TCO_007 try to understand what are you trying to do with the if check, it doesn't seem you understand what is it for. You are casting damager to snowball BEFORE you make sure it is one.
     
Thread Status:
Not open for further replies.

Share This Page