HELP! My first plugin, how to make it configureable by the users?

Discussion in 'Plugin Development' started by MultiBana, Oct 11, 2011.

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

    MultiBana

    I recently made my first plugin that stops people from cursing.
    How can i make that who ever downloads it and uses it can configure and change the words that they want to ban?
     
  2. Offline

    arnie231

    config maybe ?
     
  3. Offline

    Flammantis

  4. Offline

    MultiBana

    I'm asking HOW to make that config file...
     
  5. Offline

    arnie231

  6. Offline

    Flammantis

    If you go into the link I gave you (my source) you can look how my config file is made in onEnable()
     
  7. Offline

    arnie231

    if you go to the link i sent you will see that all the old ways of using configs are broken with the new RB!
     
  8. Offline

    Sagacious_Zed Bukkit Docs

    Not true, you can still use the old configs for now, But if you are building new code, might as well use the non deprecated API.
     
  9. Offline

    Dinnerbone Bukkit Team Member

    Let's say you want to get a string to display to the user. I don't know why. Maybe you like strings.

    Code:
    Server.broadcast(getConfig().getString("my-super-string"))
    This reads the string called "my-super-string" in the config.yml of your plugins directory (if it exists).

    But let's say now that you want to provide a default value, incase they haven't configured the plugin. Add a new file to your plugin like you did for plugin.yml, called "config.yml". Put this inside there:

    Code:
    my-super-string: Hello World!
    Any settings in this file will be used as defaults for the users file. Now, if they don't have this set, it'll use "Hello World!".

    But let's say that you want to make sure they have a config.yml, and populate it with the default values if they don't have them. Put this in your onEnable:

    Code:
    getConfig().options().copyDefaults(true);
    saveConfig();
    Any questions? ;D
     
  10. Offline

    Mentioum

    No questions @Dinnerbone - Looks great man - Nice job. I still want to buy you a drink if you ever come up to London.
     
  11. Offline

    MultiBana

    TNX! Exactly what i was looking for, i am pretty much a noob with java i started learning yesterday but i really catch up fast! Just one thing, will this create a config.yml in plugin folder?
     
  12. Offline

    Dinnerbone Bukkit Team Member

    If the file doesn't exist, it'll make it when it saves :)
     
    RROD likes this.
  13. Offline

    MultiBana

    sorry for double post my internet is slow so i did refresh and had to write it again...
     
  14. Offline

    4am

    If plugins/iheartstrings/config.yml exists, and we call copyDefaults(true), does it copy all default values, or only the ones which are not already set in the destination config.yml? Is the boolean parameter a flag to toggle between these behaviors?

    (I confess I have not looked over the JavaDoc yet, going to do that now.)

    EDIT: Seems the JavaDoc isn't updated yet. Off to github to read the source:D
     
  15. Offline

    feildmaster

    copies all... EDIT1: Oh wait. Let me relook that real quick. You have a nice question there.

    EDIT2: yes, it copies all. :p

    EDIT3: The boolean is indeed a flag to toggle between the behaviors.
     
  16. Offline

    Dinnerbone Bukkit Team Member

  17. Offline

    Smex

    How do I get now an IntList?
    There doesn't seem to be a method for it?
     
  18. Offline

    rominos2

    ARRG !

    How can I get a lit in config like that ?
    Code:
    List:
       N1:
         Some nodes
       N2:
         Other nodes
    1) I used getList("List") --> now get null
    2) I try getKeys("List") with an iterator
    But when I want to use getString() to access nodes, it gets null

    Please help ! For two days now on Configs
     
  19. Offline

    Aza24

    How would I create another config file e.g waypoints.yml in the same way?
     
  20. Offline

    thehutch

    File waypoints = new File(this.getDataFolder() + File.separator + "waypoint.yml") {}
    I dont see why this wouldnt work if not probably becaue its part of the old API and therefore might not work
     
  21. Offline

    vaxoc

    @Dinnerbone (or anyone else that can help) I tried what you said. I put this in my onEnable():
    PHP:
    getConfig().options().copyDefaults(true);
    saveConfig();
    Then, I made a file in the same location as my plugin.yml called "config.yml" that looks like this:
    Code:
    Wake-Message: has woken up
    Sleep-Message: has gone to sleep
    Allow-Everyone-To-Use-List-Command-When-Using-OP-System: true
    Show-Alerts-For-OP: true
    Show-OPs-In-Sleeping-Players-List: true
    The problem is: when I export and run the plugin, I get this error:
    Code:
    [SEVERE] Error occurred while enabling SleepNotify v1.5 (Is it up to date?): me.vaxoc.plugins.sleepnotify.SleepNotify.getConfig()Lorg/bukkit/configuration/file/FileConfiguration;
    java.lang.NoSuchMethodError: me.vaxoc.plugins.sleepnotify.SleepNotify.getConfig()Lorg/bukkit/configuration/file/FileConfiguration;
        at me.vaxoc.plugins.sleepnotify.SleepNotify.onEnable(SleepNotify.java:48)
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:126)
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:941)
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:280)
        at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:174)
        at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:157)
        at net.minecraft.server.MinecraftServer.e(MinecraftServer.java:297)
        at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:284)
        at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:152)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:348)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:417)
    Help is appreciated! :)
     
  22. Offline

    thehutch

    whats at line 48 @vaxoc in your SleepNotify class?
     
  23. Offline

    vaxoc

    Hey. It's:
    PHP:
    getConfig().options().copyDefaults(true);
     
  24. Offline

    thehutch

    Could you paste a little more than that please? maybe your mainclass just so we can see it. sorry I did say line 48 but I should have said the mainclass sorry
     
  25. Offline

    vaxoc

    Okay. The main class:
    PHP:
    package me.vaxoc.plugins.sleepnotify;

    import java.util.ArrayList;
    import java.util.logging.Logger;

    import org.bukkit.entity.Player;
    import org.bukkit.event.Event.Priority;
    import org.bukkit.event.Event.Type;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.plugin.java.JavaPlugin;
    import com.nijiko.permissions.PermissionHandler;
    import com.nijikokun.bukkit.Permissions.Permissions;
    import org.bukkit.plugin.Plugin;

    public class 
    SleepNotify extends JavaPlugin {
        
    Player player;
        private 
    PlayerListener playerListener = new SleepNotifyPlayerListener();
        public static 
    PermissionHandler Permissions;
        protected static 
    boolean pbDetected;
        protected static 
    boolean permissionsDetected;
        protected static 
    boolean useOp;
        protected static 
    String WAKE_MESSAGE "has woken up";
        protected static 
    String SLEEP_MESSAGE "has gone to sleep";
        protected static 
    boolean CMD_ALL true;
        protected static 
    boolean OP_ALERT true;
        protected static 
    boolean LIST_OP true;
        public static 
    ArrayList<Playersleeping = new ArrayList<Player>();
        
    Logger log Logger.getLogger("Minecraft");

        public 
    void onDisable() {
            
    log.info(this " is now disabled!");

        }

        public 
    void onEnable() {
            
    setupPermissions();

            
    getServer().getPluginManager().registerEvent(Type.PLAYER_BED_ENTERplayerListenerPriority.Highestthis);
            
    getServer().getPluginManager().registerEvent(Type.PLAYER_BED_LEAVEplayerListenerPriority.Highestthis);
            
    getServer().getPluginManager().registerEvent(Type.PLAYER_QUITplayerListenerPriority.Highestthis);
            
    getCommand("sleeping").setExecutor(new SleepNotifyCommands(this));

            
    // CONFIGURATION
            
    getConfig().options().copyDefaults(true);
            
    saveConfig();
            
    // END CONFIGURATION

            // Check for PermissionsBukkit
            
    Plugin pb getServer().getPluginManager().getPlugin("PermissionsBukkit");
            if (
    pb != null) {
                
    pbDetected true;
                
    log.info("[SleepNotify] Found and will use PermissionsBukkit");
            }

            
    // Check for Permissions
            
    Plugin per getServer().getPluginManager().getPlugin("Permissions");
            if (
    per != null) {
                
    permissionsDetected true;
                
    log.info("[SleepNotify] Found and will use Permissions");
            }

            
    // SuperPerms Compatibility
            
    if (pbDetected == true && permissionsDetected == true) {
                
    permissionsDetected false;
                
    pbDetected true;
                
    log.info("[SleepNotify] Found and will use PermissionsBukkit");
            }

            
    // Default to OP if no Permission System is detected
            
    if (pbDetected != true && permissionsDetected != true) {
                
    useOp true;
                
    log.info("[SleepNotify] No permission system found. Defaulting to OP");
            }
            
    // Clear list of Sleeping Players
                
    sleeping.clear();

            
    log.info("[SleepNotify]" " " this " is now enabled!");
        }

        private 
    void setupPermissions() {
            
    Plugin test this.getServer().getPluginManager().getPlugin("Permissions");

            if (
    Permissions == null) {
                if (
    test != null) {
                    
    Permissions = ((Permissions)test).getHandler();
                } else {

                }
            }
        }
        }

     
  26. Offline

    Sagacious_Zed Bukkit Docs

    I think the server is not up to date.
     
  27. Offline

    thehutch

    Ok after I just typed a lot of help out for you my copy and paste decided to overwrite it all :(. anyway this is basically what I wrote:
    use this instead call this method in onEnable() and declare this in your mainclass:
    Code:
    FileConfiguration config;
    Code:
            public void setupConfig() {
    
                config = this.getConfig();
                config.options().header("insert header here");
                config.addDefault("path.of.string", "default value here");
                // write the addDefault as many times as you like for more values
                // then do this
                config.options().copyDefaults(true);
                saveConfig();
            }
     
  28. Offline

    vaxoc

    Haha, that seems to have fixed it! I didn't know about 1337! I only knew about 1317! Thanks so much!
    Thanks! Segacious_Zed's solution fixed it! Thanks for the help though!
     
  29. Offline

    thehutch

    Oh and also do not support permissions 3.1.6 by theYeti its dead and has been confirmed by himself no need to check for his plugin only use superPerms plugins

    I just want to shoot my keyboard it keeps deleting what I type or its bukkit being bugged again ARGH!!!
    Anyway I asked if you were even generating a config with your code?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  30. Offline

    vaxoc

    Haha sorry. Didn't pick up on the question part :). I have it where it finds the "config.yml" inside the src folder like dinnerbone said. The config.yml contains the defaults. You probably already knew that :)
     
Thread Status:
Not open for further replies.

Share This Page