Solved Configuration Problem

Discussion in 'Spigot Plugin Development' started by GoRoK, Jun 24, 2016.

Thread Status:
Not open for further replies.
  1. Main
    Code (Text):

    package me.gorok.main;

    import org.bukkit.ChatColor;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener
    {
        public void onEnable()
        {
            regCmds();
            regEvents();
            loadConfig();
            System.out.println(ChatColor.GREEN + "Successfully activated " + this.getDescription().getName() +"v" + this.getDescription().getVersion());
        }
        public void onDisable()
        {
            System.out.println(ChatColor.RED + this.getDescription().getName() + " Successfully deactivated.");
        }
        public void loadConfig()
        {
            String firstline = "SignFirstLine #Otherwise known as the SignName";
            String secondline = "SignSecondLine #Otherwise known as Command(Buy, sell, redirect), use your own it will get replaced.";
            String SignThirdLine = "SignThirdLine #Otherwise known as Link";
            this.getConfig().options().header("Designed for public use, author: GoRoK. You accept the TOS when using this Plugin!");
            this.getConfig().set("SignFirstLine", firstline);
            this.getConfig().set("SignSecondLine", secondline);
            this.getConfig().set("SignThirdLine", SignThirdLine);
            this.saveConfig();
        }
        public void regCmds()
        {
            new Commands(this);
            getCommand("signhelp").setExecutor(new Commands(this));
        }
        public void regEvents()
        {
            new SignListener(this);
            getServer().getPluginManager().registerEvents(new SignListener(this), this);
        }
    }

     
    Listener which gets the Strings out of the Config
    Code (Text):

    package me.gorok.main;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.SignChangeEvent;

    public class SignListener implements Listener
    {
        private static Main plugin;
        @SuppressWarnings("static-access")
        public SignListener(Main plugin) {
            this.plugin = plugin;
        }
       
        @EventHandler
        public void onSignChange (SignChangeEvent e )
        {
            if(e.getPlayer() instanceof Player)
            {
                if(e.getLine(0).equalsIgnoreCase("[SIGNCHANGER]"))
                {
                    e.setLine(0, plugin.getConfig().getString("SignFirstLine"));
                    if(e.getLine(1).equalsIgnoreCase("command"))
                    {
                        e.setLine(1, plugin.getConfig().getString("SignSecondLine"));
                        if(e.getLine(2).equalsIgnoreCase("link"))
                                {
                                    e.setLine(2, plugin.getConfig().getString("SignThirdLine"));
                                }
                        else
                        {
                            e.setLine(2,"SecondLine error");
                        }
                    }
                    else
                    {
                        e.setLine(1, "FirstLine error.");
                    }
                   
                }
            }
            else
            {
                e.setLine(0, "FirstLine error.");
            }
        }
    }
     
    When im changing the Lines in the Config, theyll get reseted to :
    Code (Text):

    SignFirstLine: 'SignFirstLine #Otherwise known as the SignName'
    SignSecondLine: 'SignSecondLine #Otherwise known as Command(Buy, sell, redirect),
      use your own it will get replaced.'
    SignThirdLine: 'SignThirdLine #Otherwise known as Link'

     
    Which is defined in the loadConfig() method.
    But i want to save the User input, not the Defaults.

    How do i do that (I hate configurations..)
     
  2. for one thing, you cant put comments in like that. Those are going to be part of the string when you get it from the config.

    and... the only time I see you setting anything in the config is the loadConfig method:
    Code (Text):
        public void loadConfig()
        {
            String firstline = "SignFirstLine #Otherwise known as the SignName";
            String secondline = "SignSecondLine #Otherwise known as Command(Buy, sell, redirect), use your own it will get replaced.";
            String SignThirdLine = "SignThirdLine #Otherwise known as Link";
            this.getConfig().options().header("Designed for public use, author: GoRoK. You accept the TOS when using this Plugin!");
            this.getConfig().set("SignFirstLine", firstline);
            this.getConfig().set("SignSecondLine", secondline);
            this.getConfig().set("SignThirdLine", SignThirdLine);
            this.saveConfig();
        }
    and you're just setting it to those things, so anything else will always be overwritten.
     
  3. I dont want to set anything from the Pluginside, the User has to edit the Config, so no need for set anything inside the Plugin, how can i stop overwriting everytime? (FeelsDumbMan)
     
  4. In your loadConfig method only do that if the datafolder doesn't exist.
    Code (Text):
    if (!getDataFolder().exists()
    So that way you won't be overriding it every time the plugin starts
     
  5. Did it like
    Code (Text):

    public void loadConfig()
        {
            String firstline = "SignFirstLine #Otherwise known as the SignName";
            String secondline = "SignSecondLine #Otherwise known as Command(Buy, sell, redirect), use your own it will get replaced.";
            String SignThirdLine = "SignThirdLine #Otherwise known as Link";
            if (!getDataFolder().exists())
        {
                this.getConfig().options().header("Designed for public use, author: GoRoK. You accept the TOS when using this Plugin!");
                this.getConfig().set("SignFirstLine", firstline);
                this.getConfig().set("SignSecondLine", secondline);
                this.getConfig().set("SignThirdLine", SignThirdLine);
                this.saveConfig();
        }
            else
            {
                this.reloadConfig();
            }
        }
    [/code)
    now it doesnt generate a Config at the first Plugin start, or when the file doesnt exist.
     
  6. Right. Though, there's no reason to have the string declarations outside of the if statement. Just put everything inside of the if statement.
     
  7. Did that, nothing happens though.
     
  8. What do you mean nothing happens? Delete the entire 'yourPluginName' folder in the plugins folder.
     
    • Friendly Friendly x 1
  9. Im sorry, didnt noticed the Folder :p, but now it only adds the Header,
    Got it like:
    Code (Text):

    String firstline = "SignFirstLine #Otherwise known as the SignName";
                String secondline = "SignSecondLine #Otherwise known as Command(Buy, sell, redirect), use your own it will get replaced.";
                String SignThirdLine = "SignThirdLine #Otherwise known as Link";
                this.getConfig().options().header("Designed for public use, author: GoRoK. You accept the TOS when using this Plugin!");
                this.getConfig().set("SignFirstLine", firstline);
                this.getConfig().set("SignSecondLine", secondline);
                this.getConfig().set("SignThirdLine", SignThirdLine);
                this.saveConfig();
     
     
  10. Working now, thank you for the help.
     
Thread Status:
Not open for further replies.