Could someone learn me how to use CONFIG in coding?

Discussion in 'Plugin Development' started by JavaNaza, Jan 14, 2018.

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

    JavaNaza

    Hello, could someone learn me how to use configs in coding? I dont know how to save Location.
     
    Last edited: Jan 14, 2018
  2. Bukkit doesn't have Locations implemented in FileConfiguration, so you have to add it manually.

    Put this in your code:

    Code:
    public static void setLocation (FileConfiguration config, String path, Location l) {
    
        config.set(path+".world", l.getWorld().getName());
        config.set(path+".x", l.getX());
        config.set(path+".y", l.getY());
        config.set(path+".z", l.getZ());
        config.set(path+".yaw", (double)l.getYaw());
        config.set(path+".pitch", (double)l.getPitch());
       
    }
    
    public static Location getLocation (FileConfiguration config, String path) {
       
        return new Location(
                Bukkit.getWorld(config.getString(path+".world")),
                config.getDouble(path+".x"),
                config.getDouble(path+".y"),
                config.getDouble(path+".z"),
                (float)config.getDouble(path+".yaw"),
                (float)config.getDouble(path+".pitch")
        );
       
    }
    and you can use it as so:

    Code:
    Location spawn = ((Player)sender).getLocation(); // Assuming this is in a /setspawn command, as an example
    setLocation(getConfig(), "locations.spawn", spawn); // "locations." is not needed, it just organizes the config
    sender.sendMessage("Spawn set!"); // Example message
    
    Code:
    Location spawn = getLocation(getConfig(), "locations.spawn") // Assuming this is in a /spawn command
    ((Player)sender).teleport(spawn); // Teleport the player
    sender.sendMessage("Welcome back to spawn!"); // Example message
    
     
Thread Status:
Not open for further replies.

Share This Page