Tokens Shop

Discussion in 'Plugin Development' started by XgXXSnipz, Mar 22, 2014.

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

    XgXXSnipz

    I am having a plugin yml problem here is my error it says invalild plugin yml here is my plugin yml
    Code:
    name: MC-Tokens
    version: 1.0
    main: me.JoeyLangston.MCTokens.Shop
    description: A really cool eco system
    author: Joey Langston
     
    commands:
      tokens shop:
        description: open up
        aliases: [t-shop]
     
  2. Offline

    xTigerRebornx

  3. Offline

    Noxyro

    Your name as to be the same as your plugins main class (the one that extends JavaPlugin).
     
  4. Offline

    XgXXSnipz

    Oh nvm I got it

    But now i have a question every thing works fine just one thing, IM horrible with integers so this is why i need help here is my class files
    Code:java
    1. package me.JoeyLangston.MCTokens;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.configuration.file.FileConfiguration;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.inventory.Inventory;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.inventory.meta.ItemMeta;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class Shop extends JavaPlugin implements Listener {
    18.  
    19.  
    20. public static FileConfiguration config;
    21.  
    22. public static Shop plugin = null;
    23.  
    24. public void onEnable() {
    25. getServer().getPluginManager().registerEvents(new EventHandlers(), this);
    26.  
    27. config = getConfig();
    28.  
    29. plugin = this;
    30. }
    31.  
    32. public static void saveFile(){
    33. plugin.saveConfig();
    34. }
    35.  
    36. public ItemStack createItem(Material material, int amount, short shrt,
    37. String displayname, String lore) {
    38. ItemStack item = new ItemStack(material, amount, (short) shrt);
    39. ItemMeta meta = item.getItemMeta();
    40. meta.setDisplayName(displayname);
    41. ArrayList<String> Lore = new ArrayList<String>();
    42. Lore.add(lore);
    43. meta.setLore(Lore);
    44.  
    45. item.setItemMeta(meta);
    46. return item;
    47. }
    48.  
    49. static Inventory Shop;
    50. {
    51. Shop = Bukkit.createInventory(null, 9, "§0§nMC-Tokens Store");
    52.  
    53. Shop.setItem(0, createItem(Material.APPLE, 1, (short) 0, "§3GodApple","§fPrice §2 §fTokens"));
    54. }
    55.  
    56. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] a) {
    57. Player player = (Player) sender;
    58. if (cmd.getName().equalsIgnoreCase("token shop")) {
    59. player.openInventory(Shop);
    60. }
    61. return false;
    62. }
    63.  
    64. }
    65.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  5. You cant have a space in your command....
     
  6. Offline

    XgXXSnipz

    Here is second class
    Code:java
    1. package me.JoeyLangston.MCTokens;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.inventory.InventoryClickEvent;
    8. import org.bukkit.event.player.PlayerJoinEvent;
    9.  
    10. public class EventHandlers implements Listener {
    11.  
    12.  
    13.  
    14. //The place to buy and store player info
    15.  
    16. @EventHandler
    17. public void onJoin(PlayerJoinEvent e) {
    18. Player p = e.getPlayer();
    19.  
    20. if (!Shop.config.contains(p.getName())) {
    21. Shop.config.set(p.getName() + ".Tokens", 0);
    22. }
    23. }
    24.  
    25. @EventHandler
    26. public void onClick(InventoryClickEvent event) {
    27. Player p = (Player) event.getWhoClicked();
    28.  
    29. if (event.getInventory().getName().equals(Shop.Shop.getName())) {
    30. event.setCancelled(true);
    31.  
    32. if (event.getCurrentItem() == null) {
    33. return;
    34. }
    35.  
    36. if (!(event.getCurrentItem().hasItemMeta())) {
    37. return;
    38. }
    39.  
    40. if (event.getCurrentItem().getItemMeta().getDisplayName().equals("§3GodApple")) {
    41. if (MyAPI.hasEnough(p, 2)) {
    42. MyAPI.takeSilver(p, 2);
    43. p.sendMessage(ChatColor.YELLOW + "You bought the item GodApple for 2 coins");
    44.  
    45. } else {
    46. p.sendMessage("§cYou dont have enough :(!");
    47. }
    48. }
    49. }
    50. }
    51.  
    52. }
    53.  


    Last class file
    Code:java
    1. package me.JoeyLangston.MCTokens;
    2.  
    3. import org.bukkit.entity.Player;
    4.  
    5. public class MyAPI {
    6.  
    7.  
    8. //The config stuff and the money storage
    9.  
    10. public static void giveSilver(Player p, int i) {
    11. Shop.config.set(p.getName() + ".Tokens",
    12. Shop.config.getInt(p.getName() + ".Tokens", 0) + i);
    13. Shop.saveFile();
    14. p.sendMessage("§2§l$" + i + " tokens received!");
    15. }
    16.  
    17. public static void takeSilver(Player p, int i) {
    18. Shop.config.set(p.getName() + ".Tokens",
    19. Shop.config.getInt(p.getName() + ".Tokens", 0) - i);
    20. Shop.saveFile();
    21. p.sendMessage("§c§l$" + i + " tokens taken!");
    22. }
    23.  
    24. public static boolean hasEnough(Player p, int i) {
    25. if (Shop.config.getInt(p.getName() + ".Tokens") >= i)
    26. return true;
    27. return false;
    28. }
    29.  
    30. }
    31.  


    So my question is, in the MyAPI class how would I add another method so that you can give the money, and another one to check how much money you have

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

    Noxyro

    One tip for you: You should use a static HashMap to manage players money while they are on the server. This brings faster access times and the plugin doesn't need to access the config file everytime.

    To your question: You already implemented giveSilver() and takeSilver()? So, where is the problem in adding just another method getSilver()?
     
  8. Offline

    XgXXSnipz

    Noxyro Can you help

    Ya but I dont know how you would do that or to give silver

    This is what I have for check how much silver
    Code:java
    1. public static boolean getSilver(Player p, int i) {
    2. Shop.config.getInt(p.getName() + ".Tokens");
    3. return false;

    Think this will work

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

    Noxyro


    So, you just check if the player has true silver or false silver, but whatever you load from config it will always say false? ... interesting :p
     
  10. Offline

    XgXXSnipz

    Lol but will that work?
     
  11. Offline

    Noxyro


    It was a hint that you messed up...
    If you don't get what I meant with it, then you should overthink your Basic Bukkit API and Java knowledge.
     
    MrAwellstein likes this.
  12. Offline

    drtshock

    The FileConfiguration object he is accessing is saved in memory :)
     
  13. Offline

    Noxyro


    Oh, you're right ^^
    I just thought about the config file ... shame on me >.>
    But still, it's faster access and short code (in most cases) :)
     
  14. Offline

    XgXXSnipz

    OK so can you just tell me how to fix?

    Im so confused!!!!!
    Oh and how do I make it so you can give people tokens

    How can I make a command to give them tokens using my original code and not getting rid of the config

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

    Noxyro

    I was bored, so I rewrote your complete MyAPI to a MoneyAPI.

    PLEASE, don't just copy and paste it into your project without reading it or without the try of understanding what this API does at which point.


    I've created the commentary for a reason!

    Code:java
    1. import java.util.HashMap;
    2. import java.util.Map;
    3.  
    4. import org.bukkit.entity.Player;
    5.  
    6. /**
    7. * @author Noxyro
    8. * Written for XgXXSnipz as an answer to his topic in Bukkit forums.<br>
    9. * [URL]http://forums.bukkit.org/threads/247334/[/URL]
    10. *
    11. * This class can handle simple money amounts of players.<br>
    12. * Requires a valid FileConfiguration to work properly.
    13. *
    14. */
    15. public class MoneyAPI {
    16. private static Map<String, Integer> moneyMap;
    17.  
    18. /* Private access methods */
    19.  
    20. /**
    21.   * Gets the map with all loaded player money values.
    22.   * @return Map<String, Integer> moneyMap
    23.   */
    24. private static Map<String, Integer> getMoneyMap() {
    25. // Checks if the money map exists
    26. if (moneyMap == null) {
    27. // Creates a new HashMap, because there is no existing money map
    28. setMoneyMap(new HashMap<String, Integer>());
    29. }
    30.  
    31. return moneyMap;
    32. }
    33.  
    34. /**
    35.   * Sets and replaces the current map with all player money values.
    36.   * @param Map<String, Integer> moneyMap
    37.   */
    38. private static void setMoneyMap(Map<String, Integer> map) {
    39. moneyMap = map;
    40. }
    41.  
    42. /* Direct access methods */
    43.  
    44. /**
    45.   * Gets the money of a specific player by his name.<br>
    46.   * If the player is already in the money map it will get the players money out of the map.<br>
    47.   * If the player is not in the money map it will load his money map from the FileConfiguration.<br>
    48.   * <p>
    49.   * Returns 0 if an error occurred or no saved money value was found.
    50.   * @param String name
    51.   * @return Integer money
    52.   */
    53. public static int getMoney(String name) {
    54. // Checks if the name is already inside the money map
    55. if (getMoneyMap().containsKey(name)) {
    56. // Returns the money of the found player
    57. return getMoneyMap().get(name);
    58. // No entry was found
    59. } else {
    60. // Try and catch, because the FileConfiguration could be invalid or null
    61. try {
    62. // Checks if the FileConfiguration contains the given name
    63. if (Shop.config.getKeys(false).contains(name)) {
    64. // Checks if the name section contains a "Tokens" section
    65. if (Shop.config.getConfigurationSection(name).getKeys(false).contains("Tokens")) {
    66. // Get the tokens as integer from the FileConfiguration
    67. int tokens = Shop.config.getInt(name+".Tokens");
    68. // Sets and therefore creates an entry with the given name and tokens inside the money map
    69. setMoney(name, tokens);
    70. return tokens;
    71. // No "Tokens" section was found and therefore no tokens values is existent
    72. } else {
    73. // Sets and creates an entry with the given name and 0 tokens inside the money map
    74. setMoney(name, 0);
    75. // Creates the "Tokens" section inside the FileConfiguration
    76. Shop.config.set(name+".Tokens", 0);
    77. return 0;
    78. }
    79. // No section with the given name was found
    80. } else {
    81. // Sets and creates an entry with the given name and 0 tokens inside the money map
    82. setMoney(name, 0);
    83. // Creates the name and "Tokens" sections inside the FileConfiguration
    84. Shop.config.set(name+".Tokens", 0);
    85. return 0;
    86. }
    87. // Something went very wrong and the FileConfiguration or something else created an error
    88. } catch (Exception ex) {
    89. // Print error into console for debug reasons
    90. ex.printStackTrace();
    91. System.out.println("Invalid config data!");
    92. // Return 0 as last option
    93. return 0;
    94. }
    95. }
    96. }
    97.  
    98. /**
    99.   * Gets the money of a specific player.<br>
    100.   * If the player is already in the money map it will get the players money out of the map.<br>
    101.   * If the player is not in the money map it will load his money from the FileConfiguration and then put it into the money map.<br>
    102.   * <p>
    103.   * Returns 0 if an error occurred or no saved money value for the player was found inside the FileConfiguration.
    104.   * @param Player player
    105.   * @return Integer money
    106.   */
    107. public static int getMoney(Player player) {
    108. return getMoney(player.getName());
    109. }
    110.  
    111. /**
    112.   * Sets the money of a specific player by his name.
    113.   * @param String name
    114.   * @param Integer amount
    115.   */
    116. public static void setMoney(String name, int amount) {
    117. getMoneyMap().put(name, amount);
    118. }
    119.  
    120. /**
    121.   * Sets the money of a specific player.
    122.   * @param Player player
    123.   * @param Integer amount
    124.   */
    125. public static void setMoney(Player player, int amount) {
    126. setMoney(player.getName(), amount);
    127. }
    128.  
    129. /* Simple access methods */
    130.  
    131. /**
    132.   * Gives the specified player with the given name the given amount of money.
    133.   * <p>
    134.   * Automatically caps the money at Integer.MAX_VALUE.
    135.   * @param String name
    136.   * @param Integer amount
    137.   */
    138. public static void giveMoney(String name, int amount) {
    139. int money = getMoney(name) + amount;
    140. setMoney(name, (money <= Integer.MAX_VALUE ? money : Integer.MAX_VALUE));
    141. }
    142.  
    143. /**
    144.   * Gives the specified player the given amount of money.
    145.   * <p>
    146.   * Automatically caps the money at Integer.MAX_VALUE.
    147.   * @param Player player
    148.   * @param Integer amount
    149.   */
    150. public static void giveMoney(Player player, int amount) {
    151. int money = getMoney(player) + amount;
    152. setMoney(player, (money <= Integer.MAX_VALUE ? money : Integer.MAX_VALUE));
    153. }
    154.  
    155. /**
    156.   * Takes from the specified player with the given name the given amount of money.
    157.   * <p>
    158.   * Automatically caps the money at 0.
    159.   * @param String name
    160.   * @param Integer amount
    161.   */
    162. public static void takeMoney(String name, int amount) {
    163. int money = getMoney(name) - amount;
    164. setMoney(name, (money >= 0 ? money : 0));
    165. }
    166.  
    167. /**
    168.   * Takes from the specified player the given amount of money.
    169.   * <p>
    170.   * Automatically caps the money at 0.
    171.   * @param Player player
    172.   * @param Integer amount
    173.   */
    174. public static void takeMoney(Player player, int amount) {
    175. int money = getMoney(player) - amount;
    176. setMoney(player, (money >= 0 ? money : 0));
    177. }
    178. }


    Also, I don't guarantee that this is 100% flawless.

    XgXXSnipz I forgot to add a method that actually saves the values from the map to the config.

    Code:java
    1. /**
    2.   * Saves all money values from the money map to the FileConfiguration.
    3.   */
    4. public static void saveMoneyValues() {
    5. for (String name : getMoneyMap().keySet()) {
    6. Shop.config.set(name+".Tokens", getMoney(name));
    7. }
    8. }


    Add this somewhere to MoneyAPI.

    Also, I'm writting an example for your command part at the moment.

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

    XgXXSnipz

    Thx

    Thanks so much! So this class file has the ability to give and check how many tokens right, and this will work with my other class files right?

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

    Noxyro

    Here's the command part you asked for, again with complete commentary:

    Code:java
    1. // This is inside the main plugin class
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. // Check if sender is a player first
    4. if (sender instanceof Player) {
    5. // Cast sender to Player
    6. Player player = (Player) sender;
    7. // Check if the command was "tokens"
    8. if (cmd.getName().equalsIgnoreCase("tokens")) {
    9. // Check if no arguments are given
    10. if (args.length == 0) {
    11. // Return current owned tokens
    12. player.sendMessage("Your current tokens: " + MoneyAPI.getMoney(player));
    13. return true;
    14. // Check if 1 argument is given
    15. } else if (args.length == 1) {
    16. // Argument 1 is the name
    17. String name = args[0];
    18. // Get the player with the given name from server
    19. Player target = Bukkit.getServer().getPlayer(name);
    20. // Check if player is online
    21. if (target != null) {
    22. // Get the tokens of the player
    23. player.sendMessage(target.getDisplayName() + "'s current tokens: " + MoneyAPI.getMoney(target));
    24. return true;
    25. // The player with the given name is not online that means target == null
    26. } else {
    27. // Return that the player is currently not online
    28. player.sendMessage("The player " + name + " is currently not online!");
    29. return true;
    30. }
    31. // Check if 2 arguments are given
    32. } else if (args.length == 2) {
    33. player.sendMessage("You have to specify the amount of money!");
    34. return true;
    35. // Check if 3 arguments are given
    36. } else if (args.length >= 3) {
    37. // Try and catch here, because parsing could always return an invalid result and therefore an error
    38. try {
    39. // Parse an integer from argument 3
    40. int amount = Integer.parseInt(args[2]);
    41. // Check if the amount is not negative
    42. if (amount > 0) {
    43. // Check if the player has enough money
    44. if (MoneyAPI.getMoney(player) >= amount) {
    45. // Argument 2 is the name
    46. String name = args[1];
    47. // Get the player with the given name from the server
    48. Player target = Bukkit.getServer().getPlayer(name);
    49. // Check if the player is online
    50. if (target != null) {
    51. // Check if the sub-command "give" was used
    52. if (args[0].equalsIgnoreCase("give")) {
    53. // Take money from the player and give money to the target and send both a notification message
    54. MoneyAPI.takeMoney(player, amount);
    55. MoneyAPI.giveMoney(target, amount);
    56. player.sendMessage("You sent " + target.getName() + " an amount of " + amount + " tokens!");
    57. target.sendMessage("You received " + amount + " tokens from " + player.getName() + "!");
    58. return true;
    59. // Check if the sub-command "take" was used
    60. } else if (args[0].equalsIgnoreCase("take")) {
    61. // Check if the player has the permission to use this sub-command or is OP
    62. if (player.hasPermission("tokenshop.take") || player.isOp()) {
    63. // Take money from the target and give money to the player and send both a notification message
    64. MoneyAPI.giveMoney(player, amount);
    65. MoneyAPI.takeMoney(target, amount);
    66. player.sendMessage("You took" + amount + " tokens from " + player.getName() + "!");
    67. target.sendMessage(player.getName() + " took " + amount + " tokens from you!");
    68. return true;
    69. // The player has not the right permissions
    70. } else {
    71. target.sendMessage("You're missing the permission to take money from other!");
    72. return true;
    73. }
    74. }
    75. // The player with the given name is not online that means target == null
    76. } else {
    77. // Return that the player is currently not online
    78. player.sendMessage("The player " + name + " is currently not online!");
    79. return true;
    80. }
    81. // The player has not enough money
    82. } else {
    83. // Return that the player does not have enough money
    84. player.sendMessage("You don't have enough money! You're missing: " + (amount-MoneyAPI.getMoney(player)) + " tokens.");
    85. return true;
    86. }
    87. // The player typed in a negative amount or 0
    88. } else {
    89. // Return that the amount has to be higher than 0
    90. player.sendMessage("The money amount has to be higher than 0!");
    91. return true;
    92. }
    93. // The player typed in an invalid money amount
    94. } catch (Exception ex) {
    95. // Return that the money amount is invalid
    96. player.sendMessage("Invalid money amount!");
    97. return true;
    98. }
    99. }
    100. }
    101. }
    102.  
    103. // At this point the plugin just returns the command typed in into the chat, because something went pretty wrong!!!
    104. return false;
    105. }


    Have fun with it, and even more important: LEARN FROM IT!
     
  18. Offline

    XgXXSnipz

    Thx so much

    So I don't need MyAPI anymore?

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

    Noxyro

    No. I replaced it with MoneyAPI which I think is a more matching name for your needs.
     
  20. Offline

    XgXXSnipz

    Oh ok 1 more question (Wrong thread) on hypixels new mini game arena brawl how did he make floating blocks that didn't disappear and then add floating names to them?
     
  21. Offline

    Noxyro


    Stick to simple plugins first, please.
    But, he probably made FallingBlocks or custom Entities and modified them with NMS packets so that they have floating names above them.
     
  22. Offline

    XgXXSnipz

    Sorry to ask but whats the method for has money cause for my shop the player needs to have enough
     
  23. Offline

    Noxyro


    Add this to MoneyAPI:
    Code:java
    1. /**
    2.   * Checks if a player with the given name has the specified amount of money
    3.   * @return Boolean hasMoney
    4.   */
    5. public static boolean hasMoney(String name, int amount) {
    6. return (getMoney(name) >= amount);
    7. }
    8.  
    9. /**
    10.   * Checks if a player has the specified amount of money
    11.   * @return Boolean hasMoney
    12.   */
    13. public static boolean hasMoney(Player player, int amount) {
    14. return (getMoney(player) >= amount);
    15. }


    Now you can use:
    Code:java
    1. MoneyAPI.hasMoney(player, 25);
    2. // or
    3. MoneyAPI.hasMoney(playerName, 12);
     
  24. Offline

    XgXXSnipz

    thank you, your a real help

    OH AND ONE LAST THING WHAT WOULD BE THE COMMAND TO GIVE YOURSELF MONEY

    I now just though why not use the send command, well I mean like to give yourself Coins(for the staff)

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

    Noxyro


    Replace:
    Code:java
    1. // Check if the sub-command "give" was used
    2. if (args[0].equalsIgnoreCase("give")) {
    3. // Take money from the player and give money to the target and send both a notification message
    4. MoneyAPI.takeMoney(player, amount);
    5. MoneyAPI.giveMoney(target, amount);
    6. player.sendMessage("You sent " + target.getName() + " an amount of " + amount + " tokens!");
    7. target.sendMessage("You received " + amount + " tokens from " + player.getName() + "!");
    8. return true;
    9. // Check if the sub-command "take" was used
    10. } else if (args[0].equalsIgnoreCase("take")) {
    11. // Check if the player has the permission to use this sub-command or is OP
    12. if (player.hasPermission("tokenshop.take") || player.isOp()) {
    13. // Take money from the target and give money to the player and send both a notification message
    14. MoneyAPI.giveMoney(player, amount);
    15. MoneyAPI.takeMoney(target, amount);
    16. player.sendMessage("You took " + amount + " tokens from " + player.getName() + "!");
    17. target.sendMessage(player.getName() + " took " + amount + " tokens from you!");
    18. return true;
    19. // The player has not the right permissions
    20. } else {
    21. target.sendMessage("You're missing the permission to take money from others!");
    22. return true;
    23. }
    24. }

    With:
    Code:java
    1. // Check if the sub-command "give" was used
    2. if (args[0].equalsIgnoreCase("give")) {
    3. // Take money from the player and give money to the target and send both a notification message
    4. MoneyAPI.takeMoney(player, amount);
    5. MoneyAPI.giveMoney(target, amount);
    6. player.sendMessage("You sent " + target.getName() + " an amount of " + amount + " tokens!");
    7. target.sendMessage("You received " + amount + " tokens from " + player.getName() + "!");
    8. return true;
    9. // Check if the sub-command "take" was used
    10. } else if (args[0].equalsIgnoreCase("take")) {
    11. // Check if the player has the permission to use this sub-command or is OP
    12. if (player.hasPermission("tokenshop.take") || player.isOp()) {
    13. // Take money from the target and give money to the player and send both a notification message
    14. MoneyAPI.giveMoney(player, amount);
    15. MoneyAPI.takeMoney(target, amount);
    16. player.sendMessage("You took " + amount + " tokens from " + player.getName() + "!");
    17. target.sendMessage(player.getName() + " took " + amount + " tokens from you!");
    18. return true;
    19. // The player has not the right permissions
    20. } else {
    21. target.sendMessage("You're missing the permission to take money from others!");
    22. return true;
    23. }
    24. // Check if the sub-command "cheat" was used
    25. } else if (args[0].equalsIgnoreCase("cheat")) {
    26. // Check if the player has the permission to use this sub-command or is OP
    27. if (player.hasPermission("tokenshop.cheat") || player.isOp()) {
    28. // Give money to the player and send a notification message
    29. MoneyAPI.giveMoney(player, amount);
    30. // Check if the target is the player himself
    31. if (!target.getName().equalsIgnoreCase(player.getName())) {
    32. // Return a notification
    33. player.sendMessage("You cheated " + amount + " tokens for " + player.getName() + "!");
    34. return true;
    35. } else {
    36. // Return a notification
    37. player.sendMessage("You cheated " + amount + " tokens for yourself!");
    38. return true;
    39. }
    40. // The player has not the right permissions
    41. } else {
    42. target.sendMessage("You're missing the permission to cheat money!");
    43. return true;
    44. }
    45. // No valid sub-command was used
    46. } else {
    47. // Return that a valid sub-command must be used
    48. target.sendMessage("Please use a valid sub-command!");
    49. return true;
    50. }


    Then you can use "/tokens cheat XgXXSipz 42".
    Don't forget to change setup the command inside your plugins.yml
     
  26. Offline

    XgXXSnipz

    This is what I have will this work?
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. // Check if sender is a player first
    3. if (sender instanceof Player) {
    4. // Cast sender to Player
    5. Player player = (Player) sender;
    6. // Check if the command was "tokens"
    7. if (cmd.getName().equalsIgnoreCase("vt balance")) {
    8. // Check if no arguments are given
    9. if (args.length == 0) {
    10. // Return current owned tokens
    11. player.sendMessage("Your current tokens: " + MoneyAPI.getMoney(player));
    12. return true;
    13. // Check if 1 argument is given
    14.  
    15. }
    16. // Check if 2 arguments are given
    17. } else if (args.length == 2) {
    18. player.sendMessage("You have to specify the amount of money!");
    19. return true;
    20. // Check if 3 arguments are given
    21. } else if (args.length >= 3) {
    22. // Try and catch here, because parsing could always return an invalid result and therefore an error
    23. try {
    24. // Parse an integer from argument 3
    25. int amount = Integer.parseInt(args[2]);
    26. // Check if the amount is not negative
    27. if (amount > 0) {
    28. // Check if the player has enough money
    29. if (MoneyAPI.getMoney(player) >= amount) {
    30. // Argument 2 is the name
    31. String name = args[1];
    32. // Get the player with the given name from the server
    33. Player target = Bukkit.getServer().getPlayer(name);
    34. // Check if the player is online
    35. if (target != null) {
    36. // Check if the sub-command "give" was used
    37. if (args[0].equalsIgnoreCase("give")) {
    38. // Take money from the player and give money to the target and send both a notification message
    39. MoneyAPI.takeMoney(player, amount);
    40. MoneyAPI.giveMoney(target, amount);
    41. player.sendMessage("You sent " + target.getName() + " an amount of " + amount + " tokens!");
    42. target.sendMessage("You received " + amount + " tokens from " + player.getName() + "!");
    43. return true;
    44. // Check if the sub-command "take" was used
    45. } else if (args[0].equalsIgnoreCase("vt take")) {
    46. // Check if the player has the permission to use this sub-command or is OP
    47. if (player.hasPermission("vt.take")) {
    48. // Take money from the target and give money to the player and send both a notification message
    49. MoneyAPI.giveMoney(player, amount);
    50. MoneyAPI.takeMoney(target, amount);
    51. player.sendMessage("You took" + amount + " tokens from " + player.getName() + "!");
    52. target.sendMessage(player.getName() + " took " + amount + " tokens from you!");
    53. return true;
    54. // The player has not the right permissions
    55. } else {
    56. target.sendMessage(ChatColor.RED + "Im sorry you do not have the correct permission to use this command if you belive this is an error please contact the" + ChatColor.GREEN + "the Devolper or the " + ChatColor.AQUA + "The owner of the server: Croffskin");
    57. return true;
    58. }
    59. }
    60. // The player with the given name is not online that means target == null
    61. } else {
    62. // Return that the player is currently not online
    63. player.sendMessage("The player " + name + " is currently not online!");
    64. return true;
    65. }
    66. // The player has not enough money
    67. } else {
    68. // Return that the player does not have enough money
    69. player.sendMessage("You don't have enough money! You're missing: " + (amount-MoneyAPI.getMoney(player)) + " tokens.");
    70. return true;
    71. }
    72. // The player typed in a negative amount or 0
    73. } else {
    74. // Return that the amount has to be higher than 0
    75. player.sendMessage("The money amount has to be higher than 0!");
    76. return true;
    77. }
    78. // The player typed in an invalid money amount
    79. } catch (Exception ex) {
    80. // Return that the money amount is invalid
    81. player.sendMessage("Invalid money amount!");
    82. return true;
    83. }
    84. }
    85.  
    86.  
    87.  
    88.  
    89. int amount = Integer.parseInt(args[2]);
    90. // Check if the amount is not negative
    91. if (amount > 0) {
    92. // Check if the player has enough money
    93. if (MoneyAPI.getMoney(player) >= amount) {
    94. // Argument 2 is the name
    95. String name = args[1];
    96. // Get the player with the given name from the server
    97. Player target = Bukkit.getServer().getPlayer(name);
    98. // Check if the player is online
    99. if (target != null) {
    100.  
    101. }
    102. if (args[0].equalsIgnoreCase("vt give")) {
    103. if (player.hasPermission("vt.give")) {
    104. }
    105. MoneyAPI.giveMoney(target, amount);
    106. target.sendMessage("You received " + amount + " tokens from " + player.getName() + "!");
    107. } else {
    108. player.sendMessage(ChatColor.RED + "You do not have the right permission to give yourself tokens");
    109. }
    110.  
    111.  
    112. return true;
    113. }
    114.  
    115. }
    116. }
    117. return false;
    118. }
    119. }
     
  27. Offline

    Noxyro


    Please stick to the code I wrote just before and understand why I've created it that way.
    Also, you can't use spaces inside your command and/or your arguments, because every space creates a new argument.

    Here an example:

    Input: /tokenshop take XgXXSnipz 42
    Command: "tokenshop"
    Arguments: [0]: "take", [1]: "XgXXSnipz", [2]: "42"

    Input: /token shop vt take XgXXSnipz 42
    Command: "token"
    Arguments: [0]: "shop", [1]: "vt", [2]: "take", [3]: "XgXXSnipz", [4]: "42"
     
  28. Offline

    XgXXSnipz

    Im getting bracket errors on your code
     
  29. Offline

    Noxyro


    Here's the complete onCommand() method again:
    Code:java
    1. // This is inside the main plugin class
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. // Check if sender is a player first
    4. if (sender instanceof Player) {
    5. // Cast sender to Player
    6. Player player = (Player) sender;
    7. // Check if the command was "tokens"
    8. if (cmd.getName().equalsIgnoreCase("tokens")) {
    9. // Check if no arguments are given
    10. if (args.length == 0) {
    11. // Return current owned tokens
    12. player.sendMessage("Your current tokens: " + MoneyAPI.getMoney(player));
    13. return true;
    14. // Check if 1 argument is given
    15. } else if (args.length == 1) {
    16. // Argument 1 is the name
    17. String name = args[0];
    18. // Get the player with the given name from server
    19. Player target = Bukkit.getServer().getPlayer(name);
    20. // Check if player is online
    21. if (target != null) {
    22. // Get the tokens of the player
    23. player.sendMessage(target.getDisplayName() + "'s current tokens: " + MoneyAPI.getMoney(target));
    24. return true;
    25. // The player with the given name is not online that means target == null
    26. } else {
    27. // Return that the player is currently not online
    28. player.sendMessage("The player " + name + " is currently not online!");
    29. return true;
    30. }
    31. // Check if 2 arguments are given
    32. } else if (args.length == 2) {
    33. player.sendMessage("You have to specify the amount of money!");
    34. return true;
    35. // Check if 3 arguments are given
    36. } else if (args.length >= 3) {
    37. // Try and catch here, because parsing could always return an invalid result and therefore an error
    38. try {
    39. // Parse an integer from argument 3
    40. int amount = Integer.parseInt(args[2]);
    41. // Check if the amount is not negative
    42. if (amount > 0) {
    43. // Check if the player has enough money
    44. if (MoneyAPI.getMoney(player) >= amount) {
    45. // Argument 2 is the name
    46. String name = args[1];
    47. // Get the player with the given name from the server
    48. Player target = Bukkit.getServer().getPlayer(name);
    49. // Check if the player is online
    50. if (target != null) {
    51. // Check if the sub-command "give" was used
    52. if (args[0].equalsIgnoreCase("give")) {
    53. // Take money from the player and give money to the target and send both a notification message
    54. MoneyAPI.takeMoney(player, amount);
    55. MoneyAPI.giveMoney(target, amount);
    56. player.sendMessage("You sent " + target.getName() + " an amount of " + amount + " tokens!");
    57. target.sendMessage("You received " + amount + " tokens from " + player.getName() + "!");
    58. return true;
    59. // Check if the sub-command "take" was used
    60. } else if (args[0].equalsIgnoreCase("take")) {
    61. // Check if the player has the permission to use this sub-command or is OP
    62. if (player.hasPermission("tokenshop.take") || player.isOp()) {
    63. // Take money from the target and give money to the player and send both a notification message
    64. MoneyAPI.takeMoney(target, amount);
    65. player.sendMessage("You took " + amount + " tokens from " + player.getName() + "!");
    66. target.sendMessage(player.getName() + " took " + amount + " tokens from you!");
    67. return true;
    68. // The player has not the right permissions
    69. } else {
    70. target.sendMessage("You're missing the permission to take money from others!");
    71. return true;
    72. }
    73. // Check if the sub-command "cheat" was used
    74. } else if (args[0].equalsIgnoreCase("cheat")) {
    75. // Check if the player has the permission to use this sub-command or is OP
    76. if (player.hasPermission("tokenshop.cheat") || player.isOp()) {
    77. // Give money to the player and send a notification message
    78. MoneyAPI.giveMoney(player, amount);
    79. // Check if the target is the player himself
    80. if (!target.getName().equalsIgnoreCase(player.getName())) {
    81. // Return a notification
    82. player.sendMessage("You cheated " + amount + " tokens for " + player.getName() + "!");
    83. return true;
    84. } else {
    85. // Return a notification
    86. player.sendMessage("You cheated " + amount + " tokens for yourself!");
    87. return true;
    88. }
    89. // The player has not the right permissions
    90. } else {
    91. target.sendMessage("You're missing the permission to cheat money!");
    92. return true;
    93. }
    94. // No valid sub-command was used
    95. } else {
    96. // Return that a valid sub-command must be used
    97. target.sendMessage("Please use a valid sub-command!");
    98. return true;
    99. }
    100. // The player with the given name is not online that means target == null
    101. } else {
    102. // Return that the player is currently not online
    103. player.sendMessage("The player " + name + " is currently not online!");
    104. return true;
    105. }
    106. // The player has not enough money
    107. } else {
    108. // Return that the player does not have enough money
    109. player.sendMessage("You don't have enough money! You're missing: " + (amount-MoneyAPI.getMoney(player)) + " tokens.");
    110. return true;
    111. }
    112. // The player typed in a negative amount or 0
    113. } else {
    114. // Return that the amount has to be higher than 0
    115. player.sendMessage("The money amount has to be higher than 0!");
    116. return true;
    117. }
    118. // The player typed in an invalid money amount
    119. } catch (Exception ex) {
    120. // Return that the money amount is invalid
    121. player.sendMessage("Invalid money amount!");
    122. return true;
    123. }
    124. }
    125. }
    126. }
    127.  
    128. // At this point the plugin just returns the command typed in into the chat, because something went pretty wrong!!!
    129. return false;
    130. }


    And here as list of usage from I wrote to you:

    MoneyAPI:
    .getMoney(player) - Returns the money of a player
    .setMoney(player) - Sets the money of a player
    .takeMoney(player, amount) - Takes a specific amount of money from the player
    .giveMoney(player, amount) - Gives a specific amount of money to the player
    .hasMoney(player, amount) - Checks if the player has the specified amount of money
    .saveMoneyValues() - IMPORTANT! Call this in your onDisable()!

    Commands:
    IMPORTANT! Setup "tokens" as command in your plugin.yml!
    /tokens - Shows your current money
    /tokens <name> - Show the money of the given player (only if he's onliney)
    /tokens give <name> <amount> - Gives an amount of money that is taken from you to another player
    /tokens take <name> <amount> - Takes an amount of money from a player (needs Permission or OP)
    /tokens cheat <name> <amount> - Cheats an amount of money for a player
     
  30. Offline

    XgXXSnipz

    dude i am so mad i get freaking bracket errors please re-write this for me one more time, i am so sorry for this trouble
    Code:java
    1. package me.JoeyLangston.MCTokens;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.configuration.file.FileConfiguration;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.inventory.Inventory;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.meta.ItemMeta;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class Shop extends JavaPlugin implements Listener {
    19.  
    20.  
    21. //The class that stores prices and items
    22.  
    23. public static FileConfiguration config;
    24.  
    25. public static Shop plugin = null;
    26.  
    27. public void onEnable() {
    28. getServer().getPluginManager().registerEvents(new EventHandlers(), this);
    29.  
    30. config = getConfig();
    31.  
    32. plugin = this;
    33. }
    34.  
    35. public static void saveFile(){
    36. plugin.saveConfig();
    37. }
    38.  
    39. public ItemStack createItem(Material material, int amount, short shrt,
    40. String displayname, String lore) {
    41. ItemStack item = new ItemStack(material, amount, (short) shrt);
    42. ItemMeta meta = item.getItemMeta();
    43. meta.setDisplayName(displayname);
    44. ArrayList<String> Lore = new ArrayList<String>();
    45. Lore.add(lore);
    46. meta.setLore(Lore);
    47.  
    48. item.setItemMeta(meta);
    49. return item;
    50. }
    51.  
    52. static Inventory Shop;
    53.  
    54. {
    55.  
    56.  
    57. Shop = Bukkit.createInventory(null, 9, "§2§lVaultCraft Tokens");
    58.  
    59. Shop.setItem(0, createItem(Material.GOLDEN_APPLE, 1, (short) 0, "§3GodApple","§fPrice §62 §fTokens"));
    60.  
    61.  
    62. }
    63. }
    64.  
    65.  
    66. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    67. // Check if sender is a player first
    68. if (sender instanceof Player) {
    69. // Cast sender to Player
    70. Player player = (Player) sender;
    71. // Check if the command was "tokens"
    72. if (cmd.getName().equalsIgnoreCase("tokens")) {
    73. // Check if no arguments are given
    74. if (args.length == 0) {
    75. // Return current owned tokens
    76. player.sendMessage("Your current tokens: " + MoneyAPI.getMoney(player));
    77. return true;
    78. // Check if 1 argument is given
    79. } else if (args.length == 1) {
    80. // Argument 1 is the name
    81. String name = args[0];
    82. // Get the player with the given name from server
    83. Player target = Bukkit.getServer().getPlayer(name);
    84. // Check if player is online
    85. if (target != null) {
    86. // Get the tokens of the player
    87. player.sendMessage(target.getDisplayName() + "'s current tokens: " + MoneyAPI.getMoney(target));
    88. return true;
    89. // The player with the given name is not online that means target == null
    90. } else {
    91. // Return that the player is currently not online
    92. player.sendMessage("The player " + name + " is currently not online!");
    93. return true;
    94. }
    95. // Check if 2 arguments are given
    96. } else if (args.length == 2) {
    97. player.sendMessage("You have to specify the amount of money!");
    98. return true;
    99. // Check if 3 arguments are given
    100. } else if (args.length >= 3) {
    101. // Try and catch here, because parsing could always return an invalid result and therefore an error
    102. try {
    103. // Parse an integer from argument 3
    104. int amount = Integer.parseInt(args[2]);
    105. // Check if the amount is not negative
    106. if (amount > 0) {
    107. // Check if the player has enough money
    108. if (MoneyAPI.getMoney(player) >= amount) {
    109. // Argument 2 is the name
    110. String name = args[1];
    111. // Get the player with the given name from the server
    112. Player target = Bukkit.getServer().getPlayer(name);
    113. // Check if the player is online
    114. if (target != null) {
    115. // Check if the sub-command "give" was used
    116. if (args[0].equalsIgnoreCase("send")) {
    117. // Take money from the player and give money to the target and send both a notification message
    118. MoneyAPI.takeMoney(player, amount);
    119. MoneyAPI.giveMoney(target, amount);
    120. player.sendMessage("You sent " + target.getName() + " an amount of " + amount + " tokens!");
    121. target.sendMessage("You received " + amount + " tokens from " + player.getName() + "!");
    122. return true;
    123. // Check if the sub-command "take" was used
    124. } else if (args[0].equalsIgnoreCase("take")) {
    125. // Check if the player has the permission to use this sub-command or is OP
    126. if (player.hasPermission("vt.take")) {
    127. // Take money from the target and give money to the player and send both a notification message
    128. MoneyAPI.giveMoney(player, amount);
    129. MoneyAPI.takeMoney(target, amount);
    130. player.sendMessage("You took " + amount + " tokens from " + player.getName() + "!");
    131. target.sendMessage(player.getName() + " took " + amount + " tokens from you!");
    132. return true;
    133. // The player has not the right permissions
    134. } else {
    135. target.sendMessage("You're missing the permission to take money from others!");
    136. return true;
    137. }
    138. // Check if the sub-command "cheat" was used
    139. } else if (args[0].equalsIgnoreCase("give")) {
    140. // Check if the player has the permission to use this sub-command or is OP
    141. if (player.hasPermission("vt.give") || player.isOp()) {
    142. // Give money to the player and send a notification message
    143. MoneyAPI.giveMoney(player, amount);
    144. // Check if the target is the player himself
    145. if (!target.getName().equalsIgnoreCase(player.getName())) {
    146. // Return a notification
    147. player.sendMessage("You cheated " + amount + " tokens for " + player.getName() + "!");
    148. return true;
    149. } else {
    150. // Return a notification
    151. player.sendMessage("You gave " + amount + " tokens for yourself!");
    152. return true;
    153. }
    154. // The player has not the right permissions
    155. } else {
    156. target.sendMessage("You're missing the permission to cheat money!");
    157. return true;
    158. }
    159. // No valid sub-command was used
    160. } else {
    161. // Return that a valid sub-command must be used
    162. target.sendMessage("Please use a valid sub-command!");
    163. return true;
    164. }
    165. }
    166. }
    167. }
    168. }
    169. }
    170. }
    171.  
    172.  
    173.  


    wait nvm

    how would you do the plugin yml, i dont know how to do that when there is multiple commands

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

Share This Page