<commit_before>package com.pauldavdesign.mineauz.minigames.minigame.modules;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;

import com.pauldavdesign.mineauz.minigames.MinigameUtils;
import com.pauldavdesign.mineauz.minigames.Minigames;
import com.pauldavdesign.mineauz.minigames.config.EnumFlag;
import com.pauldavdesign.mineauz.minigames.config.Flag;
import com.pauldavdesign.mineauz.minigames.config.TeamSetFlag;
import com.pauldavdesign.mineauz.minigames.menu.Callback;
import com.pauldavdesign.mineauz.minigames.menu.Menu;
import com.pauldavdesign.mineauz.minigames.menu.MenuItem;
import com.pauldavdesign.mineauz.minigames.menu.MenuItemAddTeam;
import com.pauldavdesign.mineauz.minigames.menu.MenuItemList;
import com.pauldavdesign.mineauz.minigames.menu.MenuItemNewLine;
import com.pauldavdesign.mineauz.minigames.menu.MenuItemPage;
import com.pauldavdesign.mineauz.minigames.menu.MenuItemTeam;
import com.pauldavdesign.mineauz.minigames.minigame.Minigame;
import com.pauldavdesign.mineauz.minigames.minigame.MinigameModule;
import com.pauldavdesign.mineauz.minigames.minigame.Team;
import com.pauldavdesign.mineauz.minigames.minigame.TeamColor;

public class TeamsModule extends MinigameModule {
	private Map<TeamColor, Team> teams = new HashMap<TeamColor, Team>();
	private TeamSetFlag teamsFlag;
	private TeamColor defaultWinner = null;
	private EnumFlag<TeamColor> defaultWinnerFlag = new EnumFlag<TeamColor>(defaultWinner, "defaultwinner");
	
	public TeamsModule(Minigame mgm){
		super(mgm);
		teamsFlag = new TeamSetFlag(teams, "teams", getMinigame());
	}

	@Override
	public String getName() {
		return "Teams";
	}
	
	@Override
	public Map<String, Flag<?>> getFlags(){
		Map<String, Flag<?>> flags = new HashMap<String, Flag<?>>();
		flags.put(teamsFlag.getName(), teamsFlag);
		flags.put(defaultWinnerFlag.getName(), defaultWinnerFlag);
		return flags;
	}
	
	@Override
	public boolean useSeparateConfig(){
		return false;
	}

	@Override
	public void save(FileConfiguration config) {
	}

	@Override
	public void load(FileConfiguration config) {
		if(config.contains(getMinigame() + ".startposred")){ //TODO: Remove after 1.7
			if(!hasTeam(TeamColor.RED))
				addTeam(TeamColor.RED);
			Set<String> locs = config.getConfigurationSection(getMinigame() + ".startposred").getKeys(false);
			
			for(int i = 0; i < locs.size(); i++){
				getTeam(TeamColor.RED).addStartLocation(Minigames.plugin.mdata.minigameLocations(getMinigame().getName(false), 
						"startposred." + String.valueOf(i), config));
			}
		}
		if(config.contains(getMinigame() + ".startposblue")){ //TODO: Remove after 1.7
			if(!hasTeam(TeamColor.BLUE))
				addTeam(TeamColor.BLUE);
			Set<String> locs = config.getConfigurationSection(getMinigame() + ".startposblue").getKeys(false);
			
			for(int i = 0; i < locs.size(); i++){
				getTeam(TeamColor.BLUE).addStartLocation(Minigames.plugin.mdata.minigameLocations(getMinigame().getName(false), 
						"startposblue." + String.valueOf(i), config));
			}
		}
	}
	
	public static TeamsModule getMinigameModule(Minigame minigame){
		return (TeamsModule) minigame.getModule("Teams");
	}
	
	public Team getTeam(TeamColor color){
		return teams.get(color);
	}
	
	public List<Team> getTeams(){
		return new ArrayList<Team>(teams.values());
	}
	
	public Team addTeam(TeamColor color){
		return addTeam(color, "");
	}
	
	public Team addTeam(TeamColor color, String name){
		if(!teams.containsKey(color)){
			teams.put(color, new Team(color, getMinigame()));
			String sbTeam = color.toString().toLowerCase();
			getMinigame().getScoreboardManager().registerNewTeam(sbTeam);
			getMinigame().getScoreboardManager().getTeam(sbTeam).setPrefix(color.getColor().toString());
			getMinigame().getScoreboardManager().getTeam(sbTeam).setAllowFriendlyFire(false);
			getMinigame().getScoreboardManager().getTeam(sbTeam).setCanSeeFriendlyInvisibles(true);
		}
		if(!name.equals(""))
			teams.get(color).setDisplayName(name);
		return teams.get(color);
	}
	
	public void addTeam(TeamColor color, Team team){
		teams.put(color, team);
		String sbTeam = color.toString().toLowerCase();
		getMinigame().getScoreboardManager().registerNewTeam(sbTeam);
		getMinigame().getScoreboardManager().getTeam(sbTeam).setPrefix(color.getColor().toString());
		getMinigame().getScoreboardManager().getTeam(sbTeam).setAllowFriendlyFire(false);
		getMinigame().getScoreboardManager().getTeam(sbTeam).setCanSeeFriendlyInvisibles(true);
	}
	
	public boolean hasTeam(TeamColor color){
		if(teams.containsKey(color))
			return true;
		return false;
	}
	
	public void removeTeam(TeamColor color){
		if(teams.containsKey(color)){
			teams.remove(color);
			getMinigame().getScoreboardManager().getTeam(color.toString().toLowerCase()).unregister();
		}
	}
	
	public boolean hasTeamStartLocations(){
		for(Team t : teams.values()){
			if(!t.hasStartLocations())
				return false;
		}
		return true;
	}
	
	public Callback<String> getDefaultWinnerCallback(){
		return new Callback<String>() {

			@Override
			public void setValue(String value) {
				if(!value.equals("None"))
					defaultWinner = TeamColor.matchColor(value.replace(" ", "_"));
				else
					defaultWinner = null;
			}

			@Override
			public String getValue() {
				if(defaultWinner != null)
					return MinigameUtils.capitalize(defaultWinner.toString().replace("_", " "));
				return "None";
			}
		};
	}

	public void setDefaultWinner(TeamColor defaultWinner) {
		this.defaultWinner = defaultWinner;
	}
	
	public TeamColor getDefaultWinner() {
		return defaultWinner;
	}
	
	public void clearTeams(){
		teams.clear();
		defaultWinner = null;
	}

	@Override
	public void addMenuOptions(Menu menu) {
		Menu m = new Menu(6, "Teams", menu.getViewer());
		m.setPreviousPage(menu);
		List<MenuItem> items = new ArrayList<MenuItem>();
		List<String> teams = new ArrayList<String>(this.teams.size() + 1);
		for(TeamColor t : this.teams.keySet()){
			teams.add(MinigameUtils.capitalize(t.toString().replace("_", " ")));<commit_msg>Fixed default winner<commit_after>package com.pauldavdesign.mineauz.minigames.minigame.modules;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;

import com.pauldavdesign.mineauz.minigames.MinigameUtils;
import com.pauldavdesign.mineauz.minigames.Minigames;
import com.pauldavdesign.mineauz.minigames.config.Flag;
import com.pauldavdesign.mineauz.minigames.config.StringFlag;
import com.pauldavdesign.mineauz.minigames.config.TeamSetFlag;
import com.pauldavdesign.mineauz.minigames.menu.Callback;
import com.pauldavdesign.mineauz.minigames.menu.Menu;
import com.pauldavdesign.mineauz.minigames.menu.MenuItem;
import com.pauldavdesign.mineauz.minigames.menu.MenuItemAddTeam;
import com.pauldavdesign.mineauz.minigames.menu.MenuItemList;
import com.pauldavdesign.mineauz.minigames.menu.MenuItemNewLine;
import com.pauldavdesign.mineauz.minigames.menu.MenuItemPage;
import com.pauldavdesign.mineauz.minigames.menu.MenuItemTeam;
import com.pauldavdesign.mineauz.minigames.minigame.Minigame;
import com.pauldavdesign.mineauz.minigames.minigame.MinigameModule;
import com.pauldavdesign.mineauz.minigames.minigame.Team;
import com.pauldavdesign.mineauz.minigames.minigame.TeamColor;

public class TeamsModule extends MinigameModule {
	private Map<TeamColor, Team> teams = new HashMap<TeamColor, Team>();
	private TeamSetFlag teamsFlag;
	private StringFlag defaultWinner = new StringFlag(null, "defaultwinner");
	
	public TeamsModule(Minigame mgm){
		super(mgm);
		teamsFlag = new TeamSetFlag(teams, "teams", getMinigame());
	}

	@Override
	public String getName() {
		return "Teams";
	}
	
	@Override
	public Map<String, Flag<?>> getFlags(){
		Map<String, Flag<?>> flags = new HashMap<String, Flag<?>>();
		flags.put(teamsFlag.getName(), teamsFlag);
		flags.put(defaultWinner.getName(), defaultWinner);
		return flags;
	}
	
	@Override
	public boolean useSeparateConfig(){
		return false;
	}

	@Override
	public void save(FileConfiguration config) {
	}

	@Override
	public void load(FileConfiguration config) {
		if(config.contains(getMinigame() + ".startposred")){ //TODO: Remove after 1.7
			if(!hasTeam(TeamColor.RED))
				addTeam(TeamColor.RED);
			Set<String> locs = config.getConfigurationSection(getMinigame() + ".startposred").getKeys(false);
			
			for(int i = 0; i < locs.size(); i++){
				getTeam(TeamColor.RED).addStartLocation(Minigames.plugin.mdata.minigameLocations(getMinigame().getName(false), 
						"startposred." + String.valueOf(i), config));
			}
		}
		if(config.contains(getMinigame() + ".startposblue")){ //TODO: Remove after 1.7
			if(!hasTeam(TeamColor.BLUE))
				addTeam(TeamColor.BLUE);
			Set<String> locs = config.getConfigurationSection(getMinigame() + ".startposblue").getKeys(false);
			
			for(int i = 0; i < locs.size(); i++){
				getTeam(TeamColor.BLUE).addStartLocation(Minigames.plugin.mdata.minigameLocations(getMinigame().getName(false), 
						"startposblue." + String.valueOf(i), config));
			}
		}
	}
	
	public static TeamsModule getMinigameModule(Minigame minigame){
		return (TeamsModule) minigame.getModule("Teams");
	}
	
	public Team getTeam(TeamColor color){
		return teams.get(color);
	}
	
	public List<Team> getTeams(){
		return new ArrayList<Team>(teams.values());
	}
	
	public Team addTeam(TeamColor color){
		return addTeam(color, "");
	}
	
	public Team addTeam(TeamColor color, String name){
		if(!teams.containsKey(color)){
			teams.put(color, new Team(color, getMinigame()));
			String sbTeam = color.toString().toLowerCase();
			getMinigame().getScoreboardManager().registerNewTeam(sbTeam);
			getMinigame().getScoreboardManager().getTeam(sbTeam).setPrefix(color.getColor().toString());
			getMinigame().getScoreboardManager().getTeam(sbTeam).setAllowFriendlyFire(false);
			getMinigame().getScoreboardManager().getTeam(sbTeam).setCanSeeFriendlyInvisibles(true);
		}
		if(!name.equals(""))
			teams.get(color).setDisplayName(name);
		return teams.get(color);
	}
	
	public void addTeam(TeamColor color, Team team){
		teams.put(color, team);
		String sbTeam = color.toString().toLowerCase();
		getMinigame().getScoreboardManager().registerNewTeam(sbTeam);
		getMinigame().getScoreboardManager().getTeam(sbTeam).setPrefix(color.getColor().toString());
		getMinigame().getScoreboardManager().getTeam(sbTeam).setAllowFriendlyFire(false);
		getMinigame().getScoreboardManager().getTeam(sbTeam).setCanSeeFriendlyInvisibles(true);
	}
	
	public boolean hasTeam(TeamColor color){
		if(teams.containsKey(color))
			return true;
		return false;
	}
	
	public void removeTeam(TeamColor color){
		if(teams.containsKey(color)){
			teams.remove(color);
			getMinigame().getScoreboardManager().getTeam(color.toString().toLowerCase()).unregister();
		}
	}
	
	public boolean hasTeamStartLocations(){
		for(Team t : teams.values()){
			if(!t.hasStartLocations())
				return false;
		}
		return true;
	}
	
	public Callback<String> getDefaultWinnerCallback(){
		return new Callback<String>() {

			@Override
			public void setValue(String value) {
				if(!value.equals("None"))
					defaultWinner.setFlag(TeamColor.matchColor(value.replace(" ", "_")).toString());
				else
					defaultWinner.setFlag(null);
			}

			@Override
			public String getValue() {
				if(defaultWinner.getFlag() != null)
					return MinigameUtils.capitalize(defaultWinner.getFlag().replace("_", " "));
				return "None";
			}
		};
	}

	public void setDefaultWinner(TeamColor defaultWinner) {
		this.defaultWinner.setFlag(defaultWinner.toString());
	}
	
	public TeamColor getDefaultWinner() {
		return TeamColor.matchColor(defaultWinner.getFlag());
	}
	
	public void clearTeams(){
		teams.clear();
		defaultWinner = null;
	}

	@Override
	public void addMenuOptions(Menu menu) {
		Menu m = new Menu(6, "Teams", menu.getViewer());
		m.setPreviousPage(menu);
		List<MenuItem> items = new ArrayList<MenuItem>();
		List<String> teams = new ArrayList<String>(this.teams.size() + 1);
		for(TeamColor t : this.teams.keySet()){
			teams.add(MinigameUtils.capitalize(t.toString().replace("_", " ")));
{'fraction_non_alphanumeric': 0.13801807670385147, 'fraction_numerical': 0.0013028255028092175, 'mean_word_length': 5.508744038155803, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3112795', 'n_tokens_mistral': 4514, 'n_tokens_neox': 4093, 'n_words': 648}