Solved getting a location

Discussion in 'Plugin Development' started by vhbob, Apr 25, 2015.

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

    vhbob

    i have this code
    Code:
                    Location spleef = getConfig().getDouble("Spleef location");
                    p.teleport(spleef);
    but there is not a getconfig.getlocation so this doesnt work, anyone have better ideas?
     
  2. Offline

    Protophite

    store the x y and z instead.
    @vhbob
     
  3. Offline

    vhbob

    @Protophite ive done that here look
    Code:
            if (cmd.getName().equalsIgnoreCase("Spleef")) {
                String JL = "";
                Player p = (Player) sender;
                if (JL.contains("Setup")) {
                    if (p.hasPermission("spleef.owner")) {
                        Double x = p.getLocation().getX();
                        Double y = p.getLocation().getY();
                        Double z = p.getLocation().getZ();
                        getConfig().set("Spleef location", "." + x + y + z);
                    }
                }
                if (JL.contains("join")) {
                    Location spleef = getConfig().getDouble("Spleef location");
                    Location tp = spleef;
                    p.teleport(spleef);
                }
                if (JL.contains("leave")) {
    
                }
            }
     
  4. Offline

    Zombie_Striker

    Store the x,y,z and world name separately, and retrieve them all individually and construct a new location.
     
  5. Offline

    Protophite

    @vhbob
    The x, y and z need to be stored separately.

    getConfig#setDouble("name.location.x", loc.getX())
    ("name.location.y", loc.getY())
    ("name.location.z", loc.getZ())
     
  6. Offline

    vhbob

    @Protophite how would i make that into a location???

    @Protophite i have this so far
    Code:
                if (JL.contains("Setup")) {
                    if (p.hasPermission("spleef.owner")) {
                        getConfig().set("Location y", p.getLocation().getY());
                        getConfig().set("Location x", p.getLocation().getX());
                        getConfig().set("Location z", p.getLocation().getZ());
                    }
                }
                if (JL.contains("join")) {
                    Double y = getConfig().getDouble("Location y");
                    Double x = getConfig().getDouble("Location x");
                    Double z = getConfig().getDouble("Location z");
                }
    but how do i set the location

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  7. Offline

    Protophite

    @vhbob

    Code:
    getConfig().set("name.location.x", location.getX());
    getConfig().set("name.location.y", location.getY());
    getConfig().set("name.location.z", location.getZ());
    Location loc = new Location(getServer().getWorld("worldname"), x, y, z);
    
     
  8. Offline

    vhbob

    @Protophite no i mean how do i acctually set the location with a Location loc = ( blah blah blah) statement????
     
  9. Offline

    Protophite

    @vhbob
    1. Location loc = new Location(getServer().getWorld("worldname"), x, y, z);
     
  10. Offline

    vhbob

    @Protophite i need to get the x,y,z from the config

    @Protophite i have this
    Code:
            if (cmd.getName().equalsIgnoreCase("Spleef")) {
                String JL = "";
                Player p = (Player) sender;
                if (JL.contains("Setup")) {
                    if (p.hasPermission("spleef.owner")) {
                        getConfig().set("Location y", p.getLocation().getY());
                        getConfig().set("Location x", p.getLocation().getX());
                        getConfig().set("Location z", p.getLocation().getZ());
                        p.sendMessage(ChatColor.GREEN + "Location for spleef set!");
                    }
                } else if (JL.contains("join")) {
                    Double y = getConfig().getDouble("Location y");
                    Double x = getConfig().getDouble("Location x");
                    Double z = getConfig().getDouble("Location z");
                    String w = p.getWorld().getName();
                    Location loc = new Location(getServer().getWorld(w), x, y, z);
                    p.teleport(loc);
                    p.sendMessage(ChatColor.GREEN + "Spleef joined!");
                }
            }
    but it doesnt work what-so-ever, the messages dont even send....

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  11. Offline

    meguy26

    @vhbob
    I use the following methods to convert locations to strings and vice a versa, you can use them to store locations in the config as strings:
    Code:
    public class ValueSaver {
        /**
         * Converts a location to a string.
         * @param loca
         * @return location string
         */
        public static String locationToString(Location loca) {
            Location loc = loca.clone();
            return loc.getWorld().getName() + "," + String.valueOf(loc.getX())
                    + "," + String.valueOf(loc.getY()) + ","
                    + String.valueOf(loc.getZ());
        }
    
        /**
         * Converts a location from a string.
         * @param input
         * @return location
         */
        public static Location locationFromString(String input) {
            String[] data = input.split(",");
            return new Location(Bukkit.getWorld(data[0]),
                    Double.parseDouble(data[1]), Double.parseDouble(data[2]),
                    Double.parseDouble(data[3]));
        }
    }
    if you add the above class to your project, you could do something like:
    Code:
    Player p = somePlayer;
    String locString = ValueSave.locationToString(p.getLocation());
    getConfig().set("SomeRandom.Location", locString);
    //Good idea! lets just teleport the player back to were they are right now... :)
    p.teleport(ValueSaver.locationFromString(getConfig().getString("SomeRandom.Location")));
     
  12. Offline

    vhbob

    @meguy26 i have this :
    Code:
    package me.vhbob.spleef;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class spleef extends JavaPlugin implements Listener {
    
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            if (cmd.getName().equalsIgnoreCase("spleef")) {
                String JL = "";
                Player p = (Player) sender;
                if (JL.contains("setup")) {
                    if (p.hasPermission("spleef.owner")) {
                        String locString = ValueSaver.locationToString(p.getLocation());
                        getConfig().set("SomeRandom.Location", locString);
                        p.sendMessage(ChatColor.GREEN + "Location saved!");
                    }
                } else if (JL.contains("join")) {
                    p.teleport(ValueSaver.locationFromString(getConfig().getString("SomeRandom.Location")));
                    p.sendMessage(ChatColor.GREEN + "Spleef joined!");
                }
            }
            return true;
        }
    
    }
    
    but none of it works....
     
  13. Offline

    meguy26

    @vhbob
    Its not my code, its your if statements. You define JL as "", so jl will never contain anything. I assume you mean String JL = args[0]?
     
  14. Offline

    vhbob

    Ah nvm fixed it!!! thx guys
     
  15. Offline

    meguy26

    @vhbob
    were was the problem?
     
  16. Offline

    vhbob

    i had to register the args @meguy26
     
Thread Status:
Not open for further replies.

Share This Page