Search in sources :

Example 1 with WandTemplate

use of com.elmakers.mine.bukkit.api.wand.WandTemplate in project MagicPlugin by elBukkit.

the class WandCommandExecutor method onWandDelete.

public boolean onWandDelete(CommandSender sender, String wandKey) {
    MageController controller = api.getController();
    WandTemplate existing = controller.getWandTemplate(wandKey);
    if (existing == null) {
        sender.sendMessage(ChatColor.RED + "Unknown wand: " + wandKey);
        return true;
    }
    boolean hasPermission = true;
    if (sender instanceof Player) {
        Player player = (Player) sender;
        if (!player.hasPermission("Magic.wand.overwrite")) {
            if (player.hasPermission("Magic.wand.overwrite_own")) {
                String creatorId = existing.getCreatorId();
                hasPermission = creatorId != null && creatorId.equalsIgnoreCase(player.getUniqueId().toString());
            } else {
                hasPermission = false;
            }
        }
    }
    if (!hasPermission) {
        sender.sendMessage(ChatColor.RED + "You don't have permission to delete " + wandKey);
        return true;
    }
    File wandFolder = new File(controller.getConfigFolder(), "wands");
    File wandFile = new File(wandFolder, wandKey + ".yml");
    if (!wandFile.exists()) {
        sender.sendMessage(ChatColor.RED + "File doesn't exist: " + wandFile.getName());
        return true;
    }
    wandFile.delete();
    controller.unloadWandTemplate(wandKey);
    sender.sendMessage("Deleted wand " + wandKey);
    return true;
}
Also used : MageController(com.elmakers.mine.bukkit.api.magic.MageController) Player(org.bukkit.entity.Player) WandTemplate(com.elmakers.mine.bukkit.api.wand.WandTemplate) File(java.io.File)

Example 2 with WandTemplate

use of com.elmakers.mine.bukkit.api.wand.WandTemplate in project MagicPlugin by elBukkit.

the class WandCommandExecutor method onWandList.

public boolean onWandList(CommandSender sender) {
    Collection<WandTemplate> templates = api.getController().getWandTemplates();
    Map<String, ConfigurationSection> nameMap = new TreeMap<>();
    for (WandTemplate template : templates) {
        nameMap.put(template.getKey(), template.getConfiguration());
    }
    for (Map.Entry<String, ConfigurationSection> templateEntry : nameMap.entrySet()) {
        ConfigurationSection templateConfig = templateEntry.getValue();
        if (templateConfig.getBoolean("hidden", false))
            continue;
        String key = templateEntry.getKey();
        String name = api.getMessages().get("wands." + key + ".name", api.getMessages().get("wand.default_name"));
        String description = api.getMessages().get("wands." + key + ".description", "");
        description = ChatColor.YELLOW + description;
        if (!name.equals(key)) {
            description = ChatColor.BLUE + name + ChatColor.WHITE + " : " + description;
        }
        sender.sendMessage(ChatColor.AQUA + key + ChatColor.WHITE + " : " + description);
    }
    return true;
}
Also used : WandTemplate(com.elmakers.mine.bukkit.api.wand.WandTemplate) TreeMap(java.util.TreeMap) Map(java.util.Map) TreeMap(java.util.TreeMap) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 3 with WandTemplate

use of com.elmakers.mine.bukkit.api.wand.WandTemplate in project MagicPlugin by elBukkit.

the class WandCommandExecutor method onWandSave.

public boolean onWandSave(CommandSender sender, Player player, String[] parameters) {
    if (parameters.length < 1) {
        sender.sendMessage("Use: /wand save <filename>");
        return true;
    }
    Wand wand = checkWand(sender, player);
    if (wand == null) {
        return true;
    }
    MageController controller = api.getController();
    String template = parameters[0];
    WandTemplate existing = controller.getWandTemplate(template);
    if (existing != null && !player.hasPermission("Magic.wand.overwrite")) {
        String creatorId = existing.getCreatorId();
        boolean isCreator = creatorId != null && creatorId.equalsIgnoreCase(player.getUniqueId().toString());
        if (!player.hasPermission("Magic.wand.overwrite_own") || !isCreator) {
            sender.sendMessage(ChatColor.RED + "The " + template + " wand already exists and you don't have permission to overwrite it.");
            return true;
        }
    }
    String inheritTemplate = wand.getTemplateKey();
    YamlConfiguration wandConfig = new YamlConfiguration();
    ConfigurationSection wandSection = wandConfig.createSection(template);
    wand.save(wandSection, true);
    wandSection.set("creator_id", player.getUniqueId().toString());
    wandSection.set("creator", player.getName());
    // inheriting from itself.
    if (inheritTemplate != null && inheritTemplate.equals(template)) {
        String oldTemplate = null;
        if (existing != null) {
            // This gives us the collapsed configuration, including inherited values.
            // We just want the ones changed by the template we are replacing, though.
            ConfigurationSection templateConfig = existing.getConfiguration();
            WandTemplate parent = existing.getParent();
            if (parent != null) {
                oldTemplate = parent.getKey();
                ConfigurationSection parentConfig = parent.getConfiguration();
                templateConfig = ConfigurationUtils.subtractConfiguration(templateConfig, parentConfig);
            }
            ConfigurationUtils.addConfigurations(wandSection, templateConfig, false);
        }
        wandSection.set("inherit", oldTemplate);
    }
    File wandFolder = new File(controller.getConfigFolder(), "wands");
    File wandFile = new File(wandFolder, template + ".yml");
    wandFolder.mkdirs();
    try {
        wandConfig.save(wandFile);
    } catch (IOException ex) {
        ex.printStackTrace();
        sender.sendMessage(ChatColor.RED + "Can't write to file " + wandFile.getName());
        return true;
    }
    String inherit = wandSection.getString("inherit", "");
    if (!inherit.isEmpty()) {
        WandTemplate inheritConfiguration = controller.getWandTemplate(inherit);
        ConfigurationUtils.addConfigurations(wandSection, inheritConfiguration.getConfiguration(), false);
    }
    controller.loadWandTemplate(template, wandSection);
    String message = "Wand saved as " + template;
    if (existing != null) {
        message = message + ChatColor.GOLD + " (Replaced Existing)";
    }
    sender.sendMessage(message);
    return true;
}
Also used : MageController(com.elmakers.mine.bukkit.api.magic.MageController) Wand(com.elmakers.mine.bukkit.api.wand.Wand) IOException(java.io.IOException) WandTemplate(com.elmakers.mine.bukkit.api.wand.WandTemplate) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) File(java.io.File) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Aggregations

WandTemplate (com.elmakers.mine.bukkit.api.wand.WandTemplate)3 MageController (com.elmakers.mine.bukkit.api.magic.MageController)2 File (java.io.File)2 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)2 Wand (com.elmakers.mine.bukkit.api.wand.Wand)1 IOException (java.io.IOException)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)1 Player (org.bukkit.entity.Player)1