Config file

Discussion in 'Plugin Development' started by StihlBlade86, Jun 26, 2015.

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

    StihlBlade86

    ok im pretty new tot his whole config file thing so bare with me xD
    anyway how would i make this able to be changed in config? plz help!

    Code:
    World world = Bukkit.getWorld("world");
             Location Spawn = new Location(world, 2.5, 7, -2.5 );
    PS: I know how to generate the config file and set addDeafult but not how to get the location to change!
     
  2. There's 2 main methods to go about doing this.

    One method is using the already built-in serialize() method in the Location class. The other is to make your own serialize method to return a String, not a Map. I prefer the second option as I like having less lines in the configuration, however it's harder to read.

    Before typing any code, I'm changing the name 'Spawn' to 'spawnLocation' as you should always start your variable names lowercase unless static and/or final.

    First method:
    Saving the location to the config (you can use addDefault for the default location).
    Code:
    config.set("Spawn location", spawnLocation.serialize()); // Saves the spawn location in a Map.
    
    Loading the location from the config.
    Code:
    Location spawnLoc = new Location(config.getConfigurationSection("Spawn location").getValues(false)); // Get the spawn location as a Map and then create a Location from it.
    
    Second method:
    Serializing methods:
    Code:
    private static String fromLocation(Location location) {
        return location.getWorld().getName() + "," + location.getX() + "," + location.getY() + "," + location.getZ() + "," + location.getYaw() + "," + location.getPitch(); // Return a string with the location's data. Format: World,X,Y,Z,Yaw,Pitch
    }
    
    private static Location fromString(String strLocation) {
        if (strLocation != null && strLocation.contains(",")) { // If the string contains ",".
            String[] locSplit = strLocation.split(","); // Get each section of the string.
            String worldName = locSplit[0];
            double xPos = Double.parseDouble(locSplit[1]), yPos = Double.parseDouble(locSplit[2]), zPos = Double.parseDouble(locSplit[3]); // Get the positions as a double (from strings)
            float yaw = Float.parseFloat(locSplit[4]), pitch = Float.parseFloat(locSplit[5]); // Get the yaw and pitch from string.
            return new Location(Bukkit.getServer().getWorld(worldName), xPos, yPos, zPos, yaw, pitch); // Create a location.
        }
        return null;
    }
    
    Saving the location to the config (again, you can use addDefault).
    Code:
    config.set("Spawn location", fromLocation(spawnLocation));
    
    Loading the location from the config.
    Code:
    Location spawnLoc = fromString(config.getString("Spawn location"));
    
     
    StihlBlade86 likes this.
  3. Offline

    StihlBlade86

    Thanks so much! I really appreciate it! @KingFaris11

    Ok so wait i would add the second serialize after the /spawn command?




    Code:
    package me.bukkit.ku;
    
    
    import java.util.logging.Logger;
    
    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 SpawnSetter extends JavaPlugin{
    public final Logger logger = Logger.getLogger("Minecraft");
    public static SpawnSetter plugin;
    
    public void onDisable() {
    this.logger.info(ChatColor.RED +"[SpawnSetter] Has Been Disabled!");
    
    }
    
    public void onEnable() {
    
       this.logger.info(ChatColor.GREEN +"[SpawnSetter] Has Been Enabled!");
    }
    
    
       public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
         Player player = (Player) sender;
         if(commandLabel.equalsIgnoreCase("spawn"))
           if (!player.hasPermission("spawnsetter.spawn")) {
             player.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN + "SpawnSetter" + ChatColor.GOLD + "]" + ChatColor.RED + "You don't have permission!");
             return true;
         }
         
         {
             player.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN + "SpawnSetter" + ChatColor.GOLD + "]" + ChatColor.GREEN + "Sending You To Hub");
             World world = Bukkit.getWorld("world");
             Location Spawn = new Location(world, 2.5, 7, -2.5 );
             player.teleport(Spawn);
             return true;
         }
       }
    }
       
    

    @KingFaris11

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>

    config.set comes up as a syntax error

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Jun 26, 2015
  4. You're not even making a command in the correct way... there's no open brackets for the command label, etc.

    But anyway, so firstly, I'd actually store the location in a global variable, and load it in onEnable(), but the following code is for if you don't want to (for some odd reason):
    Code:
    Location spawn = fromString(getConfig().getString("Spawn location"));
    if (spawn != null) player.teleport(spawn);
    
    Also, you didn't check if the sender is a Player, etc. Basically, there's a lot of mistakes in this code, and I can't help you with them as it's late in the UK (tired).
     
Thread Status:
Not open for further replies.

Share This Page