Solved Making An Ingame Economy

Discussion in 'Plugin Development' started by CodePlaysMinecraft, Jan 18, 2015.

Thread Status:
Not open for further replies.
  1. Hi. I was wondering if their was any way to make an ingame economy without implementing 3rd party plugins like Vault or Essentials and storing their economy amount into playerName.yml files, and being able to write, read, and rewrite the information in the YAML file. I have some code below, but I'm not sure how to put it all together.
    Making the economy a variable & integer:
    Code:
    public int econ;
    Making the YAML file:
    Code:
    public void onPlayerJoin(PlayerJoinEvent event) {
    Player player = event.getPlayer();
    FileConfiguration config = null;
    File playerFile = new File("plugins/TheRealm/Economy/" + File.seperator + player.getName() + ".yml");
    if(!file.exists) {
    try {
    playerFile.createNewFile();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    Another thing I'm trying to do with this economy is using it in a scoreboard and making the number of the economy the score. (e.g. Emeralds: 108)
    Any help is appreciated. :)

    Bump? I know that this has been done before.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  2. Offline

    Lolmewn Retired Staff

    Try to think in objects more. Example: On player join, instead of creating a player file if it doesn't exist, create an EconomyPlayer object (or whatever you want to call it), and have a scheduler save all EconomyPlayers you have to disk with an interval.
     
  3. @Lolmewn What is an EconomyPlayer object? Also, could you give me an example of an interval? It's the first time I've ever heard of it.
     
  4. Offline

    Lolmewn Retired Staff

  5. Offline

    1Rogue Retired Staff

    Additionally, at its core all economies are is simply a mapping of a player to a value for money. You can implement a rudimentary economy with around 10 lines of code, but it's a matter of how you implement it for stability and ease of use that matters.

    I would recommend not steering away from Vault however as you will be cutting yourself out of a huge resource.
     
  6. @1Rogue I had originally wanted to use Vault, but some complications came out of it. One being that I couldn't implement a player's amount into a specialized scoreboard I had made because I couldn't figure out how to get the initial amount that a player had. Plus I wanted to be original.
     
    Last edited: Jan 18, 2015
  7. Offline

    1Rogue Retired Staff

    That's a noticeable downside when attempting to apply it to scoreboards (one of which I fixed in my own library by using a class proxy, but I digress).

    You can get the initial amount by simply referring to the vault economy object when you make the scoreboard.
     
  8. @1Rogue Plus, vault has a lot of static references which from what I heard could lead to RAM leaks? Anyway, I'm going to try and use vault. If I have any problems I'll be sure to contact you.
     
  9. Offline

    1Rogue Retired Staff

    Whoever told you that static causes ram leaks has no idea what they are talking about. Even more so because there's only one static non-constant reference in the entirety of the vault files that I'm seeing. Their example on implementing it is terrible, however.
     
    teej107 likes this.
  10. @1Rogue That's what I was trying to get my code from, and probably why I didn't understand it in the first place. Is there another implementing guide I could look at for Vault?
     
  11. Offline

    1Rogue Retired Staff

    In essence, all you need to do is get the Economy object from bukkit. Look at vault's method that sets the "econ" variable, and you can probably deduce which method to look at yourself to get that Economy object.
     
  12. Offline

    Captain Dory

  13. @Captain Dory Thanks! :) This solved some of my problems. See you in 6 hours, I guess? :p
     
  14. Offline

    Captain Dory

    Almost forgot :B

    So, to start off this, I made a class called methods. This is it's constructor:
    PHP:
    UUID u;
        
    File f;
        
    FileConfiguration c;
      
        public 
    Methods(UUID u) {
          
            
    this.u;
          
            
    = new File("config files/" ".yml");
            
    YamlConfiguration.loadConfiguration(f);
          
        }
    I really like this constructor, as instead of doing getMoney(Player p), setMoney(Player p), getRank(Player p) etc, you can just use one method (As long as you've got an instance of Methods in the class).

    You may or may not want to use "config files/" + u + ".yml" As this creates a YAML file called the players UUID, in a directory located in the main folder. It's conventional to put all configs inside plugins folder, but I didn't really mind as the plugin I made was private.

    Here are some of my Methods that may help you:
    PHP:
    public FileConfiguration getConfig() {
          
            return 
    c;
          
        }

    public 
    void createConfig() {
          
            try {
              
                
    f.createNewFile();
              
            } catch (
    IOException e) {
              
                
    e.printStackTrace();
              
            }
          
        }

    public 
    void createDefaults() {

            
    c.set("money"0.00);
          
        }

    public 
    void saveConfig() {
          
            try {
              
                
    getConfig().save(f);
              
            } catch (
    IOException e) {
              
                
    e.printStackTrace();
              
            }
          
        }

    public 
    double getMoney() {
          
            return 
    c.getDouble("money");
          
        }
      
        public 
    void setMoney(double amount) {
          
            
    c.set("money"amount);
          
        }
    I stored 'money' as a double.

    You want to make a file for that player if one doesn't exist already when they join, like so:
    PHP:
    @EventHandler
        
    public void onJoin(PlayerJoinEvent e) {
          
            
    Methods m = new Methods(e.getPlayer().getUniqueId());
          
            if (!(
    m.f.exists())) {
              
                
    m.createConfig();
                
    m.createDefaults();
                
    m.saveConfig();
              
            }
          
        }
    You can use the FileConfiguration variable 'c' as you would with a normal config.

    That should be everything; reply if you need more help.

    EDIT: Oh, and by the way, I use per player config files. Well, per UUID.
     
  15. @Captain Dory Thank you so much! Your methods are amazing and well though. Sorry I couldn't reply until now, I was busy with other things and decided now to try out what you had said. One thing though, is it possible if I could round the player's money to a whole number instead of 100.0 or 100.00 (Just so I could implement it into my scoreboard.)? I already changed config.set("Emeralds", 0.00) to config.set("Emeralds", 0), but that didn't help.
    Thanks so much for your help! :)

    EDIT: Never mind, just had to change double to int in the Methods class. :)
     
    Last edited: Jan 22, 2015
  16. Offline

    Captain Dory

    I'm really, really sorry for being inactive. :c

    I ended up changing mine to integers as well, not doubles; and I tried exactly what you tried :p
    The problem is that the plugin is reading and writing the money as doubles, not integers.
    I think that doing this:
    PHP:
    public int getMoney() {
        
            return 
    c.getInt("money");
        
        }
        public 
    void setMoney(int amount) {
        
            
    c.set("money"amount);
        
        }
    should fix it.
     
  17. @Captain Dory Lol, that's exactly what I did. Thanks anyway. :p
     
Thread Status:
Not open for further replies.

Share This Page