Question

Alright, so I'm trying to make a list to gather up all the gangs that have been created by players online using the plugin.

But when ever it adds an item to the YML list, it names it "true" which isn't what I want. I want the gang name added.

It also does it when I try to add the players in a gang to a list that saves with the gang owner.

This is my GangJoinCommand class.

package me.galaxywarrior6.minecraftgta.commands;


import me.galaxywarrior6.minecraftgta.MinecraftGTA;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Score;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.ScoreboardManager;

public class GangCreateCommand implements CommandExecutor{

    public MinecraftGTA plugin;
    public GangCreateCommand(MinecraftGTA instance){
        plugin = instance;  
        }

    public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]){
        Player p = (Player) sender;
        String prefix = ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "Gangs" + ChatColor.DARK_GRAY + "] ";
        if (cmd.getName().equalsIgnoreCase("gcreate")){

            if (args.length <= 0 || args.length >= 2){
                p.sendMessage(prefix + "That's an invalid command structure!");
                return true;
            }

            String UUID = p.getUniqueId().toString().toLowerCase();

            if (!(MinecraftGTA.config.getString(UUID + ".Gang").equalsIgnoreCase("none"))){
                p.sendMessage(prefix + ChatColor.RED + "You're already in a gang!");
                return true;
            }
            if (MinecraftGTA.config.getStringList("gangs").contains(args[0].toString())){
                p.sendMessage(prefix + ChatColor.RED + "That gang name is already taken!");
                return true;
            }

            if (args[0].length() >= 16){
                p.sendMessage(prefix + ChatColor.RED + "Please shorten your gangname to 15 or less characters.");
                return true;
            }

                MinecraftGTA.config.set(UUID + ".Gang", args[0]);
                MinecraftGTA.config.set(UUID + ".GangOwnership", args[0]);
                MinecraftGTA.config.set(UUID + ".GangRank", "GodFather");
                MinecraftGTA.config.set("gangs", MinecraftGTA.config.getStringList("gangs").add(args[0]));
                MinecraftGTA.config.set(UUID + ".GangMembers", MinecraftGTA.config.getStringList(UUID + ".GangMembers").add(UUID));

                p.sendMessage(prefix + ChatColor.GREEN + "You've successfully created a gang!");
                Bukkit.getServer().broadcastMessage(prefix + ChatColor.BLUE + p.getName() + ChatColor.GREEN + " has started up a gang!");

                ScoreboardManager manager = Bukkit.getScoreboardManager();
                Scoreboard board = manager.getNewScoreboard();

                Objective sidebar = board.registerNewObjective("money", "count");
                sidebar.setDisplaySlot(DisplaySlot.SIDEBAR);
                sidebar.setDisplayName("§a§l-----Stats-----");

                p.setScoreboard(board);

                Score spacer1 = sidebar.getScore(Bukkit.getOfflinePlayer("§f"));
                spacer1.setScore(12);

                Score gangs = sidebar.getScore(Bukkit.getOfflinePlayer("§5§lGang:"));
                gangs.setScore(11);

                Score gangName = sidebar.getScore(Bukkit.getOfflinePlayer("§d§l" + MinecraftGTA.config.getString(UUID + ".Gang")));
                gangName.setScore(10);

                Score spacer4 = sidebar.getScore(Bukkit.getOfflinePlayer("§f"));
                spacer4.setScore(9);

                Score dollars = sidebar.getScore(Bukkit.getOfflinePlayer("§6§lMoney:"));
                dollars.setScore(8);

                Score dollarsnum = sidebar.getScore(Bukkit.getOfflinePlayer("§e§l$" + MinecraftGTA.config.getInt(UUID + ".Dollars")));
                dollarsnum.setScore(7);

                Score spacer2 = sidebar.getScore(Bukkit.getOfflinePlayer("§7"));
                spacer2.setScore(6);

                Score kills = sidebar.getScore(Bukkit.getOfflinePlayer("§3§lKills:"));
                kills.setScore(5);

                Score killsnum = sidebar.getScore(Bukkit.getOfflinePlayer("§b§l" + MinecraftGTA.config.getInt(UUID + ".Kills")));
                killsnum.setScore(4);

                Score spacer3 = sidebar.getScore(Bukkit.getOfflinePlayer("§8"));
                spacer3.setScore(3);

                if(MinecraftGTA.config.getInt(UUID + ".WantedLv") <= 4){
                    Score wanted = sidebar.getScore(Bukkit.getOfflinePlayer("§c§lWanted:"));
                    wanted.setScore(2);

                    Score wantednum = sidebar.getScore(Bukkit.getOfflinePlayer("✩ ✩ ✩ ✩ ✩"));
                    wantednum.setScore(1);
                }else if(MinecraftGTA.config.getInt(UUID + ".WantedLv") <= 10){
                    Score wanted = sidebar.getScore(Bukkit.getOfflinePlayer("§c§lWanted:"));
                    wanted.setScore(2);

                    Score wantednum = sidebar.getScore(Bukkit.getOfflinePlayer("✭ ✩ ✩ ✩ ✩"));
                    wantednum.setScore(1);

                    p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 99999, 1));

                }else if(MinecraftGTA.config.getInt(UUID + ".WantedLv") <= 15){
                    Score wanted = sidebar.getScore(Bukkit.getOfflinePlayer("§c§lWanted:"));
                    wanted.setScore(2);

                    Score wantednum = sidebar.getScore(Bukkit.getOfflinePlayer("✭ ✭ ✩ ✩ ✩"));
                    wantednum.setScore(1);

                    p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 99999, 2));

                }else if(MinecraftGTA.config.getInt(UUID + ".WantedLv") <= 20){
                    Score wanted = sidebar.getScore(Bukkit.getOfflinePlayer("§c§lWanted:"));
                    wanted.setScore(2);

                    Score wantednum = sidebar.getScore(Bukkit.getOfflinePlayer("✭ ✭ ✭ ✩ ✩"));
                    wantednum.setScore(1);

                    p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 99999, 3));

                }else if(MinecraftGTA.config.getInt(UUID + ".WantedLv") <= 25){
                    Score wanted = sidebar.getScore(Bukkit.getOfflinePlayer("§c§lWanted:"));
                    wanted.setScore(2);

                    Score wantednum = sidebar.getScore(Bukkit.getOfflinePlayer("✭ ✭ ✭ ✭ ✩"));
                    wantednum.setScore(1);

                    p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 99999, 4));

                }else if(MinecraftGTA.config.getInt(UUID + ".WantedLv") >= 30){
                    Score wanted = sidebar.getScore(Bukkit.getOfflinePlayer("§c§lWanted:"));
                    wanted.setScore(2);

                    Score wantednum = sidebar.getScore(Bukkit.getOfflinePlayer("✭ ✭ ✭ ✭ ✭"));
                    wantednum.setScore(1);

                    p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 99999, 5));

                }
                MinecraftGTA.saveFile();
                return true;
            }
        return false;
    }
}

The area that it's adding to the lists can be found here:

        MinecraftGTA.config.set("gangs", MinecraftGTA.config.getStringList("gangs").add(args[0]));
        MinecraftGTA.config.set(UUID + ".GangMembers", MinecraftGTA.config.getStringList(UUID + ".GangMembers").add(UUID));

And this sets the config to the following:

gangs: true
5e1839e8-4c81-4dd9-80f3-18475b98f192:
  Dollars: 78
  Kills: 0
  WantedLv: 0
  LastUsername: I_Stole_The_Sock
  Gang: Tgang
  GangOwnership: Tgang
  GangRank: GodFather
  GangMembers: true
  CurrentInvite: ''
  Inviter: ''

Please tell me how to stop it from saying "true" and make it set it to the actual String I want.

Was it helpful?

Solution

According to the MemoryConfiguration javadoc (Which is one of the parent classes for the YamlConfiguration you're using), getStringList() returns a List<String>, however I'm willing to bet it actually returns an ArrayList<String>, and calling ArrayList#add() will return a boolean indicating whether the item was successfully added to the list. Therefore this line:

MinecraftGTA.config.set("gangs", MinecraftGTA.config.getStringList("gangs").add(args[0]));

Needs to become:

final List<String> gangs = MinecraftGTA.config.getStringList("gangs");
gangs.add(args[0]);
MinecraftGTA.config.set("gangs", gangs);

And the same again for the second configuration.

OTHER TIPS

In general the plugin looks good, everything seems right; but you have to look at the YamlConfiguration class a bit differently. Your top level, gangs is not a string list (so you can't set it as such); gangs is a collection of keys. Also, the gang you add to that key collection is also a collection of keys. Anything that has a title and a value is a key here. Example:

collection:
    key: value
    key: value

Therefore:

MinecraftGTA.config.set("gangs", MinecraftGTA.config.getStringList("gangs").add(args[0]));

Should be:

MinecraftGTA.config.createSection("gangs." + args[0]);

Assuming that you're not adding something to the key collection right away, which would make that line redundant because setting a value in gangs.hurp.durp automatically would create the gangs and hurp headers, and assign a value to durp, underneath hurp.

As for the second true, in theory it would work perfectly fine, but as stated in the other answer, there's a good chance that the add method returns a boolean instead of a modified string list, meaning that expanding the line would be the simplest solution. (My correction will assume you want the UUIDs to be below the gangs toplevel)

MinecraftGTA.config.set(UUID + ".GangMembers", MinecraftGTA.config.getStringList(UUID + ".GangMembers").add(UUID));

Would then be:

List<String> temp = MinecraftGTA.config.getStringList("gangs." + UUID + ".GangMembers");
temp.add(UUID);
MinecraftGTA.config.set("gangs." + UUID + ".GangMembers", temp);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top