Help making commands.

Discussion in 'Plugin Development' started by iCancer, Nov 19, 2016.

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


    So, I'm trying to make a betting plugin that allows the user to challenge his target to a bet. I want the target to be able to type /accept or /decline, and depending on which one, move forward. How would I do this??

    public boolean onCommand(CommandSender senderCommand commandString labelString[] args) {
    Player player = (Playersender;
             * Betting Command
    if (command.getName().equalsIgnoreCase("bet")) {
                if (!(
    sender instanceof Player)) {
    sender.sendMessage(consolePlugin "Invalid Permissions!!");
                if (
    args.length == || args.length 2) {
    player.sendMessage(plugin ChatColor.RED "Usage: " command.getUsage());
                for (
    Player target Bukkit.getServer().getOnlinePlayers()) {
                    if (
    target.getName().equalsIgnoreCase(args[0])) {
    target.sendMessage(plugin ChatColor.GREEN sender " wants to bet with you!!");
                        if (
    /*If target executes /accept*/) {
  2. Offline


    What does "move forward" mean?

    This may cause a ClassCastException if the console uses it. check "if sender instanceof player"

    We'd need more information about the "bet."

    Just create another CommandExecutor (or if its in your onEnable, check cmd.getName())
  3. Offline


    @ipodtouch0218 @iCancer
    To test for if the player sends/accept or /decline, you will need to register those two commands in the plugin.yml. After that, do the same thing you did for the "bet" command and either accept or decline the request.
  4. Offline



    How would I then test the command?? Could you give an example??
  5. Offline


    It is literally the same as your "bet" command. Copy that first if statement and replace "bet" with "accept"
  6. Offline


    Where would I put it in the code block??

    Do I replace >>>

    if (/*If target executes /accept*/)
    with the command /accept?

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


    No, you don't replace the if (/*target executes /accept */) you create a new if (cmd.getName().equals("accept") and then run the accept. The same for declinations. I've rearranged the code to somewhat fix what @ipodtouch0218 said, but since I'm not going to completely spoonfeed you you'll have to figure out how the accept/declination works with the base here. Use something like:

    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. if (!(sender instanceof Player)) {
    4. return;
    5. }
    7. Player p = (Player) sender;
    8. Player target = null;
    10. if (cmd.getName().equalsIgnoreCase("bet")) {
    11. if (args.length != 1) {
    12. p.sendMessage("Player not specified! Usage: " + command.getUsage());
    13. return false;
    14. }
    15. for (Player ps : Bukkit.getServer().getOnlinePlayers()) {
    16. if (args[0].equalsIgnoreCase(ps)) {
    17. target = args[0];
    18. return true;
    19. } else {
    20. p.sendMessage("Player not valid!");
    21. return false;
    22. }
    23. return false;
    24. }
    26. if (cmd.getName().equalsIgnoreCase("accept") {
    28. }
    30. if (cmd.getName().equalsIgnoreCase("decline") {
    31. // Tell the player that the target declined the trade.
    32. }
    33. }

    E: Apologies if there's any incorrect method names or typos, I'm typing directly into Bukkit.
    Last edited: Nov 19, 2016
  8. Offline


    So, how would I tie >>>
    1. if (cmd.getName().equalsIgnoreCase("bet")) {
    2. if (args.length != 1) {
    3. p.sendMessage("Player not specified! Usage: " + command.getUsage());
    4. return false;
    5. }
    6. for (Player ps : Bukkit.getServer().getOnlinePlayers()) {
    7. if (args[0].equalsIgnoreCase(ps)) {
    8. target = args[0];
    9. return true;
    10. } else {
    11. p.sendMessage("Player not valid!");
    12. return false;
    13. }
    14. return false;
    15. }

    ======== to ========

    1. if (cmd.getName().equalsIgnoreCase("accept")) {
    3. }

    I want it to be kind of like the /tpa and /tpaccept commands to where, when the player asks another player for a bet, they either do /accept of /decline. I don't see how I could do that. Could you give me an example?
  9. Offline


    Create a list of all players who are being challenged, then check if that player is in the list.
  10. Offline


    I've changed the way I am going to do it, sorry for the trouble. How would I make args[0] an integer, the amount they want to bet?
  11. Offline



    Just make sure that args[0] exists, and add a try-catch statement around this line, as there is a potential that the player may input another bit of text (for example, a name or "a")
Thread Status:
Not open for further replies.

Share This Page