[Help Needed],[Plugin Dev'ing] Can't Display Config to Player

Discussion in 'Plugin Development' started by Jessy1237, Aug 27, 2011.

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

    Jessy1237

    Ok so pretty much i have set a file not really a config but it kinda is so, i've got everything to work so far but one thing:

    I've got a problem where I've got all the coding right but I want to display a list of admins to the player that sent the command. Everything works but it displays "Your Admins Are:null" Instead of the names. Here is the code:
    Show Spoiler
    package me.Jessy1237.Curtis;

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.config.Configuration;

    public class Main extends JavaPlugin {
    public void loadConfig() {
    File configFile = new File("plugins/Curtis/admins.yml");
    File configDirectory = new File("plugins/Curtis");
    Configuration config;
    if (!configDirectory.exists()) {
    configDirectory.mkdir();
    }

    if (configFile.exists()) {

    config = new Configuration(configFile);
    config.load();

    } else {

    System.out.println("There is no config file for admins - creating file plugins/Curtis.admins.yml...Created");
    try {
    BufferedWriter writer = new BufferedWriter(new FileWriter(configFile.getCanonicalFile()));

    writer.write("admins = Curtis111,Jessy1237,adsvk");
    writer.close();

    } catch (IOException e) {
    System.out.println(e.getLocalizedMessage());
    }
    }
    }
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    if(commandLabel.equalsIgnoreCase("Curtis")) {
    Player player = (Player)sender;
    player.sendMessage(ChatColor.AQUA + "Commands");
    player.sendMessage(ChatColor.AQUA + "--------------------------------------------------------");
    player.sendMessage(ChatColor.AQUA + "Curtis - Show This Page of Commands.");
    player.sendMessage(ChatColor.AQUA + "Admins - Show a list of the admins.");
    player.sendMessage(ChatColor.AQUA + "General - Shows General info about Server.");
    player.sendMessage(ChatColor.AQUA + "Plugin Developed By: Jessy1237");
    return true;
    }
    return false;
    }
    public boolean onCommand1(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    if(commandLabel.equalsIgnoreCase("Admins")) {
    Player player = (Player)sender;
    Configuration config = this.getConfiguration();
    String admins = config.getString("admins");
    player.sendMessage(ChatColor.GRAY + "Your Admins are:" + admins);
    return true;
    }
    return false;
    }
    public boolean onCommand11(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    if(commandLabel.equalsIgnoreCase("General")) {
    Player player = (Player)sender;
    player.sendMessage(ChatColor.GREEN + "General Information");
    player.sendMessage(ChatColor.GREEN + "--------------------------------------------------------");
    player.sendMessage(ChatColor.GREEN + "Server Live Date: 8:49 PM AEST, 24/08/2011");
    player.sendMessage(ChatColor.GREEN + "Server Owner: Curtis111");
    return true;
    }
    return false;
    }
    Logger log = Logger.getLogger("Minecraft");
    public void onEnable(){
    this.loadConfig();
    log.info("[Curtis] Curtis has been enabled.");
    }
    public void onDisable(){
    log.info("[Curtis] Curtis has been disabled.");
    }

    }


    Okay now for the second the problem just a little one, i'm good at commands but for some reason the only command that works is Curtis, the others don't work. I've probably missed.

    So Hopefully I'll get some helpful suggestions soon.

    Thanks in Advanced.
     
  2. Offline

    thescreem

    You can't have multiple onCommand methods, it just won't work. Put all of the commands inside the first onCommands method and it should work.

    EDIT: You also aren't writing to the config file correctly. Instead of writing admins = admin1,admin2,admin3 you need to write: admins: admin1,admin2,admin3
    YAML wouldn't read 'admins =' as a property.

    :)
     
  3. Offline

    Jessy1237

    @thescreem Okay I understand the command I see that now but the other YAML so I shouldn't make it a config rather a properties file? and get the string of admins at a property not as a interger. I'm a little new to this so could you expand just a little on the property code. Thanks

    EDIT: I kinda got what you meant, The commands work now but it still gives the "admins:null", here is the code again (updated)

    Show Spoiler
    package me.Jessy1237.Curtis;

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.config.Configuration;

    public class Main extends JavaPlugin {
    public void loadConfig() {
    File configFile = new File("plugins/Curtis/admins.yml");
    File configDirectory = new File("plugins/Curtis");
    Configuration config;
    if (!configDirectory.exists()) {
    configDirectory.mkdir();
    }

    if (configFile.exists()) {

    config = new Configuration(configFile);
    config.load();

    } else {

    System.out.println("There is no config file for admins - creating file plugins/Curtis.admins.yml...Created");
    try {
    BufferedWriter writer = new BufferedWriter(new FileWriter(configFile.getCanonicalFile()));

    writer.write("admins: Curtis111,Jessy1237,adsvk");
    writer.close();

    } catch (IOException e) {
    System.out.println(e.getLocalizedMessage());
    }
    }
    }
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    if(commandLabel.equalsIgnoreCase("Curtis")) {
    Player player = (Player)sender;
    player.sendMessage(ChatColor.AQUA + "Commands");
    player.sendMessage(ChatColor.AQUA + "-------------------------------------------");
    player.sendMessage(ChatColor.AQUA + "Curtis - Show This Page of Commands.");
    player.sendMessage(ChatColor.AQUA + "Admins - Show a list of the admins.");
    player.sendMessage(ChatColor.AQUA + "General - Shows General info about Server.");
    player.sendMessage(ChatColor.AQUA + "Plugin Developed By: Jessy1237");
    return true;
    }
    if(commandLabel.equalsIgnoreCase("Admins")) {
    Configuration config = this.getConfiguration();
    Player player = (Player)sender;
    String admins = config.getString("admins");
    player.sendMessage(ChatColor.GRAY + "Your Admins are:" + admins);
    return true;
    }
    if(commandLabel.equalsIgnoreCase("General")) {
    Player player = (Player)sender;
    player.sendMessage(ChatColor.GREEN + "General Information");
    player.sendMessage(ChatColor.GREEN + "-------------------------------------------");
    player.sendMessage(ChatColor.GREEN + "Server Live Date: 8:49 PM AEST, 24/08/2011");
    player.sendMessage(ChatColor.GREEN + "Server Owner: Curtis111");
    return true;
    }
    return false;
    }
    Logger log = Logger.getLogger("Minecraft");
    public void onEnable(){
    this.loadConfig();
    log.info("[Curtis] Curtis has been enabled.");
    }
    public void onDisable(){
    log.info("[Curtis] Curtis has been disabled.");
    }

    }
     
  4. Offline

    thescreem

    Is the config file actually getting created? Since I don't see that you created the config file anywhere, you just write to it.

    Your code (open)

    Code:java
    1.  
    2. if (configFile.exists()) {
    3.  
    4. config = new Configuration(configFile);
    5. config.load();
    6.  
    7. } else {
    8.  
    9. System.out.println("There is no config file for admins - creating file plugins/Curtis.admins.yml...Created");
    10. try {
    11. BufferedWriter writer = new BufferedWriter(new FileWriter(configFile.getCanonicalFile()));
    12.  
    13. writer.write("admins: Curtis111,Jessy1237,adsvk");
    14. writer.close();
    15.  
    16. } catch (IOException e) {
    17. System.out.println(e.getLocalizedMessage());
    18. }
    19. }
    20.  



    @Jessy1237
    What you're doing here is just writing to the config file if it doesn't exist. Now, if the config file is non-existent, how is it possible to write to it? :p

    What you need to do is this:

    Before you make that BufferedWriter, you need to add this line of code:
    configFile.createNewFile();

    What this does is create a new file using the 'createNewFile()' method built-into the Java API. It essentially creates a file based on a File object.

    :)
     
  5. Offline

    Jessy1237

    @thescreem One problem with your theory i have tested it and it does make a file and it also writes to it, so i don't really need to add it since it automatically does it but thanks i'll add it anyway just incase but umm yer still what should i do with the property, cause i want to send it to the player as a message.
     
Thread Status:
Not open for further replies.

Share This Page