How do I use getConfig() in a SubClass

Discussion in 'Spigot Plugin Help' started by fluxfluffy, May 15, 2016.

  1. Hi all of the Spigot community!
    I was wondering how I use getConfig() in a sub class. I understand that you have to use a constructor and I have seen methods of doing it but a example would pose a great help for me!

    I would be grateful if some one could show me a example of how to make it so I can use getConfig() in a sub class using plugin.getConfig(). Thanks!
     
  2. Like this?
    Code (Text):
        private final Main plugin;

        public BlockBreak(Main plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            this.plugin = plugin;
        }
     
  3. Yes but my Eclipse is saying that a return type is missing and plugin cannot be resolved to a variable or field.
    My subclass is called pvpthelp and my main class is called Main
     
  4. if this helps this is my current code: (THIS IS THE MAIN CLASS)
    Code (Text):
    package noahnok.pvpt.data;

    import java.io.File;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;


    public class Main extends JavaPlugin implements Listener{
       
        ChatColor aqua = ChatColor.AQUA;
        ChatColor red = ChatColor.RED;
        ChatColor gold = ChatColor.GOLD;
        ChatColor bold = ChatColor.BOLD;
        int kills = 0;
       
        public void onEnable(){
            try {
                if (!getDataFolder().exists()){
                    getDataFolder().mkdirs();
                }
                File file = new File(getDataFolder(), "config.yml");
                if (!file.exists()){
                    saveDefaultConfig();
                    saveConfig();
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
             getServer().getPluginManager().registerEvents(this, this);
             this.getCommand("pvpt").setExecutor(new pvpthelp(this));
        }
        @EventHandler(priority = EventPriority.HIGHEST)
        public void PlayerJoin(PlayerJoinEvent event){
            if (!event.getPlayer().hasPlayedBefore()){
            Player player = event.getPlayer();
            String name = player.getName();
            player.sendMessage("Hello " + name);
            getConfig().set("kills" + "." +name, kills);
            saveConfig();
            }
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if (cmd.getName().equalsIgnoreCase("kills")){
                String name = sender.getName();
                sender.sendMessage(aqua + "You have " + gold + bold + getConfig().get("kills" + "." +name) + red + " Kills!");
                return true;
            }
            if (cmd.getName().equalsIgnoreCase("pvpt")){
                if (args[0].equalsIgnoreCase("kills")){
                    if (args[1].equalsIgnoreCase("set")){
                        String amount = args[3];
                        String name = args[2];
                        getConfig().set("kills" + "." +args[2], args[3]);
                        saveConfig();
                        sender.sendMessage(aqua + name + "'s" + red + " kills" + aqua + " have been set to: "+ gold + bold + amount);
                        return true;
                        }
                    return true;
                    }
                if (!args[0].equalsIgnoreCase("kills")){
                    sender.sendMessage(aqua + "Please use /pvpt kills set <name> <amount>");
                    return true;
                }
                if (!args[1].equalsIgnoreCase("set")){
                    sender.sendMessage(aqua + "Please use /pvpt kills set <name> <amount>");
                    return true;
                }
                return true;
            }
           
            return true;
        }
    }
     
    THIS IS THE SUBCLASS (pvpthelp)
    Code (Text):
    package noahnok.pvpt.data;


    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.Plugin;



    public class pvpthelp implements CommandExecutor{
    Plugin test = Bukkit.getPluginManager().getPlugin("PvpTournament");
        pvpthelp plugin;
         
        public pvpthelp(pvpthelp instance) {
        plugin = instance;
        }
        ChatColor aqua = ChatColor.AQUA;
        ChatColor red = ChatColor.RED;
        ChatColor gold = ChatColor.GOLD;
        ChatColor bold = ChatColor.BOLD;
        public pvpthelp(Main plugin){
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if (args[0].equalsIgnoreCase("help")){
                CommandSender s = sender;
                String name = sender.getName();
                s.sendMessage(ChatColor.translateAlternateColorCodes('&', "&m&b|-----<&4PvPTournament&m&b>-----|"));
                s.sendMessage(aqua + "/pvpt " + gold + "The Base Command");
                s.sendMessage(aqua + "/pvpt kills set <player> amount" + gold + "Sets a players kills");
                s.sendMessage(aqua + "/kills " + gold + "Shows how many kills you have");
                s.sendMessage("Kills:" + Bukkit.getPluginManager().getPlugin("PvpTournament").getConfig().get("kills" + "." +name));
                return true;
            }
            return false;
           
        }
    }
     
     
  5. Code (Text):

    // SUBCLASS

    public class pvpthelp implements CommandExecutor{
    Plugin test = Bukkit.getPluginManager().getPlugin("PvpTournament");
        Main plugin;
       
        public pvpthelp(Main instance) {
        plugin = instance;
        }
     
    @fluxfluffy you need your Main class in your subclass constructor, not your subclass.
     
  6. Thank you so much! I have been looking all day for this and this has really helped! Thank you!
     
    • Like Like x 1
  7. You don't need this:
    You don't need more as:
    saveDefaultConfig();
    It creats the folder and makes the config but it don't overwrite an exsisting config. (Sorry for my English)
     
  8. when I save my config, when ever I add something to it it removes all my annotation (bits with #)
    How do i fix this?
     
  9. I have the same Problem now i make this: getConfig().options().header("# Line 1# \n # Line 2 ..")
     
  10. I just found a good solution! But when creating the config it wont have any of your
    stuff:
    more:

    but it will have annotations!

    Put this in your onEnable
    getConfig().options().copyDefaults(true);
    saveConfig();

    and where you put annotations put a space between the # and words:
    # LEAVE A SPACE
    NOT
    #LEAVE A SPACE