Tutorial Custom Currency With Per-Player Config Files

Discussion in 'Resources' started by CodePlaysMinecraft, Mar 26, 2015.

Thread Status:
Not open for further replies.
  1. Hello all, I've recently have been working on a private plugin that adds your own custom currency with the help of per-player config files. So, I thought I would share this with the rest of the community.

    Okay, first, let's create the per-player config files to put our currency in. I will be making a separate class to put all of my methods in.
    Code:
        UUID u;
        File pData;
        FileConfiguration pDataConfig;
    
        public methodsClass(UUID u) {
            this.u = u;
    
            pData = new File("plugins/YourPlugin/Custom Currency/" + u + ".yml");
            pDataConfig = YamlConfiguration.loadConfiguration(pData);
        }
    Above is the constructor of the class. This will be used to get the player's UUID. The player's UUID will be the file's name. (e.g. 2c7af28d-99db-4589-be7c-2a9084531fa0.yml) I used UUIDs because of 1.8 name changes, so when a player changes their name, all of their currency wont be lost.

    Next, let's make the creation of the file, setting of the defaults, getting the config, and saving the files.
    Code:
    public void createPlayerConfig() {
                try {
                    pData.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    Code:
        public void createPlayerDefaults() {
            if (pData.length() <= 0) { // Checking if there isn't any data in the file.
                pDataConfig.set("Money", 0);
            }
        }
    Code:
        public FileConfiguration getPlayerConfig() {
            return pDataConfig;
        }
    Code:
        public void savePlayerConfig() {
            try {
                getPlayerConfig().save(pData);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    Now that we've gotten the hard part done, let's move onto the methods. We will make simple methods. getMoney(), setMoney(), giveMoney(), and takeMoney().
    Code:
        public int getMoney() {
            return pDataConfig.getInt("Money");
        }
    Code:
        public void setMoney(int amount) {
            pDataConfig.set("Money", amount);
        }
    Code:
        public void takeMoney(int amount) {
            pDataConfig.set("Money", getMoney() - amount);
        }
    Code:
        public void giveMoney(int amount) {
            pDataConfig.set("Money", getMoney() + amount);
        }
    These are very simple to implement. Whatever you do though, do NOT make these methods static.

    This is how I implement my class of methods, but you can do it any way you want (other than static).
    Code:
    MethodsClass methodsClass = new MethodsClass(player.getUniqueId()); // Make sure that you have the player.getUniqueId() in your parenthesis, otherwise, it's not going to work.
    Once you have implemented your methods class, you can use the methods like this:
    Code:
    // Getting a player's money.
    player.sendMessage("You have " + methodsClass.getMoney() + " dollars!");
    
    // Taking a player's money.
    methodsClass.takeMoney(300);
    methodsClass.savePlayerConfig(); // Make sure to save the player's config!
    player.sendMessage("300 dollars has been taken from your account.");
    
    // Giving a player money.
    methodsClass.giveMoney(300);
    methodsClass.savePlayerConfig();
    player.sendMessage("300 dollars has been addded to your account!");
    
    // Setting a player's money.
    methodsClass.setMoney(300);
    methodsClass.savePlayerConfig();
    player.sendMessage("You account balance has been set to 300!");
    To create the player's file, you need to create an onPlayerJoin event and then do the methods that we created (createPlayerConfig(), createPlayerDefaults(), and savePlayerConfig()).
    Code:
    public void onPlayerJoin(PlayerJoinEvent event) {
    Player player = event.getPlayer();
    MethodsClass methodsClass = new MethodsClass(player.getUniqueId());
    
    methodsClass.createPlayerConfig();
    methodsClass.createPlayerDefaults();
    methodsClass.savePlayerConfig();
    }
    You can also implement a players currency into a scoreboard (optional, of course).
    Code:
    Objective obj;
    MethodsClass methodsClass = new MethodsClass(player.getUniqueId());
    Score money = obj.getScore(ChatColor.DARK_GREEN + "Money");
    money.setScore(methodsClass.getMoney());
    If you have anything that should be added/changed, please tell me.
    I know that multiple threads/tutorials have been made about making your own custom currency, but I'd like to add the use of per-player configs and methods.
    Anyway, hope this helped! :)
     
    Last edited: May 3, 2015
  2. Offline

    teej107

    1. It's not that good to hard code in a File like that. There are methods the Bukkit provides. http://infernaldevelopers.com/docs/Bukkit/1.7/org/bukkit/plugin/Plugin.html#getDataFolder()
    2. Code:
      public void createPlayerConfig() {
              if (!pData.exists()) {  // Make sure the file doesn't exist before making the file.
                  try {
                      pData.createNewFile();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
      Checking to see if "pData" doesn't exists is a little redundant. http://docs.oracle.com/javase/7/docs/api/java/io/File.html#createNewFile() Reading the JavaDocs will help but it says it will create a File if a file by that name doesn't exist.

    3. Code:
      public void createPlayerDefaults() {
              if (pData.length() <= 0) { // Checking if there isn't any data in the file.
                  pDataConfig.set("My Currency", 0);
              } else {
                  return;
              }
          }
      Is there a reason why the File needs to be empty? Also that else and return statement is completely useless.
    Other than that, good tutorial. I just have one more question: Why did you use the old Java code tags and not the new ones? If you edit your thread, all your indentation will be gone.
     
    CodePlaysMinecraft likes this.
  3. @teej107 Sorry about the "plugins/YourPlugin/blah..." thing. I meant to change that but must've forgotten to.
    I have edited the "} else { return;" statement.
    I am checking the length because if someone were to edit the config file and leave it blank, when the player of that blank config folder joins, it won't create a bunch of NPEs. Instead, his currency will be set back to 0.
    Removed the checking if the file exists.
    I used the syntax tags just because I thought they looked cool. I changed them back to regular code tags.
    Thanks for the advice. :)
     
    Last edited: Mar 27, 2015
  4. Offline

    ChipDev

    I actually see no problem in the getDataFolder vs creating a config, as what if you have to store multiple files / UUIDS, You want to have a clean folder space in your plugins folder.
     
  5. EDIT:
    • Added in the making of the per-player config files.
    • Added in a scoreboard example.
    • Used code tags instead of syntax tags.
    • Removed some unnecessary code.
     
  6. Offline

    Msrules123

    Good for beginners.
    @CodePlaysMinecraft You may want to put the getPlayerConfig() method for those who would not know how to create one.
     
  7. @Msrules123 Oh wow, my bad. I'm so stupid some times. I'll post the code when I get back to the condo (currently on vacation, sorry).

    UPDATE:
    • Added the getPlayerConfig() method. (Sorry for not posting this, it must have slipped my mind. :) )

    @Msrules123

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Mar 30, 2015
  8. Offline

    Eggspurt

    Uhm, for some reason it gives this error

    "The method getDataFolder() is undefined for the type Config"

    MethodsClass methodsClass = new MethodsClass(player.getUniqueId()); // Make sure that you have the player.getUniqueId() in your parenthesis, otherwise, it's not going to work.

    I don't know how to implement this correctly, mind giving me some advice?


    Thanks for this tutorial though :)
     
    Last edited: Apr 8, 2015
  9. @Eggspurt You need to have an instance of you main class in the class your methods are in. Also, please don't directly copy and paste code, it's bad at learning.
     
  10. Offline

    Eggspurt

    After revision

    Your first class won't work because it's not in the main class I had to do this

    Code:
        public ConfigHandler(UUID u, Main m) {
            this.u = u;
    
            pData = new File(m.getDataFolder() + "/Custom Currency/" + u + ".yml");
            pDataConfig = YamlConfiguration.loadConfiguration(pData);
        }
     
    Last edited: Apr 10, 2015
  11. @Eggspurt It would have been better to do JavaPlugin that way it won't throw errors if you use this in another project in which the main class isn't called Main.
     
  12. Offline

    Eggspurt

    Ah Thanks for the Tip

    @OP
    Code:
    public void createPlayerDefaults() {
           if (pData.length() <= 0) { // Checking if there isn't any data in the file.
                pDataConfig.set("My Currency", 0);
           }
       }
    
    Shouldn't it be
    Code:
    public void createPlayerDefaults() {
           if (pData.length() <= 0) { // Checking if there isn't any data in the file.
                pDataConfig.set("Money", 0);
           }
      }
    
    I could be wrong..
     
  13. Offline

    nverdier

    @Eggspurt What does that have to do with anything?
     
  14. Offline

    Eggspurt

    Because the rest of your tutorial is using "Money" instead of Currency
     
  15. Offline

    nverdier

  16. @Eggspurt Totally didn't see that, it's fixed now. :)
     
  17. Offline

    BioBG

    Is this still works ? cuz i get some errors i can't fix.
     
  18. Offline

    Zombie_Striker

    @BioBG
    Can you post the errors?
     
  19. Offline

    BioBG

    I fix it :)
     
  20. Offline

    PratamaJr

    Error please help


    Code:
    UUID u;
        File pData;
        FileConfiguration pDataConfig;
    
        public methodsClass(UUID u) {
            this.u = u;
    
            pData = new File("plugins/YourPlugin/Custom Currency/" + u + ".yml");
            pDataConfig = YamlConfiguration.loadConfiguration(pData);
        }
    said return type of void is missing
     
  21. Offline

    timtower Administrator Administrator Moderator

    @PratamaJr Your function has no return value.
    That looks like a constructor but then what is the class?
     
Thread Status:
Not open for further replies.

Share This Page