How to get one String of the Custom File?

Discussion in 'Spigot Plugin Development' started by RuloGamer, Jun 16, 2015.

Thread Status:
Not open for further replies.
  1. hi, i want get one string of the custom file, the custom file I created from the main class, called messages.yml

    in other class (Commands), i want that that by putting a command, exit the message of the messages.yml

    I made something like this:
    MainClass.class:
    PHP:
    package me.RuloGamer.Example;

    import java.io.File;
    import java.io.IOException;
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;

    public class MainClass extends JavaPlugin {
     
       public static MainClass instance;
     
       public File msgFile;
       public FileConfiguration messages;

       public void onEnable() {
         File msgFile = new File(getDataFolder(), "messages.yml");
         messages = YamlConfiguration.loadConfiguration(msgFile);
         if (!msgFile.exists()) {
             this.messages.addDefault("prefix", "&0[&eTEST&0]");
             this.messages.options().copyDefaults(true);
             Bukkit.getConsoleSender().sendMessage("§fCustom§3FF§6A§f: §amessages.yml generated!");
             try {
               messages.save(msgFile);
             } catch (IOException e) {
               e.printStackTrace();
             }
         }
       
         getCommand("ffa").setExecutor(new Commands());
       
         Bukkit.getConsoleSender().sendMessage("§b---------------------");
         Bukkit.getConsoleSender().sendMessage("§cTEST Enabled");
         Bukkit.getConsoleSender().sendMessage("§b---------------------");
         Bukkit.getConsoleSender().sendMessage(this.messages.getString("prefix")); //this yes get the message in the console
       }
     
       public void onDisable() {
         Bukkit.getConsoleSender().sendMessage("§b----------------------");
         Bukkit.getConsoleSender().sendMessage("§cTEST Disabled");
         Bukkit.getConsoleSender().sendMessage("§b----------------------");
       }
    }

    Commands.class:
    PHP:
    ackage me.RuloGamer.Example;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class Commands implements CommandExecutor {
       
        MainClass main = MainClass.instance;
       
        String ffa = "&cUse '/ffa help' for view all commands";

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String string, String[] args) {
            if (sender instanceof Player) {
                Player p = (Player) sender;
                if (cmd.getName().equalsIgnoreCase("ffa")) {
                    if (args.length == 0) {
                        p.sendMessage(main.messages.getString("prefix") + ffa); //this no get the message, get an error :(
                    }
                }
            } else {
                sender.sendMessage("§4You don't use this command!");
            }
            return true;
        }
    }
     
    Error in the console: http://pastebin.com/JpVCvdqn

    thanks!
     
  2. You never initialized your MainClass instance variable. It is generally recommended not to use a singleton to access your main class's fields. You should use dependency injection (usually via constructor) to access them instead. Add a constructor to your Commands class, and pass an instance of your main class, then you can use that to access your main class.
     
    • Like Like x 1
    • Agree Agree x 1
  3. hi and thanks for answer, sorry but I don't quite understand, i speak spanish and english not much xD, could you tell me the code that I have to put? :(
     
Thread Status:
Not open for further replies.