Public Boolean Requerid Money to execute command?

Discussion in 'Plugin Development' started by migcabreraes, Oct 24, 2014.

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

    migcabreraes

    Hellos Guys, I'm creating my first plugin and have doubts, quierohacer with a boolean which put public / and the command you have to have an amount of money on the server to run that command? how I can do it
     
  2. Offline

    valon750

    migcabreraes

    You'll need to hook in to Vault, by adding it as an external Jar using the program of your choice :)

    Once you've done that, you'll have access to methods such as getBalance, which you can then use to check if the player has enough money.
     
    xMrPoi likes this.
  3. Offline

    xMrPoi

    (Assume getMoney(Player player); is a method you have made that returns the money of the player)

    You could do something like this:
    Code:java
    1. public final int COST = 5;
    2.  
    3. @EventHandler
    4. public void onCommand(PlayerCommandPreprocessEvent event){
    5. if(!event.getMessage().equalsIgnoreCase("test")) return;
    6. Player player = event.getPlayer();
    7. if(!getMoney(player) >= COST){
    8. player.sendMessage("Not enough money!");
    9. event.setCancelled(true);
    10. return;
    11. }
    12. // subtract money
    13. }

    I know that getting the message and seeing if it equals "test" isn't a very good way to check the command given seeing as the player could simply add in another argument and bypass this. That could be fixed later.

    If you are not using your own economy system, hooking into vault is the best way to do it.
     
  4. xMrPoi Why "if not greater than or equal to" rather than a simple "if less than"?
     
  5. Offline

    migcabreraes

    Code:java
    1. public class Main extends JavaPlugin {
    2. public final int COST = 100;
    3.  
    4. public void onEnable(){
    5. System.out.println("[Rankup] Plugin Enabled!");
    6. getServer().getPluginManager().registerEvents((Listener) this, this);
    7. saveDefaultConfig();
    8.  
    9. }
    10.  
    11. @EventHandler
    12. public void onCommand(PlayerCommandPreprocessEvent event){
    13. if(!event.getMessage().equalsIgnoreCase("rankup")) return;
    14.  
    15. Player player = event.getPlayer();
    16. if(!getMoney(player) >= COST){
    17.  
    18. player.sendMessage(getConfig().getString("MSG.Line1").replace("&", "§"));
    19.  
    20. event.setCancelled(true);
    21. return;
    22.  
    23. }
    24.  
    25.  
    26. }
    27.  
    28. public void onDisable(){
    29. System.out.println("[Rankup] Plugin Disabled!");
    30. }
    31. }

    Not work :(
    [​IMG]


    [​IMG]

    EDIT: add public final int COST= 100;
     
  6. Offline

    SmooshCakez

    Don't just copy paste code people put on here, it's usually pseudo code. Anyway, there is no getMoney() method in your class unless you import it or create it. This might help.
     
  7. Offline

    mine-care

    Out of topic but please don't use System.out.println(""); instead use getLogger().info("");
     
Thread Status:
Not open for further replies.

Share This Page