Solved Setspawn facing specific direction

Discussion in 'Plugin Development' started by KarimAKL, Mar 8, 2018.

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

    KarimAKL

    As you can probably tell from the title i wanna make a plugin where the player spawns facing a specific direction when using /spawn (like in essentials) but i wanna try to make it instead of just downloading essentials. How would i do this?
     
  2. Location#setYaw and Location#setPitch for direction. Or just serialize a location into config in like /setspawn and desert deserialize in /spawn. It'll set direction automatically.

    Sent from my SM-G903F using Tapatalk
     
  3. Offline

    KarimAKL

    Okay, i'll try looking up how to do that, thanks for the help. :)

    Okay i just looked it up and i can't seem to find anyone using it the way i want to so it's kinda hard to get an idea of how to do it. I want to get the yaw and pitch of the player and set the spawn to teleport them to the xyz with the yaw and pitch the right place.
    This is my class right now:
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 8, 2018
  4. Offline

    MattTheBeast

    @KarimAKL world.setSpawnLocation() does not let you specify yaw or pitch, if you whant to specify a yaw and pitch you will need to save your own spawn location, then teleport them to that location when they /spawn or respawn.
     
  5. Offline

    KarimAKL

    How do i do that? :/ Sorry for asking so much about so many things but i’m new to this and don’t know much about it.
     
  6. Offline

    MattTheBeast

    @KarimAKL well you will need to save your spawn location in a config, turn your location into a string, add it to your config, and load it when you need it.

    Here is some utils you can use to switch between strings and locations so you can save and load

    Code:
    
        public static String locationToString(Location location) {
            return location.getWorld().getName() + "," + location.getX() + "," + location.getY() + "," + location.getZ()
                    + "," + location.getYaw() + "," + location.getPitch();
        }
    
        public static Location stringToLocation(String string) {
            String[] split = string.split(",");
            try {
                Location location = new Location(Bukkit.getWorld(split[0]), Double.parseDouble(split[1]),
                        Double.parseDouble(split[2]), Double.parseDouble(split[3]), Float.parseFloat(split[4]),
                        Float.parseFloat(split[5]));
                return location;
            } catch (Exception exception) {
                System.err.println(string + " location not valid");
                exception.printStackTrace();
                return null;
            }
        }
    
    
     
  7. Offline

    KarimAKL

    A little late, sorry. Anyway, if i make a util with this code then how would i trigger it with a command? Like '/spawn'.
    And what should i write in my config.yml (or another file) for it to work?
     
  8. Offline

    KarimAKL

  9. Offline

    KarimAKL

  10. Offline

    user_91277742

    First, define the location to save like this
    Code:
            if(cmd.equalsIgnoreCase("setspawn")){
                String world = p.getWorld().getName();
                main.getConfig().set("Location.World", world);
                main.getConfig().set("Location.X", p.getLocation().getX());
                main.getConfig().set("Location.Y", p.getLocation().getY());
                main.getConfig().set("Location.Z", p.getLocation().getZ());
                main.getConfig().set("Location.Yaw", p.getLocation().getYaw());
                main.getConfig().set("Location.Pitch",p.getLocation().getPitch());
                main.saveConfig();
                main.reloadConfig();
             }
    
    With that code and this command, you are saving like a list , an spawn in your config file.

    Next, if you want to do the tp to that spawn like the command /spawn
    Just get the location + yaw + pitch in the config file like this
    Code:
          if(cmd.getName().euqlaignorecase("spawn")){
               float yaw = main.getConfig().getInt("Location.Yaw");
                float pitch = main.getConfig().getInt("Location.Pitch");
                Location loc = new Location(Bukkit.getWorld(main.getConfig().getString("Location.World")), main.getConfig().getDouble("Location.X"), main.getConfig().getDouble("Location.Y"), main.getConfig().getDouble("Location.Z"), yaw, pitch);
                        p.teleport(loc);
                  }
    
    I hope you understand this method :D
     
    Last edited by a moderator: Apr 18, 2018
  11. Offline

    KarimAKL

    @Ahrigumi Okay thanks, i'll try it now but could you tell me how i would do it with another yml file? Like a "locations.yml" or something like that?
    EDIT: I just tested it and it sets the location, yaw and pitch in the config as it should(i just wanna know how i would make it do like this:
    Code:
    Location:
      worldname:
        X: something
        Y: something
        Z: something
        Yaw: something
        Pitch: something
    
    )
    Also i have this error when i execute /spawn (/setspawn works fine although i can't teleport to it because of the /spawn error)
    Error:
    Code:
    >spawn
    [16:00:22 WARN]: Unexpected exception while parsing console command "spawn"
    org.bukkit.command.CommandException: Unhandled exception executing command 'spawn' in plugin Spawn v0.2
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: java.lang.IllegalArgumentException: Name cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.getWorld(CraftServer.java:1014) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.Bukkit.getWorld(Bukkit.java:496) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at me.karim.spawn.commands.CommandSpawn.onCommand(CommandSpawn.java:28) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            ... 8 more
    >spawn Karim09
    [16:00:25 WARN]: Unexpected exception while parsing console command "spawn Karim09"
    org.bukkit.command.CommandException: Unhandled exception executing command 'spawn' in plugin Spawn v0.2
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: java.lang.IllegalArgumentException: Name cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.getWorld(CraftServer.java:1014) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.Bukkit.getWorld(Bukkit.java:496) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at me.karim.spawn.commands.CommandSpawn.onCommand(CommandSpawn.java:28) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            ... 8 more
    
    If i use it as a player it comes with this message:
    "An internal error occurred while attempting to perform this command"
    And then the error comes in the console.
    My /spawn class:
    Code:
    public class CommandSpawn implements CommandExecutor {
       
        private Main plugin;
       
        public CommandSpawn(Main plugin) {
            this.plugin = plugin;
           
            plugin.getCommand("spawn").setExecutor(this);
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("spawn")) {
                float yaw = plugin.getConfig().getInt("Location.World.Yaw");
                float pitch = plugin.getConfig().getInt("Location.World.Pitch");
                Location loc = new Location(Bukkit.getWorld(plugin.getConfig().getString("Location.World")), plugin.getConfig().getDouble("Location.World.X"), plugin.getConfig().getDouble("Location.World.Y"), plugin.getConfig().getDouble("Location.World.Z"), yaw, pitch);
                if (args.length == 0) {
                    if (sender instanceof Player) {
                        Player p = (Player) sender;
                       
                        p.teleport(loc);
                        p.sendMessage(ChatColorUtil.chat("&aYou have teleported to spawn."));
                        return true;
                    } else {
                        sender.sendMessage(ChatColorUtil.chat("&cOnly players can execute this command!"));
                        return true;
                    }
                } else if (args.length == 1) {
                    Player target = Bukkit.getPlayer(args[0]);
                    if (target != null) {
                        target.teleport(loc);
                        sender.sendMessage(ChatColorUtil.chat("&aYou have teleported " + target + " to spawn."));
                        target.sendMessage(ChatColorUtil.chat("&aYou have been teleported to spawn."));
                        return true;
                    } else {
                        sender.sendMessage(ChatColorUtil.chat("&cPlayer not found."));
                        return true;
                    }
                } else {
                    sender.sendMessage(ChatColorUtil.chat("&cIncorrect usage! Use /spawn [player]"));
                    return true;
                }
            }
            return false;
        }
    
    }
    
     
    Last edited by a moderator: Apr 19, 2018
  12. Offline

    user_91277742

    @KarimAKL
    1º- To create a custom file like "locations.yml" is so easy, just create in the mainclass a public file like
    Code:
        public File locationFile = new File(this.getDataFolder() + "/locations.yml");
        public FileConfiguration locations = YamlConfiguration.loadConfiguration(locationFile);
    
    2º - The error is telling, you are trying to do a tp a player to a location "null" , so... make sure, you are getting the "correct way" when you do the comamnd . I want to say ..

    Code:
    "Location.World.Yaw"
    
    *Here, you are getting the yaw in the Location.World , BUT, look the caps in Location , World and Yaw .

    *If you have in your location.yml this structure
    Code:
    Location:
    worldname:
    X: something
    Y: something
    Z: something
    Yaw: something
    Pitch: something
    
    In worldname you dont have caps.

    * If the error persist , Try to register the command "/spawn" in your onEnable mainClass

    * If the error persist, try to change your oncommand structure like this..
    Code:
            if(sender instanceof Player) {
                Player p = (Player) sender;
                if(cmd.getName().equalsIgnoreCase("spawn")) {
                        float yaw = main.getConfig().getInt("Location.Yaw");
                        float pitch = main.getConfig().getInt("Location.Pitch");
                        Location loc = new Location(Bukkit.getWorld(main.getConfig().getString("Location.World")), main.getConfig().getDouble("Location.X"), main.getConfig().getDouble("Location.Y"), main.getConfig().getDouble("Location.Z"), yaw, pitch);
                        p.teleport(loc);
                        return true;
                    }
                }
                if(args[0].equalsIgnoreCase("setspawn")){
                   if(args.length == 1) {
                     //stuff
    
     
    Last edited by a moderator: Apr 19, 2018
  13. Offline

    KarimAKL

    @Ahrigumi Okay so now i know how to create a custom file but how would i get it in the command? (I wanna save the cords, yaw and pitch there instead of in the config) What o you mean "In worldname you dont have caps."? This is the config without me changing anything:
    Code:
    Location:
      World:
        X: 211.8973977411929
        Y: 65.0
        Z: -150.5141493593773
        Yaw: -165.49138
        Pitch: 3.3194127
    
    
    What do you mean "correct way"? Here is the /setspawn code:
    Code:
    public class CommandSetspawn implements CommandExecutor {
       
        private Main plugin;
       
        public CommandSetspawn(Main plugin) {
            this.plugin = plugin;
           
            plugin.getCommand("setspawn").setExecutor(this);
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("setspawn")) {
                if (sender instanceof Player) {
                    Player p = (Player) sender;
                    String world = p.getWorld().getName();
                   
                    plugin.getConfig().set("Location.World", world);
                    plugin.getConfig().set("Location.World.X", p.getLocation().getX());
                    plugin.getConfig().set("Location.World.Y", p.getLocation().getY());
                    plugin.getConfig().set("Location.World.Z", p.getLocation().getZ());
                    plugin.getConfig().set("Location.World.Yaw", p.getLocation().getYaw());
                    plugin.getConfig().set("Location.World.Pitch", p.getLocation().getPitch());
                    plugin.saveConfig();
                    plugin.reloadConfig();
                    p.sendMessage(ChatColorUtil.chat("&aSpawn set."));
                    return true;
                } else {
                    sender.sendMessage(ChatColorUtil.chat("&cOnly players can execute this command!"));
                    return true;
                }
            }
            return false;
        }
    
    }
    
    
    The /spawn code:
    Code:
    public class CommandSpawn implements CommandExecutor {
       
        private Main plugin;
       
        public CommandSpawn(Main plugin) {
            this.plugin = plugin;
           
            plugin.getCommand("spawn").setExecutor(this);
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("spawn")) {
                float yaw = plugin.getConfig().getInt("Location.World.Yaw");
                float pitch = plugin.getConfig().getInt("Location.World.Pitch");
                Location loc = new Location(Bukkit.getWorld(plugin.getConfig().getString("Location.World")), plugin.getConfig().getDouble("Location.World.X"), plugin.getConfig().getDouble("Location.World.Y"), plugin.getConfig().getDouble("Location.World.Z"), yaw, pitch);
                if (args.length == 0) {
                    if (sender instanceof Player) {
                        Player p = (Player) sender;
                       
                        p.teleport(loc);
                        p.sendMessage(ChatColorUtil.chat("&aYou have teleported to spawn."));
                        return true;
                    } else {
                        sender.sendMessage(ChatColorUtil.chat("&cOnly players can execute this command!"));
                        return true;
                    }
                } else if (args.length == 1) {
                    Player target = Bukkit.getPlayer(args[0]);
                    if (target != null) {
                        target.teleport(loc);
                        sender.sendMessage(ChatColorUtil.chat("&aYou have teleported " + target + " to spawn."));
                        target.sendMessage(ChatColorUtil.chat("&aYou have been teleported to spawn."));
                        return true;
                    } else {
                        sender.sendMessage(ChatColorUtil.chat("&cPlayer not found."));
                        return true;
                    }
                } else {
                    sender.sendMessage(ChatColorUtil.chat("&cIncorrect usage! Use /spawn [player]"));
                    return true;
                }
            }
            return false;
        }
    
    }
    
    
    The config is empty until /setspawn is used.
    And here is the main class:
    Code:
    public class Main extends JavaPlugin {
       
        public void onEnable() {
            new CommandSetspawn(this);
            new CommandSpawn(this);
            saveDefaultConfig();
        }
    
    }
    
    
    Also the util i got:
    Code:
    public class SpawnUtil {
       
        public static String locationToString(Location location) {
            return location.getWorld().getName() + "," + location.getX() + "," + location.getY() + "," + location.getZ()
                    + "," + location.getYaw() + "," + location.getPitch();
        }
    
        public static Location stringToLocation(String string) {
            String[] split = string.split(",");
            try {
                Location location = new Location(Bukkit.getWorld(split[0]), Double.parseDouble(split[1]),
                        Double.parseDouble(split[2]), Double.parseDouble(split[3]), Float.parseFloat(split[4]),
                        Float.parseFloat(split[5]));
                return location;
            } catch (Exception exception) {
                System.err.println(string + " location not valid");
                exception.printStackTrace();
                return null;
            }
        }
    
    }
    
    
    I hope this helps you a little more, thanks for the help. :)
     
  14. Offline

    user_91277742

    @KarimAKL
    To set the coords to the new yml file and get the coords is too easy.
    To set the coords
    Code:
      main.locations.set(blablabla);
    
    And to get the coords
    Code:
    main.locations.getDouble(blablabla);
    
    * The correct way i mean like this
    Code:
    Location.World.Z
    
    Is different if you put tha same but with no caps like this
    Code:
    Location.world.Z
    
    I really do not recommend use static for this.

     
  15. Offline

    KarimAKL

    @Ahrigumi Oh so i need to use plugin.locations.WhateverINeedToDo(blablabla) instead of plugin.getConfig().WhateverINeedToDo(blablabla)? I see, if that's the case then you are right, it is easy. :p Anyway, so by the "correct way" you meant that i needed to make all the "world" to "World" instead? Also i use static because i don't know how to get it without static. :/
    EDIT: Btw how would i do the "plugin.saveConfig();" with the custom one? Maybe: "plugin.saveConfig(locations);" or something like that?
     
    Last edited by a moderator: Apr 20, 2018
  16. Offline

    user_91277742

    @KarimAKL That its!
    I want to say to the "correct way" , If you save your coords like
    Code:
    Location.World.Z
    
    You need to get now the coords with
    Code:
    Location.World.Z
    
    not
    Code:
    Location.world.Z
    
    To save your custom yml
    just do this
    Code:
    class.locations.save(class.locationsFile);
    
    You can use a void method to remove your static , something like this
    Code:
        public void spawnMethod(Player p){
                String world = locationConfig.getString("Location.World");
                double x = locationConfig.getDouble("Location.X");
                double y = locationConfig.getDouble("Location.Y");
                double z = locationConfig.getDouble("Location.Z");
                Location loc = new Location(Bukkit.getWorld(world), x, y, z);
                p.teleport(loc);
            }
    
    Because , in your static method, are you trying to do that right?
     
  17. Offline

    KarimAKL

    @Ahrigumi I'm pretty sure that everywhere i have the string "Location.World.Z" it says it W in caps. I just tried saving my custom yml file like this:
    Code:
    plugin.locations.save(plugin.locationFile);
    
    But it comes with the following error:
    - "Unhandled exception type IOException"
    What does that mean?
    Also, what static are you talking about? The SpawnUtils class i have? If so then should i just remove it all and type that instead?
     
  18. Offline

    user_91277742

    @KarimAKL
    This error makes reference to
    Code:
    plugin.locations.save(plugin.locationFile);
    
    ?
     
  19. @KarimAKL
    Did you surround the save with a try catch?
    Also do plugin.locations.load (plugin.locationFile);
    And that needs a try catch as well
     
  20. Offline

    KarimAKL

    @Ahrigumi Yes it does. @Blackwing_Forged No i didn't, am i supposed to do that when using a custom yml file? Also that "load" method you mentioned is instead of the "plugin.reloadConfig();", right?
     
  21. @KarimAKL
    Yes you're supposed to surround the save and load method with try and catch

    try {
    plugin.locations.save(plugin.locationsFile);
    plugin.locations.load(plugin.locationsFile);
    } catch (IOException | InvalidConfigurationException e) {
    e.printStackTrace();
    }
     
  22. Offline

    KarimAKL

    @Blackwing_Forged Could you tell me what the try catch and the "} catch (IOException | InvalidConfigurationException e) {
    e.printStackTrace();
    }" does?
     
  23. @KarimAKL
    try means that it tries to do the code in the try block and if it succeeds then it continues but if it fails and has an error thats what catch is for. It catches the error and does the code inside the catch block instead of breaking your plugin
     
  24. Offline

    KarimAKL

    @Blackwing_Forged Okay, then what does the "
    (IOException | InvalidConfigurationException e)
    " do? (Also the e.printStackTrace(); )
     
  25. @KarimAKL
    Those are the error types that it looks for, so if the error type is an IOException or a InvalidConfigurationException then it will do the catch block, and e.printStackTrace() just prints the error in the console
     
  26. Offline

    KarimAKL

    @Blackwing_Forged Okay but itsn't "or" "||" instead of "|"? Anyway, i'll try this when i'm back at my pc, thanks. :)
     
  27. @KarimAKL
    Yes but that isn't for or, its kinda like a combiner or something instead of a comma
     
  28. Offline

    KarimAKL

    @Blackwing_Forged Oh okay, thanks. :) As i said: I'll try it when i'm back at my pc, thanks again. :)
     
  29. Offline

    KarimAKL

    @Blackwing_Forged Okay i'm finally back at my pc and i just tried it and it doesn't come with any errors, thanks.
    @Ahrigumi i wanted to ask you what you were talking about when you mentioned a static method.
     
  30. Offline

    KarimAKL

Thread Status:
Not open for further replies.

Share This Page