Having trouble with softdepend and Vault.

Discussion in 'Plugin Development' started by Colby l, Jul 5, 2012.

  1. Offline

    Colby l

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    So I want to make it so my plugin, Fly Payment, supports an Economy paying system, (so you pay in economy currency instead of items) but when I try and make Vault a softdepend, it comes with errors when I have EconomyEnabled: true in the config, and Vault isn't present.

    Here's the code for the Main Class (I commented were the error/confusion is)
    package com.ahellhound.bukkit.flypayment;

    import java.util.HashMap;
    import java.util.Map;
    import java.util.logging.Logger;

    import net.milkbowl.vault.Vault;
    import net.milkbowl.vault.economy.Economy;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerKickEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements CommandExecutor,Listener {
    Logger log = Logger.getLogger("Minecraft");

    private Integer[] stac = new Integer[5];
    private Integer[] amnt = new Integer[5];
    private Long[] timer = new Long[5];
    public static Economy econ = null;
    public boolean EconEnabled;


    private Map<Player, Integer> schedulerID = new HashMap<Player, Integer>();
    private Map<Player, Integer> protectFlightv = new HashMap<Player, Integer>();


    private Vault getVault() {
    Plugin plugin = getServer().getPluginManager().getPlugin("Vault");

    if (plugin == null || !(plugin instanceof Vault)) {
    return null; }

    return (Vault) plugin;
    }


    @Override
    public void onEnable() {


    if (EconEnabled && !setupEconomy()) {
    log.info("[" + getDescription().getName() + "] " + "You Need Vault! If You Do Not Want To Use Vault See The Configuration For Details.");
    getServer().getPluginManager().disablePlugin(this);
    return;
    }

    /*With the above code, whats suppose to happen is if you have EconomyEnabled in the config to true, and you don't have Vault, the error message is "You Need Vault! If You Do Not Want To Use Vault See The Configuration For Details." But it returns with "Plugin Fly Payment is attempting to register in class net/milkbowl/vault/Vault, which does not exist. Ignoring events registered in class com.ahellhound.bukkit.flypayment.Main" */

    this.getConfig().options().copyDefaults(true);
    this.saveConfig();
    reloadConfiguration();
    getServer().getPluginManager().registerEvents(this, this);
    log.info("[" + getDescription().getName() + "] " + getDescription().getName() + " version " + getDescription().getVersion() + " is now enabled.");
    log.info("[" + getDescription().getName() + "]" + " Made By AhellHound");
    }


    private void reloadConfiguration() {
    reloadConfig();

    for(Integer i = 1; i <= 5;) {
    stac[i - 1] = getConfig().getInt("ChargeItemIDTier" + i.toString());
    amnt[i - 1] = getConfig().getInt("AmountOfItemsToChargeTier" + i.toString());
    timer[i - 1] = getConfig().getLong("FlightTimerTier" + i.toString());
    i++;
    }
    EconEnabled = getConfig().getBoolean("EconomyEnabled");
    }

    private boolean setupEconomy() {
    if (getVault() == null) {
    return false;
    }
    RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    if (rsp == null) {
    return false;
    }
    econ = rsp.getProvider();
    return econ != null;
    }



    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){


    if (!(sender instanceof Player)){
    sender.sendMessage(ChatColor.RED + "You must be a player!");
    return true;
    }

    Player p = (Player) sender;

    if(args.length != 1){
    p.sendMessage(ChatColor.RED + "Use <command> <on:eek:ff>");
    return true;
    }

    Integer tier = null;
    for(Integer i = 1; i <= 5;) {
    if (p.hasPermission("flyp.fly." + i.toString())){
    tier = i - 1;
    break;
    }
    i++;
    }
    if(tier == null) {
    p.sendMessage(ChatColor.RED + "You lack the permission to do this!");
    return true;
    }

    if (args[0].equalsIgnoreCase("on")){
    if (p.getAllowFlight()){
    p.sendMessage(ChatColor.GREEN + "Flying is already enabled, " + p.getName() + "!");
    return true;
    }

    PlayerInventory inventory = p.getInventory();

    if(!p.hasPermission("flyp.bypass") && !inventory.contains(stac[tier],amnt[tier])) {
    p.sendMessage(ChatColor.GREEN + "You need "+ amnt[tier] + " " + Material.getMaterial(stac[tier]).name() + ", " + p.getName() + ", to start flying!");
    return true;
    }

    double timeform = (timer[tier] / 20) / 60;

    if (!p.getAllowFlight()){
    if (!p.hasPermission("flyp.bypass")){
    inventory.removeItem(new ItemStack(stac[tier],amnt[tier]));
    p.sendMessage(ChatColor.GREEN + "Flying enabled. I'll take that " + Material.getMaterial(stac[tier]).name() + ", " + p.getName() + ("."));
    p.setAllowFlight(true);
    p.setFlying(true);
    if(timer[tier] != 0 && !p.hasPermission("flyp.notimeout")) {
    final Player player = p;
    schedulerID.put(p, getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {public void run() {disableFlight(player);}}, timer[tier]));
    p.sendMessage(ChatColor.GREEN + "You Have " + timeform + " Minutes until your flight is disabled.");
    if(timer[tier] > 200){
    protectFlightv.put(p, getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {public void run() {protectFlight(player);}}, timer[tier] - 200));
    }
    }
    }

    else {
    p.sendMessage(ChatColor.GREEN + "Flying enabled, " + p.getName() + ("."));
    p.setAllowFlight(true);
    p.setFlying(true);
    }

    }
    }
    else if (args[0].equalsIgnoreCase("off")){
    if (!p.getAllowFlight()){
    p.sendMessage(ChatColor.GREEN + "Flying is already disabled " + p.getName() + "!");
    return true;
    }
    p.sendMessage(ChatColor.GREEN + "Flying disabled.");
    p.setFlying(false);
    p.setAllowFlight(false);
    if(schedulerID.containsKey(p)) {
    getServer().getScheduler().cancelTask(schedulerID.get(p));
    schedulerID.remove(p);
    }
    if(protectFlightv.containsKey(p)) {
    getServer().getScheduler().cancelTask(protectFlightv.get(p));
    protectFlightv.remove(p);
    }
    }
    else if (args[0].equalsIgnoreCase("reload")) {
    if(!p.hasPermission("flyp.reload")) {
    p.sendMessage(ChatColor.RED + "You lack the permission to do this!");
    return true;
    }
    reloadConfiguration();
    }
    else p.sendMessage(ChatColor.RED + "Invalid argument!");
    return true;
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onPlayerQuit(PlayerQuitEvent e) {
    Player p = e.getPlayer();
    if(schedulerID.containsKey(p)) {
    getServer().getScheduler().cancelTask(schedulerID.get(p));
    schedulerID.remove(p);
    }
    if(protectFlightv.containsKey(p)) {
    getServer().getScheduler().cancelTask(protectFlightv.get(p));
    protectFlightv.remove(p);
    }
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onPlayerKick(PlayerKickEvent e) {
    Player p = e.getPlayer();
    if(schedulerID.containsKey(p)) {
    getServer().getScheduler().cancelTask(schedulerID.get(p));
    schedulerID.remove(p);
    }
    if(protectFlightv.containsKey(p)) {
    getServer().getScheduler().cancelTask(protectFlightv.get(p));
    protectFlightv.remove(p);
    }
    }

    public void disableFlight(Player p) {
    p.sendMessage(ChatColor.RED + "Flying disabled, you ran out of time. Use /Fly On to start flying again.");
    p.setAllowFlight(false);
    p.setFlying(false);
    if(schedulerID.containsKey(p)) {
    getServer().getScheduler().cancelTask(schedulerID.get(p));
    schedulerID.remove(p);
    }
    if(protectFlightv.containsKey(p)) {
    getServer().getScheduler().cancelTask(protectFlightv.get(p));
    protectFlightv.remove(p);
    }

    }

    public void protectFlight(Player p) {
    p.sendMessage(ChatColor.RED + "Your your flight runs out in 10 Seconds!");
    protectFlightv.remove(p);

    }




    }


    Configuration

    #Fly Payment By AhellHound
    #Default config
    #----------------------------------------------
    #ChargeItemIDTier1: 264
    #AmountOfItemsToChargeTier1: 1
    #FlightTimerTier1: 0 (Times are in ticks, so 20 ticks = 1 second.)
    #----------------------------------------------
    #If you only want one price throughout the server, then just use Tier 1
    #and give that permission to everyone.
    #If you don't want a group to be timed, set the FlightTimerTier(1-5) to 0.

    #**NEEDS VAULT** If you want to charge people with an Economy currency from most Economy plugins. **NEEDS VAULT**
    EconomyEnabled: false

    #Permission: FlyP.Fly.1
    ChargeItemIDTier1: 264
    AmountOfItemsToChargeTier1: 1
    FlightTimerTier1: 0

    #Permission: FlyP.Fly.2
    ChargeItemIDTier2: 264
    AmountOfItemsToChargeTier2: 1
    FlightTimerTier2: 0

    #Permission: FlyP.Fly.3
    ChargeItemIDTier3: 264
    AmountOfItemsToChargeTier3: 1
    FlightTimerTier3: 0

    #Permission: FlyP.Fly.4
    ChargeItemIDTier4: 264
    AmountOfItemsToChargeTier4: 1
    FlightTimerTier4: 0

    #Permission: FlyP.Fly.5
    ChargeItemIDTier5: 264
    AmountOfItemsToChargeTier5: 1
    FlightTimerTier5: 0


    Plugin.yml

    name: Fly Payment
    author: AhellHound
    main: com.ahellhound.bukkit.flypayment.Main
    version: 0.4
    softdepend: [Vault]
    commands:
    fly:
    description: Start and stop flying.
    usage: /<command> <on:eek:ff>
    permissions:
    flyp.fly:
    description: Allows you to use the /Fly on and /Fly off commands.
    default: op
    flyp.bypass:
    description: Bypasses the cost of an item to fly.
    default: op
    flyp.notimeout:
    description: Bypasses the time limit after flying (if its defined in the config).
    default: op


    I know I may be putting this in a confusing way, so if you have questions to clarify please ask, as this error has stumped some of my friends, and is very irritating! Thank you very much for even attempting to go through the code!
    Thanks for reading and the feedback,
    Colby
  2. Offline

    coldandtired

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    You don't need this if you require Vault for economy:
    RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    if (rsp == null) {
    return false;
    }
    econ = rsp.getProvider();
    return econ != null;
    }

    You also check EconEnabled before you set it.
  3. Offline

    Colby l

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    @coldandtired
    Thanks for the reply and help, but that didn't solve this issue. I still get "[SEVERE] Plugin Fly Payment is attempting to register in class net/milkbowl/vault/Vault, which does not exist. Ignoring events registered in class com.ahellhound.bukkit.flypayment.Main" :(

    This post has been edited 1 time. It was last edited by Colby l Jul 5, 2012.
  4. Offline

    Njol

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    If vault is not loaded neither is the Economy class, thus Java throws a ClassNotFoundException on the line where you try to get the service provider. Either add a try...catch to catch the exception or add an if statement so that the code is only executed if vault is present (Bukkit.getPluginManager().getPlugin("Vault") != null)
  5. Offline

    gcflames5

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    What do you set the econ to then? Because rps is always null for me.

    This post has been edited 2 times. It was last edited by gcflames5 Nov 11, 2012.
  6. Offline

    coldandtired

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    if (getServer().getPluginManager().getPlugin("Vault") != null)
    {
    RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
    if (economyProvider != null) economy = economyProvider.getProvider();
    }

    That's all you need.

    EDIT: Hmmm, looking at it now, that's exactly what you already have.

    This post has been edited 1 time. It was last edited by coldandtired Nov 11, 2012.

Share This Page