Java Code Examples for org.bukkit.configuration.file.FileConfiguration#getKeys()

The following examples show how to use org.bukkit.configuration.file.FileConfiguration#getKeys() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConfigLoader.java    From StackMob-3 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean updateConfig(){
    // Get the latest version of the file from the jar.
    InputStream is = sm.getResource(filename +  ".yml");
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
    FileConfiguration includedFile = YamlConfiguration.loadConfiguration(reader);
    // Load a copy of the current file to check for later.
    FileConfiguration originalFile = YamlConfiguration.loadConfiguration(file);
    // Loop through the values of the latest version and set any that are not present.
    for(String key : includedFile.getKeys(true)){
        if(!(getCustomConfig().contains(key))){
            getCustomConfig().set(key, includedFile.get(key));
        }
    }
    // Save the changes made, copy the default file.
    if(!(getCustomConfig().saveToString().equals(originalFile.saveToString()))){
        try {
            copyDefault();
            fc.save(file);
            return true;
        }catch (IOException e){
            return false;
        }
    }
    return false;
}
 
Example 2
Source File: SignScript.java    From DungeonsXL with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param name   the name of the Announcer
 * @param config the config that stores the information
 */
public SignScript(String name, FileConfiguration config) {
    this.name = name;
    signs = new ArrayList<>(config.getKeys(false).size());

    int i = 0;
    for (String key : config.getKeys(false)) {
        List<String> lines = config.getStringList(key);
        if (lines.size() != 4) {
            MessageUtil.log("Found an invalid sign (ID: " + key + ") in script \"" + name + "\". Every sign must have 4 text lines.");
            continue;
        }
        signs.add(i, lines.toArray(new String[4]));
        i++;
    }
}
 
Example 3
Source File: ConfigUpdater.java    From BetonQuest with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unused")
private void update_from_v25() {
    try {
        for (ConfigPackage pack : Config.getPackages().values()) {
            String packName = pack.getName();
            FileConfiguration events = pack.getEvents().getConfig();
            for (String key : events.getKeys(false)) {
                String event = events.getString(key);
                if (event.startsWith("journal ")) {
                    events.set(key, "journal add " + event.substring(8));
                }
            }
            pack.getEvents().saveConfig();
        }
    } catch (Exception e) {
        LogUtils.getLogger().log(Level.WARNING, ERROR);
        LogUtils.logThrowable(e);
    }
    LogUtils.getLogger().log(Level.INFO, "Added \"add\" keyword to journal events");
    config.set("version", "v26");
    instance.saveConfig();
}
 
Example 4
Source File: BlockStorage.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
private void loadChunks() {
    File chunks = new File(PATH_CHUNKS + "chunks.sfc");

    if (chunks.exists()) {
        FileConfiguration cfg = YamlConfiguration.loadConfiguration(chunks);

        for (String key : cfg.getKeys(false)) {
            try {
                if (world.getName().equals(PatternUtils.SEMICOLON.split(key)[0])) {
                    SlimefunPlugin.getRegistry().getChunks().put(key, new BlockInfoConfig(parseJSON(cfg.getString(key))));
                }
            }
            catch (Exception x) {
                Slimefun.getLogger().log(Level.WARNING, x, () -> "Failed to load " + chunks.getName() + " in World " + world.getName() + '(' + key + ") for Slimefun " + SlimefunPlugin.getVersion());
            }
        }
    }
}
 
Example 5
Source File: GUIs.java    From MineTinker with GNU General Public License v3.0 5 votes vote down vote up
private static HashMap<String, String> getExplanations(FileConfiguration root, String config) {
	HashMap<String, String> explanations = new HashMap<>();
	String start = "GUIs.ConfigurationEditor." + config + ".";

	for (String key : root.getKeys(true)) {
		String explanation = LanguageManager.getString(start + key);
		if (!explanation.equals("")) {
			explanations.put(key, explanation);
		}
	}

	return explanations;
}
 
Example 6
Source File: LocaleManager.java    From Civs with GNU General Public License v3.0 5 votes vote down vote up
private void loadLanguageFromConfig(FileConfiguration config, String name) {
    HashMap<String, String> currentLanguage = new HashMap<>();

    for (String translationKey : config.getKeys(false)) {
        currentLanguage.put(translationKey, config.getString(translationKey));
    }

    languageMap.put(name, currentLanguage);
}
 
Example 7
Source File: FallbackConfigUtil.java    From Civs with GNU General Public License v3.0 5 votes vote down vote up
public static FileConfiguration getConfigFullPath(File originalFile, String url) {
    FileConfiguration config = new YamlConfiguration();
    try {
        InputStream inputStream = FallbackConfigUtil.class.getResourceAsStream(url);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        config.load(reader);
        if (originalFile != null && originalFile.exists()) {
            FileConfiguration configOverride = new YamlConfiguration();
            configOverride.load(originalFile);
            for (String key : configOverride.getKeys(true)) {
                if (configOverride.get(key) instanceof ConfigurationSection) {
                    continue;
                }
                config.set(key, configOverride.get(key));
            }
        }
    } catch (Exception e) {
        if (originalFile != null) {
            Civs.logger.log(Level.SEVERE, "File name: {0}", originalFile.getName());
        }
        if (url != null) {
            Civs.logger.log(Level.SEVERE, "Resource path: {0}", url);
        }
        Civs.logger.log(Level.SEVERE, "Unable to load config", e);
    }

    return config;
}
 
Example 8
Source File: CrackshotLoader.java    From QualityArmory with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isCrackshotGun(FileConfiguration crackshotFile) {
	for (String s : crackshotFile.getKeys(false))
		if (crackshotFile.contains(s + ".Item_Information"))
			return true;
	return false;
}
 
Example 9
Source File: BlockStorage.java    From Slimefun4 with GNU General Public License v3.0 4 votes vote down vote up
private void loadBlocks(File directory) {
    long total = directory.listFiles().length;
    long start = System.currentTimeMillis();
    long done = 0;
    long timestamp = System.currentTimeMillis();
    long totalBlocks = 0;
    int delay = SlimefunPlugin.getCfg().getInt("URID.info-delay");

    try {
        for (File file : directory.listFiles()) {
            if (file.getName().equals("null.sfb")) {
                Slimefun.getLogger().log(Level.WARNING, "File with corrupted blocks detected!");
                Slimefun.getLogger().log(Level.WARNING, "Slimefun will simply skip this File, you should look inside though!");
                Slimefun.getLogger().log(Level.WARNING, file.getPath());
            }
            else if (file.getName().endsWith(".sfb")) {
                if (timestamp + delay < System.currentTimeMillis()) {
                    int progress = Math.round((((done * 100.0F) / total) * 100.0F) / 100.0F);
                    Slimefun.getLogger().log(Level.INFO, "Loading Blocks... {0}% done (\"{1}\")", new Object[] { progress, world.getName() });
                    timestamp = System.currentTimeMillis();
                }

                FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);

                for (String key : cfg.getKeys(false)) {
                    Location l = deserializeLocation(key);
                    String chunkString = locationToChunkString(l);

                    try {
                        totalBlocks++;
                        String json = cfg.getString(key);
                        Config blockInfo = parseBlockInfo(l, json);

                        if (blockInfo != null && blockInfo.contains("id")) {
                            if (storage.containsKey(l)) {
                                // It should not be possible to have two blocks on the same location. Ignore the
                                // new entry if a block is already present and print an error to the console.

                                Slimefun.getLogger().log(Level.INFO, "Ignoring duplicate block @ {0}, {1}, {2}", new Object[] { l.getBlockX(), l.getBlockY(), l.getBlockZ() });
                                Slimefun.getLogger().log(Level.INFO, "New: {0} | Old: {1}", new Object[] { key, serializeBlockInfo(storage.get(l)) });
                                continue;
                            }

                            storage.put(l, blockInfo);

                            if (SlimefunPlugin.getRegistry().getTickerBlocks().contains(file.getName().replace(".sfb", ""))) {
                                Set<Location> locations = SlimefunPlugin.getRegistry().getActiveTickers().getOrDefault(chunkString, new HashSet<>());
                                locations.add(l);
                                SlimefunPlugin.getRegistry().getActiveTickers().put(chunkString, locations);

                                if (!SlimefunPlugin.getRegistry().getActiveChunks().contains(chunkString)) {
                                    SlimefunPlugin.getRegistry().getActiveChunks().add(chunkString);
                                }
                            }
                        }
                    }
                    catch (Exception x) {
                        Slimefun.getLogger().log(Level.WARNING, x, () -> "Failed to load " + file.getName() + '(' + key + ") for Slimefun " + SlimefunPlugin.getVersion());
                    }
                }

                done++;
            }
        }
    }
    finally {
        long time = (System.currentTimeMillis() - start);
        Slimefun.getLogger().log(Level.INFO, "Loading Blocks... 100% (FINISHED - {0}ms)", time);
        Slimefun.getLogger().log(Level.INFO, "Loaded a total of {0} Blocks for World \"{1}\"", new Object[] { totalBlocks, world.getName() });

        if (totalBlocks > 0) {
            Slimefun.getLogger().log(Level.INFO, "Avg: {0}ms/Block", DoubleHandler.fixDouble((double) time / (double) totalBlocks, 3));
        }
    }
}