Confused=.=

Discussion in 'Plugin Development' started by jacklin213, Nov 16, 2012.

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

    jacklin213

    I am making a plugin which requires you to be able to store playernames into a hashmap.
    after a hour i found out i didnt know how to send all the values from the hashmap to the sender.

    Now im stuck on what type of data im dealing with, im so confused.

    Main (open)
    Code:java
    1. package me.jacklin213.stafflist;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.Collection;
    5. import java.util.logging.Logger;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.plugin.PluginDescriptionFile;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class StaffList extends JavaPlugin {
    16.  
    17. public final Logger logger = Logger.getLogger("Minecraft");
    18. public static StaffList plugin;
    19. PluginDescriptionFile pdfFile;
    20. public StaffData sd;
    21.  
    22. public void onDisable() {
    23. logger.info(String.format("[%s] Disabled Version %s", getDescription()
    24. .getName(), getDescription().getVersion()));
    25. }
    26.  
    27. public void onEnable() {
    28. logger.info(String.format("[%s] Enabled Version %s by jacklin213",
    29. getDescription().getName(), getDescription().getVersion()));
    30. }
    31.  
    32. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String args[]){
    33. if (commandLabel.equalsIgnoreCase("stafflist")){
    34. if(sender.hasPermission("stafflist.use")){
    35. Collection<ArrayList<String>> stafflist = sd.getAll();
    36. // im stuck here
    37. sender.sendMessage(stafflist);
    38. return true;
    39. } else {
    40. sender.sendMessage(ChatColor.RED + "You do not have the permission to use this command!");
    41. return true;
    42. }
    43. } else if (commandLabel.equalsIgnoreCase("stafflist") && args.length == 2){
    44. if (args[0].equalsIgnoreCase("add")){
    45. if (sender.hasPermission("stafflist.add")){
    46. Player player = Bukkit.getServer().getPlayer(args[1]);
    47. sd.addUser(player);
    48. return true;
    49. } else {
    50. sender.sendMessage(ChatColor.RED + "You do not have the permission to use this command!");
    51. return true;
    52. }
    53. } else if (args[0].equalsIgnoreCase("remove")){
    54. if (sender.hasPermission("stafflist.remove")){
    55. Player player = Bukkit.getServer().getPlayer(args[1]);
    56. sd.removeUser(player);
    57. return true;
    58. } else {
    59. sender.sendMessage(ChatColor.RED + "You do not have the permission to use this command!");
    60. return true;
    61. }
    62. } else {
    63. sender.sendMessage(ChatColor.RED + "Not a valid command!");
    64. }
    65. return true;
    66. }
    67.  
    68. return false;
    69. }
    70. }
    71.  


    DataClass (open)
    Code:java
    1. package me.jacklin213.stafflist;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.Collection;
    5. import java.util.HashMap;
    6. import java.util.Map;
    7.  
    8. import org.bukkit.entity.Player;
    9.  
    10. public class StaffData {
    11. private Map<String, ArrayList<String>> staffNames = null;
    12.  
    13. public StaffData() {
    14. this.staffNames = new HashMap<String, ArrayList<String>>();
    15. }
    16.  
    17. public void addUser(Player player) {
    18. this.staffNames.put(player.getName(), null);
    19. }
    20.  
    21. public void removeUser(Player player) {
    22. this.staffNames.remove(player.getName());
    23. }
    24.  
    25. public Collection<ArrayList<String>> getAll(){
    26. return staffNames.values();
    27. }
    28.  
    29.  
    30. }
    31.  
     
  2. Offline

    Hoolean

    jacklin213

    What do you want to send to the client? The keys in the hashmap, or the values?
     
  3. Offline

    Assult

    He is sending the values
     
  4. Offline

    Shiny Quagsire

    If you're getting a list and printing it, you could do a for loop like this:
    Code:
    String listoplayers = "";
    for(String player : StaffData.getNames())
      listoplayers += ", " + player;
    Then just print the resulting string to the player/console.
     
  5. Offline

    cman1885

    StringBuilder
     
  6. Offline

    jacklin213

    ahh for loop, k thanks all will try it out
     
  7. Offline

    jacklin213

    hum anyone c y this isnt working?
    Code:java
    1. if(sender.hasPermission("stafflist.use")){
    2. String allstaff = sd.getAllUsers();
    3. if (allstaff != null){
    4. sender.sendMessage("[" +ChatColor.DARK_GREEN + "StaffList" + ChatColor.RESET + " ] The Staff List is empty!");
    5. return true;
    6. } else {
    7. sender.sendMessage(allstaff);
    8. return true;
    9. }
    10. } else {
    11. sender.sendMessage(ChatColor.RED + "You do not have the permission to use this command!");
    12. return true;
    13. }

    The StaffData Class (updated)
    Code:java
    1. package me.jacklin213.stafflist;
    2.  
    3. import java.util.HashMap;
    4. import java.util.Map;
    5.  
    6. import org.bukkit.entity.Player;
    7.  
    8. public class StaffData {
    9. public Map<String, String> staffNames = null;
    10.  
    11. public StaffData() {
    12. this.staffNames = new HashMap<String, String>();
    13. }
    14.  
    15. public void addUser(Player player) {
    16. this.staffNames.put(player.getName(), null);
    17. }
    18.  
    19. public void removeUser(Player player) {
    20. this.staffNames.remove(player.getName());
    21. }
    22.  
    23. public String getUser(Player player){
    24. return this.staffNames.get(player.getName());
    25. }
    26.  
    27. public String getAllUsers(){
    28. String listofplayers = "";
    29. for(String player : staffNames.values())
    30. listofplayers += ", " + player;
    31. return listofplayers;
    32. }
    33. }
    34.  
    35.  
    36.  
     
  8. Offline

    zack6849

    I dont know if hashmaps are the way to go about this, i use Lists for it

    List<String> staff = new ArrayList<String>();
    staff.add(player.getName());
    String stafflist = staff.toString().replaceAll("[\\['']|['\\]'']", "");
    sender.sendMessage(stafflist)
    you can also check the length of it and see make it formatted nicely
    for example
    Code:
    String[] derp = staff.toArray();
    if(derp.length == 1){
      sender.sendMessage("The only staff member is : " staff.get(0);
    }
    if(derp.length == 2){
      sender.sendMessage("The staff members are :" staff.get(0) + " and " + staff.get(1);
    }
    if(derp.length >= 3){
      sender.sendMessage(staff.toString().replaceAll("[\\['']|['\\]'']", ""));
    }
    
     
  9. Offline

    jacklin213

    does this get the whole list or just the 1st and 2nd values
    EDIT: nvm ill just do a for loop , forgot about this XD
    thanks for the reminder though
     
  10. Offline

    zack6849

    You could use a for loop i suppose, depends on what exactly you're trying to do.

    for example if you wanted to see if the list staff contained a users name out of everyone online you'd do something like this
    Code:
    for(Player p : Bukkit.getOnlinePlayers()){
      if(staff.contains(p.getName()){
        //do some stuff
      }
    }
     
  11. Offline

    jacklin213

    i was think about this

    Code:java
    1. String listofplayers = " "
    2.  
    3. for(int i = 0; i <derp.length(); i++)
    4. listofplayers += derp.get(i);
    5.  
    6. sender.sendMessage(listofplayers);
     
  12. Offline

    zack6849

    that would work too i think :p
     
Thread Status:
Not open for further replies.

Share This Page