Search in sources :

Example 56 with ConfigurationSection

use of org.bukkit.configuration.ConfigurationSection in project MagicPlugin by elBukkit.

the class Mage method load.

@Override
public boolean load(MageData data) {
    try {
        if (data == null) {
            finishLoad(data);
            return true;
        }
        boundWands.clear();
        Map<String, ItemStack> boundWandItems = data.getBoundWands();
        if (boundWandItems != null) {
            for (ItemStack boundWandItem : boundWandItems.values()) {
                try {
                    Wand boundWand = controller.getWand(boundWandItem);
                    boundWands.put(boundWand.getTemplateKey(), boundWand);
                } catch (Exception ex) {
                    controller.getLogger().log(Level.WARNING, "Failed to load bound wand for " + playerName + ": " + boundWandItem, ex);
                }
            }
        }
        this.data = data.getExtraData();
        this.properties.load(data.getProperties());
        this.properties.loadProperties();
        this.classes.clear();
        Map<String, ConfigurationSection> classProperties = data.getClassProperties();
        for (Map.Entry<String, ConfigurationSection> entry : classProperties.entrySet()) {
            // ... what to do if missing templates? Don't want to lose data. Will need to think about this.
            String mageClassKey = entry.getKey();
            MageClassTemplate classTemplate = controller.getMageClass(mageClassKey);
            if (classTemplate != null) {
                MageClass newClass = new MageClass(this, classTemplate);
                newClass.load(entry.getValue());
                classes.put(mageClassKey, newClass);
            }
        }
        // Link up parents
        for (MageClass mageClass : classes.values()) {
            assignParent(mageClass);
        }
        // Load activeClass
        setActiveClass(data.getActiveClass());
        cooldownExpiration = data.getCooldownExpiration();
        fallProtectionCount = data.getFallProtectionCount();
        fallProtection = data.getFallProtectionDuration();
        if (fallProtectionCount > 0 && fallProtection > 0) {
            fallProtection = System.currentTimeMillis() + fallProtection;
        }
        gaveWelcomeWand = data.getGaveWelcomeWand();
        playerName = data.getName();
        lastDeathLocation = data.getLastDeathLocation();
        lastCast = data.getLastCast();
        destinationWarp = data.getDestinationWarp();
        if (destinationWarp != null) {
            if (!destinationWarp.isEmpty()) {
                Location destination = controller.getWarp(destinationWarp);
                if (destination != null) {
                    Plugin plugin = controller.getPlugin();
                    controller.info("Warping " + getEntity().getName() + " to " + destinationWarp + " on login");
                    TeleportTask task = new TeleportTask(getController(), getEntity(), destination, 4, true, true, null);
                    Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, task, 1);
                } else {
                    controller.info("Failed to warp " + getEntity().getName() + " to " + destinationWarp + " on login, warp doesn't exist");
                }
            }
            destinationWarp = null;
        }
        getUndoQueue().load(data.getUndoData());
        respawnInventory = data.getRespawnInventory();
        respawnArmor = data.getRespawnArmor();
        restoreOpenWand = data.isOpenWand();
        BrushData brushData = data.getBrushData();
        if (brushData != null) {
            brush.load(brushData);
        }
        if (controller.isInventoryBackupEnabled()) {
            restoreInventory = data.getStoredInventory();
            restoreLevel = data.getStoredLevel();
            restoreExperience = data.getStoredExperience();
        }
    } catch (Exception ex) {
        controller.getLogger().log(Level.WARNING, "Failed to load player data for " + playerName, ex);
        return false;
    }
    finishLoad(data);
    return true;
}
Also used : Wand(com.elmakers.mine.bukkit.wand.Wand) LostWand(com.elmakers.mine.bukkit.api.wand.LostWand) TeleportTask(com.elmakers.mine.bukkit.action.TeleportTask) 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) Location(org.bukkit.Location) CastSourceLocation(com.elmakers.mine.bukkit.api.magic.CastSourceLocation) Plugin(org.bukkit.plugin.Plugin)

Example 57 with ConfigurationSection

use of org.bukkit.configuration.ConfigurationSection 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 58 with ConfigurationSection

use of org.bukkit.configuration.ConfigurationSection in project MagicPlugin by elBukkit.

the class MageClass method getEffectiveConfiguration.

public ConfigurationSection getEffectiveConfiguration(boolean includeMage) {
    ConfigurationSection effectiveConfiguration = ConfigurationUtils.cloneConfiguration(getConfiguration());
    ConfigurationSection templateConfiguration = ConfigurationUtils.cloneConfiguration(template.getConfiguration());
    for (String key : templateConfiguration.getKeys(false)) {
        MagicPropertyType propertyRoute = propertyRoutes.get(key);
        if (propertyRoute != null && propertyRoute != type) {
            templateConfiguration.set(key, null);
        }
    }
    ConfigurationUtils.overlayConfigurations(effectiveConfiguration, templateConfiguration);
    if (parent != null) {
        ConfigurationSection parentConfiguration = parent.getEffectiveConfiguration(includeMage);
        ConfigurationUtils.overlayConfigurations(effectiveConfiguration, parentConfiguration);
    } else if (includeMage) {
        // If we have a parent, it has already incorporated Mage data
        ConfigurationSection mageConfiguration = mageProperties.getConfiguration();
        ConfigurationUtils.overlayConfigurations(effectiveConfiguration, mageConfiguration);
    }
    return effectiveConfiguration;
}
Also used : ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 59 with ConfigurationSection

use of org.bukkit.configuration.ConfigurationSection in project MagicPlugin by elBukkit.

the class MageCommandExecutor method onTabComplete.

@Override
public Collection<String> onTabComplete(CommandSender sender, String commandName, String[] args) {
    List<String> options = new ArrayList<>();
    if (args.length == 1) {
        addIfPermissible(sender, options, "Magic.commands.mage.", "add");
        addIfPermissible(sender, options, "Magic.commands.mage.", "remove");
        addIfPermissible(sender, options, "Magic.commands.mage.", "configure");
        addIfPermissible(sender, options, "Magic.commands.mage.", "describe");
        addIfPermissible(sender, options, "Magic.commands.mage.", "upgrade");
        addIfPermissible(sender, options, "Magic.commands.mage.", "getdata");
        addIfPermissible(sender, options, "Magic.commands.mage.", "setdata");
        addIfPermissible(sender, options, "Magic.commands.mage.", "check");
        addIfPermissible(sender, options, "Magic.commands.mage.", "debug");
        addIfPermissible(sender, options, "Magic.commands.mage.", "reset");
        addIfPermissible(sender, options, "Magic.commands.mage.", "unbind");
        addIfPermissible(sender, options, "Magic.commands.mage.", "activate");
        addIfPermissible(sender, options, "Magic.commands.mage.", "unlock");
        addIfPermissible(sender, options, "Magic.commands.mage.", "lock");
        addIfPermissible(sender, options, "Magic.commands.mage.", "levelspells");
        addIfPermissible(sender, options, "Magic.commands.mage.", "attribute");
    } else if (args.length == 2 && sender.hasPermission("Magic.commands.mage.others")) {
        options.addAll(api.getPlayerNames());
    }
    if (args.length == 3 || args.length == 2) {
        CommandSender target = args.length == 2 ? sender : DeprecatedUtils.getPlayer(args[1]);
        String subCommand = args[0];
        String subCommandPNode = "Magic.commands.mage." + subCommand;
        if (subCommand.equalsIgnoreCase("setdata") || subCommand.equalsIgnoreCase("getdata")) {
            if (target != null) {
                Mage mage = controller.getMage(target);
                ConfigurationSection data = mage.getData();
                options.addAll(data.getKeys(false));
            }
        }
        if (subCommand.equalsIgnoreCase("add")) {
            Collection<SpellTemplate> spellList = api.getSpellTemplates(sender.hasPermission("Magic.bypass_hidden"));
            for (SpellTemplate spell : spellList) {
                addIfPermissible(sender, options, subCommandPNode, spell.getKey(), true);
            }
            addIfPermissible(sender, options, subCommandPNode, "brush", true);
        }
        if (subCommand.equalsIgnoreCase("remove")) {
            if (target != null) {
                Mage mage = controller.getMage(target);
                MageClass mageClass = mage.getActiveClass();
                if (mageClass != null) {
                    options.addAll(mageClass.getSpells());
                }
            }
            options.add("brush");
        }
        if (subCommand.equalsIgnoreCase("configure") || subCommand.equalsIgnoreCase("describe") || subCommand.equalsIgnoreCase("upgrade")) {
            for (String key : BaseMagicProperties.PROPERTY_KEYS) {
                options.add(key);
            }
            for (String protection : api.getController().getDamageTypes()) {
                options.add("protection." + protection);
            }
        }
        if (subCommand.equalsIgnoreCase("attribute")) {
            for (String attribute : api.getController().getAttributes()) {
                options.add(attribute);
            }
        }
        if (subCommand.equalsIgnoreCase("add")) {
            Collection<SpellTemplate> spellList = api.getSpellTemplates(sender.hasPermission("Magic.bypass_hidden"));
            for (SpellTemplate spell : spellList) {
                addIfPermissible(sender, options, subCommandPNode, spell.getKey(), true);
            }
            addIfPermissible(sender, options, subCommandPNode, "brush", true);
        }
        if (args[0].equalsIgnoreCase("lock") || args[0].equalsIgnoreCase("unlock") || args[0].equalsIgnoreCase("activate")) {
            options.addAll(api.getController().getMageClassKeys());
        }
    }
    return options;
}
Also used : MageClass(com.elmakers.mine.bukkit.api.magic.MageClass) Mage(com.elmakers.mine.bukkit.api.magic.Mage) ArrayList(java.util.ArrayList) CommandSender(org.bukkit.command.CommandSender) ConfigurationSection(org.bukkit.configuration.ConfigurationSection) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate)

Example 60 with ConfigurationSection

use of org.bukkit.configuration.ConfigurationSection in project MagicPlugin by elBukkit.

the class MageCommandExecutor method onMageSetData.

public boolean onMageSetData(CommandSender sender, Player player, String[] args) {
    Mage mage = controller.getMage(player);
    if (args.length == 1) {
        ConfigurationSection data = mage.getData();
        String key = args[0];
        if (!data.contains(key)) {
            sender.sendMessage(ChatColor.RED + "No data found with key " + ChatColor.AQUA + key + ChatColor.RED + " for " + ChatColor.DARK_AQUA + player.getDisplayName());
            return true;
        }
        data.set(key, null);
        sender.sendMessage(ChatColor.GOLD + "Removed data for key " + ChatColor.AQUA + key + ChatColor.GOLD + " for " + ChatColor.DARK_AQUA + player.getDisplayName());
        return true;
    }
    if (args.length != 2) {
        return false;
    }
    if (args[0].equals("*")) {
        long value = 0;
        try {
            value = Long.parseLong(args[1]);
        } catch (Exception ex) {
            sender.sendMessage(ChatColor.RED + "Cast count must be a number");
            return true;
        }
        Collection<Spell> spells = mage.getSpells();
        for (Spell spell : spells) {
            spell.setCastCount(value);
        }
        sender.sendMessage(ChatColor.GOLD + "Set all spell cast counts to " + ChatColor.AQUA + value + ChatColor.GOLD + " for " + ChatColor.DARK_AQUA + player.getDisplayName());
        return true;
    }
    Spell spell = mage.getSpell(args[0]);
    if (spell != null) {
        long value = 0;
        try {
            value = Long.parseLong(args[1]);
        } catch (Exception ex) {
            sender.sendMessage(ChatColor.RED + "Cast count must be a number");
            return true;
        }
        spell.setCastCount(value);
        sender.sendMessage(ChatColor.GOLD + "Set " + ChatColor.AQUA + spell.getName() + ChatColor.GOLD + " cast count to " + ChatColor.AQUA + value + ChatColor.GOLD + " for " + ChatColor.DARK_AQUA + player.getDisplayName());
        return true;
    }
    ConfigurationSection data = mage.getData();
    String key = args[0];
    String value = args[1];
    data.set(key, value);
    sender.sendMessage(ChatColor.GOLD + "Set " + ChatColor.AQUA + key + ChatColor.GOLD + " to " + ChatColor.AQUA + value + ChatColor.GOLD + " for " + ChatColor.DARK_AQUA + player.getDisplayName());
    return true;
}
Also used : Mage(com.elmakers.mine.bukkit.api.magic.Mage) Spell(com.elmakers.mine.bukkit.api.spell.Spell) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Aggregations

ConfigurationSection (org.bukkit.configuration.ConfigurationSection)263 ArrayList (java.util.ArrayList)37 HashMap (java.util.HashMap)29 MemoryConfiguration (org.bukkit.configuration.MemoryConfiguration)27 ItemStack (org.bukkit.inventory.ItemStack)26 File (java.io.File)22 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)22 IOException (java.io.IOException)21 Map (java.util.Map)18 Mage (com.elmakers.mine.bukkit.api.magic.Mage)17 Material (org.bukkit.Material)16 Nullable (javax.annotation.Nullable)14 Location (org.bukkit.Location)13 FileConfiguration (org.bukkit.configuration.file.FileConfiguration)11 MaterialAndData (com.elmakers.mine.bukkit.block.MaterialAndData)10 Player (org.bukkit.entity.Player)10 EntityType (org.bukkit.entity.EntityType)9 List (java.util.List)8 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)7 InvalidConfigurationException (org.bukkit.configuration.InvalidConfigurationException)7