Lists with variables as their title

Discussion in 'Plugin Development' started by JeroenV, Aug 8, 2012.

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

    JeroenV

    // Let me rephrase my problem since I might have found a better solution, I'm still stuck with it though.

    For example can I create a list inside a config that starts off empty but then add I can add playernames to it without it requiring an actual list inside the code.

    So for example you have a config named 'playername.yml', then I want to put an empty list inside named 'PartyMembers:'.

    Now for example everytime uses a command '/paccept' (after checking, that part is all done) it will add the senders name to that list.

    I can only see 'configname.set(PartyMembers, <playername>)' but I can't seem to find 'configname.add(PartyMembers, <playername)'. I know I could use '.set' to set a list inside PartyMembers, but there lies the problem. I can't make a list in the code because it would need to be unique per partyleader because every partyleader has different partymembers so I can't make a list named 'PartyMembers' and then keep adding names to it.

    I hope someone can help.

    Ok, I need it for a party system. I would like to make an array/list that has the partyleadersname as title and then the party members in the list. Is there anyway I could do this?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  2. I recommend doing a separate class for your party, for the sake of OOP.
    In that you could have a Player/String variable as party leader and a collection of party members.
     
  3. Offline

    JeroenV

    Right but I have all my commands in the main class and that's where most of the adding is coming from, I don't really have alot of experience with implementing classes into eachother except for Listeners, or are they all listeners?

    Still even with this I wouldnd be able to wrap my head around how it would change. I'll post the code so you know what I mean.

    Code:
        @SuppressWarnings({ "unchecked", "unused" })
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    //another command
            else{
     
            if (cmd.getName().equalsIgnoreCase("createparty")) {
            Player player = (Player)sender;
            String playername = player.getPlayerListName();
            if(!HasParty.contains(playername)){
            YamlConfiguration PartyConfig = YamlConfiguration.loadConfiguration(new File(myNewDataFolder1,"[Party]"+playername+".yml"));
            HasParty.add(playername);
            IsPartyLeader.add(playername);
            }
            else{
            sender.sendMessage(ChatColor.RED + "You are already in a party."); 
            }
             
             
            return true; 
            }
         
            else{
             
            if (cmd.getName().equalsIgnoreCase("party")) {
                Player player = (Player)sender;
                String playername = player.getPlayerListName();
                if (args.length == 0) {
                sender.sendMessage(ChatColor.RED + "/party add/kick [playername]");
                }
                if (args.length == 1) {
                sender.sendMessage(ChatColor.RED + "/party add/kick [playername]"); 
                }
                if (args.length == 2) {
                if(args[0]=="add"){
                Player targetPlayer = player.getServer().getPlayer(args[1]);
                String targetPlayername = targetPlayer.getPlayerListName();
                if(targetPlayer==null){
                sender.sendMessage(ChatColor.RED + "Invalid playername."); 
                }
                else{
                if(HasParty.contains(targetPlayer)){
                sender.sendMessage(ChatColor.RED + "Player is already in a party.");
                }
                else{
                if(ActivePlayers.contains(targetPlayer)){
                if(ActivePlayers.contains(playername)){
                targetPlayer.sendMessage(ChatColor.RED + "invites you to his party. Type /paccept or /prefuse");
                HasInvitation.put(targetPlayername,true);
                YamlConfiguration PartyConfig = YamlConfiguration.loadConfiguration(new File(myNewDataFolder1,"[Party]"+targetPlayername+".yml"));
                PartyConfig.set("HasInvitation", HasInvitation.get(targetPlayername));
                PartyConfig.set("WhoInvitedPlayer", playername);
                }
                else{
                sender.sendMessage(ChatColor.RED + "Player is already inside the game."); 
                }
                 
                }
                else{
                if(!ActivePlayers.contains(playername)){
                    targetPlayer.sendMessage(ChatColor.RED + "invites you to his party. Type /paccept or /prefuse");
                    HasInvitation.put(targetPlayername,true);
                    YamlConfiguration PartyConfig = YamlConfiguration.loadConfiguration(new File(myNewDataFolder1,"[Party]"+targetPlayername+".yml"));
                    PartyConfig.set("HasInvitation", HasInvitation.get(targetPlayername)); 
                    PartyConfig.set("WhoInvitedPlayer", playername);
                }
                else{
                sender.sendMessage(ChatColor.RED + "Player is still in the lobby.");     
                }
                }
                }
                }
                }
                }
                return true;}
            else{
                if (cmd.getName().equalsIgnoreCase("p")) {
                if (args.length == 1) {
                if(args[0] == "accept"){
                    YamlConfiguration PartyConfig = YamlConfiguration.loadConfiguration(new File(myNewDataFolder1,"[Party]"+sender+".yml"));
                Boolean HasInvitation = PartyConfig.getBoolean("HasInvitation");
                if(HasInvitation==true){
                String WhoInvitedPlayer = PartyConfig.getString("WhoInvitedPlayer");
                YamlConfiguration Party = YamlConfiguration.loadConfiguration(new File(myNewDataFolder1,"[Party]"+WhoInvitedPlayer+".yml"));
                //This is where a player should be added to the partymembers list.
                }
                }
                }
             
                return true;
                }
             
            }
            }
      }
    return false; }
    Can anyone help?

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

    NSArray

    Start by getting the list from the yaml, then add the player to that list, then save it to the file.
     
  5. Offline

    JeroenV

    God, that's so simple why didn't I think about that O.O Thanks alot :)
     
Thread Status:
Not open for further replies.

Share This Page