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

The following examples show how to use org.bukkit.configuration.file.YamlConfiguration#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: SettingsManager.java    From EnchantmentsEnhance with GNU General Public License v3.0 6 votes vote down vote up
public static void generateEnchantments() throws IOException {
    Logger logger = Main.getMain().getLogger();
    logger.info("Updating enchantments to the latest...");
    int settings = 0;
    int addedSettings = 0;

    InputStream defConfigStream = Main.getMain().getResource("enchantments.yml");

    if (defConfigStream != null) {
        YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(new InputStreamReader(defConfigStream));
        for (String string : defConfig.getKeys(true)) {
            if (!enchant.contains(string)) {
                enchant.set(string, defConfig.get(string));
                addedSettings++;
            }
            if (!enchant.isConfigurationSection(string))
                settings++;
        }
    }

    logger.info("Found " + settings + " enchantment settings");
    logger.info("Added " + addedSettings + " new enchantment settings");
    if (addedSettings > 0) {
        enchant.save(enchantfile);
    }
}
 
Example 2
Source File: UCConfig.java    From UltimateChat with GNU General Public License v3.0 6 votes vote down vote up
private static YamlConfiguration updateFile(File saved) {
    YamlConfiguration finalyml = new YamlConfiguration();
    YamlConfiguration tempProts = new YamlConfiguration();
    try {
        finalyml.load(saved);

        tempProts.load(new InputStreamReader(UChat.get().getResource("assets/ultimatechat/protections.yml"), StandardCharsets.UTF_8));
    } catch (Exception e) {
        e.printStackTrace();
    }

    for (String key : tempProts.getKeys(true)) {
        Object obj = tempProts.get(key);
        if (finalyml.get(key) != null) {
            obj = finalyml.get(key);
        }
        finalyml.set(key, obj);
    }
    return finalyml;
}
 
Example 3
Source File: SaneEconomyMobKills.java    From SaneEconomy with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onEnable() {
    this.saneEconomy = (SaneEconomy) this.getServer().getPluginManager().getPlugin("SaneEconomy");
    super.onEnable();

    YamlConfiguration amountsConfig;

    if (!(new File(this.getDataFolder(), "amounts.yml").exists())) {
        amountsConfig = YamlConfiguration.loadConfiguration(new InputStreamReader(this.getClass().getResourceAsStream("/amounts.yml")));
        try {
            amountsConfig.save(new File(this.getDataFolder(), "amounts.yml"));
        } catch (IOException e) {
            throw new RuntimeException("Failed to save amounts.yml to plugin data folder!");
        }
    } else {
        amountsConfig = YamlConfiguration.loadConfiguration(new File(this.getDataFolder(), "amounts.yml"));
    }

    for (String entityTypeName : amountsConfig.getKeys(false)) {
        double value = amountsConfig.getDouble(entityTypeName);
        this.killAmounts.put(entityTypeName, value);
    }

    this.getServer().getPluginManager().registerEvents(new EntityDamageListener(this), this);
}
 
Example 4
Source File: Util.java    From QuickShop-Reremake with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Parse colors for the YamlConfiguration.
 *
 * @param config yaml config
 */
public static void parseColours(@NotNull YamlConfiguration config) {
    Set<String> keys = config.getKeys(true);
    for (String key : keys) {
        String filtered = config.getString(key);
        if (filtered == null) {
            continue;
        }
        if (filtered.startsWith("MemorySection")) {
            continue;
        }
        filtered = parseColours(filtered);
        config.set(key, filtered);
    }
}
 
Example 5
Source File: TLocaleInstance.java    From TabooLib with MIT License 5 votes vote down vote up
public void load(YamlConfiguration configuration, boolean cleanup) {
    int originNodes = map.size();
    int updateNodes = 0;
    if (cleanup) {
        map.clear();
    }
    for (String s : configuration.getKeys(true)) {
        boolean updated = false;
        Object value = configuration.get(s);
        if (value instanceof TLocaleSerialize) {
            updated = map.put(s, Collections.singletonList((TLocaleSerialize) value)) != null;
        }
        // TabooLib 4.x 兼容
        else if (TabooLibAPI.isOriginLoaded() && value.getClass().getName().startsWith("com.ilummc.tlib.resources.type")) {
            updated = map.put(s, Collections.singletonList(fromPluginVersion(value))) != null;
        } else if (value instanceof List && !((List) value).isEmpty()) {
            if (isListString((List) value)) {
                updated = map.put(s, Collections.singletonList(TLocaleText.of(value))) != null;
            } else {
                updated = map.put(s, ((List<?>) value).stream().map(o -> {
                    if (o instanceof TLocaleSerialize) {
                        return (TLocaleSerialize) o;
                    }
                    // TabooLib 4.x 兼容
                    else if (TabooLibAPI.isOriginLoaded() && o.getClass().getName().startsWith("com.ilummc.tlib.resources.type")) {
                        return fromPluginVersion(o);
                    }
                    return TLocaleText.of(String.valueOf(o));
                }).collect(Collectors.toList())) != null;
            }
        } else if (!(value instanceof ConfigurationSection)) {
            String str = String.valueOf(value);
            updated = map.put(s, Collections.singletonList(str.length() == 0 ? TLocaleSerialize.getEmpty() : TLocaleText.of(str))) != null;
        }
        if (updated) {
            updateNodes++;
        }
    }
    latestUpdateNodes.set(originNodes - updateNodes);
}
 
Example 6
Source File: SettingsManager.java    From EnchantmentsEnhance with GNU General Public License v3.0 5 votes vote down vote up
public static void generateConfig() throws IOException {

        Logger logger = Main.getMain().getLogger();
        logger.info("Updating config to the latest...");
        int settings = 0;
        int addedSettings = 0;

        InputStream defConfigStream = Main.getMain().getResource("config.yml");

        backupConfig();
        if (defConfigStream != null) {
            YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(new InputStreamReader(defConfigStream));
            for (String string : defConfig.getKeys(true)) {
                if (string.startsWith("enhance")) {
                    continue;
                }
                if (!config.contains(string)) {
                    config.set(string, defConfig.get(string));
                    addedSettings++;
                }
                if (!config.isConfigurationSection(string))
                    settings++;
            }
        }


        logger.info("Found " + settings + " config settings");
        logger.info("Added " + addedSettings + " new config settings");
        if (addedSettings > 0) {
            config.save(cfile);
        }
    }
 
Example 7
Source File: YamlEnumTest.java    From NovaGuilds with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testConfig() throws Exception {
	System.out.println();
	System.out.println("Testing config enums...");
	YamlConfiguration config = getConfig();
	final List<String> configEnumNames = new ArrayList<>();
	for(ConfigWrapper v : Config.values()) {
		configEnumNames.add(v.getName());
	}

	int missingCount = 0;
	for(String key : config.getKeys(true)) {
		boolean ig = config.isConfigurationSection(key);
		for(String ignore : ignoreConfig) {
			if(key.startsWith(ignore)) {
				ig = true;
				break;
			}
		}

		if(!ig) {
			String name = StringUtils.replace(key, ".", "_").toUpperCase();
			if(!configEnumNames.contains(name)) {
				if(missingCount == 0) {
					System.out.println("Missing keys:");
				}

				System.out.println(name + ",");
				missingCount++;
			}
		}
	}

	if(missingCount == 0) {
		System.out.println("All values are present in Config enum");
	}
	else {
		throw new Exception("Found " + missingCount + " missing Config enums");
	}
}
 
Example 8
Source File: YamlEnumTest.java    From NovaGuilds with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testMessages() throws Exception {
	System.out.println();
	System.out.println("Testing message wrappers...");
	File motherFile = new File(YamlParseTest.resourcesDirectory, "lang/en-en.yml");
	YamlConfiguration motherConfiguration = Lang.loadConfiguration(motherFile);
	final List<String> messageEnumNames = new ArrayList<>();

	for(MessageWrapper v : Message.values()) {
		messageEnumNames.add(v.getName());
	}

	int missingCount = 0;
	for(String key : motherConfiguration.getKeys(true)) {
		if(!motherConfiguration.isConfigurationSection(key)) {
			String name = StringUtils.replace(key, ".", "_").toUpperCase();
			if(!messageEnumNames.contains(name)) {
				if(missingCount == 0) {
					System.out.println("Missing keys:");
				}

				System.out.println(name + ",");
				missingCount++;
			}
		}
	}

	if(missingCount == 0) {
		System.out.println("All values are present in Message class");
	}
	else {
		throw new Exception("Found " + missingCount + " missing Message wrappers");
	}
}
 
Example 9
Source File: YamlIncompleteTranslationTest.java    From NovaGuilds with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testTranslations() throws Exception {
	//Mother lang setup
	File motherLangFile = new File(langDir, "en-en.yml");
	YamlConfiguration motherConfiguration = YamlConfiguration.loadConfiguration(motherLangFile);
	final List<String> motherKeys = new ArrayList<>();
	for(String key : motherConfiguration.getKeys(true)) {
		if(!motherConfiguration.isConfigurationSection(key)) {
			motherKeys.add(key);
		}
	}

	//List all languages and configuration sections
	Map<String, YamlConfiguration> configurationMap = new HashMap<>();
	if(langDir.isDirectory()) {
		File[] list = langDir.listFiles();

		if(list != null) {
			for(File langFile : list) {
				if(!langFile.getName().equals("en-en.yml")) {
					configurationMap.put(StringUtils.replace(langFile.getName(), ".yml", ""), Lang.loadConfiguration(langFile));
				}
			}
		}
	}

	//Get keys from all langs
	System.out.println("Testing lang files for missing keys...");
	int globalMissingCount = 0;
	for(Map.Entry<String, YamlConfiguration> entry : configurationMap.entrySet()) {
		int missingCount = 0;
		String name = entry.getKey();
		YamlConfiguration configuration = entry.getValue();

		System.out.println("---");
		System.out.println();
		System.out.println("Testing lang: " + name);

		for(String mKey : motherKeys) {
			if(!configuration.contains(mKey)) {
				if(missingCount == 0) {
					System.out.println("Missing keys:");
				}

				System.out.println(" - " + mKey);
				missingCount++;
			}
		}

		globalMissingCount += missingCount;
	}

	if(globalMissingCount == 0) {
		System.out.println("Result: No missing keys");
	}
	else {
		throw new Exception("Found " + globalMissingCount + " missing keys in lang files");
	}
}