[2DevsStudio] - GSON Serialization/Configuration Library 2021-02-09

serialization, configuration, library, gson, json

  1. 2DevsStudio
    Native Minecraft Version:
    1.16
    Tested Minecraft Versions:
    • 1.7
    • 1.8
    • 1.9
    • 1.10
    • 1.11
    • 1.12
    • 1.13
    • 1.14
    • 1.15
    • 1.16
    Source Code:
    https://github.com/slighterr12/SimpleJSONConfig
    Contributors:
    2DevsStudio
    SimpleJSONConfig

    Main features:

    How to use:

    Code (Java):
    Register SimpleJsonConfig in your plugin

    public class Main extends JavaPlugin {
     
        @Override
        public void onEnable() {
            // Default directory is YourPlugin/configuration/
            SimpleJSONConfig.INSTANCE.register(this);
            // Or Specify default directory
            SimpleJSONConfig.INSTANCE.register(this, new File("default/config/directory"));
        }
    }
    Code (Java):
    Just create a new class which will be your configuration.

    Class should be annotated as `@Configuration` and should extend `Config` class


    @Configuration( "config" )        //OR
    @Configuration( "config.json" )   //OR
    @Configuration( "path/to/config" )

    @SuppressWarnings( "FieldMayBeFinal" ) // SimpleJsonConfig not supporting final modifiers yet

    @Getter //We recommend using lombok to access fields, but you can make all the fields public as well
    public class MyConfig extends Config {
        private String joinMessage = "Default join message";
        private List<ItemStack> startingEquipment = new ArrayList<>(
                Collections.singletonList(new ItemStack(Material.DIRT)));
        @Comment( "This is Comment which appear in the config file as well" )
        private YourType something;
    }

    Code (Java):
    public class Main {
        private MyConfig config = Config.getConfig(MyConfig.class);
     
        // ... your logic
    }


    Code (Java):
    import com.twodevsstudio.simplejsonconfig.api.Config;
    import com.twodevsstudio.simplejsonconfig.interfaces.Autowired;

    public class Main {
        @Autowired
        private static MyConfig config;
     
        // ... your logic
    }

    Adding dependency:

    Maven:


    Code (Text):
    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>

    <dependecies>
    <dependency>
        <groupId>com.github.slighterr12</groupId>
        <artifactId>SimpleJSONConfig</artifactId>
        <version>9360e6b</version>
        <scope>compiled</scope> <!-- Better if only one plugin uses SimpleJsonConfig, no cross plugin config sharing -->
        <scope>provided</scope> <!-- Add SimpleJsonConfig to your plugins folder, enables cross plugin config sharing -->
    </dependency>
    </dependecies>

    Gradle:


    Code (Text):
    repositories {
        maven { url 'https://jitpack.io' }
    }

    dependencies {
        implementation 'com.github.slighterr12:SimpleJSONConfig:9360e6b' //Better if only one plugin uses SimpleJsonConfig, no cross plugin config sharing
        compileOnly 'com.github.slighterr12:SimpleJSONConfig:9360e6b'    //Add SimpleJsonConfig to your plugins folder, enables cross plugin config sharing
    }

    To exclude field from serialization just add a `transient` modifier

    Code (Java):
    @Getter
    @Configuration( "playerConfig" )
    public class MyConfig extends Config {
        private String playerName = "Slighter";
        private transient Player cachedPlayer; //<- this field is not included in the config
     
        public Player getCachedPlayer() {
         
            if (cachedPlayer == null) {
                this.cachedPlayer = Bukkit.getOfflinePlayer(playerName);
            }
            return this.cachedPlayer;
        }
    }

    Additional Features:

    Code (Java):
    You can also serialize objects which are not configuration using `Serializer`

    public class MyClass {
     
        private static final Serializer SERIALIZER = Serializer.getInst();
     
        private String aString = "Text";
        private int anInt = 10;
     
        public void save(File targetFile) {
         
            SERIALIZER.saveConfig(this, targetFile);
        }
     
        public static MyClass load(File sourceFile) {
         
            return SERIALIZER.loadConfig(MyClass.class, sourceFile);
        }
    }
    Code (Java):
    You can easily save and reload your config


    @Getter
    @Setter
    @Configuration( "config" )
    public class MyConfig extends Config {
        private String joinMessage = "Default join message";
    }

    Code (Java):
    public class MyClass {
     
        @Autowired
        public static MyConfig config;
     
        public void foo() {
         
            config.setJoinMessage("Better Join Message");
            config.save(); //Save config file
        }
    }

    Code (Java):
    public class ReloadCommand implements CommandExecutor {
     
        @Override
        public boolean onCommand(CommandSender sender, Command command, String name, String[] args) {
         
            Config.getConfig(MyConfig.class).reload(); // Reload single config
            Config.reloadAll(); //Reload all configs
            return true;
        }
    }
    Zdziszkee likes this.

Recent Reviews

  1. Zdziszkee
    Zdziszkee
    5/5,
    Version: 2021-02-09
    Creators of this plugin are awesome, reliable. Library itself is very useful and amazing in it's simplicity
    1. 2DevsStudio