package org.black_ixx.bossshop.managers;

import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;

import org.black_ixx.bossshop.BossShop;
import org.black_ixx.bossshop.misc.NoEconomy;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;

public class VaultHandler{

    private BossShop mPlugin;
    private Permission perms;
    private Economy economy;
    
    public VaultHandler(BossShop pPlugin){
        this.mPlugin=pPlugin;
        Plugin VaultPlugin=Bukkit.getServer().getPluginManager().getPlugin("Vault");
        if(VaultPlugin==null){
            this.mPlugin.warn("未发现Vault插件,如果你想使用经济系统,请前往: http://dev.bukkit.org/server-mods/vault/ 下载");
            return;
        }
        this.mPlugin.info("Vault found.");
        setupEconomy();
        setupPermissions();
    }
    
    public Permission getPermission(){
        if(perms==null) this.setupPermissions();
        return perms;
    }

    public Economy getEconomy(){
        if(economy==null) this.setupEconomy();
        return economy;
    }

    private void setupEconomy(){
        RegisteredServiceProvider<Economy> economyProvider=Bukkit.getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
        if(economyProvider==null){
            this.mPlugin.warn("未发现Economy类型插件... 如果你需要用到经济系统: http://plugins.bukkit.org/.");
            economy=new NoEconomy(this.mPlugin);
            return;
        }
        economy=economyProvider.getProvider();
    }

    private void setupPermissions(){
        RegisteredServiceProvider<Permission> rsp=Bukkit.getServer().getServicesManager().getRegistration(Permission.class);
        perms=rsp.getProvider();
        if(perms==null){
            this.mPlugin.warn("未发现Permissions类型插件... 如果你需要用到权限系统: http://plugins.bukkit.org/");
            return;
        }
    }

}
