<commit_before>
package net.craftstars.general.items;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.bukkit.util.config.Configuration;
import org.bukkit.util.config.ConfigurationNode;

import net.craftstars.general.General;

public class Kits {
	public static HashMap<String, Kit> kits = new HashMap<String, Kit>();
	public static HashMap<GotKit, Long> players = new HashMap<GotKit, Long>();
	
	public static boolean loadKits() {
		kits.clear();
		File folder = General.plugin.getDataFolder();
		File oldKits = new File(folder, "general.kits");
		if(oldKits.exists()) loadOldKits();
		File kitsFile = new File(folder, "kits.yml");
		if(!kitsFile.exists()) return !kits.isEmpty();
		Configuration kitsYml = new Configuration(kitsFile);
		kitsYml.load();
		for(String key : kitsYml.getKeys()) {
			ConfigurationNode kitNode = kitsYml.getNode(key);
			ConfigurationNode itemsNode = kitNode.getNode("items");
			if(itemsNode == null) {
				General.logger.warn("Kit '" + key + "' has no items and has been skipped.");
				continue;
			}
			int delay = kitNode.getInt("delay", 0);
			double cost = kitNode.getDouble("cost", 0.0);
			Kit kit = new Kit(key, delay, cost);
			for(String id : itemsNode.getKeys()) {
				ItemID item = Items.validate(id);
				if(!item.isValid()) {
					General.logger.warn("Kit '" + key + "' has an invalid item '" + id + "' which has been skipped.");
					continue;
				}
				kit.add(item, itemsNode.getInt(id, 1));
			}
			kits.put(key, kit);
		}
		return true;
	}
	
	@Deprecated
	public static boolean loadOldKits() {
		boolean foundAnException = false;
		Exception exceptionToShow = new Exception("If you see this, something's VERY wrong.");
		try {
			File dataFolder = General.plugin.getDataFolder();
			BufferedReader br = new BufferedReader(new FileReader(new File(dataFolder, "general.kits")));
			String l;
			int lineNumber = 1;
			kits.clear();
			String list;
			String[] listing;<commit_msg>Tweak the kits.yml format so that duplicate keys do not get eaten.<commit_after>
package net.craftstars.general.items;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.bukkit.util.config.Configuration;
import org.bukkit.util.config.ConfigurationNode;

import net.craftstars.general.General;

public class Kits {
	public static HashMap<String, Kit> kits = new HashMap<String, Kit>();
	public static HashMap<GotKit, Long> players = new HashMap<GotKit, Long>();
	
	public static boolean loadKits() {
		kits.clear();
		File folder = General.plugin.getDataFolder();
		File oldKits = new File(folder, "general.kits");
		if(oldKits.exists()) loadOldKits();
		File kitsFile = new File(folder, "kits.yml");
		if(!kitsFile.exists()) return !kits.isEmpty();
		Configuration kitsYml = new Configuration(kitsFile);
		kitsYml.load();
		for(String key : kitsYml.getKeys()) {
			ConfigurationNode kitNode = kitsYml.getNode(key);
			List<?> itemsNode = kitNode.getList("items");
			if(itemsNode == null) {
				General.logger.warn("Kit '" + key + "' has no items and has been skipped.");
				continue;
			}
			int delay = kitNode.getInt("delay", 0);
			double cost = kitNode.getDouble("cost", 0.0);
			Kit kit = new Kit(key, delay, cost);
			for(Object id : itemsNode) {
				String itemName;
				int amount = 1;
				if(id instanceof String)
					itemName = (String) id;
				else if(id instanceof Integer)
					itemName = ((Integer) id).toString();
				else if(id instanceof Map) {
					Map<?,?> map = (Map<?,?>) id;
					if(map.size() != 1) {
						warnMalformed(key, id);
						continue;
					}
					Object[] keys = map.keySet().toArray(), values = map.values().toArray();
					if(keys[0] instanceof String) itemName = (String) keys[0];
					else if(keys[0] instanceof Integer) itemName = ((Integer) keys[0]).toString();
					else {
						warnMalformed(key, id);
						continue;
					}
					if(values[0] instanceof Integer) amount = (Integer) values[0];
					else {
						warnMalformed(key, id);
						continue;
					}
				} else {
					warnMalformed(key, id);
					continue;
				}
				ItemID item = Items.validate(itemName);
				if(!item.isValid()) {
					General.logger.warn("Kit '" + key + "' has an invalid item '" + id + "' which has been skipped.");
					continue;
				}
				kit.add(item, amount);
			}
			kits.put(key, kit);
		}
		return true;
	}

	private static void warnMalformed(String kit, Object id) {
		General.logger.warn("Kit '" + kit + "' has a malformed entry: \"" + id.toString() + "\"");
	}
	
	@Deprecated
	public static boolean loadOldKits() {
		boolean foundAnException = false;
		Exception exceptionToShow = new Exception("If you see this, something's VERY wrong.");
		try {
			File dataFolder = General.plugin.getDataFolder();
			BufferedReader br = new BufferedReader(new FileReader(new File(dataFolder, "general.kits")));
			String l;
			int lineNumber = 1;
			kits.clear();
			String list;
			String[] listing;
{'fraction_non_alphanumeric': 0.14040442579168255, 'fraction_numerical': 0.0032430370087752768, 'mean_word_length': 3.801282051282051, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5617818', 'n_tokens_mistral': 1954, 'n_tokens_neox': 1653, 'n_words': 434}