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); } }
TCO_007 aside the unchecked casting, do you have the EventHandler annotation on the method and have you registered your Listener
Oh fail lol This is what I have now but it says there is an error with the teleport. Code:java @EventHandler 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 = (Location)shooter.getLocation(); Location DamagedLocation = (Location)hit.getLocation(); shooter.teleport(DamagedLocation); hit.teleport(ShooterLocation); } } fireblast709 EDIT by Moderator: merged posts, please use the edit button instead of double posting.
TCO_007 You still have unchecked casting. What is the error? You can also pass Entity to the teleport parameter.
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 @EventHandler 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 = (Location)shooter.getLocation(); Location DamagedLocation = (Location)hit.getLocation(); shooter.teleport((Entity)DamagedLocation); hit.teleport((Entity)ShooterLocation); } } EDIT by Moderator: merged posts, please use the edit button instead of double posting.
TCO_007 You cast Snowball to damaged, Player to shooter, Player to entity without checking first. teleport can accept entity instance as a parameter.
So should it look like this Gater12 Code:java @EventHandler 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 = (Location)shooter.getLocation(); Location DamagedLocation = (Location)hit.getLocation(); shooter.teleport((Snowball)DamagedLocation); hit.teleport((Player)ShooterLocation); } }
TCO_007 final Player shooter =(Player) snow.getShooter(); final Player hit =(Player) e.getEntity(); This should go inside the if statement
ok Thank you! Ill test it now to see if it works. Like this correct? Code:java @EventHandler public void onReverser(EntityDamageByEntityEvent e){ Snowball snow = (Snowball) e.getDamager(); if (e.getEntity() instanceof Player && e.getDamager() instanceof Snowball){ final Player shooter =(Player) snow.getShooter(); final Player hit =(Player) e.getEntity(); Location ShooterLocation = (Location)shooter.getLocation(); Location DamagedLocation = (Location)hit.getLocation(); shooter.teleport((Snowball)DamagedLocation); hit.teleport((Player)ShooterLocation); } }
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.