Solved What Do I Assign to "Economy" Vault API

Discussion in 'Plugin Development' started by Shortninja66, Jan 6, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Shortninja66

    I am having issues with Vault API economy working with my EventListener, a NPE to be exact. I have debugged the error, which is the following:
    Code:
        public static Economy econ = null;
        public static EconomyResponse r;
    As you can see the Economy is null, but I really have no idea what to assign to it, For some reason, leaving it null works fine in the Main Class, which has "withdrawPlayer" methods. All I really need to know is what to assign, as I have had no luck with Google.
     
  2. Offline

    teej107

    1. Encapsulation
    2. Remove those static modifiers
    3. Have you looked at Vault's BukkitDev page?
     
  3. Offline

    Skionz

    @Shortninja66 Of course your getting an NPE...
    A simple 10 second google search and this would have already been solved.
     
  4. Offline

    Shortninja66

    I understand that I probably should have done a more thorough search on Google. I didn't think I would get an NPE at all due to the fact that this was setup exactly the same in my main class, where I have a command that utilizes economy.
     
  5. Offline

    Skionz

    @Shortninja66 If you attempt to use a field that is null, you will get an NPE.
     
  6. Offline

    pookeythekid

    @Shortninja66 Vault API provides a method for you to assign your Economy on its GitHub page.


    @teej107 Last I checked Vault's BukkitDev page's method doesn't work, and is different from its GitHub page.
     
  7. Offline

    teej107

    Nope. Still works.
     
  8. Offline

    pookeythekid

    @teej107 Hm. They are a little different, though.
     
  9. Offline

    teej107

  10. Offline

    1Rogue

  11. Unfortunately that's most likely where he got the public static part from. Vault is terrible at spreading this practice. :(
     
    teej107 likes this.
  12. Offline

    Shortninja66

    I used a null field in my main class, even utilized it in a command.. I'm not saying you're wrong, because I know what a NPE is, but it makes no sense to me as why I wouldn't get a NPE in my main class where the same method is used and is null.

    Yeah, that is where I got the static from. I supposed Vault would have it setup right
     
    AdamQpzm likes this.
  13. Offline

    nverdier

  14. Offline

    Skionz

    Because you initialized it in your main class.
     
  15. Offline

    1Rogue

    AdamQpzm likes this.
  16. Offline

    Shortninja66

    I also initialized it in my Listener?
     
  17. Offline

    Skionz

    Post the class.
     
  18. Offline

    Shortninja66

    Here:

    Code:
    package me.bukkit.shortninja66;
    
    import java.util.List;
    
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Server;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerItemConsumeEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class PotionListener implements Listener
    {
        public static Economy econ = null;
        public static EconomyResponse r;
        public PotionListener(CustomPotions plugin)
        {
            plugin.getServer().getPluginManager().registerEvents(this, plugin); 
        }
     
        public String colorize(String msg)
        {
            String coloredMsg = "";
            for(int i = 0; i < msg.length(); i++)
            {
                if(msg.charAt(i) == '&')
                    coloredMsg += '§';
                else
                    coloredMsg += msg.charAt(i);
            }
            return coloredMsg;
        }
     
        public ItemStack setMeta(ItemStack material, String name, List<String> lore)
        {
            if(((material == null || material.getType() == Material.AIR) || (name == null) && lore == null))
                return null;
         
            ItemMeta im = material.getItemMeta();
            if(name != null)
                im.setDisplayName(name);
            if(lore != null)
                im.setLore(lore);
         
            material.setItemMeta(im);
                return material;
         
        }
     
        Server server = Bukkit.getServer();
     
        public boolean setupEconomy() {
            if (Bukkit.getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = Bukkit.getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = rsp.getProvider();
            return econ != null;
        }
     
        @EventHandler
        public void onItemConsume(PlayerItemConsumeEvent event)
        {
             Player player = event.getPlayer();
          
                 if (player.getItemInHand().getItemMeta().hasDisplayName())
                 {
                     if (player.getItemInHand().getItemMeta().getDisplayName().equals("Speed II Ext."))
                     {
                         player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 3600, 1));
                     }
                     if (player.getItemInHand().getItemMeta().getDisplayName().equals("Speed III"))
                     {
                         player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 600, 2));
                     }
                  }
          }
     
    }
    Will reply tomorrow. Somehow I misplaced the code for the signs, nonetheless the economy still wouldn't work through a test event.
     
  19. Offline

    Skionz

  20. Offline

    Shortninja66

    Where would it initialized? I thought setupEconomy initialized?
     
  21. Offline

    xTigerRebornx

    @Shortninja66 setupEconomy() does initialize. You just never call it
     
    Konato_K likes this.
  22. Offline

    Shortninja66

    What do I do to initialize it? Have something reference to it like my main class checks if setupEconomy() is there in the onEnable, is that initializing?
     
  23. Offline

    1Rogue

    call setupEconomy()
     
  24. Main clas :
    Code:java
    1.  
    2. public static Plugin plugin;
    3. public Economy econ = null;
    4. private boolean setupEconomy()
    5. {
    6. RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
    7. if (economyProvider != null) {
    8. econ = economyProvider.getProvider();
    9. }
    10.  
    11. return (econ != null);
    12. }
    13.  
    14. @Override
    15. public void onEnable() {
    16. setupEconomy();
    17. plugin = this;
    18. }
    19.  

    Another class :
    Code:java
    1.  
    2. private final MainClass plugin;
    3. public AnotherClass(MainClass i) {
    4. plugin = i;
    5. }
    6.  
    7. //Get economy
    8. plugin.econ.....
    9.  


    PS to colorize a text just do this :
    Code:java
    1.  
    2. public String color(String string) {
    3. return ChatColor.translateAlternateColorCodes('&', string)
    4. }
     
  25. Offline

    1Rogue

    jfc

    do NOT use public non-final fields. Have an accessor.
     
  26. Nah
     
  27. Offline

    1Rogue

    Well then I sincerely do not respect you as a developer.

    You are literally teaching people terrible coding styles and terrible practices. This is a support forum where people go to receive help and 80-95% of all of your posts are spoonfeeding terrible, untested, unreliable code with very little maintainability or explanation.

    To paraphrase Joshua Bloch (Effective Java 2nd edition, Chapter 4.1):

    The rule of thumb is simple: make each class or member as inaccessible as possible. In other words, use the lowest possible access level consistent with the proper functioning of the software that you are writing.

    Instance fields should never be public. If an instance field is nonfinal, or is a final reference to a mutable object, then by making the field public, you give up the ability to limit the values that can be stored in the field. This
    means you also give up the ability to enforce invariants involving the field. Also, you give up the ability to take any action when the field is modified, so classes with public mutable fields are not thread-safe. Even if a field is final and refers to an immutable object, by making the field public you give up the flexibility to switch to a new internal data representation in which the field does not exist.

    The same advice applies to static fields, with the one exception. You can expose constants via public static final fields, assuming the constants form an integral part of the abstraction provided by the class. By convention, such fields have names consisting of capital letters, with words separated by underscores. It is critical that these fields contain either primitive values or references to immutable objects. A final field containing a reference to a mutable object has all the disadvantages of a nonfinal field. While the reference cannot be modified, the referenced object can be modified—with disastrous results.

    Note that a nonzero-length array is always mutable, so it is wrong for a class to have a public static final array field, or an accessor that returns such a fieldp. If a class has such a field or accessor, clients will be able to modify the contents of the array
     
    Last edited: Jan 9, 2015
  28. Offline

    Shortninja66

    It seems I have fixed the error by calling it in a way. I forgot how to really pass variables and didn't know if I should've passed it so I did the following:

    Code:
    if (!setupEconomy() )
                {
                    System.out.println("Economy has failed!");
                }
    Is this method good? I mean, it works, but I know this method probably isn't the best way to get the job done. Thanks all for trying to help though!
     
    Last edited: Jan 9, 2015
  29. Offline

    1Rogue

    That seems like a reasonable way to do it. I would print out using getLogger().log(Level.SEVERE, "Failed to initialize vault economy!"); or somesuch, though.
     
  30. Offline

    Shortninja66

    Nevermind, I'm stupid, had to update inventory :p Thanks all for the help!
     
    Last edited: Jan 9, 2015
Thread Status:
Not open for further replies.

Share This Page