[Tutorial] Making a simple currency with a the config file

Discussion in 'Resources' started by sgavster, Sep 19, 2013.

?

Was this useful?

  1. Yes

    3 vote(s)
    42.9%
  2. No

    4 vote(s)
    57.1%
Thread Status:
Not open for further replies.
  1. Offline

    sgavster

    Hi everyone! I was recently making a plugin, and I needed my own currency.
    This is how I did it:
    First on your onEnable() you need to add the default config, so put this:
    Code:java
    1. this.saveDefaultConfig();

    Now, when they log in they will need to add them to the config, so to do this we will need to register events, so we will do this with this on your onEnable():
    Code:java
    1. PluginManager pm = Bukkit.getPluginManager();
    2. pm.registerEvents(this, this);


    Now that we have that done, we need to add them. We will use PlayerJoinEvent. So we will put this code:
    Code:java
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent e)
    3. {
    4. Player p = e.getPlayer();
    5. getConfig().set(p.getName() + ".money", pgetConfig().getInt(p.getName() + ".money"));
    6. saveConfig();
    7. }


    When a player logs in it will do this in the config:
    Code:
    sgavster:
      money: 0
    You may think it will create multiple of those, but no! It is 'setting' it. It's getting that int, so no errors!

    Now making paying commands. You can mess with this however you want but this is how I did it.
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    2.  
    3. {
    4.  
    5. if(cmd.getName().equalsIgnoreCase("myplugin"))
    6.  
    7. {
    8.  
    9. if(args.length == 0)
    10. {
    11. sender.sendMessage("Not enough arguments!");
    12. }
    13. else
    14. {
    15. if (sender instanceof Player)
    16.  
    17. {
    18.  
    19. final Player p = (Player)sender;
    20.  
    21. String w = p.getWorld().getName();
    22.  
    23. if(args.length >= 1)
    24.  
    25. {
    26.  
    27. if(args[0].equalsIgnoreCase("money"))
    28.  
    29. {
    30.  
    31. switch (args.length)
    32.  
    33. {
    34.  
    35. case 1:
    36.  
    37. p.sendMessage("§9 $" + getConfig().getInt(p.getName() + ".money", 0));
    38.  
    39. break;
    40.  
    41.  
    42.  
    43. case 2:
    44.  
    45. case 3:
    46.  
    47. p.sendMessage("§4Error: §9Not enough args, or too many.");
    48.  
    49. p.sendMessage("§6/myplugin money");
    50.  
    51. p.sendMessage("§6/myplugin money give <player> <amount>");
    52.  
    53. p.sendMessage("§6/myplugin money set <player> <amount>");
    54.  
    55. saveConfig();
    56.  
    57. break;
    58.  
    59.  
    60.  
    61. case 4:
    62.  
    63. Player t = Bukkit.getPlayer(args[2]);
    64.  
    65. if (t == null)
    66.  
    67. p.sendMessage( "§4Error: §9Please enter a valid player.");
    68.  
    69. else try
    70.  
    71. {
    72.  
    73. int value = Integer.parseInt(args[3]);
    74.  
    75. if (args[1].equalsIgnoreCase("give")) {
    76.  
    77. getConfig().set(t.getName() + ".money", getConfig().getInt(t.getName() + ".money") + value);
    78.  
    79. p.sendMessage("§9You gave §2$" + args[3] + "§9's to §2" + t.getName());
    80.  
    81. t.sendMessage("§9You got §2$" + args[3] + "§9 from §2" + p.getName());
    82.  
    83. saveConfig();
    84.  
    85. }
    86.  
    87.  
    88.  
    89. if (args[1].equalsIgnoreCase("set"))
    90.  
    91. {
    92.  
    93. getConfig().set(t.getName() + ".money", value);
    94.  
    95. p.sendMessage("§9You set §2" + t.getName() + "§9's money to §2" + value);
    96.  
    97. t.sendMessage("§9Your money was set to §2$" + value + "§9 by §2" + p.getName());
    98.  
    99. saveConfig();
    100.  
    101. }
    102.  
    103. }
    104.  
    105. catch (final NumberFormatException e)
    106.  
    107. {
    108.  
    109. p.sendMessage( "§9You need to specify a valid amount.");
    110.  
    111. }
    112.  
    113.  
    114.  
    115. return true;
    116.  
    117. }
    118.  
    119. }
    120.  
    121. else if(args[0].equalsIgnoreCase("pay"))
    122.  
    123. {
    124.  
    125. switch (args.length)
    126.  
    127. {
    128.  
    129. case 1:
    130.  
    131. case 2:
    132.  
    133. p.sendMessage( "§4Error: §9You need to specify an amount.");
    134.  
    135. p.sendMessage( "§6/myplugin pay <player> <amount>");
    136.  
    137. break;
    138.  
    139. case 3:
    140.  
    141. Player t = Bukkit.getPlayer(args[1]);
    142.  
    143. if(t == null)
    144.  
    145. p.sendMessage( "§4Error: §9Please enter a valid player.");
    146.  
    147. else try
    148.  
    149. {
    150.  
    151. getConfig().set(t.getName() + ".money", getConfig().getInt(t.getName() + ".money") + Integer.parseInt(args[2]));
    152.  
    153. getConfig().set(p.getName() + ".money", getConfig().getInt(p.getName() + ".money") - Integer.parseInt(args[2]));
    154.  
    155. p.sendMessage("§9You gave §2$" + args[2] + "§9's to §2" + t.getName());
    156.  
    157. t.sendMessage("§9You got §2$" + args[2] + "§9 from §2" + p.getName());
    158.  
    159. saveConfig();
    160.  
    161. break;
    162.  
    163. }
    164.  
    165. catch (final NumberFormatException e)
    166.  
    167. {
    168.  
    169. p.sendMessage( "§9You need to specify a valid amount.");
    170.  
    171. }
    172.  
    173.  
    174.  
    175. return true;
    176.  
    177.  
    178.  
    179. }
    180.  
    181. }
    182.  
    183. }
    184.  
    185. }
    186.  
    187. }
    188.  
    189. }

    That will allow these commands:

    • /myplugin money - Lists your money
    • /myplugin money set <player> <amount> - Sets the targets health, does not take the senders money
    • /myplugin money give <player> <amount> - Gives the targets health, does not take the senders money
    • /myplugin pay <player> <amount> - Sets the targets health, does take the senders money


      Note: This probably isn't the best way to do this, it's just a way I wanted to share.
      Also, if you have any questions, please tell me!
     
  2. Offline

    Ivan

    This code is full of errors. First of all you're initializing values in the config in a very strange way:
    Code:java
    1.  
    2. getConfig().set(p.getName() + ".money", plugin.getConfig().getInt(p.getName() + ".money"));
    3.  

    Second of all, none of the /myplugin commands will work, since you're blocking them all at this line:
    Code:java
    1.  
    2. if(cmd.getName().equalsIgnoreCase("myplugin")) {
    3. sender.sendMessage("Not enough arguments!");
    4. }
    5.  

    There is no need for player to be final too.

    Instead of defining all the cases where you have too much or too less variables (case 2,3 but you would need 5 6 7 8 9 etc. too), you can just use the keyword default at the bottom which will cover all of those.
     
  3. Offline

    sgavster

    ferrybig Because, there is no "/myplugin money <player>".

    Ivan I fixed those.

    I just forgot
    if(args.length == 0)
    {.

    Also, the reason I am doing it on PlayerJoinEvent is so that it can be for each player.
    It's getting that int so if it's not already there it will create it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
Thread Status:
Not open for further replies.

Share This Page