<commit_before>package com.elmakers.mine.bukkit.data;

import com.elmakers.mine.bukkit.api.block.UndoList;
import com.elmakers.mine.bukkit.api.data.BrushData;
import com.elmakers.mine.bukkit.api.data.MageData;
import com.elmakers.mine.bukkit.api.data.MageDataStore;
import com.elmakers.mine.bukkit.api.data.SpellData;
import com.elmakers.mine.bukkit.api.data.UndoData;
import com.elmakers.mine.bukkit.api.magic.MageController;
import com.elmakers.mine.bukkit.utilities.YamlDataFile;
import com.elmakers.mine.bukkit.utility.ConfigurationUtils;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemoryConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

public class YamlMageDataStore implements MageDataStore {
    private File playerDataFolder;
    private Logger logger;
    private MageController controller;

    @Override
    public void initialize(MageController controller, ConfigurationSection configuration) {
        this.controller = controller;
        Plugin plugin = controller.getPlugin();
        logger = plugin.getLogger();
        String playerFolder = configuration.getString("folder", "players");
        playerDataFolder = new File(plugin.getDataFolder(), playerFolder);
        playerDataFolder.mkdirs();
    }

    @Override
    public void save(MageData mage) {
        File playerData = new File(playerDataFolder, mage.getId() + ".dat");
        YamlDataFile saveFile = new YamlDataFile(controller.getLogger(), playerData);
        saveFile.set("id", mage.getId());
        saveFile.set("name", mage.getName());
        saveFile.set("last_cast", mage.getLastCast());
        saveFile.set("last_death_location", ConfigurationUtils.fromLocation(mage.getLastDeathLocation()));
        Location location = mage.getLocation();
        if (location != null) {
            saveFile.set("location", ConfigurationUtils.fromLocation(location));
        }
        saveFile.set("destination_warp", mage.getDestinationWarp());
        saveFile.set("fall_protection_count", mage.getFallProtectionCount());
        saveFile.set("fall_protection", mage.getFallProtectionDuration());

        BrushData brush = mage.getBrushData();
        if (brush != null) {
            ConfigurationSection brushNode = saveFile.createSection("brush");
            try {
                Location cloneSource = brush.getCloneLocation();
                if (cloneSource != null) {
                    brushNode.set("clone_location", ConfigurationUtils.fromLocation(cloneSource));
                }
                Location cloneTarget = brush.getCloneTarget();
                if (cloneTarget != null) {
                    brushNode.set("clone_target", ConfigurationUtils.fromLocation(cloneTarget));
                }
                Location materialTarget = brush.getMaterialTarget();
                if (materialTarget != null) {
                    brushNode.set("material_target", ConfigurationUtils.fromLocation(materialTarget));
                }
                brushNode.set("map_id", brush.getMapId());
                brushNode.set("material", ConfigurationUtils.fromMaterial(brush.getMaterial()));
                brushNode.set("data", brush.getMaterialData());
                brushNode.set("schematic", brush.getSchematicName());
                brushNode.set("scale", brush.getScale());
                brushNode.set("erase", brush.isFillWithAir());
            } catch (Exception ex) {
                logger.warning("Failed to save brush data: " + ex.getMessage());
                ex.printStackTrace();
            }
        }

        UndoData undoData = mage.getUndoData();
        if (undoData != null) {
            List<Map<String, Object>> nodeList = new ArrayList<Map<String, Object>>();
            List<UndoList> undoList = undoData.getBlockList();
            for (UndoList list : undoList) {
                MemoryConfiguration listNode = new MemoryConfiguration();
                list.save(listNode);
                nodeList.add(listNode.getValues(true));
            }
            saveFile.set("undo", nodeList);
        }
        ConfigurationSection spellNode = saveFile.createSection("spells");
        for (SpellData spell : mage.getSpellData()) {
            ConfigurationSection node = spellNode.createSection(spell.getKey());
            node.set("cast_count", spell.getCastCount());
            node.set("last_cast", spell.getLastCast());
            node.set("active", spell.isActive() ? true : null);
            ConfigurationSection extra = spell.getExtraData();
            if (extra != null) {
                ConfigurationUtils.addConfigurations(node, extra);
            }
        }

        Map<String, ItemStack> boundWands = mage.getBoundWands();
        if (boundWands != null && boundWands.size() > 0) {
            ConfigurationSection wandSection = saveFile.createSection("wands");
            for (Map.Entry<String, ItemStack> wandEntry : boundWands.entrySet()) {
                controller.serialize(wandSection,  wandEntry.getKey(), wandEntry.getValue());
            }<commit_msg>Don't serialize wands with invalid keys<commit_after>package com.elmakers.mine.bukkit.data;

import com.elmakers.mine.bukkit.api.block.UndoList;
import com.elmakers.mine.bukkit.api.data.BrushData;
import com.elmakers.mine.bukkit.api.data.MageData;
import com.elmakers.mine.bukkit.api.data.MageDataStore;
import com.elmakers.mine.bukkit.api.data.SpellData;
import com.elmakers.mine.bukkit.api.data.UndoData;
import com.elmakers.mine.bukkit.api.magic.MageController;
import com.elmakers.mine.bukkit.utilities.YamlDataFile;
import com.elmakers.mine.bukkit.utility.ConfigurationUtils;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemoryConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

public class YamlMageDataStore implements MageDataStore {
    private File playerDataFolder;
    private Logger logger;
    private MageController controller;

    @Override
    public void initialize(MageController controller, ConfigurationSection configuration) {
        this.controller = controller;
        Plugin plugin = controller.getPlugin();
        logger = plugin.getLogger();
        String playerFolder = configuration.getString("folder", "players");
        playerDataFolder = new File(plugin.getDataFolder(), playerFolder);
        playerDataFolder.mkdirs();
    }

    @Override
    public void save(MageData mage) {
        File playerData = new File(playerDataFolder, mage.getId() + ".dat");
        YamlDataFile saveFile = new YamlDataFile(controller.getLogger(), playerData);
        saveFile.set("id", mage.getId());
        saveFile.set("name", mage.getName());
        saveFile.set("last_cast", mage.getLastCast());
        saveFile.set("last_death_location", ConfigurationUtils.fromLocation(mage.getLastDeathLocation()));
        Location location = mage.getLocation();
        if (location != null) {
            saveFile.set("location", ConfigurationUtils.fromLocation(location));
        }
        saveFile.set("destination_warp", mage.getDestinationWarp());
        saveFile.set("fall_protection_count", mage.getFallProtectionCount());
        saveFile.set("fall_protection", mage.getFallProtectionDuration());

        BrushData brush = mage.getBrushData();
        if (brush != null) {
            ConfigurationSection brushNode = saveFile.createSection("brush");
            try {
                Location cloneSource = brush.getCloneLocation();
                if (cloneSource != null) {
                    brushNode.set("clone_location", ConfigurationUtils.fromLocation(cloneSource));
                }
                Location cloneTarget = brush.getCloneTarget();
                if (cloneTarget != null) {
                    brushNode.set("clone_target", ConfigurationUtils.fromLocation(cloneTarget));
                }
                Location materialTarget = brush.getMaterialTarget();
                if (materialTarget != null) {
                    brushNode.set("material_target", ConfigurationUtils.fromLocation(materialTarget));
                }
                brushNode.set("map_id", brush.getMapId());
                brushNode.set("material", ConfigurationUtils.fromMaterial(brush.getMaterial()));
                brushNode.set("data", brush.getMaterialData());
                brushNode.set("schematic", brush.getSchematicName());
                brushNode.set("scale", brush.getScale());
                brushNode.set("erase", brush.isFillWithAir());
            } catch (Exception ex) {
                logger.warning("Failed to save brush data: " + ex.getMessage());
                ex.printStackTrace();
            }
        }

        UndoData undoData = mage.getUndoData();
        if (undoData != null) {
            List<Map<String, Object>> nodeList = new ArrayList<Map<String, Object>>();
            List<UndoList> undoList = undoData.getBlockList();
            for (UndoList list : undoList) {
                MemoryConfiguration listNode = new MemoryConfiguration();
                list.save(listNode);
                nodeList.add(listNode.getValues(true));
            }
            saveFile.set("undo", nodeList);
        }
        ConfigurationSection spellNode = saveFile.createSection("spells");
        for (SpellData spell : mage.getSpellData()) {
            ConfigurationSection node = spellNode.createSection(spell.getKey());
            node.set("cast_count", spell.getCastCount());
            node.set("last_cast", spell.getLastCast());
            node.set("active", spell.isActive() ? true : null);
            ConfigurationSection extra = spell.getExtraData();
            if (extra != null) {
                ConfigurationUtils.addConfigurations(node, extra);
            }
        }

        Map<String, ItemStack> boundWands = mage.getBoundWands();
        if (boundWands != null && boundWands.size() > 0) {
            ConfigurationSection wandSection = saveFile.createSection("wands");
            for (Map.Entry<String, ItemStack> wandEntry : boundWands.entrySet()) {
                String key = wandEntry.getKey();
                if (key == null || key.isEmpty()) continue;
                controller.serialize(wandSection, key, wandEntry.getValue());
            }
{'fraction_non_alphanumeric': 0.11597773519481705, 'fraction_numerical': 0.00018249840313897252, 'mean_word_length': 2.793700242298373, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, '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': '5563664', 'n_tokens_mistral': 2968, 'n_tokens_neox': 2862, 'n_words': 521}