[Tutorial] Custom Currency

Discussion in 'Resources' started by ClassyInvader69, Jul 30, 2014.

?

Has this helped you?

  1. Yes

    36.4%
  2. Not useful

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

    ClassyInvader69

    Hi guys, ClassyInvader69 here. This is my first tutorial so please leave some feedback on how I can improve these tutorials.

    So, the topic today is about a New Currency. It can be coins, tickets or even emeralds. This currency can be used to serve a purpose of premium items. My currency will be called "Tickets".

    Firstly, we need to save the config file. In your onEnable(), add this code:

    Code:java
    1. saveDefaultConfig(); //Saves configuration file


    Next, we need to save the actual currency.
    1. We need to create an event handler for new player joins, then check if configuration file contains his name.

    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(PlayerJoinEvent event) {
    3. Player player = (Player)event.getPlayer();
    4. if(!getConfig().contains("Tickets" + player.getName()) {
    5. getConfig().set("Tickets" + player.getName(), 100);
    6. }
    7. }


    2. If a player types /stats, it will display the number of tickets he has. To do this, add this code in your onCommand.

    Code:java
    1. if(cmd.getName().equalsIgnoreCase("stats") { //Check if command is /stats
    2. Player player = (Player) sender; //Declare player as the sender of command
    3. int tickets = getConfig().getInt("Tickets." + player.getName()); //Get tickets of player in config file
    4. player.sendMessage(ChatColor.GOLD + "======= Stats =======");
    5. player.sendMessage(ChatColor.GOLD + "Tickets: " + ChatColor.GREEN + tickets);
    6. player.sendMessage(ChatColor.GOLD + "===================");


    Additional Stuff:

    To integrate this into vault, add this first in your class body.
    Code:java
    1. private static Economy economy;


    Then, add this too.

    Code:java
    1. private boolean setupEconomy() {
    2. RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
    3. if (economyProvider != null) {
    4. economy = economyProvider.getProvider(); }
    5.  
    6. return (economy != null);
    7. }


    After that, do setupEconomy() in your onEnable() and add Vault and this plugin as dependencies.

    That is all for this tutorial. More will be added in soon. If you have more for me to add, please PM me and I will add it in. Post in the comments if you have any problem. Have a nice day! :)
     
  2. Offline

    TheMcScavenger

  3. Offline

    mythbusterma

    ClassyInvader69
    This ^

    And a few more things:

    1. Don't use the player's name to associate information with them when they are not online, names are subject to change as of 1.8

    2. For storing information about every player that has ever logged into the server, it would probably be more practical to use SQL, as YAML files tend to get unruly when they are large.
     
  4. Offline

    Chiller

    TheMcScavenger mythbusterma You guys know that there is a button called Report to report the post and to tell them to move it to another section...
     
  5. Offline

    MoeMix

    This should go in the Resources section. And, this is just my own opinion, but this isn't really an efficient currency system either. This is more or less just showing how to read from a config file. If you want it to be more of a currency tut I suggest providing code on how to spend, gain, trade, etc. using the currency method rather than just displaying how much you have.
     
  6. Offline

    mythbusterma

    Chiller

    Well I forgot to mention that as well, but my criticisms were of the post itself, not the section it's in.
     
  7. Offline

    TheMcScavenger

    You know that that doesn't help anyone? How the hell are people supposed to learn if nobody points it out?

    MoeMix: already pointed out. Don't repeat things unnecessarily.
     
  8. Offline

    MoeMix

    TheMcScavenger
    I wasn't repeating unnecessarily. In the midst of typing my reply to the OP no one else provided their insight on the tut. And the forums does not update automatically (at least for me) and so therefore, I did not realize that anyone else already said what I did.

    EDIT: On second thought, you never said anything of what I just said. All you did was point out that he/she should use SQL rather than YAML, so what are you talking about?
     
  9. Offline

    TheMcScavenger

    "Officer, I didn't run a red light. When I last looked at it, it was green."
     
  10. Offline

    MoeMix

    Haha, yeah I guess that's what you could say. I mean, don't expect me to refresh every 3 seconds to make sure I don't repeat what anybody else has said lol
     
  11. Offline

    TheMcScavenger

    Haha, no, I get it. I just thought of it, and thought I'd post that instead of saying "Uck, didn't think of that" :p
     
    MoeMix likes this.
  12. Offline

    Iroh

    Moved to resources.
     
  13. ClassyInvader69 How could one integrate this into Vault to allow other plugins to use the currency feature?
     
  14. Offline

    ClassyInvader69

    DJSkepter Thanks for your feedback. :) I will add that in shortly.
     
  15. Offline

    MoeMix

    Easy, to integrate with Vault add in this method:
    Code:java
    1. private static Economy economy;
    2. private boolean setupEconomy()
    3. {
    4. RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
    5. if (economyProvider != null) {
    6. economy = economyProvider.getProvider();
    7. }
    8.  
    9. return (economy != null);
    10. }


    Call in the setupEconomy() method in your onEnable() to actually set it up :p

    Then, to allow another plugin to use it just put Vault and the Currency plugin into the plugin's build path.
     
  16. Offline

    mazentheamazin

    Debatable, if you were to cache your information correctly and save it at appropriate times (i.e shutdown) YAML files aren't that bad. Besides, YAML is meant to be used as human-friendly data serialization (source). You can use SQL practices with YAML quite easily, as you can inherit them with most data storing methods. Bukkit plugins have made it seem that YAML files are meant for configuration, and caused interpretations for its original uses to be incorrect. They may be used for that purpose and do a good job at that however its original purpose should be kept in-mind when you're storing data if you're already using the library.

    ClassyInvader69
    Sadly your tutorial is not efficient, nor really serves it's purpose. You should be caching data for points rather than reading from config every time information is needed, you also use names which have been deemed to be obsolete due to UUIDs (as players will have the abilities to change names 1.8+). You also provide little-to-no information on how to deduct, trade between players, have an API for other developers to use, or actually implement them with the in-game economy (which is actually quite useless, then why not just use the IGE for the whole thing in the first place however you decided to include it).
     
    TigerHix and mythbusterma like this.
  17. Offline

    ClassyInvader69

    mazentheamazin I am not done yet. I plan to add more tmr as I am busy
     
  18. Offline

    mythbusterma

    I get what you're saying, and I agree with it but, I've heard SnakeYAML starts to get a bit funny when you have many thousands of entries with multiple children (that could just be a rumor), which all has to be loaded into memory and can get a bit unruly.

    Also, I completely agree with the part about his post.
     
  19. Offline

    hankered

    You might wanna change
    getConfig().set("Tickets" + player.getName(), 100);
    to
    getConfig().set("Tickets." + player.getName(), 100);
     
  20. ClassyInvader69 Using SQLite or MySQL etc. as opposed to YAML is mainly if you want to execute queries. For example if you want to see who has the highest balance (/baltop etc.).

    How does just putting those lines of code integrate into vault? For example, if I have that currency as a plugin, vault and decide to make another plugin, if I was to deposit money into a player's ticket balance via vault, it wouldn't work (does it have to like extend Vault's classes or something?)
     
  21. Offline

    mazentheamazin

    mythbusterma
    I've actually never heard of this, I've seen it in use with thousands of entries with multiple children and the only time where it wasn't running efficiently was then the developer wasn't caching the information correctly. If you do however have any sources to this (I cannot find any) I'd appreciate it.

    You can do this by just sorting a Map by using TreeMap, which is actually more efficient than what you're suggesting; look here for an example.
     
    DJSkepter and mythbusterma like this.
  22. Offline

    Garris0n

    It is, by nature, cached. Only when you save/reload is the actual file touched.
     
    xTigerRebornx likes this.
  23. Offline

    mazentheamazin

  24. Offline

    MrDplugins

  25. Offline

    ClassyInvader69

    MrDplugins I was going to remove it as it was my brother who was using my account.
     
  26. Offline

    MrDplugins

    ClassyInvader69 I think its very helpful you should keep it and add to it!
     
Thread Status:
Not open for further replies.

Share This Page