Solved Teleport to a player in your crosshair

Discussion in 'Plugin Development' started by Uhlala, Feb 5, 2017.

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

    Uhlala

    I would like to know how to teleport to a player in my crosshair, but only when I reach 100 exp with an specific sword, after getting tped set level to 0 (Zero)

    Code:
    package me.lib.mw;
    import java.util.ArrayList;
    import java.util.Arrays;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class Enderman
      implements Listener
    {
           
       
       
      @EventHandler
      void rightClick(PlayerInteractEvent e)
      {
    
         
          ItemStack ed = new ItemStack(Material.IRON_SWORD);
          ItemMeta im = ed.getItemMeta();
          im.setDisplayName(ChatColor.DARK_PURPLE + "Enderman Sword");
          im.setLore(Arrays.asList(ChatColor.DARK_PURPLE + "Botao Direito para ativar skill"));
          ed.setItemMeta(im);
           Player p = e.getPlayer();
         
        if ((e.getAction() == Action.RIGHT_CLICK_AIR) || (e.getAction() == Action.RIGHT_CLICK_AIR)  && (p.getLevel() == 100))
        {
            ItemStack item = new ItemStack(Material.IRON_SWORD, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.DARK_PURPLE + "Enderman Sword");
            java.util.List<String> lore = new ArrayList<String>();
              lore.add(ChatColor.DARK_PURPLE + "Botao Direito para ativar skill");
              meta.setLore(lore);
              item.setItemMeta(meta);
    
              if(p.getItemInHand().hasItemMeta()){
                if(p.getItemInHand().getItemMeta().hasDisplayName()){
                  if(p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.DARK_PURPLE + "Enderman Sword")){
           
            PotionEffect effect = new PotionEffect(PotionEffectType.SPEED, 20, 4, false, false);
            p.addPotionEffect(effect);
            p.setLevel(0);
            p.sendMessage(ChatColor.GREEN + "Teleported");
            p.getWorld().playSound(p.getLocation(), Sound.ENDERMAN_TELEPORT, 20.0F, 20.0F);
            PotionEffect fr = new PotionEffect(PotionEffectType.SPEED, 5 * 20, 2, false, false);
            p.addPotionEffect(fr);
         }
         }
       }
      }
    }
    }
    `
     
  2. Offline

    S1ant

    Code:
      if ((e.getAction() == Action.RIGHT_CLICK_AIR) || (e.getAction() == Action.RIGHT_CLICK_AIR)  && (p.getLevel() == 100)) 
    Wait so, You want to run the code when they click the air OR Click the air with 100 levels? Did you mean to make it
    Code:
      if ( (e.getAction() == Action.RIGHT_CLICK_AIR)  && (p.getLevel() == 100)) {
    //Code Here
    }
    ?
     
  3. Offline

    mehboss

    @Uhlala
    1. Check to see if there is a player in your line of sight.
    2. Check if your xp bar experience >= 100.
    3. Teleport to them.
    4. Set your experience levels to 0/reset them.


    Sent from my iPhone using Tapatalk
     
  4. Offline

    Uhlala

    to click in the air with 100 levels
     
  5. Offline

    mehboss

    @Uhlala
    Has your question been answered?

    Also fix this line up if you haven't already.
    Code:
     if ((e.getAction() == Action.RIGHT_CLICK_AIR) || (e.getAction() == Action.RIGHT_CLICK_AIR)  && (p.getLevel() == 100))
    You are doing the if (e.getAction() == Action.RIGHT_CLICK_AIR) twice. :p
     
    Rayzr522 likes this.
  6. Offline

    mine2012craft

    Create a for loop. Loop it however many times you want depending on what distance you want.

    Get the player's eye location and direction

    Add the players direction to the players location

    Inside the for loop, create another for loop that loops through all the players in the locations chunk (or world), and name the first for loop you created "outer loop" or something else

    In that loop, check if the entities distance from the loop is less than 1 or 1.5 or whatever hit distance

    Check if it is a livingentity

    If so, get the entities location, and teleport the player, and break your outerloop by using "break outer loop"

    Better explanation will come soon, as I'm on my phone
     
  7. Offline

    Uhlala

    Kinda . How do I check if someone is in my sight and teleport to the same block that they are? Could you show me the code please?
     
  8. Offline

    Zombie_Striker

    @Uhlala
    Nope. No one will spoonfeed you code here. We are here to help your problem and teach you how to solve these problems on your own in the future. We can't do that if you have to rely on us for code.

    Here is what you need to do:
    1. Create a new Location. This is the location of where the tests will be done.
    2. create a for loop. This represents the distance from the player's face.
    3. Every time it loops, add a "noramlized" version of the player's eyesight to the location above (get the normalized direction by using Player.getLocation().getDirection.normalize())
    4. if the block at that new location is not equal to air, break out of the loop. The player is looking at a block.
    5. After that, for loop through all the players in that world.
    6. If the distance between location is less than 0.5, the player is looking at that player. Get the distance by using player.getLocation().distance(other player.getLocation);
     
    Uhlala likes this.
  9. Offline

    Rayzr522

    @Zombie_Striker
    You don't need to use a for loop to check if they're looking at a block, just use getTargetBlock, pass it null and a distance, and check if the returned block's type is AIR. If it's not AIR, then you also happen to have the location of the block which you can use for checking distance to players. You don't even need to do that though, as there is a getNearbyEntities method in World. There are much easier ways to do what you're saying ;)
     
    Zombie_Striker and mehboss like this.
  10. Offline

    ipodtouch0218

    @Rayzr522
    How would that work? If you have a player right in front of you, getTargetBlock won't return null, but the block that you WOULD be looking at. You can't tell if the vector ever intersects with a player. Also, you can't use getTargetBlock to get the player because they won't be at the nearest block.
     
  11. Offline

    Rayzr522

    @ipodtouch0218 I'm aware of the flaws, I am simply suggesting how to improve @Zombie_Striker 's suggestion. What I said would do exactly what he was suggesting (if I understood him correctly), just with about 1/15 of the code.

    Maybe I understood wrong?
     
    Zombie_Striker likes this.
  12. Offline

    mine2012craft

    @Uhlala Like Zombie_Striker said, we won't spoonfeed you. I'm sure you can follow our steps simply

    Remember, For Loops basically loop through a collection of items, such as a list, or loop through numbers.

    For Numbers, it looks similar to this:

    Code:
    for (int i = 0; i < 21 ; i += 1){
    //Stuff Here
    }
    In this example, this keeps adding 1 to the int, "i" until it is greater than, or equal two, 21. As long as it is not, it will keep executing the code within the for loop. In this instance, this is what you need for your example.
     
Thread Status:
Not open for further replies.

Share This Page