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 (Move your mouse to reveal the content) Main (open) Main (close) Code:java package me.jacklin213.stafflist; import java.util.ArrayList;import java.util.Collection;import java.util.logging.Logger; import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.command.Command;import org.bukkit.command.CommandSender;import org.bukkit.entity.Player;import org.bukkit.plugin.PluginDescriptionFile;import org.bukkit.plugin.java.JavaPlugin; public class StaffList extends JavaPlugin { public final Logger logger = Logger.getLogger("Minecraft"); public static StaffList plugin; PluginDescriptionFile pdfFile; public StaffData sd; public void onDisable() { logger.info(String.format("[%s] Disabled Version %s", getDescription() .getName(), getDescription().getVersion())); } public void onEnable() { logger.info(String.format("[%s] Enabled Version %s by jacklin213", getDescription().getName(), getDescription().getVersion())); } public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String args[]){ if (commandLabel.equalsIgnoreCase("stafflist")){ if(sender.hasPermission("stafflist.use")){ Collection<ArrayList<String>> stafflist = sd.getAll(); // im stuck here sender.sendMessage(stafflist); return true; } else { sender.sendMessage(ChatColor.RED + "You do not have the permission to use this command!"); return true; } } else if (commandLabel.equalsIgnoreCase("stafflist") && args.length == 2){ if (args[0].equalsIgnoreCase("add")){ if (sender.hasPermission("stafflist.add")){ Player player = Bukkit.getServer().getPlayer(args[1]); sd.addUser(player); return true; } else { sender.sendMessage(ChatColor.RED + "You do not have the permission to use this command!"); return true; } } else if (args[0].equalsIgnoreCase("remove")){ if (sender.hasPermission("stafflist.remove")){ Player player = Bukkit.getServer().getPlayer(args[1]); sd.removeUser(player); return true; } else { sender.sendMessage(ChatColor.RED + "You do not have the permission to use this command!"); return true; } } else { sender.sendMessage(ChatColor.RED + "Not a valid command!"); } return true; } return false; }} DataClass (Move your mouse to reveal the content) DataClass (open) DataClass (close) Code:java package me.jacklin213.stafflist; import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.Map; import org.bukkit.entity.Player; public class StaffData { private Map<String, ArrayList<String>> staffNames = null; public StaffData() { this.staffNames = new HashMap<String, ArrayList<String>>(); } public void addUser(Player player) { this.staffNames.put(player.getName(), null); } public void removeUser(Player player) { this.staffNames.remove(player.getName()); } public Collection<ArrayList<String>> getAll(){ return staffNames.values(); } }
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.
hum anyone c y this isnt working? Code:java if(sender.hasPermission("stafflist.use")){ String allstaff = sd.getAllUsers(); if (allstaff != null){ sender.sendMessage("[" +ChatColor.DARK_GREEN + "StaffList" + ChatColor.RESET + " ] The Staff List is empty!"); return true; } else { sender.sendMessage(allstaff); return true; } } else { sender.sendMessage(ChatColor.RED + "You do not have the permission to use this command!"); return true; } The StaffData Class (updated) Code:java package me.jacklin213.stafflist; import java.util.HashMap;import java.util.Map; import org.bukkit.entity.Player; public class StaffData { public Map<String, String> staffNames = null; public StaffData() { this.staffNames = new HashMap<String, String>(); } public void addUser(Player player) { this.staffNames.put(player.getName(), null); } public void removeUser(Player player) { this.staffNames.remove(player.getName()); } public String getUser(Player player){ return this.staffNames.get(player.getName()); } public String getAllUsers(){ String listofplayers = ""; for(String player : staffNames.values()) listofplayers += ", " + player; return listofplayers; }}
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("[\\['']|['\\]'']", "")); }
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
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 } }
i was think about this Code:java String listofplayers = " " for(int i = 0; i <derp.length(); i++) listofplayers += derp.get(i); sender.sendMessage(listofplayers);