Checking if a config contains a player

Discussion in 'Plugin Development' started by MCCoding, Nov 28, 2013.

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

    MCCoding

    I'm adding a players name to a config with i'm trying to do is check if the players name is in the config allow them to create a group else message them saying you are already in the group and then display the group name. What i'm doing now is

    Code:java
    1. if(!settings.groupConfig().contains(player.getName())) {


    and this does not work just allowing them to create multiple groups what i have tried all the ways i could think of and nothing is working this is what my config looks like

    Code:
    Group Name:
      owner: MCCOding
      name: Group Name
      members: &id001
      - MCCoding
     
  2. Offline

    jimuskin

    Is this in the default bukkit config.yml? Then have you tried this?
    Code:java
    1. if(getConfig().getStringList("group.path").contains(player.getName())) {
     
  3. Offline

    MCCoding

    jimuskin
    Yes it's just the default config.yml here is what i added and it still allowing me to create multiple groups

    Code:java
    1. if(!settings.groupConfig().getStringList(".members").contains(player.getName())) {
     
  4. Offline

    jimuskin

    MCCoding What is settings.groupConfig()?

    anyway, once you have checked that they are in that list, remove them from that list and add them to a new list.
     
  5. Offline

    MCCoding

    jimuskin
    The method i use to create a group is this

    Code:java
    1. public static List <String> members = new ArrayList <String>();
    2.  
    3. public static void createGroup(String player, String name) {
    4. members.add(player);
    5. settings.groupConfig().set(name.toLowerCase() + ".owner", player);
    6. settings.groupConfig().set(name.toLowerCase() + ".name", name);
    7. settings.groupConfig().set(name.toLowerCase() + ".members", members);
    8. settings.groupSaveConfig();
    9. }
    10.  
     
  6. Offline

    geNAZt

    Code:java
    1. public boolean isPlayerInGroup(String player) {
    2. for(String key : settings.groupConfig.getKeys(false)) {
    3. List<String> membersInGroup = settings.groupConfig().getStringList(key + ".members");
    4.  
    5. if(membersInGroup.contains(player)) {
    6. return true;
    7. }
    8. }
    9.  
    10. return false;
    11. }


    This function can be used to terminate if the given player is in a Group. Usage:

    Code:java
    1. if(xyz.isPlayerInGroup("geNAZt")) {
    2. player.sendMessage("You are already in another Group!");
    3. } else {
    4. //Create a new Group or whatevar
    5. }
     
    MCCoding likes this.
  7. Offline

    MCCoding

    geNAZt
    Ah okay i see now, thanks for that it seems to work but how would i go about getting the group name the player is in as well?
     
  8. Offline

    geNAZt

    Code:java
    1. public String getPlayerGroup(String player) {
    2. for(String key : settings.groupConfig.getKeys(false)) {
    3. List<String> membersInGroup = settings.groupConfig().getStringList(key + ".members");
    4.  
    5. if(membersInGroup.contains(player)) {
    6. return settings.groupConfig().get(key + ".name");
    7. }
    8. }
    9.  
    10. return null;
    11. }


    Code:java
    1. if(xyz.isPlayerInGroup("geNAZt")) {
    2. player.sendMessage("You are already in " + xyz.getPlayerGroup("geNAZt"));
    3. } else {
    4. //Create a new Group or whatevar
    5. }
     
    MCCoding likes this.
  9. Offline

    MCCoding

    geNAZt
    Thanks it's working! how do i stop the group when you delete it from going like this into a empty string test: {} how do i remove it from the config here is what i have so far

    Code:java
    1. public static void deleteGroup(String player, String name) {
    2. if(settings.groupConfig().getString(name.toLowerCase() + ".owner").equals(player)) {
    3. settings.groupConfig().set(name + ".owner", null);
    4. settings.groupConfig().set(name + ".name", null);
    5. settings.groupConfig().set(name + ".members", null);
    6. settings.groupSaveConfig();
    7.  
    8. } else {
    9.  
    10. Player player1 = plugin.getServer().getPlayer(player);
    11. player1.sendMessage("You are not the leader");
    12. }
    13. }
     
  10. Offline

    AoH_Ruthless

    MCCoding
    {} is basically a representation of null.
    Instead of doing
    Code:
    Test: 
    which could throw errors, it sets the value of Test: to {}, which does nothing.
     
  11. Offline

    MCCoding

    geNAZt AoH_Ruthless
    Okay, here is new new delete method but when i'm not the owner of the group it does not display the "You are not the leader" message it just gives me a internal server error saying this is null

    Code:java
    1. Player player1 = plugin.getServer().getPlayer(player);


    Here is the whole method

    Code:java
    1. public static void deleteGroup(String player, String name) {
    2. if(settings.groupConfig().getString(name + ".owner").equals(player)) {
    3. settings.groupConfig().set(name, "");
    4. settings.groupConfig().set(name + ".owner", "");
    5. settings.groupConfig().set(name + ".name", "");
    6. settings.groupConfig().set(name + ".members", "");
    7. settings.groupSaveConfig();
    8.  
    9. } else {
    10. Player player1 = plugin.getServer().getPlayer(player);
    11. player1.sendMessage("You are not the leader");
    12. }
    13. }
     
  12. Offline

    AoH_Ruthless

    MCCoding Just eliminate that entire line, and use player.sendMessage(message goes here);
     
  13. Offline

    MCCoding

    AoH_Ruthless
    Okay it seems to work the only thing is that now if a player is not the leader it won't delete the group it will display the message saying the "MCoding has deleted the group group" but will do nothing how do i stop that?
     
  14. Offline

    AoH_Ruthless

  15. Offline

    MCCoding

    AoH_Ruthless
    Don't worry i fixed it that part but i still don't understand on how to remove the group name :{} in the config
     
  16. Offline

    MCCoding

    AoH_Ruthless geNAZt
    I'm trying to make it when you search your group and where it displays the players names that are in your group the online players will be green and the offline players will be red i gave it a go and it would give you two strings one with the online players in the group and one with the offline players. if someone could give m e some it would be good pseudo code would be great.
     
Thread Status:
Not open for further replies.

Share This Page