[LIB] Oops, they broke our Configuration files [1317+]

Discussion in 'Resources' started by codename_B, Oct 11, 2011.

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

    codename_B

    This lib aims to replicate the removed functionality by emulating the old style Configuration object while still maintaining the integrity of the new one. It will be included in bPermissions 1.7.4 onwards, so of course you can just include bPermissions as a lib in your project, or you can refactor the class into a different package, entirely up to yourself.

    Code:
    package de.bananaco.permissions.oldschool;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Set;
    
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    /**
     * Substitute for the old Bukkit configuration object
     * Replicates the old functionality in the new object
     * @author codename_B
     *
     */
    public class Configuration extends YamlConfiguration {
    	private final File file;
    	
    	/**
    	 * A substitute for getConfiguration();
    	 * @author desht
    	 * @param plugin
    	 */
    	public Configuration(JavaPlugin plugin) {
    		this(new File(plugin.getDataFolder(), "config.yml"));
    		load();
    	}
    	
    	public Configuration(String fileName) {
    		this(new File(fileName));
    	}
    	
    	public Configuration(File file) {
    		super();
    		if(file == null) 
    			System.err.println("File should not be null!");
    		this.file = file;
    	}
    	
    	public void load() {
    	try {
    	// First do checks to create the initial file
    		if(!file.exists()) {
    			if(file.getParentFile()!=null)
    			file.getParentFile().mkdirs();
    			if(file.createNewFile())
    				super.save(file);
    			else
    				throw new Exception("Cannot load: File can not be created!");
    		}
    	// Then do checks to save the file
    	super.load(file);
    	} catch (Exception e) {
    	e.printStackTrace();
    		}
    	}
    	
    	public void save() {
    	try {
    		if(!file.exists()) {
    			if(file.getParentFile()!=null)
    			file.getParentFile().mkdirs();
    			if(file.createNewFile())
    				super.save(file);
    			else
    				throw new Exception("Cannot save: File can not be created!");
    		}
    		else
    		super.save(file);
    	} catch (Exception e) {
    	e.printStackTrace();
    	}
    	}
    
    	public List<String> getKeys() {
    		Set<String> keys = super.getKeys(false);
    		List<String> lkeys = new ArrayList<String>();
    		if(keys != null)
    		for(String key : keys)
    			lkeys.add(key);
    		return lkeys;
    	}
    	
    	public List<String> getKeys(String path) {
    		List<String> lkeys = new ArrayList<String>();
    		ConfigurationSection cs = super.getConfigurationSection(path);
    		
    		if(cs == null)
    			return lkeys;
    		
    		Set<String> keys = cs.getKeys(false);
    		
    		if(keys != null)
    		for(String key : keys)
    			lkeys.add(key);
    		return lkeys;
    	}
    
    	public void setProperty(String path, Object object) {
    		super.set(path, object);
    	}
    
    	/**
    	 * Should work according to the javadocs, but doesn't.
    	 * Fixed in the latest bukkit builds
    	 * @param path
    	 */
    	public void removeProperty(String path) {
    		try {
    		super.set(path, null);
    		} catch (Exception e) {
    		}
    	}
    
    	public List<String> getStringList(String path, List<String> def) {
    		if(def == null)
    			def = new ArrayList<String>();
    		
    		List<?> list = super.getList(path, def);
    		if(list == null)
    			return def;
    		try {
    		@SuppressWarnings("unchecked")
    		List<String> sList = (List<String>) list;
    		return sList;
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return def;
    	}
    
    }
    
     
  2. Offline

    arnie231

    thank you ;)
     
  3. Offline

    iffa

    Add JavaDocs pl0x.
     
  4. Offline

    codename_B

    Or you could.
     
  5. Offline

    iffa

    Why me?
     
  6. Offline

    codename_B

    Because you asked for them, and you can use a keyboard.
     
    RROD likes this.
  7. Offline

    iffa

    GODDAMMIT what is with all this yellow stuff? Y u no make good coed
     
  8. Offline

    Dinnerbone Bukkit Team Member

    Most people have to change... 3 lines? 5? Seriously, using this is more work than just using the new Config. Use the new config!
     
  9. Offline

    DomovoiButler

    is the jd.bukkit.org/apidocs updated to the latest config?
     
  10. Offline

    Dinnerbone Bukkit Team Member

  11. Offline

    thehutch

    @Dinnerbone could you or someone create a new configuration guide like CaptainAwesome7's last one and then add it to the HUGE_plugin_tutorial because then this would all seem a lot easier. unless your gonna do your plugin tutorial on this new setup? :)
     
  12. Offline

    Dinnerbone Bukkit Team Member

    I am working on some tutorials.
     
  13. Offline

    thehutch

    yay any estimate on when these might be out? not to be impatient but so all of the minecraft servers get their plugins working again on this new build. :p
     
  14. Offline

    Dinnerbone Bukkit Team Member

  15. Offline

    thehutch

    not for me I have to change loads since my plugin is mostly based upon my configuration also im rusty in Java so everything everyone is saying isnt making much sense to me :(
     
  16. Offline

    calthor

    so eh, how do you save an array, list, set or arraylist in the config with this update? :eek:


    EDIT:
    another question, is there a way to read something like this?
    Code:
    list:
    -key1:0
     key2:1024
     key3:512
     key4:32768
     key5:168
    
    -key1:640
     key2:44
     key3:864
     key4:2048
     key5:96
    
     
  17. Offline

    Dinnerbone Bukkit Team Member

    config.set(someList)

    And

    List<Map<String, Object>> values = config.getList("list");
     
  18. Offline

    calthor

    seems to be over 100 lines for me, but that's okay for now.
    please don't change the config stuff too often :)
     
  19. Offline

    kuzi117

    Is it bad that I have like 300 lines worth? :p
     
  20. Offline

    K3V1N32

    you just saved me having to rewrite/add approx 667 lines of code(my plugin uses alot of yml files lol)
    THANKYOU!!
     
    codename_B likes this.
Thread Status:
Not open for further replies.

Share This Page