Search in sources :

Example 1 with MageData

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

the class MagicSaveCommandExecutor method onCommand.

@Override
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) {
    Player player;
    if (args.length < 1) {
        return false;
    }
    if (sender instanceof Player) {
        player = (Player) sender;
        if (!player.hasPermission("Magic.commands.msave")) {
            return false;
        }
    }
    player = DeprecatedUtils.getPlayer(args[0]);
    if (player == null) {
        return false;
    }
    String executeCommand = "";
    for (int i = 1; i < args.length; i++) {
        executeCommand = executeCommand + args[i] + " ";
    }
    MagicController controller = (MagicController) api.getController();
    Mage mage = controller.getMage(player);
    final String cmd = executeCommand.trim().replace("@p", mage.getName());
    final Plugin plugin = controller.getPlugin();
    controller.saveMage(mage, true, new MageDataCallback() {

        @Override
        public void run(MageData data) {
            if (cmd.length() > 0) {
                plugin.getServer().dispatchCommand(sender, cmd);
            }
        }
    });
    return true;
}
Also used : Player(org.bukkit.entity.Player) MageDataCallback(com.elmakers.mine.bukkit.api.data.MageDataCallback) MageData(com.elmakers.mine.bukkit.api.data.MageData) Mage(com.elmakers.mine.bukkit.api.magic.Mage) MagicController(com.elmakers.mine.bukkit.magic.MagicController) Plugin(org.bukkit.plugin.Plugin)

Example 2 with MageData

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

the class MagicController method save.

public void save(boolean asynchronous) {
    if (!initialized)
        return;
    maps.save(asynchronous);
    final List<YamlDataFile> saveData = new ArrayList<>();
    final List<MageData> saveMages = new ArrayList<>();
    if (savePlayerData && mageDataStore != null) {
        savePlayerData(saveMages);
    }
    info("Saving " + saveMages.size() + " players");
    saveSpellData(saveData);
    saveLostWands(saveData);
    if (mageDataStore != null) {
        if (asynchronous) {
            Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {

                @Override
                public void run() {
                    synchronized (saveLock) {
                        for (MageData mageData : saveMages) {
                            mageDataStore.save(mageData, null);
                        }
                        for (YamlDataFile config : saveData) {
                            config.save();
                        }
                        info("Finished saving");
                    }
                }
            });
        } else {
            synchronized (saveLock) {
                for (MageData mageData : saveMages) {
                    mageDataStore.save(mageData, null);
                }
                for (YamlDataFile config : saveData) {
                    config.save();
                }
                info("Finished saving");
            }
        }
    }
    SaveEvent saveEvent = new SaveEvent(asynchronous);
    Bukkit.getPluginManager().callEvent(saveEvent);
}
Also used : SaveEvent(com.elmakers.mine.bukkit.api.event.SaveEvent) MageData(com.elmakers.mine.bukkit.api.data.MageData) YamlDataFile(com.elmakers.mine.bukkit.data.YamlDataFile) ArrayList(java.util.ArrayList)

Example 3 with MageData

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

the class MagicController method sendPlayerToServer.

@Override
public void sendPlayerToServer(final Player player, final String server) {
    MageDataCallback callback = new MageDataCallback() {

        @Override
        public void run(MageData data) {
            Bukkit.getScheduler().runTaskLater(plugin, new ChangeServerTask(plugin, player, server), 1);
        }
    };
    info("Moving " + player.getName() + " to server " + server, 1);
    Mage mage = getRegisteredMage(player);
    if (mage != null) {
        playerQuit(mage, callback);
    } else {
        callback.run(null);
    }
}
Also used : MageDataCallback(com.elmakers.mine.bukkit.api.data.MageDataCallback) MageData(com.elmakers.mine.bukkit.api.data.MageData) Mage(com.elmakers.mine.bukkit.api.magic.Mage)

Example 4 with MageData

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

the class MagicController method saveMage.

public void saveMage(Mage mage, boolean asynchronous, final MageDataCallback callback, boolean wandInventoryOpen) {
    if (!savePlayerData) {
        if (callback != null) {
            callback.run(null);
        }
        return;
    }
    info("Saving player data for " + mage.getName() + " (" + mage.getId() + ") " + (asynchronous ? "" : " synchronously"));
    final MageData mageData = new MageData(mage.getId());
    if (mageDataStore != null && mage.save(mageData)) {
        if (wandInventoryOpen) {
            mageData.setOpenWand(true);
        }
        if (asynchronous) {
            Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {

                @Override
                public void run() {
                    synchronized (saveLock) {
                        try {
                            mageDataStore.save(mageData, callback);
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            });
        } else {
            synchronized (saveLock) {
                try {
                    mageDataStore.save(mageData, callback);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}
Also used : MageData(com.elmakers.mine.bukkit.api.data.MageData) InvalidConfigurationException(org.bukkit.configuration.InvalidConfigurationException) IOException(java.io.IOException) ParseException(java.text.ParseException)

Example 5 with MageData

use of com.elmakers.mine.bukkit.api.data.MageData 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

MageData (com.elmakers.mine.bukkit.api.data.MageData)6 MageDataCallback (com.elmakers.mine.bukkit.api.data.MageDataCallback)2 Mage (com.elmakers.mine.bukkit.api.magic.Mage)2 ArrayList (java.util.ArrayList)2 UndoList (com.elmakers.mine.bukkit.api.block.UndoList)1 BrushData (com.elmakers.mine.bukkit.api.data.BrushData)1 SpellData (com.elmakers.mine.bukkit.api.data.SpellData)1 UndoData (com.elmakers.mine.bukkit.api.data.UndoData)1 SaveEvent (com.elmakers.mine.bukkit.api.event.SaveEvent)1 YamlDataFile (com.elmakers.mine.bukkit.data.YamlDataFile)1 MagicController (com.elmakers.mine.bukkit.magic.MagicController)1 File (java.io.File)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)1 InvalidConfigurationException (org.bukkit.configuration.InvalidConfigurationException)1 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)1 Player (org.bukkit.entity.Player)1