[Vault] Balance Change Event

Discussion in 'Resources' started by Ultimate_n00b, Jun 14, 2013.

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

    Ultimate_n00b

    I have seen several people asking Sleaker if he would add a BalanceChangeEvent to vault, yet he has not added it (for reasons googlable).

    Anyway, here is how I decided to do it.

    First, setup vault economy as usual. My economy variable I have called econ. Then, you'll need a hashmap:

    Code:java
    1. //Economy variable from vault
    2. private static Economy econ = null;
    3. //Storage of what a players balance is
    4. HashMap<String, Double>balance = new HashMap<String, Double>();
    5.  


    Then, you'll need to use the PlayerJoinEvent to get the players starting balance. Do something like this:

    Code:java
    1. private static Economy econ = null;
    2. HashMap<String, Double>balance = new HashMap<String, Double>();
    3.  
    4. //Make sure to get a players starting balance
    5. @EventHandler
    6. public void onPlayerJoin(PlayerJoinEvent event){
    7. balance.put(event.getPlayer().getName(), econ.getBalance(event.getPlayer().getName()));
    8. }


    Just to avoid memory leaks and duplicates, remember to remove it when the player leaves:

    Code:java
    1. private static Economy econ = null;
    2. HashMap<String, Double>balance = new HashMap<String, Double>();
    3.  
    4. @EventHandler
    5. public void onPlayerJoin(PlayerJoinEvent event){
    6. balance.put(event.getPlayer().getName(), econ.getBalance(event.getPlayer().getName()));
    7. }
    8.  
    9. //And we have to remove it to avoid memory leaks and duplicates
    10. @EventHandler
    11. public void onPlayerLeave(PlayerQuitEvent event){
    12. if(balance.containsKey(event.getPlayer().getName()))
    13. balance.remove(event.getPlayer().getName());
    14. }


    Now that you got that, you have to make a way to constant check (and update) whether the players balance equals what he has, and what is stored. So, we'll make a repeating task in the onEnable:

    Code:java
    1. private static Economy econ = null;
    2. HashMap<String, Double>balance = new HashMap<String, Double>();
    3.  
    4. @EventHandler
    5. public void onPlayerJoin(PlayerJoinEvent event){
    6. balance.put(event.getPlayer().getName(), econ.getBalance(event.getPlayer().getName()));
    7. }
    8.  
    9. @EventHandler
    10. public void onPlayerLeave(PlayerQuitEvent event){
    11. if(balance.containsKey(event.getPlayer().getName()))
    12. balance.remove(event.getPlayer().getName());
    13. }
    14.  
    15. public void onEnable(){
    16. //Make a new task
    17. getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    18. @Override
    19. public void run() {
    20. for (Player p : Bukkit.getServer().getOnlinePlayers()) {
    21. //Checking if the players balance is the same as we have, if not continue
    22. if(econ.getBalance(p.getName()) != balance.get(p.getName())){
    23. //Do stuff here
    24.  
    25. //Once we're done, we have to update the players money
    26. balance.remove(p.getName());
    27. balance.put(p.getName(), econ.getBalance(p.getName()));
    28. }
    29. }
    30. }
    31. //20 ticks = 1 second. If you want to make it faster (may be laggier) make this lower
    32. }, 20, 20);
    33. }


    And whatever you want to do when his balance changes, you put it where I put //Do stuff here.
    If you want to get the players old balance before it changes, just get it out of the hashmap. To get the new balance, just get it from vault.

    (This is my first tutorial. If it sucks/doesn't work/is the definition of swag let me know)
     
    Giinger likes this.
  2. Offline

    nick4fake

    Thanks a lot!
     
  3. Offline

    Ultimate_n00b

    Whooo someone finally found this useful!
     
  4. Offline

    Blah1

    If you have like 500 players on a server this is not going to be very efficient.
     
  5. Offline

    Ultimate_n00b

    Works fine for when I needed it for large servers.
     
  6. If using Essentials, you can just hook into Essentials and listen to: UserBalanceUpdateEvent
     
  7. Offline

    Ultimate_n00b

    That didn't exist about a year ago, when I released this.
     
    KingFaris11 likes this.
Thread Status:
Not open for further replies.

Share This Page