[FIXED] How can I round these co-ords? also how can I tp to co-ords

Discussion in 'Plugin Development' started by Themuddfamily, Jul 22, 2012.

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

    Themuddfamily

    Ok so I have 2 problems I don't know how to teleport people to certain co-ords and also how to round those co-ords this is the code I have so far :
    Code:
    package me.themuddfamily.plugins.CourtJustice;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import org.bukkit.configuration.file.YamlConfiguration;
     
    public class main extends JavaPlugin implements Listener {
        File configFile;
        File usersFile;
     
        FileConfiguration config;
        FileConfiguration users;
     
        @Override
        public void onDisable() {
            // in here, we need to save all our yamls if we have not yet,
            //  this maybe also a critical part cause some methods you might have forgotten
            //  to use the saveYamls(); on some of your methods that uses config.set or *.set(path,value)
            //  so we will use saveYamls(); method here to auto save what we have done
            saveYamls();
        }
     
        @Override
        public void onEnable() {
            // for error free purposes, you need to have all this on top of your onEnable first
            //  and then do whatever you want to do with your plugin like
            //  Events Registering and .gets and .sets just below all this
         
            configFile = new File(getDataFolder(), "config.yml");  ////
            usersFile = new File(getDataFolder(), "users.yml");
     
            // then we use firstRun(); method
            try {
                firstRun();
            } catch (Exception e) {
                e.printStackTrace();
            }
     
            // and we declare the FileConfigurations using YamlConfigurations and
            // then we just use loadYamls(); method
            // this is the critical part, this is needed cause if we do not use this,
            // it will read from the yml located at your jar, not in /plugins/<pluginName>/*yml.
            config = new YamlConfiguration();
            users = new YamlConfiguration();
            loadYamls();
     
            /*
            * do whatever you wantyour plugin to do below this comment
            *    like your listeners, Loggers and stuff
            */
        }
     
        /*
        * in this firstRun(); method, we checked if each File that we initialized does not exists
        *  if it does not exists, we load the yaml located at your jar file, then save it in
        *  the File(/plugins/<pluginName>/*.yml)
        * only needed at onEnable()
        */
        private void firstRun() throws Exception {
            if(!configFile.exists()){                        // checks if the yaml does not exists
                configFile.getParentFile().mkdirs();        // creates the /plugins/<pluginName>/ directory if not found
                copy(getResource("config.yml"), configFile); // copies the yaml from your jar to the folder /plugin/<pluginName>
            }
            if(!usersFile.exists()){
                usersFile.getParentFile().mkdirs();
                copy(getResource("users.yml"), usersFile);
            }
        }
     
        /*
        * this copy(); method copies the specified file from your jar
        *    to your /plugins/<pluginName>/ folder
        */
        private void copy(InputStream in, File file) {
            try {
                OutputStream out = new FileOutputStream(file);
                byte[] buf = new byte[1024];
                int len;
                while((len=in.read(buf))>0){
                    out.write(buf,0,len);
                }
                out.close();
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
        /*
        * in here, each of the FileConfigurations loaded the contents of yamls
        *  found at the /plugins/<pluginName>/*yml.
        * needed at onEnable() after using firstRun();
        * can be called anywhere if you need to reload the yamls.
        */
        public void loadYamls() {
            try {
                config.load(configFile); //loads the contents of the File to its FileConfiguration
                users.load(usersFile);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
        /*
        * save all FileConfigurations to its corresponding File
        * optional at onDisable()
        * can be called anywhere if you have *.set(path,value) on your methods
        */
        public void saveYamls() {
            try {
                config.save(configFile); //saves the FileConfiguration to its File
                users.save(usersFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
     
        @Override
        public boolean onCommand(final CommandSender sender, final Command cmd,
                final String label, final String[] args) {
            Player player = null;
            if (sender instanceof Player) {
                player = (Player) sender;
            }
            final Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("court")) {
                if (player == null) {
                    sender.sendMessage("this command can only be run by a player");
                } else {
                    if (args.length == 0) {
                        sender.sendMessage(ChatColor.GRAY + "[" + ChatColor.GREEN
                                + "Court" + ChatColor.BLUE + "Justice"
                                + ChatColor.GRAY + "]" + ChatColor.AQUA + " by "
                                + ChatColor.GOLD + "Themuddfamily!");
                        return true;
                    }
                    if (args[0].equalsIgnoreCase("Help")) {
                        sender.sendMessage(ChatColor.AQUA
                                + "----CourtJustice Help----");
                        sender.sendMessage(ChatColor.RED + "/court help -"
                                + ChatColor.GREEN + " This menu.");
                        sender.sendMessage(ChatColor.RED
                                + "/court arrest <player> -" + ChatColor.GREEN
                                + " arrests the person.");
                        sender.sendMessage(ChatColor.RED + "/court SetJail -"
                                + ChatColor.GREEN + " sets jail location.");
                        sender.sendMessage(ChatColor.RED + "/court SetCourt -"
                                + ChatColor.GREEN + " sets court location.");
                        sender.sendMessage(ChatColor.AQUA
                                + "----------------------");
                    }
                    if (args[0].equalsIgnoreCase("SetJail")) {
                        sender.sendMessage(ChatColor.RED + "Cannot Set Jail.");
                        final double x = p.getLocation().getX();
                        final double y = p.getLocation().getY();
                        final double z = p.getLocation().getZ();
                        final String w = p.getWorld().getName();
                        config.set("jail",
                                x + " " + y + " " + z + " " + w);
                     
                    }
                    if (args[0].equalsIgnoreCase("SetCourt")) {
                        sender.sendMessage(ChatColor.RED + "Cannot Set Court.");
                        final double x = p.getLocation().getX();
                        final double y = p.getLocation().getY();
                        final double z = p.getLocation().getZ();
                        final String w = p.getWorld().getName();
                        config.set("court",
                                x + " " + y + " " + z + " " + w);
            
                    }
                    if (args[0].equalsIgnoreCase("Arrest")) {
                        if (args[1] == null) {
                            return false;
                        }
                        if (Bukkit.getServer().getPlayer(args[0]) != null) {
                            return false;
                        }
                        sender.sendMessage(ChatColor.RED + "Cannot Arrest "
                                + args[1]);
                    }
                    if (args[0].equalsIgnoreCase("tp")) {
                        sender.sendMessage(ChatColor.GREEN
                                + "You have been teleported to the court! Co-ords : " + config.getString("court"));
                        player.getServer().dispatchCommand(
                                player.getServer().getConsoleSender(),
                                "tppos " + player.getPlayerListName()
                                        + " " + config.getString("court"));
                    }
                }
                return true;
            }
            return false;
        }
    }
    
     
  2. Offline

    r0306

    Themuddfamily
    To define a coordinate, create a new Location object.
    Code:
    Location location = new Location(world, x, y, z);
    Teleport the player with
    Code:
    player.teleport(location);
     
  3. Offline

    Themuddfamily

    Location location = new Location(config.getString("court"));
    gives an error tho?
     
  4. You are only doing 1 string in the arguments, you need a world, int, int, int.
     
  5. Offline

    r0306

  6. Offline

    Themuddfamily

    Thanks for this! I have marked it as fixed
     
Thread Status:
Not open for further replies.

Share This Page