Teleporting players in the air

Discussion in 'Plugin Development' started by UnRatable, Dec 27, 2014.

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

    UnRatable

    Ok so i've been messing with teleporting.. heres the code I currently got
    Code:
         private int getLocX(Player player) {
             return player.getLocation().getBlockX();
         }
        
         private int getLocY(Player player) {
             return player.getLocation().getBlockY();
         }
        
         private int getLocZ(Player player) {
             return player.getLocation().getBlockZ();
         }
    Now im stuck.. I want to teleport the player into the air when he is damaged by another player.. But I cant seem to figure out how to add to the getLocY. If you know of a method to add values to these please tell me.
    Example
    getLocY gets Y: 66 but I want to add say 5 blocks to it then it would be Y: 71.
     
  2. Offline

    HeadGam3z

    You could use add(), but you'd need to change your method around a little bit.
     
  3. Offline

    1Rogue

    Those methods are essentially useless facade methods, you already have access to the player object and thus their entire location:

    Code:java
    1. player.teleport(player.getLocation().add(0, 5, 0)); //Teleport 5 blocks higher from current location
     
  4. Offline

    UnRatable

    @HeadGam3z What do you mean change it around a bit?

    Heres my event and the rest of the code
    Code:
         private int getLocX(Player player) {
             return player.getLocation().getBlockX();
         }
       
         private int getLocY(Player player) {
             return player.getLocation().getBlockY();
         }
       
         private int getLocZ(Player player) {
             return player.getLocation().getBlockZ();
         }
       
         @EventHandler
         public void onPunch(EntityDamageByEntityEvent e){
         if ((e.getEntity() instanceof Player) && (e.getDamager() instanceof Player)){
         Player damagee = (Player) e.getEntity();
         Player damager = (Player) e.getDamager();
         damagee.teleport(getLocX(damagee), getLocY(damagee), getLocZ(damagee));
         damager.sendMessage("Thrown");
             }
         }
    EDIT: Oh ok you didnt add the method earlier :D
     
    Last edited: Dec 27, 2014
  5. Offline

    HeadGam3z

    What I mean is, you cannot invoke the method add() on method that returns an int.

    Any who, I recommend what @1Rogue said.
     
  6. Offline

    UnRatable

    Yep @1Rogue thanks I got it working. Im just wondering if it would be possible to make it more natural to launch the player instead of just teleporting the damagee
     
  7. Offline

    HeadGam3z

    Increase the player's y every x milliseconds.
     
  8. Offline

    Skionz

    @UnRatable Use Player#setVelocity(Vector) to make it look smooth.
     
  9. Offline

    UnRatable

    @Skionz Oh ok I didn't think of that :D thanks for the quick response :)
     
    Skionz likes this.
  10. Offline

    Experminator

    @UnRatable
    Code:
    @EventHandler
    public void onPunch(EntityDamageByEntityEvent e){
       if((e.getEntity() instanceof Player) && (e.getDamager() instanceof Player)){
            Player player = (Player) e.getEntity();
            Player damager = (Player) e.getDamager();
           
            player.getLocation().setVelocity(new Velocity(0, 5, 0)); // Throw Player Smoothly Into the Air.
            damager.sendMessage(player.getName() + " Thrown Into The Air."); // Send message to the thrower.
        }
    }
     
  11. Offline

    UnRatable

    @Experminator That doesent really work. player.getLocation().setVelocity(new Velocity (0, 5, 0)); The red part does not work in eclipse for me for some reason.. My current code is
    Code:
         @EventHandler
         public void onPunch(EntityDamageByEntityEvent e){
         if ((e.getEntity() instanceof Player) && (e.getDamager() instanceof Player)){
         Player damagee = (Player) e.getEntity();
         Player damager = (Player) e.getDamager();
         damager.sendMessage(ChatColor.GREEN + "Working");
         damagee.setVelocity(new Vector(damagee.getVelocity().getX(), 1.50, damagee.getVelocity().getZ()));
             }
         }
    This works but sometimes it doesent. Its quite weird.. The message is to see if the thing works or not
     
  12. Offline

    1Rogue

    That's because .setVelocity() takes a Vector. Velocity isn't a defined class.
     
  13. Offline

    UnRatable

    @1Rogue well.. I changed Velocity to Vector but it asks me for a cast.. Should I add it to make it work or should I do something else? :)
     
  14. Offline

    xTigerRebornx

    @UnRatable Make sure you've imported the right Vector. You want Bukkit's Vector, not Java's.
     
  15. Offline

    UnRatable

Thread Status:
Not open for further replies.

Share This Page