Advertisement
FrenchLyfe

ConfigurationManager

Sep 10th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. package net.pixelrift.PlayerArenas;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.Map;
  6. import java.util.UUID;
  7.  
  8. import org.spongepowered.api.Sponge;
  9.  
  10. import ninja.leaping.configurate.commented.CommentedConfigurationNode;
  11. import ninja.leaping.configurate.loader.ConfigurationLoader;
  12.  
  13. public class ConfigurationManager {
  14.      
  15.      CommentedConfigurationNode arena;
  16.  
  17.         private static ConfigurationManager instance = new ConfigurationManager();
  18.  
  19.         public static ConfigurationManager getInstance(){
  20.             return instance;
  21.         }
  22.  
  23.         private ConfigurationLoader<CommentedConfigurationNode> configLoader;
  24.         private CommentedConfigurationNode config;
  25.  
  26.         public void setup(File configFile, ConfigurationLoader<CommentedConfigurationNode> configLoader){
  27.             this.configLoader = configLoader;
  28.  
  29.             if (!configFile.exists()){
  30.                 try {
  31.                     configFile.createNewFile();
  32.                     loadConfig();
  33.                     initDefaultConfig();
  34.                 } catch (IOException e) {
  35.                     e.printStackTrace();
  36.                 }
  37.             } else {
  38.                 loadConfig();
  39.             }
  40.         }
  41.  
  42.         public CommentedConfigurationNode getConfig(){
  43.             return config;
  44.         }
  45.        
  46.         public Map<Object, ? extends CommentedConfigurationNode> getArenaMap() {
  47.             return getConfig().getNode("PlayerArenas", "Arenas").getChildrenMap();
  48.             //could also just use config.getNode("PlayerArenas", "Arenas").getChildrenMap(), but that shouldn't make a difference
  49.         }
  50.        
  51.         public void saveConfig(){
  52.             try {
  53.                 configLoader.save(config);
  54.             } catch (IOException e) {
  55.                 e.printStackTrace();
  56.             }
  57.         }
  58.        
  59.         public CommentedConfigurationNode getArenaConfigNode() {
  60.             return arena;
  61.         }
  62.  
  63.         public void loadConfig(){
  64.             try {
  65.                 config = configLoader.load();
  66.             } catch (IOException e) {
  67.                 e.printStackTrace();
  68.             }
  69.         }
  70.  
  71.         private void initDefaultConfig() {
  72.             arena = config.getNode("PlayerArenas", "Arenas")
  73.                     .setComment("Here you can define all the broadcaster that you need.");
  74.             CommentedConfigurationNode defaultArena = arena.getNode("-1")
  75.                     .setComment("Default Arena, virtually useless");
  76.             defaultArena.getNode("enabled").setValue("false");
  77.             saveConfig();
  78.  
  79.         }
  80.        
  81.         public void setupArenaConfig(int name, UUID uuid) {
  82.             getConfig().getNode("PlayerArenas", "Arenas", name )
  83.                 .setComment("Values for Arena #" + name);
  84.            
  85.             getConfig().getNode("PlayerArenas", "Arenas", name , "enabled").setValue(false);
  86.             getConfig().getNode("PlayerArenas", "Arenas", name, "player").setValue(Sponge.getGame().getServer().getPlayer(uuid).toString());
  87.             saveConfig();
  88.         }    
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement