[HELP] Poke Plugin!

Discussion in 'Plugin Development' started by clutchmasterman, Nov 2, 2013.

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

    clutchmasterman

    im making a plugin similar to facebook's poking system.

    Code:java
    1. public void onEnable() {
    2. getLogger().info("Hey Dood");
    3. }
    4.  
    5. public void onDisable() {
    6. getLogger().info("Bye Dood");
    7. }
    8.  
    9. public boolean onCommand(CommandSender sender, Command cmd, String command, String[] args){
    10. if(sender instanceof Player) {
    11. Player p = (Player) sender;
    12. if(command.equalsIgnoreCase("poke")){
    13. if(args.length == 0){
    14. p.sendMessage("----------------" + ChatColor.GREEN + " Poke Help " + ChatColor.WHITE + "----------------");
    15. p.sendMessage(ChatColor.AQUA + "/Poke" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Shows All Poke Commands!");
    16. p.sendMessage(ChatColor.AQUA + "/Poke <Name>" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Pokes A Player!");
    17. p.sendMessage(ChatColor.AQUA + "/Poke Check" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Checks How Many Pokes You Have!");
    18. p.sendMessage(ChatColor.AQUA + "/Poke Back <Name>" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Pokes Players Back!");
    19. p.sendMessage("------------------------------------------");
    20. }
    21.  
    22.  
    23.  
    24. }if(args.length == 1){
    25. if(p.getServer().getPlayer(args[0]) != null){
    26. Player tp = p.getServer().getPlayer(args[0]);
    27. if(sender instanceof Player) {
    28. counter++;
    29. p.sendMessage(ChatColor.GREEN + "Poked " + ChatColor.GOLD + tp.getName() + ChatColor.GREEN + "!");
    30. tp.sendMessage(ChatColor.GOLD + p.getName() + ChatColor.GREEN + " Has Poked You!");
    31. tp.sendMessage(ChatColor.GREEN + "Do " + ChatColor.AQUA + "/Poke Back <Name> " + ChatColor.GREEN + "To Poke Him Back!");
    32. }
    33.  
    34.  
    35.  
    36. }if(args[0].equalsIgnoreCase("check")){
    37. if(counter == 0){
    38. p.sendMessage(ChatColor.GREEN + "Nobody Has Poked You!");
    39. }else{
    40. p.sendMessage(ChatColor.GREEN + "There Are " + ChatColor.GOLD + counter + ChatColor.GREEN + " Player('s) That Poked You!");
    41. }
    42.  
    43.  
    44.  
    45. }else if(args[0].equalsIgnoreCase("back")){
    46. if(p.getServer().getPlayer(args[0]) != null){
    47. Player tp = p.getServer().getPlayer(args[0]);
    48. if(counter == 0){
    49. p.sendMessage(ChatColor.GOLD + tp.getName() + ChatColor.GREEN + "No One Poked You!");
    50. }else{
    51. counter++;
    52. tp.sendMessage(ChatColor.GOLD + p.getName() + ChatColor.GREEN + " Has Poked You!");
    53. tp.sendMessage(ChatColor.GREEN + "Do " + ChatColor.AQUA + "/Poke Back <Name> " + ChatColor.GREEN + "To Poke Him Back!");
    54. }
    55.  
    56.  
    57.  
    58.  
    59. }
    60. }
    61. }
    62. }
    63. return false;
    64. }
    65. }


    But when a player tries to poke a player back using /poke back <name> it doesnt do anything. plz help!
     
  2. Offline

    Tss1410

    you forgot if(args.length==2){
     
  3. Offline

    finalblade1234

    In your onEnable() add getCommand("commandname").setExecutor(this);
    Also define it in your plugin.yml
    And your onCommand had alot of syntax problems... but here's the fixed version
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String command, String[] args){
    2. if(sender instanceof Player) {
    3. Player p = (Player) sender;
    4. if(command.equalsIgnoreCase("poke")){
    5. if(args.length == 0){
    6. p.sendMessage("----------------" + ChatColor.GREEN + " Poke Help " + ChatColor.WHITE + "----------------");
    7. p.sendMessage(ChatColor.AQUA + "/Poke" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Shows All Poke Commands!");
    8. p.sendMessage(ChatColor.AQUA + "/Poke <Name>" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Pokes A Player!");
    9. p.sendMessage(ChatColor.AQUA + "/Poke Check" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Checks How Many Pokes You Have!");
    10. p.sendMessage(ChatColor.AQUA + "/Poke Back <Name>" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Pokes Players Back!");
    11. p.sendMessage("------------------------------------------");
    12. }else if(args.length == 1){
    13. if(p.getServer().getPlayer(args[0]) != null){
    14. Player tp = p.getServer().getPlayer(args[0]);
    15. if(sender instanceof Player) {
    16. counter++;
    17. p.sendMessage(ChatColor.GREEN + "Poked " + ChatColor.GOLD + tp.getName() + ChatColor.GREEN + "!");
    18. tp.sendMessage(ChatColor.GOLD + p.getName() + ChatColor.GREEN + " Has Poked You!");
    19. tp.sendMessage(ChatColor.GREEN + "Do " + ChatColor.AQUA + "/Poke Back <Name> " + ChatColor.GREEN + "To Poke Him Back!");
    20. }
    21. }
    22. }
    23.  
    24.  
    25.  
    26. }else if(args[0].equalsIgnoreCase("check")){
    27. if(counter == 0){
    28. p.sendMessage(ChatColor.GREEN + "Nobody Has Poked You!");
    29. }else{
    30. p.sendMessage(ChatColor.GREEN + "There Are " + ChatColor.GOLD + counter + ChatColor.GREEN + " Player('s) That Poked You!");
    31. }
    32.  
    33.  
    34.  
    35. }else if(args[0].equalsIgnoreCase("back")){
    36. if(p.getServer().getPlayer(args[0]) != null){
    37. Player tp = p.getServer().getPlayer(args[0]);
    38. if(counter == 0){
    39. p.sendMessage(ChatColor.GOLD + tp.getName() + ChatColor.GREEN + "No One Poked You!");
    40. }else{
    41. counter++;
    42. tp.sendMessage(ChatColor.GOLD + p.getName() + ChatColor.GREEN + " Has Poked You!");
    43. tp.sendMessage(ChatColor.GREEN + "Do " + ChatColor.AQUA + "/Poke Back <Name> " + ChatColor.GREEN + "To Poke Him Back!");
    44. }
    45.  
    46.  
    47.  
    48.  
    49. }
    50. }
    51. return true;
    52. }
    53. return false;
    54. }
     
  4. Offline

    clutchmasterman

    ok, finalblade1234 i Fixed all my onCommand errors so now it looks like this:

    Code:java
    1. package rateme;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class rateme extends JavaPlugin{
    10. public static rateme plugin;
    11. int counter;
    12.  
    13. public void onEnable() {
    14. getCommand("poke").setExecutor(this);
    15. getLogger().info("Hey Dood");
    16. }
    17.  
    18. public void onDisable() {
    19. getLogger().info("Bye Dood");
    20. }
    21.  
    22. public boolean onCommand(CommandSender sender, Command cmd, String command, String[] args){
    23. if(sender instanceof Player) {
    24. Player p = (Player) sender;
    25. if(command.equalsIgnoreCase("poke")){
    26. if(args.length == 0){
    27. p.sendMessage("----------------" + ChatColor.GREEN + " Poke Help " + ChatColor.WHITE + "----------------");
    28. p.sendMessage(ChatColor.AQUA + "/Poke" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Shows All Poke Commands!");
    29. p.sendMessage(ChatColor.AQUA + "/Poke <Name>" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Pokes A Player!");
    30. p.sendMessage(ChatColor.AQUA + "/Poke Check" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Checks How Many Pokes You Have!");
    31. p.sendMessage(ChatColor.AQUA + "/Poke Back <Name>" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Pokes Players Back!");
    32. p.sendMessage("------------------------------------------");
    33. }else if(args.length == 1){
    34. if(p.getServer().getPlayer(args[0]) != null){
    35. Player tp = p.getServer().getPlayer(args[0]);
    36. if(sender instanceof Player) {
    37. counter++;
    38. p.sendMessage(ChatColor.GREEN + "Poked " + ChatColor.GOLD + tp.getName() + ChatColor.GREEN + "!");
    39. tp.sendMessage(ChatColor.GOLD + p.getName() + ChatColor.GREEN + " Has Poked You!");
    40. tp.sendMessage(ChatColor.GREEN + "Do " + ChatColor.AQUA + "/Poke Back <Name> " + ChatColor.GREEN + "To Poke Him Back!");
    41. }
    42. }
    43. }
    44.  
    45.  
    46.  
    47. }else if(args[0].equalsIgnoreCase("check")){
    48. if(counter == 0){
    49. p.sendMessage(ChatColor.GREEN + "Nobody Has Poked You!");
    50. }else{
    51. p.sendMessage(ChatColor.GREEN + "There Are " + ChatColor.GOLD + counter + ChatColor.GREEN + " Player('s) That Poked You!");
    52. }
    53.  
    54.  
    55.  
    56. }else if(args[0].equalsIgnoreCase("back")){
    57. if(p.getServer().getPlayer(args[0]) != null){
    58. Player tp = p.getServer().getPlayer(args[0]);
    59. if(counter == 0){
    60. p.sendMessage(ChatColor.GOLD + tp.getName() + ChatColor.GREEN + "No One Poked You!");
    61. }else{
    62. counter++;
    63. tp.sendMessage(ChatColor.GOLD + p.getName() + ChatColor.GREEN + " Has Poked You!");
    64. tp.sendMessage(ChatColor.GREEN + "Do " + ChatColor.AQUA + "/Poke Back <Name> " + ChatColor.GREEN + "To Poke Him Back!");
    65. }
    66.  
    67.  
    68.  
    69.  
    70. }
    71. }
    72. return true;
    73. }
    74. return false;
    75. }}


    but i dont know how to define it in my plugin.yml:

    Code:
    name: Poke
    author: Lemond
    version: 1.0
    description: poke like facebook!
    main: rateme.rateme
    commands:
      poke:
        description: pokes like FB!
    On what Line?

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

    1Rogue

    You check if args[0] is "back", and then get a player using args[0]
     
  6. Offline

    clutchmasterman

    it, is?
     
  7. Offline

    1Rogue

    Read your code
     
  8. Offline

    clutchmasterman

    yeah on "back" args are = to 0 unless you mean something else?
    what do you mean exactly?
     
  9. Offline

    1Rogue

    Lines 35 & 36 of your onCommand
     
  10. Offline

    clutchmasterman

    so like that:
    Code:java
    1. }else if(args.length == 2){
    2. if(p.getServer().getPlayer(args[1]) != null){
    3. Player tp = p.getServer().getPlayer(args[1]);
    4. if(sender instanceof Player) {
    5. counter++;
    6. p.sendMessage(ChatColor.GREEN + "Poked " + ChatColor.GOLD + tp.getName() + ChatColor.GREEN + "!");
    7. tp.sendMessage(ChatColor.GOLD + p.getName() + ChatColor.GREEN + " Has Poked You!");
    8. tp.sendMessage(ChatColor.GREEN + "Do " + ChatColor.AQUA + "/Poke Back <Name> " + ChatColor.GREEN + "To Poke Him Back!");
     
  11. Offline

    maxben34

    Just so you know, you don't need an onDisable method. (This won't fix anything however). In your plugin.yml make sure that you reference every command underneath the commands: part of the yml.
    If you don't add the command into the yml then it won't work properly on your server.
     
  12. Offline

    clutchmasterman

    very sorry if im being a noob right now but it this ok?:

    Code:
    name: Poke
    author: Lemond
    version: 1.1.0
    description: poke like facebook!
    main: rateme.rateme
    commands:
      poke:
        description: Shows All Poke Commands!
      poke check:
        description: Checks How Many Pokes You Have!
      poke back:
        description: Pokes Players Back!
     
  13. Offline

    maxben34

    Actually... I thought that they weren't args of the command. I'm not the best at using commands with args... but if you want, you can use commands like ./check and ./back and i'm sure you'll have a much easier time.
     
  14. Offline

    clutchmasterman

    well to poke someone I need to have multiple args....
     
  15. Offline

    maxben34

    Not necessarily. Now... im only giving you this solution because I don't know how to use multiple args... but you can check if args.length == 0 for each command and instead of having the command be an argument of ./poke... you can have each command as separate from the poke command... if that makes any sense .
     
  16. Offline

    clutchmasterman

    ok but now i have to override "back" form the essentials plugin plus it wouldnt make much sense to say "back (Player name)"

    and sooner or later i have to get familier with args

    but thats cool bro, if you cant help you cant help :p

    Anyone else out there in the coding world that can help?

    Code:java
    1. package rateme;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class rateme extends JavaPlugin{
    10. public static rateme plugin;
    11. int counter;
    12.  
    13. public void onEnable() {
    14. getCommand("poke").setExecutor(this);
    15. getConfig().options().copyDefaults(true);
    16. saveConfig();
    17. getLogger().info("Hey Dood");
    18. }
    19.  
    20. public void onDisable() {
    21. saveConfig();
    22. getLogger().info("Bye Dood");
    23. }
    24.  
    25. public boolean onCommand(CommandSender sender, Command cmd, String command, String[] args){
    26. if(sender instanceof Player) {
    27. Player p = (Player) sender;
    28. if(command.equalsIgnoreCase("poke")){
    29. if(args.length == 0){
    30. p.sendMessage("----------------" + ChatColor.GREEN + " Poke Help " + ChatColor.WHITE + "----------------");
    31. p.sendMessage(ChatColor.AQUA + "/Poke" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Shows All Poke Commands!");
    32. p.sendMessage(ChatColor.AQUA + "/Poke <Name>" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Pokes A Player!");
    33. p.sendMessage(ChatColor.AQUA + "/Poke Check" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Checks How Many Pokes You Have!");
    34. p.sendMessage(ChatColor.AQUA + "/Poke Back <Name>" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Pokes Players Back!");
    35. p.sendMessage("------------------------------------------");
    36. }
    37. }if(args.length == 1){
    38. if(p.getServer().getPlayer(args[0]) != null){
    39. Player tp = p.getServer().getPlayer(args[0]);
    40. if(sender instanceof Player) {
    41. counter++;
    42. p.sendMessage(ChatColor.GREEN + "Poked " + ChatColor.GOLD + tp.getName() + ChatColor.GREEN + "!");
    43. tp.sendMessage(ChatColor.GOLD + p.getName() + ChatColor.GREEN + " Has Poked You!");
    44. tp.sendMessage(ChatColor.GREEN + "Do " + ChatColor.AQUA + "/Poke Back <Name> " + ChatColor.GREEN + "To Poke Him Back!");
    45. }
    46.  
    47.  
    48.  
    49.  
    50. }else if(args[0].equalsIgnoreCase("check")){
    51. if(args.length == 1){
    52. if(counter == 0){
    53. p.sendMessage(ChatColor.GREEN + "Nobody Has Poked You!");
    54. }else{
    55. p.sendMessage(ChatColor.GREEN + "There Are " + ChatColor.GOLD + counter + ChatColor.GREEN + " Player('s) That Poked You!");
    56. }
    57.  
    58.  
    59.  
    60. }else if(args[0].equalsIgnoreCase("back")){
    61. }if(args.length == 2){
    62. if(p.getServer().getPlayer(args[1]) != null){
    63. Player tp = p.getServer().getPlayer(args[0]);
    64. if(counter == 0){
    65. p.sendMessage(ChatColor.GOLD + tp.getName() + ChatColor.GREEN + "No One Poked You!");
    66. }else{
    67. counter++;
    68. tp.sendMessage(ChatColor.GOLD + p.getName() + ChatColor.GREEN + " Has Poked You!");
    69. tp.sendMessage(ChatColor.GREEN + "Do " + ChatColor.AQUA + "/Poke Back <Name> " + ChatColor.GREEN + "To Poke Him Back!");
    70. }
    71. }
    72. }
    73. }
    74. }
    75. }
    76. return false;
    77. }
    78. }


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

    ceoepts

    First of all the int counter is not set for all players it is set for just this java project. This means you have to create another soulution like a HashMap. if a player on your server is named check or back that means that noone will be able to use the other commands. xD think i just found a major error the part
    Code:
                        }else if(args[0].equalsIgnoreCase("back")){
                        }if(args.length == 2){
                            if(p.getServer().getPlayer(args[1]) != null){
                                Player tp = p.getServer().getPlayer(args[0]);
                                if(counter == 0){
                                    p.sendMessage(ChatColor.GOLD + tp.getName() + ChatColor.GREEN + "No One Poked You!");
                                }else{
                                    counter++;
                                    tp.sendMessage(ChatColor.GOLD + p.getName() + ChatColor.GREEN + " Has Poked You!");
                                    tp.sendMessage(ChatColor.GREEN + "Do " + ChatColor.AQUA + "/Poke Back <Name> " + ChatColor.GREEN + "To Poke Him Back!");
                                }
                            }
                        }
    is messed up.... its opening and closing brackets like crazy. If you are using eclipse try clicking on a bracket to see where it ends also you have already checked if args.length == 1 why do it again line(61)
    ok so i have fixed those problems with the brackets now. Now im gonna do you a favor and replace the counters with a hashmap. LOOK IT UP. YOU SHOULD KNOW IT.

    What were you thinking on this part?:
    Code:
                        if(args.length == 2){
                            if(p.getServer().getPlayer(args[1]) != null){
                                Player tp = p.getServer().getPlayer(args[0]);
                                if(counter == 0){
                                    p.sendMessage(ChatColor.GOLD + tp.getName() + ChatColor.GREEN + " No One Poked You!");
                                }else{
                                    counter++;
                                    tp.sendMessage(ChatColor.GOLD + p.getName() + ChatColor.GREEN + " Has Poked You!");
                                    tp.sendMessage(ChatColor.GREEN + "Do " + ChatColor.AQUA + "/Poke Back <Name> " + ChatColor.GREEN + "To Poke Him Back!");
                                }
                            }
                        }
    It shoould say that tp has not poked you. Right?
    here is a fast fix. I dont need to explain how its logic
    Btw NEVER DO CAPS ON ALL FIRST LETTERS!
    how would it feel if i did my whole post with caps on first letter. Lets try.
    So Now Ive Changed All The "Counter" Parts To HashMap Instead
    You Might Need To Add Some Return Statements To Avoid Getting Any Errors Or I Guess You Could Just Do A Try Catch LOOK IT UP.
    You Do Not Need To Put A Command For Arguments In The Plugin.yml It Should Look Like This

    Code:
    name: Poke
    author: Lemond
    version: 1.1.0
    description: poke like facebook!
    main: me.cm.poke.main
    commands:
      poke:
          description: THE POKE PLUGIN N SHIT
    Ok So Now You Have A Error Here On The Back Command:
    Player tp = p.getServer().getPlayer(args[0]);
    It should be args[1]
    I HAVE NO IDEA WHATS WORONG WITH BACK BUT THIS WORKS:
    Code:
    package me.cm.poke;
     
    import java.util.HashMap;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class main extends JavaPlugin{
     
        public static HashMap<Player, Integer> pokes = new HashMap<Player, Integer>();
     
        public void onEnable(){
            System.out.println("Pokessss me! :D");
     
        }
     
        public void onDisable(){
            System.out.println("Y u dont wanna poke me? D:");
     
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String command, String[] args){
            if(sender instanceof Player) {
                Player p = (Player) sender;
                if(command.equalsIgnoreCase("poke")){
                    if(args.length == 0){
                        p.sendMessage("----------------" + ChatColor.GREEN + " Poke Help " + ChatColor.WHITE + "----------------");
                        p.sendMessage(ChatColor.AQUA + "/Poke" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Shows All Poke Commands!");
                        p.sendMessage(ChatColor.AQUA + "/Poke <Name>" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Pokes A Player!");
                        p.sendMessage(ChatColor.AQUA + "/Poke Check" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Checks How Many Pokes You Have!");
                        p.sendMessage(ChatColor.AQUA + "/Poke Back <Name>" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Pokes Players Back!");
                        p.sendMessage("------------------------------------------");
                    }
                }if(args.length == 1){
                    if(p.getServer().getPlayer(args[0]) != null){
                        Player tp = p.getServer().getPlayer(args[0]);
                        if(sender instanceof Player) {
                            if(pokes.containsKey(tp)){
                                pokes.put(tp, pokes.get(tp) + 1);
                            }else{
                                pokes.put(tp, 1);
                            }
                            p.sendMessage(ChatColor.GREEN + "Poked " + ChatColor.GOLD + tp.getName() + ChatColor.GREEN + "!");
                            tp.sendMessage(ChatColor.GOLD + p.getName() + ChatColor.GREEN + " has poked you!");
                            tp.sendMessage(ChatColor.GREEN + "Do " + ChatColor.AQUA + "/Poke Back <Name> " + ChatColor.GREEN + "to poke him back!");
                        }
     
     
     
     
                    }else if(args[0].equalsIgnoreCase("check")){
                        if(args.length == 1){
                            if(!pokes.containsKey(p)){
                                p.sendMessage(ChatColor.GREEN + "You have no pokes! Use " + ChatColor.AQUA + "/Poke <Name>");
                            }else{
                                p.sendMessage(ChatColor.GREEN + "There are " + ChatColor.GOLD + pokes.get(p) + ChatColor.GREEN + " Player('s) that poked you!");
                            }
                        }
     
     
     
                    }else if(args[0].equalsIgnoreCase("back")){
                        if(args.length == 2){
                            if(p.getServer().getPlayer(args[1]) != null){
                                Player tp = p.getServer().getPlayer(args[1]);
                                if(!pokes.containsKey(p)){
                                    p.sendMessage(ChatColor.GREEN + "You have no pokes! Use " + ChatColor.AQUA + "/Poke <Name>");
                                }else{
                                    pokes.put(tp, 1);
                                    tp.sendMessage(ChatColor.GOLD + p.getName() + ChatColor.GREEN + " has poked you!");
                                    tp.sendMessage(ChatColor.GREEN + "Do " + ChatColor.AQUA + "/Poke Back <Name> " + ChatColor.GREEN + "to poke him back!");
                                }
                            }
                        }
                    }
                }
            }
            return false;
        }
    }
    
     
  18. Offline

    clutchmasterman

    in }else if(args[0].equalsIgnoreCase("back")){ shouldnt the args = 1 in that statement?

    ugggggggggggg

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

    ceoepts

    no bcs the length of the array does give you how many args there is back 1 arg and the name is another
     
  20. Offline

    d33k40

    dude for counters use this:
    Code:java
    1. public HashMap<String , Integer> pokeCounter = new HashMap<String , Integer>();


    so just make a method to ++ it or just delete it and another to check the counter.

    Code:java
    1. public void manageCounter(Player player, Boolean delete)
    2. {
    3. if(delete)
    4. {
    5. pokeCounter.remove(player.getName());
    6. return;
    7. }
    8. if(pokeCounter.containsKey(player.getName()))
    9. {
    10. pokeCounter.put(player.getName(), pokeCounter.get(player.getName()) + 1);
    11. }
    12. else
    13. {
    14. pokeCounter.put(player.getName(), 1);
    15. }
    16. }
    17.  
    18. public int getCounter(Player player)
    19. {
    20. if(pokeCounter.containsKey(player.getName()))
    21. {
    22. return pokeCounter.get(player.getName());
    23. }
    24. else
    25. {
    26. return 0;
    27. }
    28. }
     
  21. Offline

    clutchmasterman

    uhhhh... wut o.0
     

  22. A HashMap lets you store a data value with a "key" and then later retrieve the data with the same key. In this case, a player's name in the form of a String is passed to the HashMap to retrieve an integer value.

    The code works something like this:

    Code:java
    1. public void manageCounter(Player player, Boolean delete)
    2. {
    3. if(delete)
    4. {
    5. pokeCounter.remove(player.getName());
    6. return;
    7. }


    If "delete" is true, the player's name is removed from the map. The corresponding value is therefore deleted.

    Code:java
    1. if(pokeCounter.containsKey(player.getName()))
    2. {
    3. pokeCounter.put(player.getName(), pokeCounter.get(player.getName()) + 1);
    4. }
    5. else
    6. {
    7. pokeCounter.put(player.getName(), 1);
    8. }
    9. }


    If the map has the player's name in it already, its value is incremented by one. This is done by first retrieving the value from the map, then adding one and putting it back in the map.

    If it doesn't contain the player's name, then the name is added along with a 1. This keeps it from throwing an exception- you can't read data for a key that isn't in the map.

    Code:java
    1. public int getCounter(Player player)
    2. {
    3. if(pokeCounter.containsKey(player.getName()))
    4. {
    5. return pokeCounter.get(player.getName());
    6. }
    7. else
    8. {
    9. return 0;
    10. }


    This grabs a value from the map and returns it. If the map doesn't have the player's name, then the player must never have been poked, so it returns a 0.

    Does that help clear things up?
     
  23. Offline

    clutchmasterman

  24. Offline

    xTrollxDudex

    -_________-

    Look at all these answers....
    And this is your response:
     
  25. Offline

    d33k40

    Dont waste time on people that dont take time to learn basics.
     
Thread Status:
Not open for further replies.

Share This Page