Search in sources :

Example 1 with UndoData

use of com.elmakers.mine.bukkit.api.data.UndoData in project MagicPlugin by elBukkit.

the class Mage method save.

@Override
public boolean save(MageData data) {
    if (loading)
        return false;
    try {
        data.setName(getName());
        data.setId(getId());
        data.setLastCast(lastCast);
        data.setLastDeathLocation(lastDeathLocation);
        data.setLocation(location);
        data.setDestinationWarp(destinationWarp);
        data.setCooldownExpiration(cooldownExpiration);
        long now = System.currentTimeMillis();
        if (fallProtectionCount > 0 && fallProtection > now) {
            data.setFallProtectionCount(fallProtectionCount);
            data.setFallProtectionDuration(fallProtection - now);
        } else {
            data.setFallProtectionCount(0);
            data.setFallProtectionDuration(0);
        }
        BrushData brushData = new BrushData();
        brush.save(brushData);
        data.setBrushData(brushData);
        UndoData undoData = new UndoData();
        getUndoQueue().save(undoData);
        data.setUndoData(undoData);
        data.setSpellData(this.spellData.values());
        if (boundWands.size() > 0) {
            Map<String, ItemStack> wandItems = new HashMap<>();
            for (Map.Entry<String, Wand> wandEntry : boundWands.entrySet()) {
                wandItems.put(wandEntry.getKey(), wandEntry.getValue().getItem());
            }
            data.setBoundWands(wandItems);
        }
        data.setRespawnArmor(respawnArmor);
        data.setRespawnInventory(respawnInventory);
        data.setOpenWand(false);
        if (activeWand != null) {
            if (activeWand.hasStoredInventory()) {
                data.setStoredInventory(Arrays.asList(activeWand.getStoredInventory().getContents()));
            }
            if (activeWand.isInventoryOpen()) {
                data.setOpenWand(true);
            }
        }
        data.setGaveWelcomeWand(gaveWelcomeWand);
        data.setExtraData(this.data);
        data.setProperties(properties.getConfiguration());
        Map<String, ConfigurationSection> classProperties = new HashMap<>();
        for (Map.Entry<String, MageClass> entry : classes.entrySet()) {
            classProperties.put(entry.getKey(), entry.getValue().getConfiguration());
        }
        data.setClassProperties(classProperties);
        String activeClassKey = activeClass == null ? null : activeClass.getTemplate().getKey();
        data.setActiveClass(activeClassKey);
    } catch (Exception ex) {
        controller.getPlugin().getLogger().log(Level.WARNING, "Failed to save player data for " + playerName, ex);
        return false;
    }
    return true;
}
Also used : HashMap(java.util.HashMap) Wand(com.elmakers.mine.bukkit.wand.Wand) LostWand(com.elmakers.mine.bukkit.api.wand.LostWand) UndoData(com.elmakers.mine.bukkit.api.data.UndoData) ItemStack(org.bukkit.inventory.ItemStack) Map(java.util.Map) HashMap(java.util.HashMap) BrushData(com.elmakers.mine.bukkit.api.data.BrushData) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 2 with UndoData

use of com.elmakers.mine.bukkit.api.data.UndoData in project MagicPlugin by elBukkit.

the class ConfigurationMageDataStore method save.

public static void save(MageController controller, MageData mage, ConfigurationSection saveFile) {
    saveFile.set("id", mage.getId());
    saveFile.set("name", mage.getName());
    saveFile.set("last_cast", mage.getLastCast());
    saveFile.set("cooldown_expiration", mage.getCooldownExpiration());
    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) {
            controller.getLogger().warning("Failed to save brush data: " + ex.getMessage());
            ex.printStackTrace();
        }
    }
    UndoData undoData = mage.getUndoData();
    if (undoData != null) {
        List<Map<String, Object>> nodeList = new ArrayList<>();
        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");
    Collection<SpellData> spellData = mage.getSpellData();
    if (spellData != null) {
        for (SpellData spell : spellData) {
            ConfigurationSection node = spellNode.createSection(spell.getKey().getKey());
            node.set("cast_count", spell.getCastCount());
            node.set("last_cast", spell.getLastCast());
            node.set("last_earn", spell.getLastEarn());
            node.set("cooldown_expiration", spell.getCooldownExpiration());
            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());
        }
    }
    Map<Integer, ItemStack> respawnArmor = mage.getRespawnArmor();
    if (respawnArmor != null) {
        ConfigurationSection armorSection = saveFile.createSection("respawn_armor");
        for (Map.Entry<Integer, ItemStack> entry : respawnArmor.entrySet()) {
            controller.serialize(armorSection, Integer.toString(entry.getKey()), entry.getValue());
        }
    }
    Map<Integer, ItemStack> respawnInventory = mage.getRespawnInventory();
    if (respawnInventory != null) {
        ConfigurationSection inventorySection = saveFile.createSection("respawn_inventory");
        for (Map.Entry<Integer, ItemStack> entry : respawnInventory.entrySet()) {
            controller.serialize(inventorySection, Integer.toString(entry.getKey()), entry.getValue());
        }
    }
    List<ItemStack> storedInventory = mage.getStoredInventory();
    if (storedInventory != null) {
        saveFile.set("inventory", storedInventory);
    }
    saveFile.set("experience", mage.getStoredExperience());
    saveFile.set("level", mage.getStoredLevel());
    saveFile.set("open_wand", mage.isOpenWand());
    saveFile.set("gave_welcome_wand", mage.getGaveWelcomeWand());
    ConfigurationSection extraData = mage.getExtraData();
    if (extraData != null) {
        ConfigurationSection dataSection = saveFile.createSection("data");
        ConfigurationUtils.addConfigurations(dataSection, extraData);
    }
    ConfigurationSection properties = mage.getProperties();
    if (properties != null) {
        ConfigurationSection propertiesSection = saveFile.createSection("properties");
        ConfigurationUtils.addConfigurations(propertiesSection, properties);
    }
    Map<String, ConfigurationSection> classProperties = mage.getClassProperties();
    if (classProperties != null) {
        ConfigurationSection classesSection = saveFile.createSection("classes");
        for (Map.Entry<String, ConfigurationSection> entry : classProperties.entrySet()) {
            ConfigurationSection classSection = classesSection.createSection(entry.getKey());
            ConfigurationUtils.addConfigurations(classSection, entry.getValue());
        }
    }
    saveFile.set("active_class", mage.getActiveClass());
}
Also used : ArrayList(java.util.ArrayList) MemoryConfiguration(org.bukkit.configuration.MemoryConfiguration) UndoList(com.elmakers.mine.bukkit.api.block.UndoList) SpellData(com.elmakers.mine.bukkit.api.data.SpellData) UndoData(com.elmakers.mine.bukkit.api.data.UndoData) ItemStack(org.bukkit.inventory.ItemStack) HashMap(java.util.HashMap) Map(java.util.Map) BrushData(com.elmakers.mine.bukkit.api.data.BrushData) Location(org.bukkit.Location) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 3 with UndoData

use of com.elmakers.mine.bukkit.api.data.UndoData in project MagicPlugin by elBukkit.

the class ConfigurationMageDataStore method load.

public static MageData load(MageController controller, String id, ConfigurationSection saveFile) {
    MageData data = new MageData(id);
    // Load brush data
    ConfigurationSection brushConfig = saveFile.getConfigurationSection("brush");
    if (brushConfig != null) {
        BrushData brushData = new BrushData();
        try {
            brushData.setCloneLocation(ConfigurationUtils.getLocation(brushConfig, "clone_location"));
            brushData.setCloneTarget(ConfigurationUtils.getLocation(brushConfig, "clone_target"));
            brushData.setMaterialTarget(ConfigurationUtils.getLocation(brushConfig, "material_target"));
            brushData.setSchematicName(brushConfig.getString("schematic", ""));
            brushData.setMapId((short) brushConfig.getInt("map_id", -1));
            brushData.setMaterial(ConfigurationUtils.getMaterial(brushConfig, "material", Material.AIR));
            brushData.setMaterialData((short) brushConfig.getInt("data", 0));
            brushData.setScale(brushConfig.getDouble("scale", 1));
            brushData.setFillWithAir(brushConfig.getBoolean("erase", true));
            data.setBrushData(brushData);
        } catch (Exception ex) {
            controller.getLogger().warning("Failed to load brush data: " + ex.getMessage());
            ex.printStackTrace();
        }
    }
    // Load bound wand data
    if (saveFile.contains("wands")) {
        HashMap<String, ItemStack> boundWands = new HashMap<>();
        ConfigurationSection wands = saveFile.getConfigurationSection("wands");
        Set<String> keys = wands.getKeys(false);
        for (String key : keys) {
            ItemStack boundWand = controller.deserialize(wands, key);
            if (boundWand == null) {
                controller.getLogger().warning("Error loading bound wand: " + key);
            } else {
                boundWands.put(key, boundWand);
            }
        }
        data.setBoundWands(boundWands);
    }
    // Load properties
    data.setProperties(saveFile.getConfigurationSection("properties"));
    // Load classes
    Map<String, ConfigurationSection> classProperties = new HashMap<>();
    ConfigurationSection classes = saveFile.getConfigurationSection("classes");
    if (classes != null) {
        Set<String> classKeys = classes.getKeys(false);
        for (String classKey : classKeys) {
            classProperties.put(classKey, classes.getConfigurationSection(classKey));
        }
    }
    data.setClassProperties(classProperties);
    data.setActiveClass(saveFile.getString("active_class"));
    // Load extra data
    data.setExtraData(saveFile.getConfigurationSection("data"));
    // Fall protection data
    data.setFallProtectionCount(saveFile.getLong("fall_protection_count", 0));
    data.setFallProtectionDuration(saveFile.getLong("fall_protection", 0));
    // Random data and mage properties
    data.setName(saveFile.getString("name", ""));
    data.setLastDeathLocation(ConfigurationUtils.getLocation(saveFile, "last_death_location"));
    data.setLocation(ConfigurationUtils.getLocation(saveFile, "location"));
    data.setLastCast(saveFile.getLong("last_cast", 0));
    data.setCooldownExpiration(saveFile.getLong("cooldown_expiration", 0));
    data.setDestinationWarp(saveFile.getString("destination_warp"));
    // Load undo queue
    UndoData undoData = new UndoData();
    Collection<ConfigurationSection> nodeList = ConfigurationUtils.getNodeList(saveFile, "undo");
    if (nodeList != null) {
        for (ConfigurationSection listNode : nodeList) {
            // The owner will get set by UndoQueue.load
            // This is .. kind of hacky, but allows us to use UndoList as a data
            // storage mechanism instead of making a separate DAO for it right now.
            UndoList list = new com.elmakers.mine.bukkit.block.UndoList(null);
            list.load(listNode);
            undoData.getBlockList().add(list);
        }
    }
    data.setUndoData(undoData);
    // Load spell data
    ConfigurationSection spellSection = saveFile.getConfigurationSection("spells");
    if (spellSection != null) {
        Set<String> keys = spellSection.getKeys(false);
        Map<String, SpellData> spellDataMap = new HashMap<>();
        for (String key : keys) {
            ConfigurationSection node = spellSection.getConfigurationSection(key);
            SpellData spellData = spellDataMap.get(key);
            if (spellData == null) {
                spellData = new SpellData(key);
                spellDataMap.put(key, spellData);
            }
            spellData.setCastCount(spellData.getCastCount() + node.getLong("cast_count", 0));
            spellData.setLastCast(Math.max(spellData.getLastCast(), node.getLong("last_cast", 0)));
            spellData.setLastEarn(Math.max(spellData.getLastEarn(), node.getLong("last_earn", 0)));
            spellData.setCooldownExpiration(Math.max(spellData.getCooldownExpiration(), node.getLong("cooldown_expiration", 0)));
            node.set("cast_count", null);
            node.set("last_cast", null);
            node.set("last_earn", null);
            node.set("cooldown_expiration", null);
            spellData.setExtraData(node);
        }
        data.setSpellData(spellDataMap.values());
    }
    // Load respawn inventory
    ConfigurationSection respawnData = saveFile.getConfigurationSection("respawn_inventory");
    if (respawnData != null) {
        Collection<String> keys = respawnData.getKeys(false);
        Map<Integer, ItemStack> respawnInventory = new HashMap<>();
        for (String key : keys) {
            try {
                int index = Integer.parseInt(key);
                ItemStack item = controller.deserialize(respawnData, key);
                respawnInventory.put(index, item);
            } catch (Exception ex) {
                controller.getLogger().log(Level.WARNING, "Error loading respawn inventory for " + id, ex);
            }
        }
        data.setRespawnInventory(respawnInventory);
    }
    // Load respawn armor
    ConfigurationSection respawnArmorData = saveFile.getConfigurationSection("respawn_armor");
    if (respawnArmorData != null) {
        Collection<String> keys = respawnArmorData.getKeys(false);
        Map<Integer, ItemStack> respawnArmor = new HashMap<>();
        for (String key : keys) {
            try {
                int index = Integer.parseInt(key);
                ItemStack item = controller.deserialize(respawnArmorData, key);
                respawnArmor.put(index, item);
            } catch (Exception ex) {
                controller.getLogger().log(Level.WARNING, "Error loading respawn armor inventory for " + id, ex);
            }
        }
        data.setRespawnArmor(respawnArmor);
    }
    // Load brush data
    if (saveFile.contains("brush")) {
        try {
            ConfigurationSection node = saveFile.getConfigurationSection("brush");
            BrushData brushData = new BrushData();
            brushData.setCloneLocation(ConfigurationUtils.getLocation(node, "clone_location"));
            brushData.setCloneTarget(ConfigurationUtils.getLocation(node, "clone_target"));
            brushData.setMaterialTarget(ConfigurationUtils.getLocation(node, "material_target"));
            brushData.setSchematicName(node.getString("schematic"));
            brushData.setMapId((short) node.getInt("map_id"));
            brushData.setMaterial(ConfigurationUtils.getMaterial(node, "material"));
            brushData.setMaterialData((short) node.getInt("data"));
            brushData.setScale(node.getDouble("scale"));
            brushData.setFillWithAir(node.getBoolean("erase"));
            data.setBrushData(brushData);
        } catch (Exception ex) {
            ex.printStackTrace();
            controller.getLogger().warning("Failed to load brush data: " + ex.getMessage());
        }
    }
    // Load stored inventory
    if (saveFile.contains("inventory")) {
        @SuppressWarnings("unchecked") List<ItemStack> inventory = (List<ItemStack>) saveFile.getList("inventory");
        data.setStoredInventory(inventory);
    }
    if (saveFile.contains("experience")) {
        data.setStoredExperience((float) saveFile.getDouble("experience"));
    }
    if (saveFile.contains("level")) {
        data.setStoredLevel(saveFile.getInt("level"));
    }
    data.setOpenWand(saveFile.getBoolean("open_wand", false));
    data.setGaveWelcomeWand(saveFile.getBoolean("gave_welcome_wand", false));
    return data;
}
Also used : HashMap(java.util.HashMap) UndoList(com.elmakers.mine.bukkit.api.block.UndoList) SpellData(com.elmakers.mine.bukkit.api.data.SpellData) MageData(com.elmakers.mine.bukkit.api.data.MageData) UndoList(com.elmakers.mine.bukkit.api.block.UndoList) ArrayList(java.util.ArrayList) List(java.util.List) ItemStack(org.bukkit.inventory.ItemStack) UndoData(com.elmakers.mine.bukkit.api.data.UndoData) BrushData(com.elmakers.mine.bukkit.api.data.BrushData) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Aggregations

BrushData (com.elmakers.mine.bukkit.api.data.BrushData)3 UndoData (com.elmakers.mine.bukkit.api.data.UndoData)3 HashMap (java.util.HashMap)3 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)3 ItemStack (org.bukkit.inventory.ItemStack)3 UndoList (com.elmakers.mine.bukkit.api.block.UndoList)2 SpellData (com.elmakers.mine.bukkit.api.data.SpellData)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 MageData (com.elmakers.mine.bukkit.api.data.MageData)1 LostWand (com.elmakers.mine.bukkit.api.wand.LostWand)1 Wand (com.elmakers.mine.bukkit.wand.Wand)1 List (java.util.List)1 Location (org.bukkit.Location)1 MemoryConfiguration (org.bukkit.configuration.MemoryConfiguration)1