SpigotLib - ConfigurationAPI

Jul 3, 2016
SpigotLib - ConfigurationAPI
  • [​IMG]



    Example Config.java files
    If you would like to load a configuration, it's highly suggested to make a new Config.java for it, for handling data more easyli:


    Your Config.java file should look like:


    PHP:
    public class Config {[/COLOR][/COLOR]
    [COLOR=#0000b3][COLOR=#000000]    @ConfigSerialization.ConfigOptions(comment = "Path to automatic config backup, remove this line to disable config autobackups.")
       public static String backup="plugins/ServerUtils/backups/config-backup(<time>).yml";

        @ConfigSerialization.ConfigOptions(comment = "Players will need to have su.command.<command> permission (spaces replaced with dots in order to use the command)")
        public static boolean nocmd;

        @ConfigSerialization.ConfigOptions(comment = "Enable editing signs easily by right clicking on them, if you have su.se.rightclick permission")
        public static boolean signeditor;

        @ConfigSerialization.ConfigOptions(comment = "Make jump pads easily")
        public static HashMap<BlockData,Vector> jumpPads=new HashMap<BlockData, Vector>();

        @ConfigSerialization.ConfigOptions(comment = "Disable pads, if you don't need them to improve your servers performance")
        public static boolean enablePads;

        @ConfigSerialization.ConfigOptions(comment = "Disable food level lose for players having su.nofoodlose permission")
        public static boolean noFoodLevelLose;

        @ConfigSerialization.ConfigOptions(comment = "Commands executed on player join\n" +
                " Usable variables:\n" +
                "  - SpigotLibs VariableAPIs every variable\n" +
                " Usable prefixes:\n" +
                "  - NORMAL:<message> -> force player to enter message\n" +
                "  - OP:<message> -> force player to enter message, as he would be op\n" +
                "  - CONSOLE:<command> -> execute commands from console\n" +
                "  - LOG:<message> -> log message to console\n" +
                "  - ABM:<message> -> send action bar message to player\n" +
                "  - TS:<fade-in> <showtime> <fade-out> -> set players title settings\n" +
                "  - TITLE:<message> -> send title message to player\n" +
                "  - SUBTITLE:<message> -> set subtitle message of a player (need to use the TITLE prefix before this in order to show the subtitle)\n" +
                "  - MSG:<message> -> send SpigotLibs formatted JSON tags supported message to player.")
        public static ArrayList<Command> joinCommands=new ArrayList<Command>();

        @ConfigSerialization.ConfigOptions(comment = "Command aliases, players need to have the su.alias.<name> permission to use them\n"+
            "Use prefix ARGS:<number> to make sure, that the executor has entered enough arguments.")
        public static HashMap<String,ArrayList<Command>> aliases= new HashMap<String, ArrayList<Command>>();
    }


    You don't need to put these comments to it, if you don't want to, and you can also create any kind of custom objects which will be saved totally correctly:


    PHP:
    package gyurix.pointrunner;

    import gyurix.serverutils.Command;
    import gyurix.spigotutils.BlockData;
    import org.bukkit.Location;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.UUID;

    /**
    * Created by GyuriX on 2015.08.07..
    */

    public class Config {
        public static Settings settings;
        public static Signs signs= new Signs();
        public static HashMap<UUID,Stats> stats;
        public static class Signs{
            public static ArrayList<String> create,waiting,starting,inGame,beforeNextRound,nextRound,finish;
        }
        public static class Settings{
            public int minplayer=Config.settings.minplayer;
            public int maxplayer=Config.settings.maxplayer;
            public int countdownStart=Config.settings.countdownStart;
            public int countdownRounds=Config.settings.countdownRounds;
            public int gametime=Config.settings.gametime;
            public int delayStart=Config.settings.delayStart;
            public int delayNextRound=Config.settings.delayNextRound;
            public int delayEnd=Config.settings.delayEnd;
            public int rounds=Config.settings.rounds;
            public int delayBlockWalk=Config.settings.delayBlockWalk;
            public Cmds commands;
            public HashMap<BlockData,Long> points;
            public Stats stats;
            public static class Cmds{
                public HashMap<Integer,ArrayList<Command>> countdownStart= new HashMap<>(),
                        countdownRound= new HashMap<>(),
                        countdownInGame= new HashMap<>(),
                        waiting= new HashMap<>();
                public ArrayList<Command> spectateStart= new ArrayList<>()
                ,spectateStarter= new ArrayList<>()
                ,spectateLeaveArena=new ArrayList<>()
                ,playerJoin= new ArrayList<>()
                ,playerJoined= new ArrayList<>()
                ,roundWinner= new ArrayList<>()
                ,roundLoser= new ArrayList<>()
                ,logoutPlayer= new ArrayList<>()
                ,logoutSpectator= new ArrayList<>()
                ,logoutBeforeRound= new ArrayList<>()
                ,logoutBeforeStart= new ArrayList<>()
                ,winner= new ArrayList<>()
                ,loser= new ArrayList<>();
            }

        }
        public static class Stats{
            int roundWins,roundLoses,wins,loses,logouts;
        }
        public static HashMap<String,Game> games= new HashMap<>();
    }
     

    ConfigOptions

    From the above example you know, that you can save almost ANY KIND OF object or class to the Config, but there can be situations, when you don't want to save something. You can disable the serializing of some fields by using this ConfigOptions annotation.

    PHP:
    @Target({java.lang.annotation.ElementType.FIELD})

        @Retention(RetentionPolicy.RUNTIME)
        public @interface ConfigOptions {
            String defaultValue() default "null";

            String comment() default "";

            boolean serialize() default true;
        }
    As you can see from this tiny source code, this annotation have 3 parameters:
    defaultValue: The string format of the value of the default setting, which shouldn't be saved.
    comment: The comment above this field in the config
    serialize: The possibility to disable serializing some fields




    Using the ConfigFile:

    PHP:
    package gyurix.spigotlib.example;
    import gyurix.configfile.ConfigFile;
    import gyurix.spigotlib.SU;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    import java.io.File;

    public class Main extends JavaPlugin implements Listener {

        public static ConfigFile kf;

        @Override
        public void onEnable(){
            //You should load your config when your plugin is enabled.
            load();
        }
        public void save(){
            //Saving can be done using this simple method call.
            kf.save();
        }

        public void load(){
            //Copy the config.yml file from the jar file
            SU.saveResources(this, "config.yml");
            //Load the config file
            kf=new ConfigFile(new File(getDataFolder() + File.separator + "config.yml"))
            //Deserialize the config to the static Config class.
            kf.data.deserialize(Config.class);
            //That's all, now you can edit anything in your static Config class, and save it, when you would like to.
        }

        public void reload(){
            //Reloading a configuration can be done in two steps:
            //First you need to reload the file
            kf.reload();
            //Then you need to deserialize it again to your Config.class
            kf.data.deserialize(Config.class);
        }
    }


    [​IMG]
  • Loading...
  • Loading...