Teleport set Yaw set Pitch

Discussion in 'Plugin Development' started by BrainyXS, Nov 15, 2017.

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

    BrainyXS

    Code:
    los.setPitch(p.getPitch());
     los.setYaw(p.getYaw());
    It dont workd please help me, how i do that?
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    BrainyXS

    Code:
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.BlockFace;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.Bukkit;
    import org.bukkit.Sound;
    
    
    public class teleport extends JavaPlugin implements Listener {
        @EventHandler
        public void onEvent(PlayerInteractEvent event){
            ItemStack gegenstand = event.getItem();
            ItemMeta metaData = gegenstand.getItemMeta();
            Player p = event.getPlayer();
              
            if(metaData.getDisplayName().equals("Teleportzauberstab")){
                p.playSound(p.getLocation(), Sound.ENTITY_ENDERMEN_TELEPORT, 2F, 1F);
                Block bl = event.getClickedBlock();
                Location los = bl.getLocation();
                los.setX(los.getX() + 0.5);
                los.setZ(los.getZ() + 0.5);
                los.setY(los.getY() + 1);
                los.setPitch(p.getPitch());
                los.setYaw(p.getYaw());
                p.teleport(los);
            }
        }
        public void onEnable() {
            this.getLogger().info("Teleportstab");
            ItemStack Teleportzauberstab = new ItemStack(Material.STICK);
            ItemMeta metaData = Teleportzauberstab.getItemMeta();
            metaData.setDisplayName("Teleportzauberstab");
            Teleportzauberstab.setItemMeta(metaData);
            Teleportzauberstab.addUnsafeEnchantment(Enchantment.LUCK, 1);
          
            ShapedRecipe TeleportzauberstabRezept = new ShapedRecipe(Teleportzauberstab);
            TeleportzauberstabRezept.shape("BPB", "USU", "USU");
            TeleportzauberstabRezept.setIngredient('B', Material.BLAZE_ROD);
            TeleportzauberstabRezept.setIngredient('P', Material.ENDER_PEARL);
            TeleportzauberstabRezept.setIngredient('U', Material.BLAZE_POWDER);
            TeleportzauberstabRezept.setIngredient('S', Material.STICK);
            this.getServer().addRecipe(TeleportzauberstabRezept);
              
            PluginManager pluginManager = this.getServer().getPluginManager();
            pluginManager.registerEvents(this, this);
        }
    }
     
    Last edited by a moderator: Nov 15, 2017
  4. Offline

    timtower Administrator Administrator Moderator

    @BrainyXS I don't see those calls in this code.
     
  5. Offline

    BrainyXS

    sorry, i have postet the wrong code, here is the new
     
  6. Offline

    timtower Administrator Administrator Moderator

    @BrainyXS What are the values when you print them to the console?
     
  7. Offline

    MightyOne

    @BrainyXS the class player does not have the methods getYaw() or getPitch() thats for Locations only. And if i got it right check again if the held item even has a displayname. I already told yoo that once...
     
  8. Offline

    BrainyXS

    no matter, i have 1000 Plugins without that, on evey rightcklick my console will be spamed witch 10000 Errors, I only code for my fun, not for server...
     
  9. Offline

    MightyOne

    @BrainyXS of course that is your thing but for your information about standards that is not a clean code... i guess. I am no expert
     
  10. Offline

    Tabuu_

    Location location = new Location(world, x, y, z, pitch, yaw);
     
  11. Offline

    Blares

    Do you really need to set the pitch and yaw.

    By the way if you want to get it from config which is very efficient do this:

    Code:
    
    config.yml:
    
    Spawn:
      X: 165
      Y: 235
      Z: 124
    
    class:
    
    World w = p.getWorld();
    
    int x = this.getConfig().getInt("Spawn.X");
    int y = this.getConfig().getInt("Spawn.Y");
    int z = this.getConfig().getInt("Spawn.Z");
    
    //if this is an error and config and all that. Make a method in your main class that returns an instance of your main class / plugin.
    
    p.teleport(w, x, y, z);
    
    I don't think you need to set yaws and pitches.
     
  12. Offline

    Tabuu_

    There are my functions for converting location in a config.

    Code:
        public static Location stringToLocation(String string) {
            String[] args = string.split(" ");
    
            World world = Bukkit.getWorld(args[0]);
            int x = Integer.parseInt(args[1]), y = Integer.parseInt(args[2]), z = Integer.parseInt(args[3]);
    
            float yaw = 0f, pitch = 0f;
    
            if (args.length > 4) {
                yaw = Float.parseFloat(args[4]);
                pitch = Float.parseFloat(args[5]);
            }
    
            return new Location(world, x, y, z, yaw, pitch);
        }
    
        public static String locationToString(Location location) {
            String world = location.getWorld().getName();
    
            int x = location.getBlockX(), y = location.getBlockY(), z = location.getBlockZ();
    
            float yaw = location.getYaw(), pitch = location.getPitch();
    
            return world + " " + x + " " + y + " " + z + " " + yaw + " " + pitch;
        }
    EDIT:
    The input/output would look like this: world x y z yaw pitch
     
  13. Objects can have more than one constructor, of which the Location object has 2:

    Location(World world, double x, double y, double z)
    Constructs a new Location with the given coordinates
    Location(World world, double x, double y, double z, float yaw, float pitch)
    Constructs a new Location with the given coordinates and direction

    and in the OP's post he doesn't really state anything about configuration files.

    @BrainyXS try this:

    Code:
    Block bl = event.getClickedBlock();
    Location los = bl.getLocation();
    
    p.teleport(new Location(p.getWorld(), los.getX()+0.5, los.getY()+0.5, los.getZ()+ 1, p.getPitch(), p.getYaw());
    That just cleans your code up a little, and adds the world.
     
  14. Offline

    BrainyXS

    it works anything with this:

     
Thread Status:
Not open for further replies.

Share This Page