Creating a simple economy plugin

Discussion in 'Plugin Development' started by ratedam, Feb 4, 2014.

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

    ratedam

    Evening to you all!

    I'd like to develop a simple economy plugin system but there's one problem. I don't have too much knowledge in developing under Bukkit's library.

    My main difficulties:
    - Storing the player data so that after him relogging beeing able to retreive their data again
    - Making the same above w/ the server reload

    I'm only asking for some help on this cuz i really got stuck (i know how to do the rest like, when the user sends "more" adds one more coin to their wallet).

    Cheers and thank you!
     
  2. Offline

    Maurdekye

    ratedam Well it'd be easiest to store player money info in the config file. I suggest you implement these two simple getter and setter commands, which are the basis of all money systems;
    Code:java
    1. public void setMoney(Player ply, int amount) {
    2. getConfig().set(ply.getName() + ".money", amount);
    3. saveconfig();
    4. }
    5.  
    6. public int getMoney(Player ply) {
    7. if (!getConfig().containsKey(ply.getName() + ".money"))
    8. setMoney(ply, 0);
    9. return getConfig().getInt(ply.getName() + ".money");
    10. }
     
  3. Offline

    ratedam

    Hello Maurdekye
    Let's imagine that my name is ratedam and i have a amount of 3.
    In the: getConfig().set(ply.getName() + ".money", amount); <- This will write in plain text |ratedam.money,1| correct?
    Whsat do you mean with if (!getConfig().containsKey(ply.getName() + ".money")) ? You are checking if the config DOES NOT contain the key correct?

    In here
    return getConfig().getInt(ply.getName() + ".money");
    You are returning the integer value of the key with the ratedam.money correct?

    Thank you for your help.
     
  4. Offline

    Maurdekye

    ratedam
    If you use setMoney(ratedam, 3), then it will write this to the config;
    Code:
    ratedam:
      money: 3
    And yes, you are correct. That line checks if the config contains the player's account information, and if not, it generates a new entry with them having 0 money.
     
  5. Offline

    ratedam

    Maurdekye

    So if i have an onCommand with the command "more" do i mention the setMoney method like this?
    Code:java
    1. public int onCommand(CommandSender sender, Command command, String label, String[] args) {
    2. Player player = (sender) Player;
    3. if(label.equalsIgnoreCase("more")){
    4. setMoney(Player ply, 1);
    5. }
     
  6. Offline

    Maurdekye

    ratedam Well, if they have 0 in their account, that would make it 1. But if they had 2, it would still make it 1. If they had 4000, it would make it 1. If you want to add money, you have to change that to this line;
    Code:java
    1. setMoney(player, getMoney(player) + 1);

    That would add 1 to a player's account.
     
  7. Offline

    ratedam

    Thanks Maurdekye

    So the correct way to do this on a online server with for example 10 people online on it would be right after each send of the command "more" save it right?
     
  8. Offline

    Maurdekye

    ratedam The setMoney() command automatically saves it after changing the value, so you don't need to worry about that. Just use these commands, and don't worry about compatibility.
     
  9. Offline

    ratedam

    Maurdekye are you sure that the getConfig().containsKey() exists? I can only find the contains.

    Cheers

    EDIT: Nevermind, got it! Parsed the player objet into a string and then insteand of checking if not existed checked fr a null value :D

    TYVM mate!

    Maurdekye i don't want to open a new post so here i go:

    I created another class to implement an EventHandler on PlayerFish. Here my second class
    Code:java
    1. package com.angelo.save;
    2.  
    3. import org.bukkit.event.EventHandler;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.event.player.PlayerFishEvent;
    6.  
    7.  
    8. public class fishing implements Listener {
    9. @EventHandler
    10. public void PlayerFish(PlayerFishEvent e) {
    11. setMoney(player,getMoney(player)+1);
    12. int current = getMoney(player);
    13. player.sendMessage("You have "+current+" coins");
    14. }
    15. }
    16.  

    I get an error in the "player" that couldn't be resolved into a variable. I created inside this same class the Player player = e.getPlayer(); and then the error saying that the method getMoney() is undefined for the type fishing. How can i solve this?

    And here's my main class
    Code:java
    1.  
    2. package com.angelo.save;
    3.  
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.PluginDescriptionFile;
    11. import org.bukkit.plugin.PluginManager;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. import com.angelo.clicar.CListener;
    15.  
    16. public class Savetest extends JavaPlugin {
    17. public final Logger logger = Logger.getLogger("Minecraft");
    18. public static Savetest plugin; //para mencionar o nome no futuro
    19.  
    20. public void onDisable(){
    21. PluginDescriptionFile pdfFile = this.getDescription(); //Atualizar descrição
    22. this.logger.info(pdfFile.getName() + " has been disabled"); //vai buscar o nome
    23. }
    24.  
    25. public void onEnable(){
    26. PluginDescriptionFile pdfFile = this.getDescription(); //Atualizar descrição
    27. this.logger.info(pdfFile.getName() + " Version "+ pdfFile.getVersion() + " has been enabled");
    28. PluginManager pm=getServer().getPluginManager();
    29. pm.registerEvents(new fishing(),this);
    30. }
    31.  
    32. public void setMoney(Player ply, int amount) {
    33. getConfig().set(ply.getName() + ".fishing", amount);
    34. saveConfig();
    35. }
    36.  
    37. public int getMoney(Player ply) {
    38. if (getConfig().getString(ply.getName() + ".fishing")==null)
    39. setMoney(ply, 0);
    40. return getConfig().getInt(ply.getName() + ".fishing");
    41. }
    42.  
    43.  


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

    Maurdekye

    ratedam First of all, in your fishing class the variable player isn't defined. You have to add Player player = e.getPlayer() in the listener. Secondly, the getMoney() method is a part of main class, Savetest, and isn't in scope of the Fishing class. The first thing you need to do is pass an instance of your main class to your fishing class when you create it. In your main class, change this line;
    Code:java
    1. pm.registerEvents(new fishing(),this);

    to this line;
    Code:java
    1. pm.registerEvents(new fishing(this),this);


    And in your fishing class, add these lines near the top, within your class;
    Code:java
    1. Savetest parent;
    2.  
    3. fishing (Savetest parent) {
    4. this.parent = parent;
    5. }

    And finally, change getMoney(player) to parent.getMoney(player).
     
Thread Status:
Not open for further replies.

Share This Page