Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.

Commit

Permalink
Fix and rebase team register
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablete1234 committed Sep 8, 2016
1 parent feffa58 commit 18229a1
Show file tree
Hide file tree
Showing 12 changed files with 401 additions and 148 deletions.
14 changes: 6 additions & 8 deletions src/main/java/in/twizmwaz/cardinal/command/JoinCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,23 @@
import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.minecraft.util.commands.CommandException;
import in.twizmwaz.cardinal.GameHandler;
import com.sk89q.minecraft.util.commands.CommandPermissionsException;
import in.twizmwaz.cardinal.chat.ChatConstant;
import in.twizmwaz.cardinal.chat.LocalizedChatMessage;
import in.twizmwaz.cardinal.module.modules.teamRegister.TeamRegisterModule;
import in.twizmwaz.cardinal.util.ChatUtil;
import in.twizmwaz.cardinal.util.Teams;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.Map;

public class JoinCommand {

@Command(aliases = {"join", "play"}, desc = "Join a team.", usage = "[team]")
public static void join(final CommandContext cmd, CommandSender sender) throws CommandException {
Map<String,String> playerTeams = GameHandler.getGameHandler().getMatch().getModules().getModule(TeamRegisterModule.class).getPlayersTeams();
if (!(sender instanceof Player)) {
throw new CommandException(ChatConstant.ERROR_CONSOLE_NO_USE.getMessage(ChatUtil.getLocale(sender)));
}
if (playerTeams.containsKey(sender.getName())) {
throw new CommandException(ChatUtil.getWarningMessage(ChatColor.RED + new LocalizedChatMessage(ChatConstant.ERROR_NO_PERMISSION).getMessage(ChatUtil.getLocale(sender))));
if (TeamRegisterModule.getRegisteredPlayers().contains(((Player) sender).getUniqueId())) {
throw new CommandPermissionsException();
}
try {
Teams.setPlayerTeam((Player) sender, cmd.argsLength() > 0 ? cmd.getJoinedStrings(0) : "");
Expand All @@ -38,6 +33,9 @@ public static void leave(final CommandContext cmd, CommandSender sender) throws
if (!(sender instanceof Player)) {
throw new CommandException(ChatConstant.ERROR_CONSOLE_NO_USE.getMessage(ChatUtil.getLocale(sender)));
}
if (TeamRegisterModule.getRegisteredPlayers().contains(((Player) sender).getUniqueId())) {
throw new CommandPermissionsException();
}
try {
Teams.setPlayerTeam((Player) sender, Teams.getTeamById("observers").get().getName());
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public void run() {
Bukkit.getBanList(BanList.Type.NAME).addBan(args.getString(0), ChatColor.RED + "Permanently Banned" + ChatColor.GOLD + " \u00BB " + reason, null, sender.getName());
}
});

}

@Command(aliases = {"mute"}, usage = "<player>", desc = "Prevents a player from talking.", min = 1)
Expand Down
100 changes: 57 additions & 43 deletions src/main/java/in/twizmwaz/cardinal/command/TeamCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.minecraft.util.commands.CommandPermissions;
import com.sk89q.minecraft.util.commands.CommandPermissionsException;
import com.sk89q.minecraft.util.commands.NestedCommand;
import in.twizmwaz.cardinal.Cardinal;
import in.twizmwaz.cardinal.GameHandler;
import in.twizmwaz.cardinal.chat.ChatConstant;
import in.twizmwaz.cardinal.chat.LocalizedChatMessage;
Expand All @@ -14,6 +16,7 @@
import in.twizmwaz.cardinal.match.MatchState;
import in.twizmwaz.cardinal.module.modules.team.TeamModule;
import in.twizmwaz.cardinal.module.modules.teamRegister.TeamRegisterModule;
import in.twizmwaz.cardinal.util.AsyncCommand;
import in.twizmwaz.cardinal.util.Authenticator;
import in.twizmwaz.cardinal.util.ChatUtil;
import in.twizmwaz.cardinal.util.Players;
Expand All @@ -26,7 +29,6 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class TeamCommands {
Expand All @@ -42,6 +44,9 @@ public static void force(final CommandContext cmd, CommandSender sender) throws
if (!team.isPresent()) {
throw new CommandException(ChatConstant.ERROR_NO_TEAM_MATCH.getMessage(ChatUtil.getLocale(sender)));
}
if (TeamRegisterModule.isRegistered(team.get()) || TeamRegisterModule.getRegisteredPlayers().contains(player.getUniqueId())) {
throw new CommandPermissionsException();
}
if (team.get().contains(player)) {
throw new CommandException(new LocalizedChatMessage(ChatConstant.ERROR_ALREADY_ON_TEAM, Players.getName(player, false) + ChatColor.RED, team.get().getCompleteName()).getMessage(ChatUtil.getLocale(sender)));
}
Expand All @@ -57,6 +62,9 @@ public static void alias(final CommandContext cmd, CommandSender sender) throws
if (!team.isPresent()) {
throw new CommandException(new LocalizedChatMessage(ChatConstant.ERROR_NO_TEAM_MATCH).getMessage(ChatUtil.getLocale(sender)));
}
if (TeamRegisterModule.isRegistered(team.get())) {
throw new CommandPermissionsException();
}
String msg = cmd.getJoinedStrings(1);
ChatUtil.getGlobalChannel().sendLocalizedMessage(new UnlocalizedChatMessage(ChatColor.GRAY + "{0}", new LocalizedChatMessage(ChatConstant.GENERIC_TEAM_ALIAS, team.get().getCompleteName() + ChatColor.GRAY, team.get().getColor() + msg + ChatColor.GRAY)));
team.get().setName(msg);
Expand All @@ -66,6 +74,9 @@ public static void alias(final CommandContext cmd, CommandSender sender) throws
@Command(aliases = {"shuffle"}, desc = "Shuffles the teams.")
@CommandPermissions("cardinal.team.shuffle")
public static void shuffle(final CommandContext cmd, CommandSender sender) throws CommandException {
if (TeamRegisterModule.getRegisteredSize() > 0) {
throw new CommandPermissionsException();
}
if (GameHandler.getGameHandler().getMatch().getState().equals(MatchState.WAITING) || GameHandler.getGameHandler().getMatch().getState().equals(MatchState.STARTING)) {
List<Player> playersToShuffle = new ArrayList<>();
for (Player player : Bukkit.getOnlinePlayers()) {
Expand Down Expand Up @@ -112,62 +123,65 @@ public static void myTeam(final CommandContext cmd, CommandSender sender) throws
sender.sendMessage(new UnlocalizedChatMessage(ChatColor.GRAY + "{0}", new LocalizedChatMessage(ChatConstant.GENERIC_ON_TEAM, team.get().getCompleteName())).getMessage(((Player) sender).getLocale()));
}
}
@Command(aliases = {"register"}, desc = "Registers an OCN team to play a match", min = 1, max = 1, usage = "<ocnteam>")

@Command(aliases = {"register"}, desc = "Registers a team to play a match", min = 1, usage = "<ocnteam>")
@CommandPermissions("cardinal.register")
public static void register(CommandContext cmd, CommandSender sender) throws CommandException {
String ocnteam = cmd.getString(0);
TeamModule usedTeam = null;
ArrayList registeredTeams = GameHandler.getGameHandler().getMatch().getModules().getModule(TeamRegisterModule.class).getRegisteredTeams();
Map<String, TeamModule> usedTeams = GameHandler.getGameHandler().getMatch().getModules().getModule(TeamRegisterModule.class).getUsedTeams();
if (registeredTeams.contains(ocnteam)) {
final String registerTeam = cmd.getJoinedStrings(0);
if (TeamRegisterModule.isRegistered(registerTeam)) {
throw new CommandException(new LocalizedChatMessage(ChatConstant.ERROR_TEAM_ALREADY_REGISTERED).getMessage(ChatUtil.getLocale(sender)));
}
if((Teams.getTeams().size() - 1 == usedTeams.size())) {
if ((Teams.getTeams().size() - 1 == TeamRegisterModule.getRegisteredSize())) {
throw new CommandException(new LocalizedChatMessage(ChatConstant.ERROR_CANT_REGISTER_MORE_TEAMS).getMessage(ChatUtil.getLocale(sender)));
}
for (TeamModule team : Teams.getTeams()) {
if (!team.isObserver() && !usedTeams.containsKey(team.getName())) {
usedTeam = team;
try {
Authenticator.downloadTeamDocument(ocnteam);
} catch (IOException e) {
throw new CommandException(new LocalizedChatMessage(ChatConstant.ERROR_CANT_GET_TEAM_LIST).getMessage(ChatUtil.getLocale(sender)));
}
usedTeams.put(ocnteam, team);
usedTeam.setName(ocnteam);
Bukkit.getPluginManager().callEvent(new TeamNameChangeEvent(usedTeam));
registeredTeams.add(ocnteam);
sender.sendMessage(new UnlocalizedChatMessage(ChatColor.GREEN + "{0}", new LocalizedChatMessage(ChatConstant.GENERIC_TEAM_REGISTERED, ChatColor.GOLD + ocnteam)).getMessage(ChatUtil.getLocale(sender)));
for(Player p : Bukkit.getOnlinePlayers()) {
GameHandler.getGameHandler().getMatch().getModules().getModule(TeamRegisterModule.class).addRegisteredPlayer(p.getName(), ocnteam);
}
return;
for (final TeamModule team : Teams.getTeams()) {
if (!team.isObserver() && !TeamRegisterModule.isRegistered(team)) {
Bukkit.getScheduler().runTaskAsynchronously(Cardinal.getInstance(), new AsyncCommand(cmd, sender) {
@Override
public void run() {
try {
Authenticator.downloadTeamDocument(registerTeam);
} catch (IOException e) {
sender.sendMessage(ChatColor.RED + new LocalizedChatMessage(ChatConstant.ERROR_CANT_GET_TEAM_LIST).getMessage(ChatUtil.getLocale(sender)));
return;
}
TeamRegisterModule.addRegisteredTeam(registerTeam, team);
Bukkit.getPluginManager().callEvent(new TeamNameChangeEvent(team));
sender.sendMessage(new UnlocalizedChatMessage(ChatColor.GREEN + "{0}",
new LocalizedChatMessage(ChatConstant.GENERIC_TEAM_REGISTERED, ChatColor.GOLD + registerTeam)
).getMessage(ChatUtil.getLocale(sender)));
}
});
break;
}
}
}
@Command(aliases = {"unregister"}, desc = "Unregisters an OCN team on match end", min = 1, max = 1, usage = "<ocnteam>")

@Command(aliases = {"registered"}, desc = "Lists registered players", min = 0)
@CommandPermissions("cardinal.register")
public static void registered(CommandContext cmd, CommandSender sender) throws CommandException {
String teams = "";
for (String teamName : TeamRegisterModule.getRegisteredTeamNames()) {
teams += ChatColor.GOLD + teamName + ChatColor.RED + ", ";
}
if (teams.equals("")) teams = ", ";
sender.sendMessage(ChatColor.GOLD + "Registered teams: " + teams.substring(0, teams.length() - 2) + ".");
}

@Command(aliases = {"unregister"}, desc = "Unregisters a team on match end", min = 1, usage = "<team>")
@CommandPermissions("cardinal.register")
public static void unregister(CommandContext cmd, CommandSender sender) throws CommandException {
String team = cmd.getString(0);
ArrayList<String> registeredTeams = GameHandler.getGameHandler().getMatch().getModules().getModule(TeamRegisterModule.class).getRegisteredTeams();
Map<String, TeamModule> usedTeams = GameHandler.getGameHandler().getMatch().getModules().getModule(TeamRegisterModule.class).getUsedTeams();
Map<String, String> playersTeams = GameHandler.getGameHandler().getMatch().getModules().getModule(TeamRegisterModule.class).getPlayersTeams();
if(usedTeams.containsKey(team)) {
for(String playerName : playersTeams.keySet()) {
if(playersTeams.get(playerName) == team) {
Player player = Bukkit.getPlayer(playerName);
GameHandler.getGameHandler().getMatch().getModules().getModule(TeamRegisterModule.class).removeRegisteredPlayer(player);
}
}
usedTeams.remove(team);
registeredTeams.remove(team);
String team = cmd.getJoinedStrings(0);
if (TeamRegisterModule.isRegistered(team)) {
TeamRegisterModule.unregisterTeam(team);
sender.sendMessage(new UnlocalizedChatMessage(ChatColor.GREEN + "{0}", new LocalizedChatMessage(ChatConstant.GENERIC_TEAM_UNREGISTERED, ChatColor.GOLD + team)).getMessage(ChatUtil.getLocale(sender)));
} else {
StringBuilder stringBuilder = new StringBuilder();
for(String ocnteam : registeredTeams) {
stringBuilder.append(ChatColor.GOLD + " " + ocnteam + ChatColor.RED + ",");
String teams = "";
for (String teamName : TeamRegisterModule.getRegisteredTeamNames()) {
teams += ChatColor.GOLD + teamName + ChatColor.RED + ", ";
}
throw new CommandException(new LocalizedChatMessage(ChatConstant.ERROR_CANT_FIND_ANY_REGISTERED_TEAM, stringBuilder.toString()).getMessage(ChatUtil.getLocale(sender)));
if (teams.equals("")) teams = ", ";
throw new CommandException(new LocalizedChatMessage(ChatConstant.ERROR_CANT_FIND_ANY_REGISTERED_TEAM, teams.substring(0, teams.length() - 2) + ".").getMessage(ChatUtil.getLocale(sender)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ public void onEntityDamage(EntityDamageEvent event) {
}
}

@EventHandler
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerConnect(PlayerLoginEvent event) {
if (Bukkit.getBanList(BanList.Type.NAME).isBanned(event.getPlayer().getName())) {
event.disallow(PlayerLoginEvent.Result.KICK_BANNED, Bukkit.getBanList(BanList.Type.NAME).getBanEntry(event.getPlayer().getName()).getReason());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
import in.twizmwaz.cardinal.chat.UnlocalizedChatMessage;
import in.twizmwaz.cardinal.event.CycleCompleteEvent;
import in.twizmwaz.cardinal.event.PlayerChangeTeamEvent;
import in.twizmwaz.cardinal.match.Match;
import in.twizmwaz.cardinal.module.Module;
import in.twizmwaz.cardinal.module.modules.respawn.RespawnModule;
import in.twizmwaz.cardinal.module.modules.team.TeamModule;
import in.twizmwaz.cardinal.module.modules.teamRegister.TeamRegisterModule;
import in.twizmwaz.cardinal.module.modules.tutorial.Tutorial;
import in.twizmwaz.cardinal.util.Contributor;
import in.twizmwaz.cardinal.util.Players;
import in.twizmwaz.cardinal.util.Teams;
Expand All @@ -26,16 +22,9 @@

import java.util.List;
import java.util.Locale;
import java.util.Map;

public class TeamManagerModule implements Module {

private final Match match;

protected TeamManagerModule(Match match) {
this.match = match;
}

@Override
public void unload() {
HandlerList.unregisterAll(this);
Expand All @@ -45,12 +34,12 @@ public void unload() {
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
Players.resetPlayer(player);
String playerName = event.getPlayer().getName();
Map<String, String> playerTeams = GameHandler.getGameHandler().getMatch().getModules().getModule(TeamRegisterModule.class).getPlayersTeams();
Teams.getTeamById("observers").get().add(player, true, false);
if(playerTeams.containsKey(playerName)) {
Teams.getTeamByName(playerTeams.get(playerName)).get().add(player);
}
}

@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin2(PlayerJoinEvent event) {
Player player = event.getPlayer();
event.setJoinMessage(null);
for (Player player1 : Bukkit.getOnlinePlayers()) {
if (!player1.equals(player)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class TeamManagerModuleBuilder implements ModuleBuilder {

@Override
public ModuleCollection<TeamManagerModule> load(Match match) {
return new ModuleCollection<>(new TeamManagerModule(match));
return new ModuleCollection<>(new TeamManagerModule());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package in.twizmwaz.cardinal.module.modules.teamRegister;

import com.google.common.collect.Lists;
import org.bukkit.configuration.ConfigurationSection;

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

class StringFilters {

private final StringFilter teamFilter;
private final StringFilter uuidFilter;

public StringFilters(ConfigurationSection config) {
teamFilter = parse(config, "team");
uuidFilter = parse(config, "uuid");
}

private StringFilter parse(ConfigurationSection config, String child) {
if (config != null && config.getKeys().contains(child)) {
List<StringFilter> result = Lists.newArrayList();
List<Map<?, ?>> filters = config.getMapList(child);
for (Map<?, ?> filter : filters) {
if (filter.containsKey("case") && filter.get("case") instanceof String) {
result.add(new CaseFilter(((String) filter.get("case")).equalsIgnoreCase("lowercase")));
} else if (filter.containsKey("match") && filter.containsKey("replace")
&& filter.get("match") instanceof String && filter.get("replace") instanceof String) {
result.add(new MatchFilter((String) filter.get("match"), (String) filter.get("replace")));
}
}
return new FilterList(result);
}
return new FilterList(new ArrayList<StringFilter>());
}

protected String getTeam(String team) {
return teamFilter.transform(team);
}

protected String getUuid(String uuid) {
return uuidFilter.transform(uuid);
}

private interface StringFilter {

String transform(String string);

}

private class MatchFilter implements StringFilter {

private final String regex;
private final String replace;

public MatchFilter(String regex, String replace) {
this.regex = regex;
this.replace = replace;
}

@Override
public String transform(String string) {
return string.replaceAll(regex, replace);
}

}

private class CaseFilter implements StringFilter {

private final boolean lowerCase;

public CaseFilter(boolean lowerCase) {
this.lowerCase = lowerCase;
}

@Override
public String transform(String string) {
return lowerCase ? string.toLowerCase() : string.toUpperCase();
}

}

private class FilterList implements StringFilter {

List<StringFilter> filters;

public FilterList(List<StringFilter> filters) {
this.filters = filters;
}

@Override
public String transform(String string) {
for (StringFilter filter : filters) {
string = filter.transform(string);
}
return string;
}

}

}
Loading

0 comments on commit 18229a1

Please sign in to comment.