Yaw and Pitch for SIMPLE Hub Plugin

Discussion in 'Spigot Plugin Development' started by Reprisal, Aug 21, 2016.

Thread Status:
Not open for further replies.
  1. Hello,

    I am fairly new to the Development community, and have been working on and Developing "Test Plugins" for a couple weeks now. I recently started making an extremely simple Hub plugin. In this plugin you simply "/sethub" in your Hub World, and users can simply type in "/Hub" in any other worlds, and be teleported back to the Hub World. Now you can simply do "X, Y, Z" and call it a day, but my OCD says you need to be exactly on the spawn! EXACTLY! So, this is where Yaw and Pitch come in. The problem is I'm having difficulty getting it to function. Every time I try and type,

    "player.teleport(x, y, z, pitch, yaw);"

    it gives an error on "teleport" saying,

    "The method teleport(Location, PlayerTeleportEvent.TeleportCause) in the type Entity is not applicable for the arguments (double, double, double, double, double)"

    Let me tell you I've tried many solutions on the web. None seem to have worked. Maybe I'm just missing something, or I am just literally stupid.

    Here is my Coding as of right now. Again, let me say it is kind of a mess since I've been fooling with a bunch of stuff to get this simple thing to work.

    Code (Text):
    package me.reprisal;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    public class Hub extends JavaPlugin {
           
            public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
                   
                    Player p = (Player) sender;
                   
                    if (cmd.getName().equalsIgnoreCase("SetHub")) {
                            getConfig().set("spawn.world", p.getLocation().getWorld().getName());
                            getConfig().set("spawn.x", Double.valueOf(p.getLocation().getX()));
                            getConfig().set("spawn.y", Double.valueOf(p.getLocation().getY()));
                            getConfig().set("spawn.z", Double.valueOf(p.getLocation().getZ()));
                            getConfig().set("spawn.pitch", Double.valueOf(p.getLocation().getPitch()));
                            getConfig().set("spawn.yaw", Double.valueOf(p.getLocation().getYaw()));
                            p.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "The Hub has been successfully set!");
                            return true;
                    }
                   
                    if (cmd.getName().equalsIgnoreCase("Hub")) {
                        if (getConfig().getConfigurationSection("Hub") == null) {
                                    p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "The spawn has not been set!");
                                    return true;
                            }
                            World w = Bukkit.getServer().getWorld(getConfig().getString("spawn.world"));
                            double x = getConfig().getDouble("spawn.x");
                            double y = getConfig().getDouble("spawn.y");
                            double z = getConfig().getDouble("spawn.z");
                            double pitch = getConfig().getDouble("Pitch");
                            double yaw = getConfig().getDouble("Yaw");
                            p.teleport(x, y, z, pitch, yaw);
                            p.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "You have been successfully teleported to the Hub!");
                    }
                    return true;
            }
    }

    Thanks in advance to any help you give!
    I'll try my best to answer any questions you have!
     
  2. Choco

    Moderator

    You realize your issue is literally outlined in the sentence you just typed out, right?
    Entity#teleport() takes (Location, TeleportCause) parameters, and not (double, double, double, double, double) parameters
     
  3. You realize I'm a nub, and I don't know what that means. :p
     
  4. Choco

    Moderator

    [​IMG]
    Okay, baby steps.

    - You're trying to do, "player.teleport(x, y, z, pitch, yaw)"
    - player.teleport() doesn't take (x, y, z, pitch, yaw) parameters. It takes (Location, TeleportCause) parameters

    I cannot get much clearer than that

    EDIT: This is what Javadocs are for
    https://hub.spigotmc.org/javadocs/s...ent.player.PlayerTeleportEvent.TeleportCause)
     
  5. Here, I edited your code for you, I generally don't do this.
    Code not tested and may not work
    Code (Text):

    package me.itsjay.test;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    public class Hub extends JavaPlugin {
           
            public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
                   
                    Player p = (Player) sender;
                   
                    if (cmd.getName().equalsIgnoreCase("SetHub")) {
                            getConfig().set("spawn.world", p.getLocation().getWorld().getName());
                            getConfig().set("spawn.x", Double.valueOf(p.getLocation().getX()));
                            getConfig().set("spawn.y", Double.valueOf(p.getLocation().getY()));
                            getConfig().set("spawn.z", Double.valueOf(p.getLocation().getZ()));
                            getConfig().set("spawn.pitch", Double.valueOf(p.getLocation().getPitch()));
                            getConfig().set("spawn.yaw", Double.valueOf(p.getLocation().getYaw()));
                            p.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "The Hub has been successfully set!");
                            return true;
                    }
                   
                    if (cmd.getName().equalsIgnoreCase("Hub")) {
                        if (getConfig().getConfigurationSection("Hub") == null) {
                                    p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "The spawn has not been set!");
                                    return true;
                            }
                            World w = Bukkit.getServer().getWorld(getConfig().getString("spawn.world"));
                            double x = getConfig().getDouble("spawn.x");
                            double y = getConfig().getDouble("spawn.y");
                            double z = getConfig().getDouble("spawn.z");
                            float pitch = (float) getConfig().getDouble("Pitch");
                            float yaw = (float) getConfig().getDouble("Yaw");
                            Location tploc = new Location(w, x, y, z);
                            tploc = new Location(w, x, y, z);
                            tploc.setPitch(pitch);
                            tploc.setYaw(yaw);
                            p.teleport(tploc);
                            p.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "You have been successfully teleported to the Hub!");
                    }
                    return true;
            }
    }
     
    You were using the wrong values, Pitch and Yaw are floats, not doubles btw.
    For this you would want to create a location, set pitch and yaw then teleport the player to that location.
     
    • Funny Funny x 1
  6. Choco

    Moderator

    I just find this utterly hilarious. You're declaring a new location, redeclaring it, then settings its pitch and yaw separately even though there's a constructor to provide such values
    Code (Java):
    Location location = new Location(world, x, y, z, pitch, yaw);
    https://hub.spigotmc.org/javadocs/s....World, double, double, double, float, float)

    Also, do not spoonfeed... Not only is it bad for the OP, you're teaching horrible habits with the code you provided.
    1. Naming conventions are awfully written. Needed fixing (That was in the OP's thread)
    2. Didn't check if instanceof Player before casting. Needed fixing (That was in OP's thread)
    3. Horrible way to set location values
    4. Get configuration values, store them, and use when necessary. Saves grabbing information from the configuration file every single time the "/hub" command is executed
     
    • Agree Agree x 1
  7. Yeah, you're right, I wasn't paying much attention.
    Code (Text):
    double x = getConfig().getDouble("spawn.x");
    double y = getConfig().getDouble("spawn.y");
    double z = getConfig().getDouble("spawn.z");
    float pitch = (float) getConfig().getDouble("Pitch");
    float yaw = (float) getConfig().getDouble("Yaw");
    p.teleport(new Location(w, x, y, z, pitch, yaw));
    Edit: ^ read what he said, I'm just trying to get your code to work but its far from perfect.
     
  8. Jesus.
    Jesus. Some people. If the OP didn't see his problem when you first said it accepts a Location object, he won't now.

    When using Entity#teleport method, you need to create a new Location object. Use "new Location(world, x, y, z, yaw, pitch)" where world is World object (See LivingEntity#getWorld() , also see Bukkit.getServer().getWorld(world)).

    May I suggest this?
     
    • Winner Winner x 1

  9. I've tried this before, but now it wants me to remove Yaw and Pitch...
     
  10. Choco

    Moderator

    Make sure yaw and pitch are floats, not doubles
     
    • Agree Agree x 1
Thread Status:
Not open for further replies.