How should i do this!!!

Discussion in 'Spigot Plugin Development' started by Deger, Jun 7, 2016.

  1. Hi i have a spawn plugin, that sets the spawn and saves the spawn x,y,z etc in a config.yml file, and i want it to be set into a data.yml!

    How should i do it!
    Code:
    Code (Text):
    package me.deger.tp;

    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 Core extends JavaPlugin {

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You need to be a player to use this command!");
                return true;
            }
            Player p = (Player) sender;
            if (label.equalsIgnoreCase("tp")) {
                if (args.length == 0) {
                    p.sendMessage(ChatColor.RED + "Please specify a player");
                    return true;
                }
                if(args.length > 1){
                    p.sendMessage(ChatColor.RED + "Usage: /tp <playername>");
                    return true;
                }
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if(target == null){
                    p.sendMessage(ChatColor.RED + "Could not find player " + ChatColor.WHITE + "\""  + ChatColor.GREEN + args[0] + ChatColor.WHITE + "\"");
                    return true;
                }
                p.teleport(target.getLocation());
                return true;
            }

            if (label.equalsIgnoreCase("setspawn")){
                getConfig().set("spawn.world", p.getLocation().getWorld().getName());
                getConfig().set("spawn.x", p.getLocation().getX());
                getConfig().set("spawn.y", p.getLocation().getY());
                getConfig().set("spawn.z", p.getLocation().getZ());
                getConfig().set("spawn.yaw", p.getLocation().getYaw());
                getConfig().set("spawn.pitch", p.getLocation().getPitch());
                saveConfig();

                p.sendMessage(ChatColor.GREEN + "Spawn set!");
                return true;
            }
            try{
                if(label.equalsIgnoreCase("spawn")){
                    if(getConfig().getConfigurationSection("spawn") == null){
                        p.sendMessage(ChatColor.RED + "The spawn has not yet 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 yaw = (float) getConfig().getDouble("spawn.yaw");
                    double pitch = (float) getConfig().getDouble("spawn.pitch");
                    p.teleport(new Location(w, x, y, z) );
                    return true;
                }
            }catch(Exception e){
                e.printStackTrace();

            }
            return false;
        }

    }
     
     
  2. try{
    if(label.equalsIgnoreCase("spawn")){
    should be:
    if(label.equalsIgnoreCase("spawn")){
     
  3. Aha, i just had it to check if i had any error
     
  4. Code (Text):
    if(command.getName().equalsIgnoreCase("spawn"))
     
    • Agree Agree x 1
  5. I just copied this code from a message i sent, and not my up to date

    What does it matter?, they both work
     
    #5 Deger, Jun 7, 2016
    Last edited by a moderator: Jun 9, 2016
  6. click
     
  7. This code is the code your should use:
    Code (Text):

    package me.deger.tp;

    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 Core extends JavaPlugin {

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You need to be a player to use this command!");
                return true;
            }
            Player p = (Player) sender;
            if (label.equalsIgnoreCase("tp")) {
                if (args.length == 0) {
                    p.sendMessage(ChatColor.RED + "Please specify a player");
                    return true;
                }
                if(args.length > 1){
                    p.sendMessage(ChatColor.RED + "Usage: /tp <playername>");
                    return true;
                }
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if(target == null){
                    p.sendMessage(ChatColor.RED + "Could not find player " + ChatColor.WHITE + "\""  + ChatColor.GREEN + args[0] + ChatColor.WHITE + "\"");
                    return true;
                }
                p.teleport(target.getLocation());
                return true;
            }

            if (label.equalsIgnoreCase("setspawn")){
                getConfig().set("spawn.world", p.getLocation().getWorld().getName());
                getConfig().set("spawn.x", p.getLocation().getX());
                getConfig().set("spawn.y", p.getLocation().getY());
                getConfig().set("spawn.z", p.getLocation().getZ());

                saveConfig();

                p.sendMessage(ChatColor.GREEN + "Spawn set!");
                return true;
            }
                if(label.equalsIgnoreCase("spawn")){
                    if(getConfig().getConfigurationSection("spawn") == null){
                        p.sendMessage(ChatColor.RED + "The spawn has not yet been set!");
                        return true;
                    }
               
    Location spawn = new Location(Bukkit.getWorld((String) config.get("spawn.world")), config.getDouble("spawn.x"), config.getDouble("spawn.y"), config.getDouble("spawn.z"));


                    p.teleport(spawn);
                    return true;
                }
            }
            return false;
        }

    }
    Now, I can't just do that, I'm not spoonfeeding. Let me explain why this will work.
    This code is not needed for a basic spawn plugin:
    Code (Text):
    double yaw = (float) getConfig().getDouble("spawn.yaw");
                    double pitch = (float) getConfig().getDouble("spawn.pitch");
    and
    Code (Text):
    getConfig().set("spawn.yaw", p.getLocation().getYaw());
                getConfig().set("spawn.pitch", p.getLocation().getPitch());
    Then, the easier way to teleport is a slightly more organized version of what you already put.
    Code (Text):
    Location spawn = new Location(Bukkit.getWorld((String) config.get("spawn.world")), config.getDouble("spawn.x"), config.getDouble("spawn.y"), config.getDouble("spawn.z"));
    Basically, the old tutorial you probably used from BukkitDev outdates Yaw and Pitch, and they cause glitches if you don't use it carefully. This should help! Also, you don't need a try/catch statement.
     
    #7 Dartanman, Jun 7, 2016
    Last edited: Jun 7, 2016
  8. Or you could not put either one and just have a different class for each command, then, you won't need either one, and you can add aliases in the plugin.yml
     
    • Agree Agree x 1

  9. I want to have a yaw and pitch, becuase where i look, my setspawn should also look that way.....
     
  10. Then make a new post, with "Help with Yaw and Pitch"
     
  11. For the command to setspawn:
    float pitch = p.getLocation().getPitch();
    float yaw = p.getLocation().getYaw();

    For the teleport (for example):

    Location l = new Location("sampleworld", x, y, z, pitch, yaw);
     
  12. I agree with you on this, kinda off topic, but why in the world do people use the "label#equalsIgnoreCase("command"); method?
     
  13. I'm not even sure why people still use it instead of command.getName()....
     
  14. Tux

    Tux

    Aliases. The label used can be an alias.
     
    • Like Like x 1
    • Agree Agree x 1
  15. When you register a command, you must provide a fallback prefix for it to use. This means that the server will try to register your command with #getName(), but if it's taken, will append the prefix with a ':' to the front of the name as many times as it needs to before it's not taken. That's what #getLabel() returns.
     
    • Useful Useful x 1
  16. This thread confused the hell out of me. OP asks how to use custom config files like data.yml and everyone derails to how to work commands... wtf


    And no. That is NOT what getLabel returns... The label is the alias used to trigger the command...
     
  17. Just thinking the same thing while reading these posts :p.

    To create a custom configuration file, read up on Bukkit's Configuration API reference.
    http://wiki.bukkit.org/Configuration_API_Reference
     
    • Useful Useful x 1
  18. Thank you kind sir.
     
  19. Dont thank him. He gave you wrong information...

    The label is the alias used to trigger the command. Not what he said it is..